blob: 73effca152f9cc97d41270907785dd03646c6454 [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"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070033#include "gc_root.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070034#include "gc/accounting/card_table-inl.h"
Mathieu Chartierc56057e2014-05-04 13:18:58 -070035#include "indirect_reference_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070036#include "interpreter/interpreter.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070037#include "jni.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070038#include "mirror/art_field-inl.h"
39#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040#include "mirror/class-inl.h"
41#include "mirror/class_loader.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042#include "mirror/object-inl.h"
43#include "mirror/object_array-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070044#include "mirror/string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045#include "mirror/throwable.h"
jgu216d031042014-09-10 06:57:17 -040046#include "nativebridge/native_bridge.h"
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080047#include "parsed_options.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070048#include "reflection.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070049#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070050#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070051#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070052#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070053#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054#include "utf.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070055#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070056
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070057namespace art {
58
Brian Carlstrom7934ac22013-07-26 10:54:15 -070059static const size_t kMonitorsInitial = 32; // Arbitrary.
60static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070061
Brian Carlstrom7934ac22013-07-26 10:54:15 -070062static const size_t kLocalsInitial = 64; // Arbitrary.
63static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070064
Brian Carlstrom7934ac22013-07-26 10:54:15 -070065static size_t gGlobalsInitial = 512; // Arbitrary.
66static size_t gGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Elliott Hughes2ced6a52011-10-16 18:44:48 -070067
Brian Carlstrom7934ac22013-07-26 10:54:15 -070068static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
69static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Ian Rogersdf20fe02011-07-20 20:34:16 -070070
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080071static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070072 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070073 return soa.Vm()->AddWeakGlobalReference(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -070074}
75
Jeff Hao19c5d372013-03-15 14:33:43 -070076static bool IsBadJniVersion(int version) {
77 // We don't support JNI_VERSION_1_1. These are the only other valid versions.
78 return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
79}
80
Elliott Hughes6b436852011-08-12 10:16:44 -070081// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
82// separated with slashes but aren't wrapped with "L;" like regular descriptors
83// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
84// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
85// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -070086static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -070087 std::string result;
88 // Add the missing "L;" if necessary.
89 if (name[0] == '[') {
90 result = name;
91 } else {
92 result += 'L';
93 result += name;
94 result += ';';
95 }
96 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -070097 if (result.find('.') != std::string::npos) {
98 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
99 << "\"" << name << "\"";
100 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700101 }
102 return result;
103}
104
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800105static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, mirror::Class* c,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700106 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700107 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800108 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700109 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800110 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
111 "no %s method \"%s.%s%s\"",
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700112 kind, c->GetDescriptor(&temp), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700113}
114
Sebastien Hertzfa65e842014-07-03 09:39:53 +0200115static void ReportInvalidJNINativeMethod(const ScopedObjectAccess& soa, mirror::Class* c,
116 const char* kind, jint idx, bool return_errors)
117 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
118 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method in "
119 << PrettyDescriptor(c) << " in " << c->GetDexCache()->GetLocation()->ToModifiedUtf8()
120 << ": " << kind << " is null at index " << idx;
121 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
122 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
123 "%s is null at index %d", kind, idx);
124}
125
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800126static mirror::Class* EnsureInitialized(Thread* self, mirror::Class* klass)
127 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
128 if (LIKELY(klass->IsInitialized())) {
129 return klass;
130 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700131 StackHandleScope<1> hs(self);
132 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
133 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(h_klass, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800134 return nullptr;
135 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700136 return h_klass.Get();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800137}
138
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700139static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
140 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700141 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800142 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800143 if (c == nullptr) {
144 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700145 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800146 mirror::ArtMethod* method = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700147 if (is_static) {
148 method = c->FindDirectMethod(name, sig);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700149 } else if (c->IsInterface()) {
150 method = c->FindInterfaceMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700151 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700152 method = c->FindVirtualMethod(name, sig);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800153 if (method == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700154 // No virtual method matching the signature. Search declared
155 // private methods and constructors.
156 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700157 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700158 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800159 if (method == nullptr || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700160 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800161 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700162 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700163 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700164}
165
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800166static mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700167 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800168 mirror::ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700169 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
170 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogersef28b142012-11-30 14:22:18 -0800171 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700172 }
Brian Carlstromce888532013-10-10 00:32:58 -0700173 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800174 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700175 return method->GetDeclaringClass()->GetClassLoader();
176 }
177 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800178 mirror::ClassLoader* class_loader =
179 soa.Decode<mirror::ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
180 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700181 return class_loader;
182 }
183 // See if the override ClassLoader is set for gtests.
184 class_loader = soa.Self()->GetClassLoaderOverride();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800185 if (class_loader != nullptr) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800186 // If so, CommonCompilerTest should have set UseCompileTimeClassPath.
Brian Carlstromce888532013-10-10 00:32:58 -0700187 CHECK(Runtime::Current()->UseCompileTimeClassPath());
188 return class_loader;
189 }
190 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800191 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700192}
193
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700194static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
195 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700196 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700197 StackHandleScope<2> hs(soa.Self());
198 Handle<mirror::Class> c(
199 hs.NewHandle(EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class))));
200 if (c.Get() == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800201 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700202 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800203 mirror::ArtField* field = nullptr;
204 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700205 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
206 if (sig[1] != '\0') {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700207 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(c->GetClassLoader()));
Ian Rogers98379392014-02-24 16:53:16 -0800208 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700209 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700210 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700211 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800212 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700213 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700214 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800215 ThrowLocation throw_location;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700216 StackHandleScope<1> hs(soa.Self());
217 Handle<mirror::Throwable> cause(hs.NewHandle(soa.Self()->GetException(&throw_location)));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700218 soa.Self()->ClearException();
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700219 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800220 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800221 "no type \"%s\" found and so no field \"%s\" "
222 "could be found in class \"%s\" or its superclasses", sig, name,
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700223 c->GetDescriptor(&temp));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700224 soa.Self()->GetException(nullptr)->SetCause(cause.Get());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800225 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700226 }
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700227 std::string temp;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700228 if (is_static) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700229 field = mirror::Class::FindStaticField(soa.Self(), c, name,
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700230 field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700231 } else {
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700232 field = c->FindInstanceField(name, field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700233 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800234 if (field == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800235 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
236 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
237 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700238 sig, name, c->GetDescriptor(&temp));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800239 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700240 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700241 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700242}
243
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800244static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700245 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700246 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700247 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800248 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
249 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
250 "%s offset=%d length=%d %s.length=%d",
251 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700252}
Ian Rogers0571d352011-11-03 19:51:38 -0700253
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700254static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
255 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700256 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800257 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
258 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
259 "offset=%d length=%d string.length()=%d", start, length,
260 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700261}
Elliott Hughes814e4032011-08-23 12:07:56 -0700262
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700263int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700264 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700265 // Turn the const char* into a java.lang.String.
266 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800267 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700268 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700269 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700270
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700271 // Choose an appropriate constructor and set up the arguments.
272 jvalue args[2];
273 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800274 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700275 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800276 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700277 signature = "(Ljava/lang/String;)V";
278 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800279 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700280 signature = "(Ljava/lang/Throwable;)V";
281 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700282 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700283 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
284 args[0].l = s.get();
285 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700286 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700287 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800288 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800289 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700290 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800291 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700292 return JNI_ERR;
293 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700294
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800295 ScopedLocalRef<jthrowable> exception(
296 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
297 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700298 return JNI_ERR;
299 }
Ian Rogersef28b142012-11-30 14:22:18 -0800300 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800301 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800302 soa.Self()->SetException(throw_location, soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700303 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700304}
305
Elliott Hughes462c9442012-03-23 18:47:50 -0700306static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800307 if (vm == nullptr || p_env == nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -0700308 return JNI_ERR;
309 }
310
Elliott Hughes462c9442012-03-23 18:47:50 -0700311 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700312 Thread* self = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800313 if (self != nullptr) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700314 *p_env = self->GetJniEnv();
315 return JNI_OK;
316 }
317
318 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
319
320 // No threads allowed in zygote mode.
321 if (runtime->IsZygote()) {
322 LOG(ERROR) << "Attempt to attach a thread in the zygote";
323 return JNI_ERR;
324 }
325
Elliott Hughes462c9442012-03-23 18:47:50 -0700326 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800327 const char* thread_name = nullptr;
328 jobject thread_group = nullptr;
329 if (args != nullptr) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700330 if (IsBadJniVersion(args->version)) {
331 LOG(ERROR) << "Bad JNI version passed to "
332 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
333 << args->version;
334 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700335 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700336 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700337 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700338 }
Elliott Hughes75770752011-08-24 17:52:38 -0700339
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800340 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800341 *p_env = nullptr;
Ian Rogers120f1c72012-09-28 17:17:10 -0700342 return JNI_ERR;
343 } else {
344 *p_env = Thread::Current()->GetJniEnv();
345 return JNI_OK;
346 }
Elliott Hughes75770752011-08-24 17:52:38 -0700347}
348
Elliott Hughes79082e32011-08-25 12:07:32 -0700349class SharedLibrary {
350 public:
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800351 SharedLibrary(const std::string& path, void* handle, mirror::Object* class_loader)
Elliott Hughes79082e32011-08-25 12:07:32 -0700352 : path_(path),
353 handle_(handle),
Yong WU355383f2014-07-24 21:32:15 +0800354 needs_native_bridge_(false),
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700355 class_loader_(GcRoot<mirror::Object>(class_loader)),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700356 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700357 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700358 jni_on_load_thread_id_(Thread::Current()->GetThreadId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700359 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700360 }
361
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -0700362 mirror::Object* GetClassLoader() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700363 return class_loader_.Read();
Elliott Hughes79082e32011-08-25 12:07:32 -0700364 }
365
366 std::string GetPath() {
367 return path_;
368 }
369
370 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700371 * Check the result of an earlier call to JNI_OnLoad on this library.
372 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700373 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700374 bool CheckOnLoadResult()
375 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700376 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700377 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700378 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
379 bool okay;
380 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700381 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700382
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700383 if (jni_on_load_thread_id_ == self->GetThreadId()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700384 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
385 // caller can continue.
386 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
387 okay = true;
388 } else {
389 while (jni_on_load_result_ == kPending) {
390 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700391 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700392 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700393
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700394 okay = (jni_on_load_result_ == kOkay);
395 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
396 << (okay ? "succeeded" : "failed") << "]";
397 }
398 }
399 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700400 return okay;
401 }
402
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700403 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700404 Thread* self = Thread::Current();
405 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700406
Elliott Hughes79082e32011-08-25 12:07:32 -0700407 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700408 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700409
410 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700411 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700412 }
413
Yong WU355383f2014-07-24 21:32:15 +0800414 void SetNeedsNativeBridge() {
415 needs_native_bridge_ = true;
416 }
417
418 bool NeedsNativeBridge() const {
419 return needs_native_bridge_;
420 }
421
Elliott Hughes79082e32011-08-25 12:07:32 -0700422 void* FindSymbol(const std::string& symbol_name) {
423 return dlsym(handle_, symbol_name.c_str());
424 }
425
Yong WU355383f2014-07-24 21:32:15 +0800426 void* FindSymbolWithNativeBridge(const std::string& symbol_name, mirror::ArtMethod* m)
427 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
428 CHECK(NeedsNativeBridge());
429
430 uint32_t len = 0;
431 const char* shorty = nullptr;
432 if (m != nullptr) {
433 shorty = m->GetShorty(&len);
434 }
Calin Juravle93de4272014-08-12 20:55:20 +0100435 return android::NativeBridgeGetTrampoline(handle_, symbol_name.c_str(), shorty, len);
Yong WU355383f2014-07-24 21:32:15 +0800436 }
437
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800438 void VisitRoots(RootCallback* visitor, void* arg) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700439 if (!class_loader_.IsNull()) {
440 class_loader_.VisitRoot(visitor, arg, 0, kRootVMInternal);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800441 }
442 }
443
Elliott Hughes79082e32011-08-25 12:07:32 -0700444 private:
445 enum JNI_OnLoadState {
446 kPending,
447 kFailed,
448 kOkay,
449 };
450
451 // Path to library "/system/lib/libjni.so".
452 std::string path_;
453
454 // The void* returned by dlopen(3).
455 void* handle_;
456
Yong WU355383f2014-07-24 21:32:15 +0800457 // True if a native bridge is required.
458 bool needs_native_bridge_;
459
Elliott Hughes79082e32011-08-25 12:07:32 -0700460 // The ClassLoader this library is associated with.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700461 GcRoot<mirror::Object> class_loader_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700462
463 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700464 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700465 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700466 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700467 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700468 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700469 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700470 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700471};
472
Elliott Hughes79082e32011-08-25 12:07:32 -0700473// This exists mainly to keep implementation details out of the header file.
474class Libraries {
475 public:
476 Libraries() {
477 }
478
479 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700480 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700481 }
482
Elliott Hughesae80b492012-04-24 10:43:17 -0700483 void Dump(std::ostream& os) const {
484 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700485 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700486 if (!first) {
487 os << ' ';
488 }
489 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700490 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700491 }
492 }
493
494 size_t size() const {
495 return libraries_.size();
496 }
497
Elliott Hughes79082e32011-08-25 12:07:32 -0700498 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700499 auto it = libraries_.find(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800500 return (it == libraries_.end()) ? nullptr : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700501 }
502
503 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700504 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700505 }
506
507 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800508 void* FindNativeMethod(mirror::ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700509 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700510 std::string jni_short_name(JniShortName(m));
511 std::string jni_long_name(JniLongName(m));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800512 const mirror::ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700513 for (const auto& lib : libraries_) {
514 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700515 if (library->GetClassLoader() != declaring_class_loader) {
516 // We only search libraries loaded by the appropriate ClassLoader.
517 continue;
518 }
519 // Try the short name then the long name...
Yong WU355383f2014-07-24 21:32:15 +0800520 void* fn = nullptr;
521 if (UNLIKELY(library->NeedsNativeBridge())) {
522 fn = library->FindSymbolWithNativeBridge(jni_short_name, m);
523 if (fn == nullptr) {
524 fn = library->FindSymbolWithNativeBridge(jni_long_name, m);
525 }
526 } else {
527 fn = library->FindSymbol(jni_short_name);
528 if (fn == nullptr) {
529 fn = library->FindSymbol(jni_long_name);
530 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700531 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800532 if (fn != nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800533 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
534 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700535 return fn;
536 }
537 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700538 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700539 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700540 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700541 LOG(ERROR) << detail;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800542 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -0700543 }
544
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800545 void VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800546 for (auto& lib_pair : libraries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800547 lib_pair.second->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800548 }
549 }
550
Elliott Hughes79082e32011-08-25 12:07:32 -0700551 private:
Mathieu Chartier5369c402014-08-25 13:08:22 -0700552 AllocationTrackingSafeMap<std::string, SharedLibrary*, kAllocatorTagJNILibrarires> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700553};
554
Ian Rogers2d10b202014-05-12 19:15:18 -0700555#define CHECK_NON_NULL_ARGUMENT(value) \
556 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, nullptr)
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700557
Ian Rogers2d10b202014-05-12 19:15:18 -0700558#define CHECK_NON_NULL_ARGUMENT_RETURN_VOID(value) \
559 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, )
560
561#define CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(value) \
562 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, 0)
563
564#define CHECK_NON_NULL_ARGUMENT_RETURN(value, return_val) \
565 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, return_val)
566
567#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value, return_val) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800568 if (UNLIKELY(value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700569 JniAbortF(name, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700570 return return_val; \
Ian Rogersbc939662013-08-15 10:26:54 -0700571 }
572
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700573#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800574 if (UNLIKELY(length != 0 && value == nullptr)) { \
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700575 JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700576 return; \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700577 }
578
Elliott Hughescdf53122011-08-19 15:46:09 -0700579class JNI {
580 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700581 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700582 return JNI_VERSION_1_6;
583 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700584
Ian Rogers25e8b912012-09-07 11:31:36 -0700585 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700586 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800587 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700588 }
589
Elliott Hughescdf53122011-08-19 15:46:09 -0700590 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700591 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700592 Runtime* runtime = Runtime::Current();
593 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700594 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700595 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800596 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700597 if (runtime->IsStarted()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700598 StackHandleScope<1> hs(soa.Self());
599 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader(soa)));
Ian Rogers98379392014-02-24 16:53:16 -0800600 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700601 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800602 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700603 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700604 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700605 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700606
Ian Rogers62f05122014-03-21 11:21:29 -0700607 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700608 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700609 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700610 return soa.EncodeMethod(mirror::ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700611 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700612
Ian Rogers62f05122014-03-21 11:21:29 -0700613 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700614 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700615 ScopedObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -0700616 return soa.EncodeField(mirror::ArtField::FromReflectedField(soa, jlr_field));
Elliott Hughescdf53122011-08-19 15:46:09 -0700617 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700618
Elliott Hughescdf53122011-08-19 15:46:09 -0700619 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700620 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700621 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800622 mirror::ArtMethod* m = soa.DecodeMethod(mid);
623 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -0700624 jobject art_method = soa.AddLocalReference<jobject>(m);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200625 jobject reflect_method;
626 if (m->IsConstructor()) {
627 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Constructor);
628 } else {
629 reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
630 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700631 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800632 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700633 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800634 SetObjectField(env, reflect_method,
635 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700636 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700637 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700638
Elliott Hughescdf53122011-08-19 15:46:09 -0700639 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700640 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700641 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800642 mirror::ArtField* f = soa.DecodeField(fid);
Brian Carlstromea46f952013-07-30 01:26:50 -0700643 jobject art_field = soa.AddLocalReference<jobject>(f);
644 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
645 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800646 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700647 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800648 SetObjectField(env, reflect_field,
649 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700650 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700651 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700652
Elliott Hughes37f7a402011-08-22 18:56:01 -0700653 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700654 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700655 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800656 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700657 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700658 }
659
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700660 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700661 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700662 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800663 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700664 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700665 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700666
Narayan Kamath1268b742014-07-11 19:15:11 +0100667 // Note: java_class1 should be safely castable to java_class2, and
668 // not the other way around.
Elliott Hughes37f7a402011-08-22 18:56:01 -0700669 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700670 CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
671 CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700672 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800673 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
674 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Narayan Kamath1268b742014-07-11 19:15:11 +0100675 return c2->IsAssignableFrom(c1) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700676 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700677
Elliott Hughese84278b2012-03-22 10:06:53 -0700678 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700679 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800680 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700681 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700682 return JNI_TRUE;
683 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700684 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800685 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
686 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700687 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700688 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700689 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700690
Elliott Hughes37f7a402011-08-22 18:56:01 -0700691 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700692 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800693 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
694 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700695 return JNI_ERR;
696 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800697 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
698 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700699 return JNI_OK;
700 }
701
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700702 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700703 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800704 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700705 }
706
707 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700708 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700709 }
710
711 static void ExceptionClear(JNIEnv* env) {
Serguei Katkova309d762014-05-26 11:23:39 +0700712 ScopedObjectAccess soa(env);
713 soa.Self()->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700714 }
715
716 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700717 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700718
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700719 // If we have no exception to describe, pass through.
720 if (!soa.Self()->GetException(nullptr)) {
721 return;
722 }
723
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700724 StackHandleScope<3> hs(soa.Self());
725 // TODO: Use nullptr instead of null handles?
726 auto old_throw_this_object(hs.NewHandle<mirror::Object>(nullptr));
727 auto old_throw_method(hs.NewHandle<mirror::ArtMethod>(nullptr));
728 auto old_exception(hs.NewHandle<mirror::Throwable>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -0800729 uint32_t old_throw_dex_pc;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200730 bool old_is_exception_reported;
Ian Rogers62d6c772013-02-27 08:32:07 -0800731 {
732 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800733 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700734 old_throw_this_object.Assign(old_throw_location.GetThis());
735 old_throw_method.Assign(old_throw_location.GetMethod());
736 old_exception.Assign(old_exception_obj);
Ian Rogers62d6c772013-02-27 08:32:07 -0800737 old_throw_dex_pc = old_throw_location.GetDexPc();
Sebastien Hertz9f102032014-05-23 08:59:42 +0200738 old_is_exception_reported = soa.Self()->IsExceptionReportedToInstrumentation();
Ian Rogers62d6c772013-02-27 08:32:07 -0800739 soa.Self()->ClearException();
740 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800741 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700742 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700743 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
744 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800745 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700746 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700747 << PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700748 } else {
749 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800750 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800751 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700752 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800753 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700754 }
755 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700756 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800757 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700758
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700759 soa.Self()->SetException(gc_safe_throw_location, old_exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200760 soa.Self()->SetExceptionReportedToInstrumentation(old_is_exception_reported);
Elliott Hughescdf53122011-08-19 15:46:09 -0700761 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700762
Elliott Hughescdf53122011-08-19 15:46:09 -0700763 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700764 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800765 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700766 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700767 }
768
Ian Rogers25e8b912012-09-07 11:31:36 -0700769 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700770 LOG(FATAL) << "JNI FatalError called: " << msg;
771 }
772
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700773 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700774 // TODO: SOA may not be necessary but I do it to please lock annotations.
775 ScopedObjectAccess soa(env);
776 if (EnsureLocalCapacity(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700777 return JNI_ERR;
778 }
Ian Rogersef28b142012-11-30 14:22:18 -0800779 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700780 return JNI_OK;
781 }
782
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700783 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700784 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800785 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700786 soa.Env()->PopFrame();
787 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700788 }
789
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700790 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700791 // TODO: SOA may not be necessary but I do it to please lock annotations.
792 ScopedObjectAccess soa(env);
793 return EnsureLocalCapacity(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700794 }
795
Elliott Hughescdf53122011-08-19 15:46:09 -0700796 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700797 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800798 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800799 // Check for null after decoding the object to handle cleared weak globals.
800 if (decoded_obj == nullptr) {
801 return nullptr;
802 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700803 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700804 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700805 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700806 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700807 return reinterpret_cast<jobject>(ref);
808 }
809
810 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800811 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700812 return;
813 }
Ian Rogersef28b142012-11-30 14:22:18 -0800814 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700815 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800816 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700817 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700818
819 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
820 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
821 << "failed to find entry";
822 }
823 }
824
825 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700826 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800827 return AddWeakGlobalReference(soa, soa.Decode<mirror::Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700828 }
829
830 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700831 if (obj != nullptr) {
832 ScopedObjectAccess soa(env);
833 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700834 }
835 }
836
837 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700838 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800839 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800840 // Check for null after decoding the object to handle cleared weak globals.
841 if (decoded_obj == nullptr) {
842 return nullptr;
843 }
844 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700845 }
846
847 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800848 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700849 return;
850 }
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700851 ScopedObjectAccess soa(env);
Ian Rogersef28b142012-11-30 14:22:18 -0800852 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700853
Ian Rogersef28b142012-11-30 14:22:18 -0800854 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700855 if (!locals.Remove(cookie, obj)) {
856 // Attempting to delete a local reference that is not in the
857 // topmost local reference frame is a no-op. DeleteLocalRef returns
858 // void and doesn't throw any exceptions, but we should probably
859 // complain about it so the user will notice that things aren't
860 // going quite the way they expect.
861 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
862 << "failed to find entry";
863 }
864 }
865
866 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700867 if (obj1 == obj2) {
868 return JNI_TRUE;
869 } else {
870 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800871 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
872 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700873 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700874 }
875
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700876 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700877 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700878 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800879 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800880 if (c == nullptr) {
881 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700882 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700883 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700884 }
885
Ian Rogersbc939662013-08-15 10:26:54 -0700886 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700887 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700888 va_start(args, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700889 CHECK_NON_NULL_ARGUMENT(java_class);
890 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700891 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700892 va_end(args);
893 return result;
894 }
895
Elliott Hughes72025e52011-08-23 17:50:30 -0700896 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700897 CHECK_NON_NULL_ARGUMENT(java_class);
898 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700899 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800900 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800901 if (c == nullptr) {
902 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700903 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800904 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800905 if (result == nullptr) {
906 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700907 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700908 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700909 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800910 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800911 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700912 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800913 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700914 }
915
Elliott Hughes72025e52011-08-23 17:50:30 -0700916 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700917 CHECK_NON_NULL_ARGUMENT(java_class);
918 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700919 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800920 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800921 if (c == nullptr) {
922 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700923 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800924 mirror::Object* result = c->AllocObject(soa.Self());
925 if (result == nullptr) {
926 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700927 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700928 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700929 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800930 if (soa.Self()->IsExceptionPending()) {
931 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700932 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800933 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700934 }
935
Ian Rogersbc939662013-08-15 10:26:54 -0700936 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700937 CHECK_NON_NULL_ARGUMENT(java_class);
938 CHECK_NON_NULL_ARGUMENT(name);
939 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700940 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700941 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700942 }
943
Ian Rogersbc939662013-08-15 10:26:54 -0700944 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
945 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700946 CHECK_NON_NULL_ARGUMENT(java_class);
947 CHECK_NON_NULL_ARGUMENT(name);
948 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700949 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700950 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700951 }
952
Elliott Hughes72025e52011-08-23 17:50:30 -0700953 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700954 va_list ap;
955 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700956 CHECK_NON_NULL_ARGUMENT(obj);
957 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700958 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700959 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700960 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700961 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700962 }
963
Elliott Hughes72025e52011-08-23 17:50:30 -0700964 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700965 CHECK_NON_NULL_ARGUMENT(obj);
966 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700967 ScopedObjectAccess soa(env);
968 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
969 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700970 }
971
Elliott Hughes72025e52011-08-23 17:50:30 -0700972 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700973 CHECK_NON_NULL_ARGUMENT(obj);
974 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700975 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -0700976 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
977 args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700978 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700979 }
980
Elliott Hughes72025e52011-08-23 17:50:30 -0700981 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700982 va_list ap;
983 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -0700984 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
985 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700986 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700987 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700988 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700989 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700990 }
991
Elliott Hughes72025e52011-08-23 17:50:30 -0700992 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700993 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
994 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700995 ScopedObjectAccess soa(env);
996 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700997 }
998
Elliott Hughes72025e52011-08-23 17:50:30 -0700999 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001000 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1001 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001002 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001003 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1004 args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 }
1006
Elliott Hughes72025e52011-08-23 17:50:30 -07001007 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001008 va_list ap;
1009 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001010 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1011 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001012 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001013 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001015 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001016 }
1017
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001019 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1020 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001021 ScopedObjectAccess soa(env);
1022 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001023 }
1024
Elliott Hughes72025e52011-08-23 17:50:30 -07001025 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001026 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1027 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001028 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001029 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1030 args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001031 }
1032
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 va_list ap;
1035 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001036 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1037 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001038 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001039 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001040 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001041 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001042 }
1043
Elliott Hughes72025e52011-08-23 17:50:30 -07001044 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001045 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1046 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001047 ScopedObjectAccess soa(env);
1048 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 }
1050
Elliott Hughes72025e52011-08-23 17:50:30 -07001051 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001052 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1053 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001054 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001055 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1056 args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 }
1058
Elliott Hughes72025e52011-08-23 17:50:30 -07001059 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 va_list ap;
1061 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001062 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1063 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001064 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001065 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001067 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001068 }
1069
Elliott Hughes72025e52011-08-23 17:50:30 -07001070 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001071 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1072 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001073 ScopedObjectAccess soa(env);
1074 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001075 }
1076
Elliott Hughes72025e52011-08-23 17:50:30 -07001077 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001078 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1079 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001080 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001081 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1082 args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 }
1084
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 va_list ap;
1087 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001088 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1089 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001090 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001091 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001093 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 }
1095
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001097 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1098 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001099 ScopedObjectAccess soa(env);
1100 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 }
1102
Elliott Hughes72025e52011-08-23 17:50:30 -07001103 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001104 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1105 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001106 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001107 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1108 args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 }
1110
Elliott Hughes72025e52011-08-23 17:50:30 -07001111 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001112 va_list ap;
1113 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001114 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1115 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001116 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001117 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001119 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 }
1121
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001123 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1124 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001125 ScopedObjectAccess soa(env);
1126 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001127 }
1128
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001130 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1131 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001132 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001133 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1134 args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 }
1136
Elliott Hughes72025e52011-08-23 17:50:30 -07001137 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001138 va_list ap;
1139 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001140 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1141 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001142 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001143 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001145 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 }
1147
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001149 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1150 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001151 ScopedObjectAccess soa(env);
1152 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 }
1154
Elliott Hughes72025e52011-08-23 17:50:30 -07001155 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001156 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1157 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001158 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001159 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1160 args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001161 }
1162
Elliott Hughes72025e52011-08-23 17:50:30 -07001163 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 va_list ap;
1165 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001166 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1167 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001168 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001169 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001170 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001171 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 }
1173
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001175 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1176 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001177 ScopedObjectAccess soa(env);
1178 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001179 }
1180
Elliott Hughes72025e52011-08-23 17:50:30 -07001181 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001182 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1183 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001184 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001185 return InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid,
1186 args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 }
1188
Elliott Hughes72025e52011-08-23 17:50:30 -07001189 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001190 va_list ap;
1191 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001192 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1193 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001194 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001195 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001196 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 }
1198
Elliott Hughes72025e52011-08-23 17:50:30 -07001199 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001200 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1201 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001202 ScopedObjectAccess soa(env);
1203 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 }
1205
Elliott Hughes72025e52011-08-23 17:50:30 -07001206 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001207 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1208 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001209 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001210 InvokeVirtualOrInterfaceWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 }
1212
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001213 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001215 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001216 CHECK_NON_NULL_ARGUMENT(obj);
1217 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001218 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001219 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1220 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001221 va_end(ap);
1222 return local_result;
1223 }
1224
Ian Rogersbc939662013-08-15 10:26:54 -07001225 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1226 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001227 CHECK_NON_NULL_ARGUMENT(obj);
1228 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001229 ScopedObjectAccess soa(env);
1230 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1231 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 }
1233
Ian Rogersbc939662013-08-15 10:26:54 -07001234 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1235 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001236 CHECK_NON_NULL_ARGUMENT(obj);
1237 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001238 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001239 JValue result(InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001240 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 }
1242
Ian Rogersbc939662013-08-15 10:26:54 -07001243 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1244 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001245 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001246 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001247 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1248 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001249 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001250 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001251 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001252 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 }
1254
Ian Rogersbc939662013-08-15 10:26:54 -07001255 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1256 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001257 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1258 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001259 ScopedObjectAccess soa(env);
1260 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 }
1262
Ian Rogersbc939662013-08-15 10:26:54 -07001263 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1264 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001265 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1266 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001267 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001268 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 }
1270
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001271 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001273 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001274 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1275 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001276 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001277 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001279 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 }
1281
Ian Rogersbc939662013-08-15 10:26:54 -07001282 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1283 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001284 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1285 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001286 ScopedObjectAccess soa(env);
1287 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 }
1289
Ian Rogersbc939662013-08-15 10:26:54 -07001290 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1291 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001292 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1293 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001294 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001295 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 }
1297
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001298 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001300 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001301 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1302 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001303 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001304 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001306 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 }
1308
Ian Rogersbc939662013-08-15 10:26:54 -07001309 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1310 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001311 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1312 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001313 ScopedObjectAccess soa(env);
1314 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 }
1316
Ian Rogersbc939662013-08-15 10:26:54 -07001317 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1318 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001319 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1320 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001321 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001322 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 }
1324
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001325 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001327 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001328 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1329 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001330 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001331 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001333 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 }
1335
Ian Rogersbc939662013-08-15 10:26:54 -07001336 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1337 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001338 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1339 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001340 ScopedObjectAccess soa(env);
1341 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 }
1343
Ian Rogersbc939662013-08-15 10:26:54 -07001344 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1345 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001346 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1347 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001348 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001349 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001350 }
1351
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001352 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001354 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001355 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1356 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001357 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001358 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001359 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001360 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 }
1362
Ian Rogersbc939662013-08-15 10:26:54 -07001363 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1364 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001365 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1366 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001367 ScopedObjectAccess soa(env);
1368 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 }
1370
Ian Rogersbc939662013-08-15 10:26:54 -07001371 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1372 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001373 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1374 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001375 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001376 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001377 }
1378
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001379 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001380 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001381 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001382 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1383 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001384 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001385 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001387 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001388 }
1389
Ian Rogersbc939662013-08-15 10:26:54 -07001390 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1391 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001392 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1393 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001394 ScopedObjectAccess soa(env);
1395 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 }
1397
Ian Rogersbc939662013-08-15 10:26:54 -07001398 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1399 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001400 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1401 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001402 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001403 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001404 }
1405
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001406 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001407 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001408 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001409 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1410 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001411 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001412 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001414 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001415 }
1416
Ian Rogersbc939662013-08-15 10:26:54 -07001417 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1418 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001419 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1420 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001421 ScopedObjectAccess soa(env);
1422 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001423 }
1424
Ian Rogersbc939662013-08-15 10:26:54 -07001425 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1426 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001427 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1428 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001429 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001430 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001431 }
1432
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001433 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001435 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001436 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1437 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001438 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001439 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001441 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001442 }
1443
Ian Rogersbc939662013-08-15 10:26:54 -07001444 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1445 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001446 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1447 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001448 ScopedObjectAccess soa(env);
1449 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001450 }
1451
Ian Rogersbc939662013-08-15 10:26:54 -07001452 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1453 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001454 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1455 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001456 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001457 return InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 }
1459
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001460 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001461 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001462 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001463 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1464 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001465 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001466 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001467 va_end(ap);
1468 }
1469
Brian Carlstromea46f952013-07-30 01:26:50 -07001470 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1471 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001472 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1473 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001474 ScopedObjectAccess soa(env);
1475 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001476 }
1477
Ian Rogersbc939662013-08-15 10:26:54 -07001478 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1479 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001480 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1481 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001482 ScopedObjectAccess soa(env);
Ian Rogers53b8b092014-03-13 23:45:53 -07001483 InvokeWithJValues(soa, soa.Decode<mirror::Object*>(obj), mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001484 }
1485
Ian Rogersbc939662013-08-15 10:26:54 -07001486 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001487 CHECK_NON_NULL_ARGUMENT(java_class);
1488 CHECK_NON_NULL_ARGUMENT(name);
1489 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001490 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001491 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001492 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001493
Ian Rogersbc939662013-08-15 10:26:54 -07001494 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1495 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001496 CHECK_NON_NULL_ARGUMENT(java_class);
1497 CHECK_NON_NULL_ARGUMENT(name);
1498 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001499 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001500 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001501 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001502
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001503 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001504 CHECK_NON_NULL_ARGUMENT(obj);
1505 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001506 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001507 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1508 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001509 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001510 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001511
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001512 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001513 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001514 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001515 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001516 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001517 }
1518
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001519 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001520 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1521 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001522 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001523 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1524 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1525 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001526 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001527 }
1528
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001529 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001530 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001531 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001532 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1533 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001534 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001535 }
1536
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001537#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001538 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1539 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001540 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001541 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1542 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001543 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001544
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001545#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001546 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001547 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001548 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001549 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001550
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001551#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001552 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1553 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001554 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001555 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1556 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001557 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001558
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001559#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001560 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001561 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001562 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001563 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001564
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001565 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001566 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001567 }
1568
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001569 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001570 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001571 }
1572
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001573 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001574 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001575 }
1576
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001577 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001578 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001579 }
1580
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001581 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001582 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001583 }
1584
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001585 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001586 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001587 }
1588
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001589 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001590 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001591 }
1592
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001593 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001594 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 }
1596
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001597 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001598 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 }
1600
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001601 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001602 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 }
1604
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001605 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001606 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 }
1608
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001609 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001610 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 }
1612
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001613 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001614 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001615 }
1616
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001617 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001618 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001619 }
1620
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001621 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001622 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001623 }
1624
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001625 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001626 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001627 }
1628
1629 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001630 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001631 }
1632
1633 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001634 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001635 }
1636
1637 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001638 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001639 }
1640
1641 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001642 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001643 }
1644
1645 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001646 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001647 }
1648
1649 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001650 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001651 }
1652
1653 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001654 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001655 }
1656
1657 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001658 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001659 }
1660
1661 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001662 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001663 }
1664
1665 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001666 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001667 }
1668
1669 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001670 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001671 }
1672
1673 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001674 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001675 }
1676
1677 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001678 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001679 }
1680
1681 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001682 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001683 }
1684
1685 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001686 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001687 }
1688
1689 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001690 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 }
1692
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001693 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001695 va_start(ap, mid);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001696 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001697 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001698 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001699 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 va_end(ap);
1701 return local_result;
1702 }
1703
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001704 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001705 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001706 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001707 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001708 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 }
1710
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001711 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001712 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001713 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001714 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001715 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001716 }
1717
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001718 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001720 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001721 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001722 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001723 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001725 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001726 }
1727
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001728 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001729 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001730 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001731 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 }
1733
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001734 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001735 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001736 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001737 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 }
1739
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001740 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001742 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001743 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001744 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001745 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001746 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001747 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 }
1749
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001750 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001751 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001752 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001753 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001754 }
1755
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001756 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001757 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001758 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001759 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 }
1761
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001762 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001763 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001764 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001765 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001766 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001767 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001768 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001769 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001770 }
1771
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001772 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001773 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001774 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001775 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001776 }
1777
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001778 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001779 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001780 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001781 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001782 }
1783
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001784 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001785 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001786 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001787 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001788 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001789 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001790 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001791 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001792 }
1793
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001794 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001795 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001796 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001797 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001798 }
1799
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001800 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001801 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001802 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001803 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001804 }
1805
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001806 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001807 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001808 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001809 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001810 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001811 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001812 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001813 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001814 }
1815
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001816 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001817 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001818 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001819 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 }
1821
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001822 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001823 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001824 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001825 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001826 }
1827
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001828 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001830 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001831 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001832 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001833 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001834 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001835 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001836 }
1837
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001838 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001839 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001840 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001841 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001842 }
1843
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001844 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001845 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001846 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001847 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001848 }
1849
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001850 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001851 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001852 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001853 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001854 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001855 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001856 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001857 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001858 }
1859
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001860 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001861 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001862 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001863 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001864 }
1865
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001866 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001867 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001868 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001869 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001870 }
1871
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001872 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001873 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001874 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001875 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001876 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001877 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001878 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001879 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001880 }
1881
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001882 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001883 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001884 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001885 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001886 }
1887
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001888 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001889 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001890 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001891 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001892 }
1893
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001894 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001895 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001896 va_start(ap, mid);
Ian Rogers2d10b202014-05-12 19:15:18 -07001897 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001898 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001899 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001900 va_end(ap);
1901 }
1902
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001903 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001904 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001905 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001906 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001907 }
1908
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001909 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001910 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001911 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001912 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001913 }
1914
Elliott Hughes814e4032011-08-23 12:07:56 -07001915 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001916 if (UNLIKELY(char_count < 0)) {
1917 JniAbortF("NewString", "char_count < 0: %d", char_count);
1918 return nullptr;
1919 }
1920 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1921 JniAbortF("NewString", "chars == null && char_count > 0");
1922 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001923 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001924 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001925 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001926 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001927 }
1928
1929 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001930 if (utf == nullptr) {
1931 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001932 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001933 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001934 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001935 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001936 }
1937
Elliott Hughes814e4032011-08-23 12:07:56 -07001938 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001939 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001940 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001941 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001942 }
1943
1944 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001945 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001946 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001947 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001948 }
1949
Ian Rogersbc939662013-08-15 10:26:54 -07001950 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1951 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001952 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001953 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001954 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001955 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001956 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001957 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001958 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001959 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1960 memcpy(buf, chars + start, length * sizeof(jchar));
1961 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001962 }
1963
Ian Rogersbc939662013-08-15 10:26:54 -07001964 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1965 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001966 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001967 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001968 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001969 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001970 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001971 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001972 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001973 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1974 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1975 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001976 }
1977
Elliott Hughes75770752011-08-24 17:52:38 -07001978 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001979 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001980 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001981 mirror::String* s = soa.Decode<mirror::String*>(java_string);
1982 mirror::CharArray* chars = s->GetCharArray();
Fred Shih56890e22014-06-02 11:11:52 -07001983 gc::Heap* heap = Runtime::Current()->GetHeap();
1984 if (heap->IsMovableObject(chars)) {
1985 if (is_copy != nullptr) {
1986 *is_copy = JNI_TRUE;
1987 }
1988 int32_t char_count = s->GetLength();
1989 int32_t offset = s->GetOffset();
1990 jchar* bytes = new jchar[char_count];
1991 for (int32_t i = 0; i < char_count; i++) {
1992 bytes[i] = chars->Get(i + offset);
1993 }
1994 return bytes;
1995 } else {
1996 if (is_copy != nullptr) {
1997 *is_copy = JNI_FALSE;
1998 }
1999 return static_cast<jchar*>(chars->GetData() + s->GetOffset());
Elliott Hughes75770752011-08-24 17:52:38 -07002000 }
Elliott Hughes814e4032011-08-23 12:07:56 -07002001 }
2002
Mathieu Chartier590fee92013-09-13 13:46:47 -07002003 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002004 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002005 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07002006 mirror::String* s = soa.Decode<mirror::String*>(java_string);
2007 mirror::CharArray* s_chars = s->GetCharArray();
2008 if (chars != (s_chars->GetData() + s->GetOffset())) {
2009 delete[] chars;
2010 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 }
2012
Elliott Hughes75770752011-08-24 17:52:38 -07002013 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Fred Shih56890e22014-06-02 11:11:52 -07002014 CHECK_NON_NULL_ARGUMENT(java_string);
2015 ScopedObjectAccess soa(env);
2016 mirror::String* s = soa.Decode<mirror::String*>(java_string);
2017 mirror::CharArray* chars = s->GetCharArray();
2018 int32_t offset = s->GetOffset();
Fred Shih56890e22014-06-02 11:11:52 -07002019 gc::Heap* heap = Runtime::Current()->GetHeap();
2020 if (heap->IsMovableObject(chars)) {
2021 StackHandleScope<1> hs(soa.Self());
2022 HandleWrapper<mirror::CharArray> h(hs.NewHandleWrapper(&chars));
2023 heap->IncrementDisableMovingGC(soa.Self());
2024 }
2025 if (is_copy != nullptr) {
2026 *is_copy = JNI_FALSE;
2027 }
2028 return static_cast<jchar*>(chars->GetData() + offset);
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 }
2030
Elliott Hughes75770752011-08-24 17:52:38 -07002031 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Fred Shih56890e22014-06-02 11:11:52 -07002032 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
2033 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07002034 gc::Heap* heap = Runtime::Current()->GetHeap();
2035 mirror::String* s = soa.Decode<mirror::String*>(java_string);
2036 mirror::CharArray* s_chars = s->GetCharArray();
2037 if (heap->IsMovableObject(s_chars)) {
2038 heap->DecrementDisableMovingGC(soa.Self());
2039 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002040 }
2041
Elliott Hughes75770752011-08-24 17:52:38 -07002042 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002043 if (java_string == nullptr) {
2044 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07002045 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002046 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07002047 *is_copy = JNI_TRUE;
2048 }
Ian Rogersef28b142012-11-30 14:22:18 -08002049 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002050 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002051 size_t byte_count = s->GetUtfLength();
2052 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002053 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07002054 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
2055 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
2056 bytes[byte_count] = '\0';
2057 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002058 }
2059
Elliott Hughes75770752011-08-24 17:52:38 -07002060 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002061 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002062 }
2063
Elliott Hughesbd935992011-08-22 11:59:34 -07002064 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002065 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002066 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002067 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07002068 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002069 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
2070 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002071 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07002072 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07002073 }
2074
Elliott Hughes814e4032011-08-23 12:07:56 -07002075 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002076 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002077 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002078 mirror::ObjectArray<mirror::Object>* array =
2079 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002080 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 }
2082
Ian Rogersbc939662013-08-15 10:26:54 -07002083 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2084 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002085 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002086 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002087 mirror::ObjectArray<mirror::Object>* array =
2088 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
2089 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002090 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 }
2092
2093 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002094 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002095 }
2096
2097 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002098 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 }
2100
2101 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002102 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 }
2104
2105 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002106 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 }
2108
2109 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002110 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002111 }
2112
2113 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002114 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002115 }
2116
2117 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002118 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 }
2120
Ian Rogers1d99e452014-01-02 17:36:41 -08002121 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2122 jobject initial_element) {
2123 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002124 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002125 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002126 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002127 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07002128
2129 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002130 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002131 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002132 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002133 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08002134 if (UNLIKELY(element_class->IsPrimitive())) {
2135 JniAbortF("NewObjectArray", "not an object type: %s",
2136 PrettyDescriptor(element_class).c_str());
2137 return nullptr;
2138 }
Ian Rogers1d99e452014-01-02 17:36:41 -08002139 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -07002140 array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08002141 if (UNLIKELY(array_class == nullptr)) {
2142 return nullptr;
2143 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002144 }
2145
Elliott Hughes75770752011-08-24 17:52:38 -07002146 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002147 mirror::ObjectArray<mirror::Object>* result =
2148 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002149 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002150 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002151 if (initial_object != nullptr) {
2152 mirror::Class* element_class = result->GetClass()->GetComponentType();
2153 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2154 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2155 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2156 PrettyDescriptor(element_class).c_str());
2157
2158 } else {
2159 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002160 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08002161 }
2162 }
Elliott Hughes75770752011-08-24 17:52:38 -07002163 }
2164 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002165 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002166 }
2167
2168 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002169 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002170 }
2171
Ian Rogersa15e67d2012-02-28 13:51:55 -08002172 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002173 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002174 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002175 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002176 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2177 JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
2178 PrettyDescriptor(array->GetClass()).c_str());
2179 return nullptr;
2180 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002181 gc::Heap* heap = Runtime::Current()->GetHeap();
2182 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002183 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002184 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002185 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002186 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002187 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002188 *is_copy = JNI_FALSE;
2189 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002190 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002191 }
2192
Ian Rogers2d10b202014-05-12 19:15:18 -07002193 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
2194 jint mode) {
2195 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2196 ScopedObjectAccess soa(env);
2197 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
2198 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2199 JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
2200 PrettyDescriptor(array->GetClass()).c_str());
2201 return;
2202 }
2203 const size_t component_size = array->GetClass()->GetComponentSize();
2204 ReleasePrimitiveArray(soa, array, component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002205 }
2206
Elliott Hughes75770752011-08-24 17:52:38 -07002207 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002208 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002209 }
2210
Elliott Hughes75770752011-08-24 17:52:38 -07002211 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002212 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002213 }
2214
Elliott Hughes75770752011-08-24 17:52:38 -07002215 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002216 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002217 }
2218
Elliott Hughes75770752011-08-24 17:52:38 -07002219 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002220 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002221 }
2222
Elliott Hughes75770752011-08-24 17:52:38 -07002223 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002224 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002225 }
2226
Elliott Hughes75770752011-08-24 17:52:38 -07002227 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002228 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002229 }
2230
Elliott Hughes75770752011-08-24 17:52:38 -07002231 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002232 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002233 }
2234
Elliott Hughes75770752011-08-24 17:52:38 -07002235 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002236 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002237 }
2238
Mathieu Chartier590fee92013-09-13 13:46:47 -07002239 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2240 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002241 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
2242 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002243 }
2244
Mathieu Chartier590fee92013-09-13 13:46:47 -07002245 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002246 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002247 }
2248
Mathieu Chartier590fee92013-09-13 13:46:47 -07002249 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002250 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002251 }
2252
Mathieu Chartier590fee92013-09-13 13:46:47 -07002253 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2254 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002255 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002256 }
2257
Mathieu Chartier590fee92013-09-13 13:46:47 -07002258 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2259 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002260 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002261 }
2262
Mathieu Chartier590fee92013-09-13 13:46:47 -07002263 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002264 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002265 }
2266
Mathieu Chartier590fee92013-09-13 13:46:47 -07002267 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002268 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002269 }
2270
Mathieu Chartier590fee92013-09-13 13:46:47 -07002271 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2272 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002273 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002274 }
2275
Ian Rogersbc939662013-08-15 10:26:54 -07002276 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2277 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002278 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002279 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002280 }
2281
Ian Rogersbc939662013-08-15 10:26:54 -07002282 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2283 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002284 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002285 }
2286
Ian Rogersbc939662013-08-15 10:26:54 -07002287 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2288 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002289 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002290 }
2291
Ian Rogersbc939662013-08-15 10:26:54 -07002292 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2293 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002294 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002295 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002296 }
2297
Ian Rogersbc939662013-08-15 10:26:54 -07002298 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2299 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002300 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002301 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002302 }
2303
Ian Rogersbc939662013-08-15 10:26:54 -07002304 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2305 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002306 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002307 }
2308
Ian Rogersbc939662013-08-15 10:26:54 -07002309 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2310 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002311 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002312 }
2313
Ian Rogersbc939662013-08-15 10:26:54 -07002314 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2315 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002316 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002317 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002318 }
2319
Ian Rogersbc939662013-08-15 10:26:54 -07002320 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2321 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002322 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002323 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002324 }
2325
Ian Rogersbc939662013-08-15 10:26:54 -07002326 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2327 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002328 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002329 }
2330
Ian Rogersbc939662013-08-15 10:26:54 -07002331 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2332 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002333 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002334 }
2335
Ian Rogersbc939662013-08-15 10:26:54 -07002336 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2337 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002338 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002339 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002340 }
2341
Ian Rogersbc939662013-08-15 10:26:54 -07002342 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2343 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002344 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002345 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002346 }
2347
Ian Rogersbc939662013-08-15 10:26:54 -07002348 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2349 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002350 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002351 }
2352
Ian Rogersbc939662013-08-15 10:26:54 -07002353 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2354 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002355 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002356 }
2357
Ian Rogersbc939662013-08-15 10:26:54 -07002358 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2359 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002360 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002361 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002362 }
2363
Ian Rogersbc939662013-08-15 10:26:54 -07002364 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2365 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002366 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2367 }
2368
Ian Rogersbc939662013-08-15 10:26:54 -07002369 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2370 jint method_count, bool return_errors) {
2371 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002372 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002373 return JNI_ERR; // Not reached.
2374 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002375 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002376 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002377 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002378 if (UNLIKELY(method_count == 0)) {
2379 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2380 << PrettyDescriptor(c);
2381 return JNI_OK;
2382 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002383 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002384 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002385 const char* name = methods[i].name;
2386 const char* sig = methods[i].signature;
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002387 const void* fnPtr = methods[i].fnPtr;
2388 if (UNLIKELY(name == nullptr)) {
2389 ReportInvalidJNINativeMethod(soa, c, "method name", i, return_errors);
2390 return JNI_ERR;
2391 } else if (UNLIKELY(sig == nullptr)) {
2392 ReportInvalidJNINativeMethod(soa, c, "method signature", i, return_errors);
2393 return JNI_ERR;
2394 } else if (UNLIKELY(fnPtr == nullptr)) {
2395 ReportInvalidJNINativeMethod(soa, c, "native function", i, return_errors);
2396 return JNI_ERR;
2397 }
Ian Rogers1eb512d2013-10-18 15:42:20 -07002398 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002399 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002400 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002401 ++sig;
2402 }
2403
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002404 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2405 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002406 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002407 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002408 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002409 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002410 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002411 << PrettyDescriptor(c) << "." << name << sig << " in "
2412 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002413 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002414 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002415 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002416 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002417 << PrettyDescriptor(c) << "." << name << sig
2418 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002419 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002420 return JNI_ERR;
2421 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002422
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002423 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002424
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002425 m->RegisterNative(soa.Self(), fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002426 }
2427 return JNI_OK;
2428 }
2429
Elliott Hughes5174fe62011-08-23 15:12:35 -07002430 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002431 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002432 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002433 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002434
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002435 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002436
Ian Rogers2d10b202014-05-12 19:15:18 -07002437 size_t unregistered_count = 0;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002438 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002439 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002440 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002441 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002442 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002443 }
2444 }
2445 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002446 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002447 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002448 m->UnregisterNative(soa.Self());
Ian Rogers2d10b202014-05-12 19:15:18 -07002449 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002450 }
2451 }
2452
Ian Rogers2d10b202014-05-12 19:15:18 -07002453 if (unregistered_count == 0) {
2454 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2455 << PrettyDescriptor(c) << "' that contains no native methods";
2456 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002457 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002458 }
2459
Ian Rogers719d1a32014-03-06 12:13:39 -08002460 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002461 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002462 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002463 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2464 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002465 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002466 return JNI_ERR;
2467 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002468 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002469 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002470 }
2471
Ian Rogers719d1a32014-03-06 12:13:39 -08002472 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002473 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002474 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002475 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002476 o->MonitorExit(soa.Self());
2477 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002478 return JNI_ERR;
2479 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002480 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002481 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002482 }
2483
2484 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002485 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002486 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002487 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002488 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002489 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002490 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002491 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002492 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002493 }
2494
Elliott Hughescdf53122011-08-19 15:46:09 -07002495 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002496 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002497 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002498 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002499 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002500 if (address == nullptr && capacity != 0) {
2501 JniAbortF("NewDirectByteBuffer", "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002502 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002503 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002504
Brian Carlstrom85a93362014-06-25 09:30:52 -07002505 // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002506 if (capacity > INT_MAX) {
2507 JniAbortF("NewDirectByteBuffer", "buffer capacity greater than maximum jint: %" PRId64, capacity);
2508 return nullptr;
2509 }
Elliott Hughesb5681212013-03-29 17:29:22 -07002510 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002511 jint capacity_arg = static_cast<jint>(capacity);
2512
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002513 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2514 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002515 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002516 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002517 }
2518
Elliott Hughesb465ab02011-08-24 11:21:21 -07002519 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002520 return reinterpret_cast<void*>(env->GetLongField(
2521 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002522 }
2523
Elliott Hughesb465ab02011-08-24 11:21:21 -07002524 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002525 return static_cast<jlong>(env->GetIntField(
2526 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002527 }
2528
Elliott Hughesb465ab02011-08-24 11:21:21 -07002529 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002530 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNIInvalidRefType);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002531
2532 // Do we definitely know what kind of reference this is?
2533 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2534 IndirectRefKind kind = GetIndirectRefKind(ref);
2535 switch (kind) {
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002536 case kLocal: {
2537 ScopedObjectAccess soa(env);
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07002538 // The local refs don't need a read barrier.
2539 if (static_cast<JNIEnvExt*>(env)->locals.Get<kWithoutReadBarrier>(ref) !=
2540 kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002541 return JNILocalRefType;
2542 }
2543 return JNIInvalidRefType;
Mathieu Chartierc645f1d2014-03-06 18:11:53 -08002544 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002545 case kGlobal:
2546 return JNIGlobalRefType;
2547 case kWeakGlobal:
2548 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002549 case kHandleScopeOrInvalid:
Elliott Hughesb465ab02011-08-24 11:21:21 -07002550 // Is it in a stack IRT?
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002551 if (static_cast<JNIEnvExt*>(env)->self->HandleScopeContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002552 return JNILocalRefType;
2553 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002554 return JNIInvalidRefType;
2555 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002556 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2557 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002558 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002559
2560 private:
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002561 static jint EnsureLocalCapacity(ScopedObjectAccess& soa, jint desired_capacity,
2562 const char* caller) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002563 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002564 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002565 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2566 return JNI_ERR;
2567 }
2568 // TODO: this isn't quite right, since "capacity" includes holes.
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002569 const size_t capacity = soa.Env()->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002570 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2571 if (!okay) {
2572 soa.Self()->ThrowOutOfMemoryError(caller);
2573 }
2574 return okay ? JNI_OK : JNI_ERR;
2575 }
2576
2577 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002578 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers1d99e452014-01-02 17:36:41 -08002579 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002580 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002581 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002582 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002583 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07002584 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002585 return soa.AddLocalReference<JniT>(result);
2586 }
2587
Ian Rogers2d10b202014-05-12 19:15:18 -07002588 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2589 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2590 const char* fn_name, const char* operation)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002591 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002592 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002593 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
2594 JniAbortF(fn_name, "attempt to %s %s primitive array elements with an object of type %s",
2595 operation, PrettyDescriptor(ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2596 PrettyDescriptor(array->GetClass()).c_str());
2597 return nullptr;
2598 }
2599 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2600 return array;
2601 }
2602
2603 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2604 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2605 CHECK_NON_NULL_ARGUMENT(java_array);
2606 ScopedObjectAccess soa(env);
2607 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2608 "GetArrayElements",
2609 "get");
2610 if (UNLIKELY(array == nullptr)) {
2611 return nullptr;
2612 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002613 // Only make a copy if necessary.
2614 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2615 if (is_copy != nullptr) {
2616 *is_copy = JNI_TRUE;
2617 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002618 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002619 size_t size = array->GetLength() * component_size;
2620 void* data = new uint64_t[RoundUp(size, 8) / 8];
2621 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002622 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002623 } else {
2624 if (is_copy != nullptr) {
2625 *is_copy = JNI_FALSE;
2626 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002627 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002628 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002629 }
2630
Ian Rogers2d10b202014-05-12 19:15:18 -07002631 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002632 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002633 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002634 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002635 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2636 "ReleaseArrayElements",
2637 "release");
2638 if (array == nullptr) {
2639 return;
2640 }
2641 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2642 }
2643
2644 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2645 size_t component_size, void* elements, jint mode)
2646 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002647 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002648 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002649 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002650 size_t bytes = array->GetLength() * component_size;
Ian Rogers2d10b202014-05-12 19:15:18 -07002651 VLOG(heap) << "Release primitive array " << soa.Env() << " array_data " << array_data
2652 << " elements " << elements;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002653 if (is_copy) {
2654 // Sanity check: If elements is not the same as the java array's data, it better not be a
2655 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2656 // copies we make?
2657 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
2658 JniAbortF("ReleaseArrayElements", "invalid element pointer %p, array elements are %p",
2659 reinterpret_cast<void*>(elements), array_data);
2660 return;
2661 }
2662 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002663 // Don't need to copy if we had a direct pointer.
2664 if (mode != JNI_ABORT && is_copy) {
2665 memcpy(array_data, elements, bytes);
2666 }
2667 if (mode != JNI_COMMIT) {
2668 if (is_copy) {
2669 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002670 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002671 // Non copy to a movable object must means that we had disabled the moving GC.
2672 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002673 }
2674 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002675 }
2676
Ian Rogers2d10b202014-05-12 19:15:18 -07002677 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2678 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2679 jsize start, jsize length, ElementT* buf) {
2680 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2681 ScopedObjectAccess soa(env);
2682 ArtArrayT* array =
2683 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2684 "GetPrimitiveArrayRegion",
2685 "get region of");
2686 if (array != nullptr) {
2687 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2688 ThrowAIOOBE(soa, array, start, length, "src");
2689 } else {
2690 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2691 ElementT* data = array->GetData();
2692 memcpy(buf, data + start, length * sizeof(ElementT));
2693 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002694 }
2695 }
2696
Ian Rogers2d10b202014-05-12 19:15:18 -07002697 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2698 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2699 jsize start, jsize length, const ElementT* buf) {
2700 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2701 ScopedObjectAccess soa(env);
2702 ArtArrayT* array =
2703 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2704 "SetPrimitiveArrayRegion",
2705 "set region of");
2706 if (array != nullptr) {
2707 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2708 ThrowAIOOBE(soa, array, start, length, "dst");
2709 } else {
2710 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2711 ElementT* data = array->GetData();
2712 memcpy(data + start, buf, length * sizeof(ElementT));
2713 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002714 }
2715 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002716};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002717
Elliott Hughes88c5c352012-03-15 18:49:48 -07002718const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002719 nullptr, // reserved0.
2720 nullptr, // reserved1.
2721 nullptr, // reserved2.
2722 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002723 JNI::GetVersion,
2724 JNI::DefineClass,
2725 JNI::FindClass,
2726 JNI::FromReflectedMethod,
2727 JNI::FromReflectedField,
2728 JNI::ToReflectedMethod,
2729 JNI::GetSuperclass,
2730 JNI::IsAssignableFrom,
2731 JNI::ToReflectedField,
2732 JNI::Throw,
2733 JNI::ThrowNew,
2734 JNI::ExceptionOccurred,
2735 JNI::ExceptionDescribe,
2736 JNI::ExceptionClear,
2737 JNI::FatalError,
2738 JNI::PushLocalFrame,
2739 JNI::PopLocalFrame,
2740 JNI::NewGlobalRef,
2741 JNI::DeleteGlobalRef,
2742 JNI::DeleteLocalRef,
2743 JNI::IsSameObject,
2744 JNI::NewLocalRef,
2745 JNI::EnsureLocalCapacity,
2746 JNI::AllocObject,
2747 JNI::NewObject,
2748 JNI::NewObjectV,
2749 JNI::NewObjectA,
2750 JNI::GetObjectClass,
2751 JNI::IsInstanceOf,
2752 JNI::GetMethodID,
2753 JNI::CallObjectMethod,
2754 JNI::CallObjectMethodV,
2755 JNI::CallObjectMethodA,
2756 JNI::CallBooleanMethod,
2757 JNI::CallBooleanMethodV,
2758 JNI::CallBooleanMethodA,
2759 JNI::CallByteMethod,
2760 JNI::CallByteMethodV,
2761 JNI::CallByteMethodA,
2762 JNI::CallCharMethod,
2763 JNI::CallCharMethodV,
2764 JNI::CallCharMethodA,
2765 JNI::CallShortMethod,
2766 JNI::CallShortMethodV,
2767 JNI::CallShortMethodA,
2768 JNI::CallIntMethod,
2769 JNI::CallIntMethodV,
2770 JNI::CallIntMethodA,
2771 JNI::CallLongMethod,
2772 JNI::CallLongMethodV,
2773 JNI::CallLongMethodA,
2774 JNI::CallFloatMethod,
2775 JNI::CallFloatMethodV,
2776 JNI::CallFloatMethodA,
2777 JNI::CallDoubleMethod,
2778 JNI::CallDoubleMethodV,
2779 JNI::CallDoubleMethodA,
2780 JNI::CallVoidMethod,
2781 JNI::CallVoidMethodV,
2782 JNI::CallVoidMethodA,
2783 JNI::CallNonvirtualObjectMethod,
2784 JNI::CallNonvirtualObjectMethodV,
2785 JNI::CallNonvirtualObjectMethodA,
2786 JNI::CallNonvirtualBooleanMethod,
2787 JNI::CallNonvirtualBooleanMethodV,
2788 JNI::CallNonvirtualBooleanMethodA,
2789 JNI::CallNonvirtualByteMethod,
2790 JNI::CallNonvirtualByteMethodV,
2791 JNI::CallNonvirtualByteMethodA,
2792 JNI::CallNonvirtualCharMethod,
2793 JNI::CallNonvirtualCharMethodV,
2794 JNI::CallNonvirtualCharMethodA,
2795 JNI::CallNonvirtualShortMethod,
2796 JNI::CallNonvirtualShortMethodV,
2797 JNI::CallNonvirtualShortMethodA,
2798 JNI::CallNonvirtualIntMethod,
2799 JNI::CallNonvirtualIntMethodV,
2800 JNI::CallNonvirtualIntMethodA,
2801 JNI::CallNonvirtualLongMethod,
2802 JNI::CallNonvirtualLongMethodV,
2803 JNI::CallNonvirtualLongMethodA,
2804 JNI::CallNonvirtualFloatMethod,
2805 JNI::CallNonvirtualFloatMethodV,
2806 JNI::CallNonvirtualFloatMethodA,
2807 JNI::CallNonvirtualDoubleMethod,
2808 JNI::CallNonvirtualDoubleMethodV,
2809 JNI::CallNonvirtualDoubleMethodA,
2810 JNI::CallNonvirtualVoidMethod,
2811 JNI::CallNonvirtualVoidMethodV,
2812 JNI::CallNonvirtualVoidMethodA,
2813 JNI::GetFieldID,
2814 JNI::GetObjectField,
2815 JNI::GetBooleanField,
2816 JNI::GetByteField,
2817 JNI::GetCharField,
2818 JNI::GetShortField,
2819 JNI::GetIntField,
2820 JNI::GetLongField,
2821 JNI::GetFloatField,
2822 JNI::GetDoubleField,
2823 JNI::SetObjectField,
2824 JNI::SetBooleanField,
2825 JNI::SetByteField,
2826 JNI::SetCharField,
2827 JNI::SetShortField,
2828 JNI::SetIntField,
2829 JNI::SetLongField,
2830 JNI::SetFloatField,
2831 JNI::SetDoubleField,
2832 JNI::GetStaticMethodID,
2833 JNI::CallStaticObjectMethod,
2834 JNI::CallStaticObjectMethodV,
2835 JNI::CallStaticObjectMethodA,
2836 JNI::CallStaticBooleanMethod,
2837 JNI::CallStaticBooleanMethodV,
2838 JNI::CallStaticBooleanMethodA,
2839 JNI::CallStaticByteMethod,
2840 JNI::CallStaticByteMethodV,
2841 JNI::CallStaticByteMethodA,
2842 JNI::CallStaticCharMethod,
2843 JNI::CallStaticCharMethodV,
2844 JNI::CallStaticCharMethodA,
2845 JNI::CallStaticShortMethod,
2846 JNI::CallStaticShortMethodV,
2847 JNI::CallStaticShortMethodA,
2848 JNI::CallStaticIntMethod,
2849 JNI::CallStaticIntMethodV,
2850 JNI::CallStaticIntMethodA,
2851 JNI::CallStaticLongMethod,
2852 JNI::CallStaticLongMethodV,
2853 JNI::CallStaticLongMethodA,
2854 JNI::CallStaticFloatMethod,
2855 JNI::CallStaticFloatMethodV,
2856 JNI::CallStaticFloatMethodA,
2857 JNI::CallStaticDoubleMethod,
2858 JNI::CallStaticDoubleMethodV,
2859 JNI::CallStaticDoubleMethodA,
2860 JNI::CallStaticVoidMethod,
2861 JNI::CallStaticVoidMethodV,
2862 JNI::CallStaticVoidMethodA,
2863 JNI::GetStaticFieldID,
2864 JNI::GetStaticObjectField,
2865 JNI::GetStaticBooleanField,
2866 JNI::GetStaticByteField,
2867 JNI::GetStaticCharField,
2868 JNI::GetStaticShortField,
2869 JNI::GetStaticIntField,
2870 JNI::GetStaticLongField,
2871 JNI::GetStaticFloatField,
2872 JNI::GetStaticDoubleField,
2873 JNI::SetStaticObjectField,
2874 JNI::SetStaticBooleanField,
2875 JNI::SetStaticByteField,
2876 JNI::SetStaticCharField,
2877 JNI::SetStaticShortField,
2878 JNI::SetStaticIntField,
2879 JNI::SetStaticLongField,
2880 JNI::SetStaticFloatField,
2881 JNI::SetStaticDoubleField,
2882 JNI::NewString,
2883 JNI::GetStringLength,
2884 JNI::GetStringChars,
2885 JNI::ReleaseStringChars,
2886 JNI::NewStringUTF,
2887 JNI::GetStringUTFLength,
2888 JNI::GetStringUTFChars,
2889 JNI::ReleaseStringUTFChars,
2890 JNI::GetArrayLength,
2891 JNI::NewObjectArray,
2892 JNI::GetObjectArrayElement,
2893 JNI::SetObjectArrayElement,
2894 JNI::NewBooleanArray,
2895 JNI::NewByteArray,
2896 JNI::NewCharArray,
2897 JNI::NewShortArray,
2898 JNI::NewIntArray,
2899 JNI::NewLongArray,
2900 JNI::NewFloatArray,
2901 JNI::NewDoubleArray,
2902 JNI::GetBooleanArrayElements,
2903 JNI::GetByteArrayElements,
2904 JNI::GetCharArrayElements,
2905 JNI::GetShortArrayElements,
2906 JNI::GetIntArrayElements,
2907 JNI::GetLongArrayElements,
2908 JNI::GetFloatArrayElements,
2909 JNI::GetDoubleArrayElements,
2910 JNI::ReleaseBooleanArrayElements,
2911 JNI::ReleaseByteArrayElements,
2912 JNI::ReleaseCharArrayElements,
2913 JNI::ReleaseShortArrayElements,
2914 JNI::ReleaseIntArrayElements,
2915 JNI::ReleaseLongArrayElements,
2916 JNI::ReleaseFloatArrayElements,
2917 JNI::ReleaseDoubleArrayElements,
2918 JNI::GetBooleanArrayRegion,
2919 JNI::GetByteArrayRegion,
2920 JNI::GetCharArrayRegion,
2921 JNI::GetShortArrayRegion,
2922 JNI::GetIntArrayRegion,
2923 JNI::GetLongArrayRegion,
2924 JNI::GetFloatArrayRegion,
2925 JNI::GetDoubleArrayRegion,
2926 JNI::SetBooleanArrayRegion,
2927 JNI::SetByteArrayRegion,
2928 JNI::SetCharArrayRegion,
2929 JNI::SetShortArrayRegion,
2930 JNI::SetIntArrayRegion,
2931 JNI::SetLongArrayRegion,
2932 JNI::SetFloatArrayRegion,
2933 JNI::SetDoubleArrayRegion,
2934 JNI::RegisterNatives,
2935 JNI::UnregisterNatives,
2936 JNI::MonitorEnter,
2937 JNI::MonitorExit,
2938 JNI::GetJavaVM,
2939 JNI::GetStringRegion,
2940 JNI::GetStringUTFRegion,
2941 JNI::GetPrimitiveArrayCritical,
2942 JNI::ReleasePrimitiveArrayCritical,
2943 JNI::GetStringCritical,
2944 JNI::ReleaseStringCritical,
2945 JNI::NewWeakGlobalRef,
2946 JNI::DeleteWeakGlobalRef,
2947 JNI::ExceptionCheck,
2948 JNI::NewDirectByteBuffer,
2949 JNI::GetDirectBufferAddress,
2950 JNI::GetDirectBufferCapacity,
2951 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002952};
2953
Elliott Hughes75770752011-08-24 17:52:38 -07002954JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002955 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002956 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002957 local_ref_cookie(IRT_FIRST_SEGMENT),
2958 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002959 check_jni(false),
Ian Rogersdd7624d2014-03-14 17:43:00 -07002960 critical(0),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002961 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002962 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002963 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002964 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002965 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002966}
2967
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002968JNIEnvExt::~JNIEnvExt() {
2969}
2970
Mathieu Chartier590fee92013-09-13 13:46:47 -07002971jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2972 if (obj == nullptr) {
2973 return nullptr;
2974 }
2975 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2976}
2977
2978void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2979 if (obj != nullptr) {
2980 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
2981 }
2982}
Elliott Hughes88c5c352012-03-15 18:49:48 -07002983void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2984 check_jni = enabled;
2985 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002986}
2987
Elliott Hughes73e66f72012-05-09 09:34:45 -07002988void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2989 locals.Dump(os);
2990 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002991}
2992
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07002993void JNIEnvExt::PushFrame(int capacity) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2994 UNUSED(capacity); // cpplint gets confused with (int) and thinks its a cast.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002995 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002996 stacked_local_ref_cookies.push_back(local_ref_cookie);
2997 local_ref_cookie = locals.GetSegmentState();
2998}
2999
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +07003000void JNIEnvExt::PopFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003001 locals.SetSegmentState(local_ref_cookie);
3002 local_ref_cookie = stacked_local_ref_cookies.back();
3003 stacked_local_ref_cookies.pop_back();
3004}
3005
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003006Offset JNIEnvExt::SegmentStateOffset() {
3007 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
3008 IndirectReferenceTable::SegmentStateOffset().Int32Value());
3009}
3010
Carl Shapiroea4dca82011-08-01 13:45:38 -07003011// JNI Invocation interface.
3012
Brian Carlstrombddf9762013-05-14 11:35:37 -07003013extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003014 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07003015 if (IsBadJniVersion(args->version)) {
3016 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003017 return JNI_EVERSION;
3018 }
Ian Rogerse63db272014-07-15 15:36:11 -07003019 RuntimeOptions options;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003020 for (int i = 0; i < args->nOptions; ++i) {
3021 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08003022 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003023 }
3024 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003025 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003026 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003027 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003028 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07003029 bool started = runtime->Start();
3030 if (!started) {
3031 delete Thread::Current()->GetJniEnv();
3032 delete runtime->GetJavaVM();
3033 LOG(WARNING) << "CreateJavaVM failed";
3034 return JNI_ERR;
3035 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07003036 *p_env = Thread::Current()->GetJniEnv();
3037 *p_vm = runtime->GetJavaVM();
3038 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003039}
3040
Elliott Hughesf2682d52011-08-15 16:37:04 -07003041extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003042 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003043 if (runtime == nullptr) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003044 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003045 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003046 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003047 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003048 }
3049 return JNI_OK;
3050}
3051
3052// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003053extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003054 return JNI_ERR;
3055}
3056
Elliott Hughescdf53122011-08-19 15:46:09 -07003057class JII {
3058 public:
3059 static jint DestroyJavaVM(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003060 if (vm == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003061 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003062 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003063 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3064 delete raw_vm->runtime;
3065 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003066 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003067
Elliott Hughescdf53122011-08-19 15:46:09 -07003068 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003069 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07003070 }
3071
3072 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003073 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07003074 }
3075
3076 static jint DetachCurrentThread(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003077 if (vm == nullptr || Thread::Current() == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003078 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003079 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003080 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3081 Runtime* runtime = raw_vm->runtime;
3082 runtime->DetachCurrentThread();
3083 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07003084 }
3085
3086 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07003087 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
3088 // and unlike other calls that take a JNI version doesn't care if you supply
3089 // JNI_VERSION_1_1, which we don't otherwise support.
3090 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07003091 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07003092 return JNI_EVERSION;
3093 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003094 if (vm == nullptr || env == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003095 return JNI_ERR;
3096 }
3097 Thread* thread = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003098 if (thread == nullptr) {
3099 *env = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07003100 return JNI_EDETACHED;
3101 }
3102 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003103 return JNI_OK;
3104 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003105};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003106
Elliott Hughes88c5c352012-03-15 18:49:48 -07003107const JNIInvokeInterface gJniInvokeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003108 nullptr, // reserved0
3109 nullptr, // reserved1
3110 nullptr, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07003111 JII::DestroyJavaVM,
3112 JII::AttachCurrentThread,
3113 JII::DetachCurrentThread,
3114 JII::GetEnv,
3115 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07003116};
3117
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08003118JavaVMExt::JavaVMExt(Runtime* runtime, ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003119 : runtime(runtime),
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003120 check_jni_abort_hook(nullptr),
3121 check_jni_abort_hook_data(nullptr),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003122 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003123 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07003124 trace(options->jni_trace_),
Elliott Hughes8daa0922011-09-11 13:46:25 -07003125 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07003126 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003127 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003128 libraries(new Libraries),
3129 weak_globals_lock_("JNI weak global reference table lock"),
3130 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
3131 allow_new_weak_globals_(true),
3132 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003133 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003134 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003135 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003136 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003137}
3138
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003139JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003140 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003141}
3142
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003143jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3144 if (obj == nullptr) {
3145 return nullptr;
3146 }
3147 MutexLock mu(self, weak_globals_lock_);
3148 while (UNLIKELY(!allow_new_weak_globals_)) {
3149 weak_globals_add_condition_.WaitHoldingLocks(self);
3150 }
3151 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3152 return reinterpret_cast<jweak>(ref);
3153}
3154
3155void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3156 MutexLock mu(self, weak_globals_lock_);
3157 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3158 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3159 << "failed to find entry";
3160 }
3161}
3162
Elliott Hughes88c5c352012-03-15 18:49:48 -07003163void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3164 check_jni = enabled;
3165 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003166}
3167
Elliott Hughesae80b492012-04-24 10:43:17 -07003168void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3169 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3170 if (force_copy) {
3171 os << " (with forcecopy)";
3172 }
Ian Rogers50b35e22012-10-04 10:09:15 -07003173 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003174 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003175 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003176 os << "; globals=" << globals.Capacity();
3177 }
3178 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003179 MutexLock mu(self, weak_globals_lock_);
3180 if (weak_globals_.Capacity() > 0) {
3181 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003182 }
3183 }
3184 os << '\n';
3185
3186 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003187 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003188 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3189 }
3190}
3191
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003192void JavaVMExt::DisallowNewWeakGlobals() {
3193 MutexLock mu(Thread::Current(), weak_globals_lock_);
3194 allow_new_weak_globals_ = false;
3195}
3196
3197void JavaVMExt::AllowNewWeakGlobals() {
3198 Thread* self = Thread::Current();
3199 MutexLock mu(self, weak_globals_lock_);
3200 allow_new_weak_globals_ = true;
3201 weak_globals_add_condition_.Broadcast(self);
3202}
3203
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003204mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3205 MutexLock mu(self, weak_globals_lock_);
3206 while (UNLIKELY(!allow_new_weak_globals_)) {
3207 weak_globals_add_condition_.WaitHoldingLocks(self);
3208 }
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -07003209 return weak_globals_.Get(ref);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003210}
3211
Elliott Hughes73e66f72012-05-09 09:34:45 -07003212void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003213 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003214 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003215 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003216 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003217 }
3218 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003219 MutexLock mu(self, weak_globals_lock_);
3220 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003221 }
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003222}
3223
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003224bool JavaVMExt::LoadNativeLibrary(const std::string& path,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07003225 Handle<mirror::ClassLoader> class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003226 std::string* detail) {
3227 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003228
3229 // See if we've already loaded this library. If we have, and the class loader
3230 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003231 // TODO: for better results we should canonicalize the pathname (or even compare
3232 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003233 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003234 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003235 {
3236 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003237 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003238 library = libraries->Get(path);
3239 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003240 if (library != nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003241 if (library->GetClassLoader() != class_loader.Get()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003242 // The library will be associated with class_loader. The JNI
3243 // spec says we can't load the same library into more than one
3244 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003245 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003246 "ClassLoader %p; can't open in ClassLoader %p",
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003247 path.c_str(), library->GetClassLoader(), class_loader.Get());
Elliott Hughes75770752011-08-24 17:52:38 -07003248 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003249 return false;
3250 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003251 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003252 << "ClassLoader " << class_loader.Get() << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003253 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003254 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003255 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003256 return false;
3257 }
3258 return true;
3259 }
3260
3261 // Open the shared library. Because we're using a full path, the system
3262 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3263 // resolve this library's dependencies though.)
3264
3265 // Failures here are expected when java.library.path has several entries
3266 // and we have to hunt for the lib.
3267
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003268 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3269 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3270 // dlopen) becomes zero from dlclose.
3271
Elliott Hughescdf53122011-08-19 15:46:09 -07003272 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003273 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003274 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
Yong WU355383f2014-07-24 21:32:15 +08003275 const char* path_str = path.empty() ? nullptr : path.c_str();
3276 void* handle = dlopen(path_str, RTLD_LAZY);
3277 bool needs_native_bridge = false;
3278 if (handle == nullptr) {
Calin Juravle93de4272014-08-12 20:55:20 +01003279 if (android::NativeBridgeIsSupported(path_str)) {
3280 handle = android::NativeBridgeLoadLibrary(path_str, RTLD_LAZY);
Yong WU355383f2014-07-24 21:32:15 +08003281 needs_native_bridge = true;
3282 }
3283 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003284 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003285
Elliott Hughes84b2f142012-09-27 09:16:28 -07003286 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003287
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003288 if (handle == nullptr) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003289 *detail = dlerror();
Colin Cross35d5c3b2014-04-23 14:56:31 -07003290 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << *detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003291 return false;
3292 }
3293
3294 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003295 // TODO: move the locking (and more of this logic) into Libraries.
3296 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003297 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003298 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003299 library = libraries->Get(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003300 if (library == nullptr) { // We won race to get libraries_lock
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003301 library = new SharedLibrary(path, handle, class_loader.Get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003302 libraries->Put(path, library);
3303 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003304 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003305 }
3306 if (!created_library) {
3307 LOG(INFO) << "WOW: we lost a race to add shared library: "
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003308 << "\"" << path << "\" ClassLoader=" << class_loader.Get();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003309 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003310 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003311
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003312 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader.Get()
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003313 << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003314
Elliott Hughes79353722013-08-02 16:52:18 -07003315 bool was_successful = false;
Yong WU355383f2014-07-24 21:32:15 +08003316 void* sym = nullptr;
3317 if (UNLIKELY(needs_native_bridge)) {
3318 library->SetNeedsNativeBridge();
3319 sym = library->FindSymbolWithNativeBridge("JNI_OnLoad", nullptr);
3320 } else {
3321 sym = dlsym(handle, "JNI_OnLoad");
3322 }
3323
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003324 if (sym == nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003325 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003326 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003327 } else {
3328 // Call JNI_OnLoad. We have to override the current class
3329 // loader, which will always be "null" since the stuff at the
3330 // top of the stack is around Runtime.loadLibrary(). (See
3331 // the comments in the JNI FindClass function.)
3332 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3333 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003334 StackHandleScope<1> hs(self);
3335 Handle<mirror::ClassLoader> old_class_loader(hs.NewHandle(self->GetClassLoaderOverride()));
3336 self->SetClassLoaderOverride(class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003337
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003338 int version = 0;
3339 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003340 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003341 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003342 version = (*jni_on_load)(this, nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -07003343 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003344
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003345 self->SetClassLoaderOverride(old_class_loader.Get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003346
Elliott Hughes79353722013-08-02 16:52:18 -07003347 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003348 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003349 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003350 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003351 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003352 // It's unwise to call dlclose() here, but we can mark it
3353 // as bad and ensure that future load attempts will fail.
3354 // We don't know how far JNI_OnLoad got, so there could
3355 // be some partially-initialized stuff accessible through
3356 // newly-registered native method calls. We could try to
3357 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003358 } else {
3359 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003360 }
Elliott Hughes79353722013-08-02 16:52:18 -07003361 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003362 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003363 }
3364
Elliott Hughes79353722013-08-02 16:52:18 -07003365 library->SetResult(was_successful);
3366 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003367}
3368
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003369void* JavaVMExt::FindCodeForNativeMethod(mirror::ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003370 CHECK(m->IsNative());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003371 mirror::Class* c = m->GetDeclaringClass();
Ian Rogers6c5cb212014-06-18 16:07:20 -07003372 // If this is a static method, it could be called before the class has been initialized.
Elliott Hughes79082e32011-08-25 12:07:32 -07003373 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003374 c = EnsureInitialized(Thread::Current(), c);
3375 if (c == nullptr) {
3376 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003377 }
3378 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003379 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003380 }
Brian Carlstrom16192862011-09-12 17:50:06 -07003381 std::string detail;
3382 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003383 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003384 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003385 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003386 native_method = libraries->FindNativeMethod(m, detail);
3387 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003388 // Throwing can cause libraries_lock to be reacquired.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003389 if (native_method == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003390 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3391 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003392 }
3393 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003394}
3395
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003396void JavaVMExt::SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003397 MutexLock mu(Thread::Current(), weak_globals_lock_);
3398 for (mirror::Object** entry : weak_globals_) {
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -07003399 // Since this is called by the GC, we don't need a read barrier.
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003400 mirror::Object* obj = *entry;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003401 mirror::Object* new_obj = callback(obj, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003402 if (new_obj == nullptr) {
3403 new_obj = kClearedJniWeakGlobal;
3404 }
3405 *entry = new_obj;
3406 }
3407}
3408
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003409void JavaVMExt::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003410 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003411 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003412 ReaderMutexLock mu(self, globals_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003413 globals.VisitRoots(callback, arg, 0, kRootJNIGlobal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003414 }
3415 {
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003416 MutexLock mu(self, libraries_lock);
3417 // Libraries contains shared libraries which hold a pointer to a class loader.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003418 libraries->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003419 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07003420 // The weak_globals table is visited by the GC itself (because it mutates the table).
3421}
3422
Elliott Hughesc8fece32013-01-02 11:27:23 -08003423void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003424 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003425 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003426 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003427 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3428 }
3429 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3430}
3431
Ian Rogersdf20fe02011-07-20 20:34:16 -07003432} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003433
3434std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3435 switch (rhs) {
3436 case JNIInvalidRefType:
3437 os << "JNIInvalidRefType";
3438 return os;
3439 case JNILocalRefType:
3440 os << "JNILocalRefType";
3441 return os;
3442 case JNIGlobalRefType:
3443 os << "JNIGlobalRefType";
3444 return os;
3445 case JNIWeakGlobalRefType:
3446 os << "JNIWeakGlobalRefType";
3447 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003448 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003449 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003450 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003451 }
3452}