blob: 37fb2dba044ffd9e0b35286c092b66e633bcad4f [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>
Elliott Hughes0af55432011-08-17 18:37:28 -070022#include <utility>
23#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024
Ian Rogersef7d42f2014-01-06 12:55:46 -080025#include "atomic.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080026#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080027#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080028#include "base/stl_util.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080029#include "base/stringpiece.h"
Ian Rogers98379392014-02-24 16:53:16 -080030#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070031#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070032#include "gc/accounting/card_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070033#include "interpreter/interpreter.h"
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070034#include "invoke_arg_array_builder.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070035#include "jni.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070036#include "mirror/art_field-inl.h"
37#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/class-inl.h"
39#include "mirror/class_loader.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040#include "mirror/object-inl.h"
41#include "mirror/object_array-inl.h"
42#include "mirror/throwable.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080043#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070044#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070045#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070046#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070047#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070048#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049#include "utf.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070050#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070051#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070052
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070053namespace art {
54
Brian Carlstrom7934ac22013-07-26 10:54:15 -070055static const size_t kMonitorsInitial = 32; // Arbitrary.
56static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070057
Brian Carlstrom7934ac22013-07-26 10:54:15 -070058static const size_t kLocalsInitial = 64; // Arbitrary.
59static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070060
Brian Carlstrom7934ac22013-07-26 10:54:15 -070061static const size_t kPinTableInitial = 16; // Arbitrary.
62static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070063
Brian Carlstrom7934ac22013-07-26 10:54:15 -070064static size_t gGlobalsInitial = 512; // Arbitrary.
65static size_t gGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Elliott Hughes2ced6a52011-10-16 18:44:48 -070066
Brian Carlstrom7934ac22013-07-26 10:54:15 -070067static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
68static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Ian Rogersdf20fe02011-07-20 20:34:16 -070069
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080070static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070071 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070072 return soa.Vm()->AddWeakGlobalReference(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -070073}
74
Jeff Hao19c5d372013-03-15 14:33:43 -070075static bool IsBadJniVersion(int version) {
76 // We don't support JNI_VERSION_1_1. These are the only other valid versions.
77 return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
78}
79
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080080static void CheckMethodArguments(mirror::ArtMethod* m, uint32_t* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -070081 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080082 const DexFile::TypeList* params = MethodHelper(m).GetParameterTypeList();
83 if (params == nullptr) {
Ian Rogers50b35e22012-10-04 10:09:15 -070084 return; // No arguments so nothing to check.
85 }
Jeff Hao5d917302013-02-27 17:57:33 -080086 uint32_t offset = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -070087 uint32_t num_params = params->Size();
Elliott Hughesb264f082012-04-06 17:10:10 -070088 size_t error_count = 0;
Jeff Hao5d917302013-02-27 17:57:33 -080089 if (!m->IsStatic()) {
90 offset = 1;
91 }
Ian Rogers50b35e22012-10-04 10:09:15 -070092 for (uint32_t i = 0; i < num_params; i++) {
93 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080094 mirror::Class* param_type = MethodHelper(m).GetClassFromTypeIdx(type_idx);
95 if (param_type == nullptr) {
Ian Rogers50b35e22012-10-04 10:09:15 -070096 Thread* self = Thread::Current();
97 CHECK(self->IsExceptionPending());
98 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -080099 << MethodHelper(m).GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
100 << self->GetException(nullptr)->Dump();
Ian Rogers50b35e22012-10-04 10:09:15 -0700101 self->ClearException();
102 ++error_count;
103 } else if (!param_type->IsPrimitive()) {
104 // TODO: check primitives are in range.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800105 mirror::Object* argument = reinterpret_cast<mirror::Object*>(args[i + offset]);
106 if (argument != nullptr && !argument->InstanceOf(param_type)) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700107 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
108 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
109 ++error_count;
110 }
Jeff Hao5d917302013-02-27 17:57:33 -0800111 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
112 offset++;
Elliott Hughesb264f082012-04-06 17:10:10 -0700113 }
114 }
115 if (error_count > 0) {
116 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
117 // with an argument.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800118 JniAbortF(nullptr, "bad arguments passed to %s (see above for details)", PrettyMethod(m).c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -0700119 }
120}
Elliott Hughesb264f082012-04-06 17:10:10 -0700121
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800122void InvokeWithArgArray(const ScopedObjectAccess& soa, mirror::ArtMethod* method,
Ian Rogers0177e532014-02-11 16:30:46 -0800123 ArgArray* arg_array, JValue* result, const char* shorty)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700124 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700125 uint32_t* args = arg_array->GetArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700126 if (UNLIKELY(soa.Env()->check_jni)) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700127 CheckMethodArguments(method, args);
Elliott Hughes4cacde82012-04-11 18:32:27 -0700128 }
Ian Rogers0177e532014-02-11 16:30:46 -0800129 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, shorty);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700130}
131
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700132static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
133 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700134 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800135 mirror::ArtMethod* method = soa.DecodeMethod(mid);
136 mirror::Object* receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700137 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800138 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700139 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800140 arg_array.BuildArgArray(soa, receiver, args);
Ian Rogers0177e532014-02-11 16:30:46 -0800141 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty());
Jeff Hao6474d192013-03-26 14:08:09 -0700142 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700143}
144
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800145static mirror::ArtMethod* FindVirtualMethod(mirror::Object* receiver, mirror::ArtMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700146 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700147 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700148}
149
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700150static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
151 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700152 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800153 mirror::Object* receiver = soa.Decode<mirror::Object*>(obj);
154 mirror::ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700155 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800156 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700157 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800158 arg_array.BuildArgArray(soa, receiver, args);
Ian Rogers0177e532014-02-11 16:30:46 -0800159 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty());
Jeff Hao6474d192013-03-26 14:08:09 -0700160 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700161}
162
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700163static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
164 jobject obj, jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700165 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800166 mirror::Object* receiver = soa.Decode<mirror::Object*>(obj);
167 mirror::ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700168 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800169 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700170 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800171 arg_array.BuildArgArray(soa, receiver, args);
Ian Rogers0177e532014-02-11 16:30:46 -0800172 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty());
Jeff Hao6474d192013-03-26 14:08:09 -0700173 return result;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700174}
175
Elliott Hughes6b436852011-08-12 10:16:44 -0700176// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
177// separated with slashes but aren't wrapped with "L;" like regular descriptors
178// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
179// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
180// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700181static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700182 std::string result;
183 // Add the missing "L;" if necessary.
184 if (name[0] == '[') {
185 result = name;
186 } else {
187 result += 'L';
188 result += name;
189 result += ';';
190 }
191 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700192 if (result.find('.') != std::string::npos) {
193 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
194 << "\"" << name << "\"";
195 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700196 }
197 return result;
198}
199
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800200static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, mirror::Class* c,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700201 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700202 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800203 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
204 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
205 "no %s method \"%s.%s%s\"",
206 kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700207}
208
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800209static mirror::Class* EnsureInitialized(Thread* self, mirror::Class* klass)
210 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
211 if (LIKELY(klass->IsInitialized())) {
212 return klass;
213 }
214 SirtRef<mirror::Class> sirt_klass(self, klass);
215 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(sirt_klass, true, true)) {
216 return nullptr;
217 }
218 return sirt_klass.get();
219}
220
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700221static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
222 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700223 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800224 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800225 if (c == nullptr) {
226 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700227 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800228 mirror::ArtMethod* method = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700229 if (is_static) {
230 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700231 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700232 method = c->FindVirtualMethod(name, sig);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800233 if (method == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700234 // No virtual method matching the signature. Search declared
235 // private methods and constructors.
236 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700237 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700238 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800239 if (method == nullptr || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700240 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800241 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700242 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700243 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700244}
245
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800246static mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700247 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800248 mirror::ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700249 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
250 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogersef28b142012-11-30 14:22:18 -0800251 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700252 }
Brian Carlstromce888532013-10-10 00:32:58 -0700253 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800254 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700255 return method->GetDeclaringClass()->GetClassLoader();
256 }
257 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800258 mirror::ClassLoader* class_loader =
259 soa.Decode<mirror::ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
260 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700261 return class_loader;
262 }
263 // See if the override ClassLoader is set for gtests.
264 class_loader = soa.Self()->GetClassLoaderOverride();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800265 if (class_loader != nullptr) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800266 // If so, CommonCompilerTest should have set UseCompileTimeClassPath.
Brian Carlstromce888532013-10-10 00:32:58 -0700267 CHECK(Runtime::Current()->UseCompileTimeClassPath());
268 return class_loader;
269 }
270 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800271 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700272}
273
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700274static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
275 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700276 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800277 SirtRef<mirror::Class> c(soa.Self(), EnsureInitialized(soa.Self(),
278 soa.Decode<mirror::Class*>(jni_class)));
279 if (c.get() == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800280 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700281 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800282 mirror::ArtField* field = nullptr;
283 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700284 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
285 if (sig[1] != '\0') {
Jeff Hao62509b62013-12-10 17:44:56 -0800286 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), c->GetClassLoader());
Ian Rogers98379392014-02-24 16:53:16 -0800287 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700288 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700289 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700290 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800291 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700292 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700293 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800294 ThrowLocation throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800295 SirtRef<mirror::Throwable> cause(soa.Self(), soa.Self()->GetException(&throw_location));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700296 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800297 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
298 "no type \"%s\" found and so no field \"%s\" could be found in class "
299 "\"%s\" or its superclasses", sig, name,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800300 ClassHelper(c.get()).GetDescriptor());
301 soa.Self()->GetException(nullptr)->SetCause(cause.get());
302 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700303 }
304 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800305 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700306 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800307 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700308 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800309 if (field == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800310 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
311 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
312 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800313 sig, name, ClassHelper(c.get()).GetDescriptor());
314 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700315 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700316 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700317}
318
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800319static void PinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700320 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700321 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700322 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700323 vm->pin_table.Add(array);
324}
325
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800326static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700327 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700328 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700329 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700330 vm->pin_table.Remove(array);
331}
332
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800333static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700334 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700335 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700336 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800337 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
338 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
339 "%s offset=%d length=%d %s.length=%d",
340 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700341}
Ian Rogers0571d352011-11-03 19:51:38 -0700342
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700343static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
344 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700345 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800346 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
347 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
348 "offset=%d length=%d string.length()=%d", start, length,
349 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700350}
Elliott Hughes814e4032011-08-23 12:07:56 -0700351
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700352int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700353 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700354 // Turn the const char* into a java.lang.String.
355 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800356 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700357 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700358 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700359
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700360 // Choose an appropriate constructor and set up the arguments.
361 jvalue args[2];
362 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800363 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700364 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800365 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700366 signature = "(Ljava/lang/String;)V";
367 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800368 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700369 signature = "(Ljava/lang/Throwable;)V";
370 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700371 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700372 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
373 args[0].l = s.get();
374 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700375 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700376 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800377 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800378 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700379 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800380 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700381 return JNI_ERR;
382 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700383
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800384 ScopedLocalRef<jthrowable> exception(
385 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
386 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700387 return JNI_ERR;
388 }
Ian Rogersef28b142012-11-30 14:22:18 -0800389 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800390 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800391 soa.Self()->SetException(throw_location, soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700392 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700393}
394
Elliott Hughes462c9442012-03-23 18:47:50 -0700395static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800396 if (vm == nullptr || p_env == nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -0700397 return JNI_ERR;
398 }
399
Elliott Hughes462c9442012-03-23 18:47:50 -0700400 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700401 Thread* self = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800402 if (self != nullptr) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700403 *p_env = self->GetJniEnv();
404 return JNI_OK;
405 }
406
407 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
408
409 // No threads allowed in zygote mode.
410 if (runtime->IsZygote()) {
411 LOG(ERROR) << "Attempt to attach a thread in the zygote";
412 return JNI_ERR;
413 }
414
Elliott Hughes462c9442012-03-23 18:47:50 -0700415 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800416 const char* thread_name = nullptr;
417 jobject thread_group = nullptr;
418 if (args != nullptr) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700419 if (IsBadJniVersion(args->version)) {
420 LOG(ERROR) << "Bad JNI version passed to "
421 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
422 << args->version;
423 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700424 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700425 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700426 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700427 }
Elliott Hughes75770752011-08-24 17:52:38 -0700428
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800429 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800430 *p_env = nullptr;
Ian Rogers120f1c72012-09-28 17:17:10 -0700431 return JNI_ERR;
432 } else {
433 *p_env = Thread::Current()->GetJniEnv();
434 return JNI_OK;
435 }
Elliott Hughes75770752011-08-24 17:52:38 -0700436}
437
Elliott Hughes79082e32011-08-25 12:07:32 -0700438class SharedLibrary {
439 public:
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800440 SharedLibrary(const std::string& path, void* handle, mirror::Object* class_loader)
Elliott Hughes79082e32011-08-25 12:07:32 -0700441 : path_(path),
442 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700443 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700444 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700445 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700446 jni_on_load_thread_id_(Thread::Current()->GetThreadId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700447 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700448 }
449
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800450 mirror::Object* GetClassLoader() {
Elliott Hughes79082e32011-08-25 12:07:32 -0700451 return class_loader_;
452 }
453
454 std::string GetPath() {
455 return path_;
456 }
457
458 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700459 * Check the result of an earlier call to JNI_OnLoad on this library.
460 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700461 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700462 bool CheckOnLoadResult()
463 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700464 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700465 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700466 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
467 bool okay;
468 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700469 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700470
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700471 if (jni_on_load_thread_id_ == self->GetThreadId()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700472 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
473 // caller can continue.
474 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
475 okay = true;
476 } else {
477 while (jni_on_load_result_ == kPending) {
478 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700479 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700480 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700481
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700482 okay = (jni_on_load_result_ == kOkay);
483 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
484 << (okay ? "succeeded" : "failed") << "]";
485 }
486 }
487 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700488 return okay;
489 }
490
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700491 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700492 Thread* self = Thread::Current();
493 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700494
Elliott Hughes79082e32011-08-25 12:07:32 -0700495 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700496 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700497
498 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700499 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700500 }
501
502 void* FindSymbol(const std::string& symbol_name) {
503 return dlsym(handle_, symbol_name.c_str());
504 }
505
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800506 void VisitRoots(RootCallback* visitor, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800507 if (class_loader_ != nullptr) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800508 visitor(&class_loader_, arg, 0, kRootVMInternal);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800509 }
510 }
511
Elliott Hughes79082e32011-08-25 12:07:32 -0700512 private:
513 enum JNI_OnLoadState {
514 kPending,
515 kFailed,
516 kOkay,
517 };
518
519 // Path to library "/system/lib/libjni.so".
520 std::string path_;
521
522 // The void* returned by dlopen(3).
523 void* handle_;
524
525 // The ClassLoader this library is associated with.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800526 mirror::Object* class_loader_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700527
528 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700529 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700530 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700531 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700532 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700533 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700534 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700535 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700536};
537
Elliott Hughes79082e32011-08-25 12:07:32 -0700538// This exists mainly to keep implementation details out of the header file.
539class Libraries {
540 public:
541 Libraries() {
542 }
543
544 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700545 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700546 }
547
Elliott Hughesae80b492012-04-24 10:43:17 -0700548 void Dump(std::ostream& os) const {
549 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700550 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700551 if (!first) {
552 os << ' ';
553 }
554 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700555 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700556 }
557 }
558
559 size_t size() const {
560 return libraries_.size();
561 }
562
Elliott Hughes79082e32011-08-25 12:07:32 -0700563 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700564 auto it = libraries_.find(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800565 return (it == libraries_.end()) ? nullptr : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700566 }
567
568 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700569 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700570 }
571
572 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800573 void* FindNativeMethod(mirror::ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700574 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700575 std::string jni_short_name(JniShortName(m));
576 std::string jni_long_name(JniLongName(m));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800577 const mirror::ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700578 for (const auto& lib : libraries_) {
579 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700580 if (library->GetClassLoader() != declaring_class_loader) {
581 // We only search libraries loaded by the appropriate ClassLoader.
582 continue;
583 }
584 // Try the short name then the long name...
585 void* fn = library->FindSymbol(jni_short_name);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800586 if (fn == nullptr) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700587 fn = library->FindSymbol(jni_long_name);
588 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800589 if (fn != nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800590 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
591 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700592 return fn;
593 }
594 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700595 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700596 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700597 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700598 LOG(ERROR) << detail;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800599 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -0700600 }
601
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800602 void VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800603 for (auto& lib_pair : libraries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800604 lib_pair.second->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800605 }
606 }
607
Elliott Hughes79082e32011-08-25 12:07:32 -0700608 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700609 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700610};
611
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700612JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
613 jvalue* args) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800614 mirror::ArtMethod* method = soa.DecodeMethod(mid);
615 mirror::Object* receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700616 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800617 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700618 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800619 arg_array.BuildArgArray(soa, receiver, args);
Ian Rogers0177e532014-02-11 16:30:46 -0800620 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty());
Jeff Hao6474d192013-03-26 14:08:09 -0700621 return result;
Elliott Hughesd07986f2011-12-06 18:27:45 -0800622}
623
Ian Rogersbc939662013-08-15 10:26:54 -0700624#define CHECK_NON_NULL_ARGUMENT(fn, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800625 if (UNLIKELY(value == nullptr)) { \
Ian Rogersbc939662013-08-15 10:26:54 -0700626 JniAbortF(#fn, #value " == null"); \
627 }
628
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700629#define CHECK_NON_NULL_MEMCPY_ARGUMENT(fn, length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800630 if (UNLIKELY(length != 0 && value == nullptr)) { \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700631 JniAbortF(#fn, #value " == null"); \
632 }
633
Elliott Hughescdf53122011-08-19 15:46:09 -0700634class JNI {
635 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700636 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700637 return JNI_VERSION_1_6;
638 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700639
Ian Rogers25e8b912012-09-07 11:31:36 -0700640 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700641 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800642 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700643 }
644
Elliott Hughescdf53122011-08-19 15:46:09 -0700645 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogersbc939662013-08-15 10:26:54 -0700646 CHECK_NON_NULL_ARGUMENT(FindClass, name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700647 Runtime* runtime = Runtime::Current();
648 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700649 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700650 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800651 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700652 if (runtime->IsStarted()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700653 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), GetClassLoader(soa));
Ian Rogers98379392014-02-24 16:53:16 -0800654 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700655 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800656 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700657 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700658 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700659 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700660
Elliott Hughescdf53122011-08-19 15:46:09 -0700661 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogersbc939662013-08-15 10:26:54 -0700662 CHECK_NON_NULL_ARGUMENT(FromReflectedMethod, java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700663 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800664 jobject art_method = env->GetObjectField(
665 java_method, WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod);
666 mirror::ArtMethod* method = soa.Decode<mirror::ArtMethod*>(art_method);
667 DCHECK(method != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700668 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700669 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700670
Elliott Hughescdf53122011-08-19 15:46:09 -0700671 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogersbc939662013-08-15 10:26:54 -0700672 CHECK_NON_NULL_ARGUMENT(FromReflectedField, java_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700673 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700674 jobject art_field = env->GetObjectField(java_field,
675 WellKnownClasses::java_lang_reflect_Field_artField);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800676 mirror::ArtField* field = soa.Decode<mirror::ArtField*>(art_field);
677 DCHECK(field != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700678 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700679 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700680
Elliott Hughescdf53122011-08-19 15:46:09 -0700681 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700682 CHECK_NON_NULL_ARGUMENT(ToReflectedMethod, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700683 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800684 mirror::ArtMethod* m = soa.DecodeMethod(mid);
685 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -0700686 jobject art_method = soa.AddLocalReference<jobject>(m);
687 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
688 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800689 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700690 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800691 SetObjectField(env, reflect_method,
692 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700693 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700694 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700695
Elliott Hughescdf53122011-08-19 15:46:09 -0700696 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700697 CHECK_NON_NULL_ARGUMENT(ToReflectedField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700698 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800699 mirror::ArtField* f = soa.DecodeField(fid);
Brian Carlstromea46f952013-07-30 01:26:50 -0700700 jobject art_field = soa.AddLocalReference<jobject>(f);
701 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
702 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800703 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700704 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800705 SetObjectField(env, reflect_field,
706 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700707 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700708 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700709
Elliott Hughes37f7a402011-08-22 18:56:01 -0700710 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -0700711 CHECK_NON_NULL_ARGUMENT(GetObjectClass, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700712 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800713 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700714 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700715 }
716
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700717 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700718 CHECK_NON_NULL_ARGUMENT(GetSuperclass, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700719 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800720 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700721 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700722 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700723
Elliott Hughes37f7a402011-08-22 18:56:01 -0700724 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogersbc939662013-08-15 10:26:54 -0700725 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class1);
726 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class2);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700727 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800728 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
729 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700730 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700731 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700732
Elliott Hughese84278b2012-03-22 10:06:53 -0700733 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700734 CHECK_NON_NULL_ARGUMENT(IsInstanceOf, java_class);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800735 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700736 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700737 return JNI_TRUE;
738 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700739 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800740 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
741 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700742 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700743 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700744 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700745
Elliott Hughes37f7a402011-08-22 18:56:01 -0700746 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700747 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800748 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
749 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700750 return JNI_ERR;
751 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800752 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
753 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700754 return JNI_OK;
755 }
756
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700757 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogersbc939662013-08-15 10:26:54 -0700758 CHECK_NON_NULL_ARGUMENT(ThrowNew, c);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800759 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700760 }
761
762 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700763 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700764 }
765
766 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700767 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700768 }
769
770 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700771 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700772
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800773 SirtRef<mirror::Object> old_throw_this_object(soa.Self(), nullptr);
774 SirtRef<mirror::ArtMethod> old_throw_method(soa.Self(), nullptr);
775 SirtRef<mirror::Throwable> old_exception(soa.Self(), nullptr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800776 uint32_t old_throw_dex_pc;
777 {
778 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800779 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Ian Rogers62d6c772013-02-27 08:32:07 -0800780 old_throw_this_object.reset(old_throw_location.GetThis());
781 old_throw_method.reset(old_throw_location.GetMethod());
782 old_exception.reset(old_exception_obj);
783 old_throw_dex_pc = old_throw_location.GetDexPc();
784 soa.Self()->ClearException();
785 }
786 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(old_exception.get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700787 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
788 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800789 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700790 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Ian Rogers62d6c772013-02-27 08:32:07 -0800791 << PrettyTypeOf(old_exception.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700792 } else {
793 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800794 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800795 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700796 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800797 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700798 }
799 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800800 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
801 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700802
Ian Rogers62d6c772013-02-27 08:32:07 -0800803 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700804 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700805
Elliott Hughescdf53122011-08-19 15:46:09 -0700806 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700807 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800808 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700809 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700810 }
811
Ian Rogers25e8b912012-09-07 11:31:36 -0700812 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700813 LOG(FATAL) << "JNI FatalError called: " << msg;
814 }
815
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700816 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800817 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700818 return JNI_ERR;
819 }
Ian Rogersef28b142012-11-30 14:22:18 -0800820 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700821 return JNI_OK;
822 }
823
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700824 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700825 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800826 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700827 soa.Env()->PopFrame();
828 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700829 }
830
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700831 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800832 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700833 }
834
Elliott Hughescdf53122011-08-19 15:46:09 -0700835 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700836 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800837 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800838 // Check for null after decoding the object to handle cleared weak globals.
839 if (decoded_obj == nullptr) {
840 return nullptr;
841 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700842 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700843 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700844 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700845 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700846 return reinterpret_cast<jobject>(ref);
847 }
848
849 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800850 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700851 return;
852 }
Ian Rogersef28b142012-11-30 14:22:18 -0800853 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700854 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800855 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700856 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700857
858 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
859 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
860 << "failed to find entry";
861 }
862 }
863
864 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700865 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800866 return AddWeakGlobalReference(soa, soa.Decode<mirror::Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700867 }
868
869 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700870 if (obj != nullptr) {
871 ScopedObjectAccess soa(env);
872 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700873 }
874 }
875
876 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700877 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800878 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800879 // Check for null after decoding the object to handle cleared weak globals.
880 if (decoded_obj == nullptr) {
881 return nullptr;
882 }
883 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700884 }
885
886 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800887 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700888 return;
889 }
Ian Rogersef28b142012-11-30 14:22:18 -0800890 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700891
Ian Rogersef28b142012-11-30 14:22:18 -0800892 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700893 if (!locals.Remove(cookie, obj)) {
894 // Attempting to delete a local reference that is not in the
895 // topmost local reference frame is a no-op. DeleteLocalRef returns
896 // void and doesn't throw any exceptions, but we should probably
897 // complain about it so the user will notice that things aren't
898 // going quite the way they expect.
899 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
900 << "failed to find entry";
901 }
902 }
903
904 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700905 if (obj1 == obj2) {
906 return JNI_TRUE;
907 } else {
908 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800909 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700910 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700911 }
912
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700913 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700914 CHECK_NON_NULL_ARGUMENT(AllocObject, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700915 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800916 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800917 if (c == nullptr) {
918 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700919 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700920 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700921 }
922
Ian Rogersbc939662013-08-15 10:26:54 -0700923 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700924 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700925 va_start(args, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700926 CHECK_NON_NULL_ARGUMENT(NewObject, java_class);
927 CHECK_NON_NULL_ARGUMENT(NewObject, mid);
928 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700929 va_end(args);
930 return result;
931 }
932
Elliott Hughes72025e52011-08-23 17:50:30 -0700933 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700934 CHECK_NON_NULL_ARGUMENT(NewObjectV, java_class);
935 CHECK_NON_NULL_ARGUMENT(NewObjectV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700936 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800937 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800938 if (c == nullptr) {
939 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700940 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800941 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800942 if (result == nullptr) {
943 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700944 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700945 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700946 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800947 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800948 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700949 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800950 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700951 }
952
Elliott Hughes72025e52011-08-23 17:50:30 -0700953 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700954 CHECK_NON_NULL_ARGUMENT(NewObjectA, java_class);
955 CHECK_NON_NULL_ARGUMENT(NewObjectA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700956 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800957 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800958 if (c == nullptr) {
959 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700960 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800961 mirror::Object* result = c->AllocObject(soa.Self());
962 if (result == nullptr) {
963 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700964 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700965 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700966 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800967 if (soa.Self()->IsExceptionPending()) {
968 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700969 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800970 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700971 }
972
Ian Rogersbc939662013-08-15 10:26:54 -0700973 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
974 CHECK_NON_NULL_ARGUMENT(GetMethodID, java_class);
975 CHECK_NON_NULL_ARGUMENT(GetMethodID, name);
976 CHECK_NON_NULL_ARGUMENT(GetMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700977 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700978 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700979 }
980
Ian Rogersbc939662013-08-15 10:26:54 -0700981 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
982 const char* sig) {
983 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, java_class);
984 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, name);
985 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700986 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700987 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700988 }
989
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700991 va_list ap;
992 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700993 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, obj);
994 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700995 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700996 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700997 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700998 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700999 }
1000
Elliott Hughes72025e52011-08-23 17:50:30 -07001001 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001002 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, obj);
1003 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001004 ScopedObjectAccess soa(env);
1005 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
1006 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 }
1008
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001010 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, obj);
1011 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001012 ScopedObjectAccess soa(env);
1013 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
1014 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001015 }
1016
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 va_list ap;
1019 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001020 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, obj);
1021 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001022 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001023 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001024 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001025 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 }
1027
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001029 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, obj);
1030 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001031 ScopedObjectAccess soa(env);
1032 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001033 }
1034
Elliott Hughes72025e52011-08-23 17:50:30 -07001035 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001036 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, obj);
1037 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001038 ScopedObjectAccess soa(env);
1039 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 }
1041
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 va_list ap;
1044 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001045 CHECK_NON_NULL_ARGUMENT(CallByteMethod, obj);
1046 CHECK_NON_NULL_ARGUMENT(CallByteMethod, mid);
1047 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001048 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001050 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 }
1052
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001054 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, obj);
1055 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001056 ScopedObjectAccess soa(env);
1057 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001058 }
1059
Elliott Hughes72025e52011-08-23 17:50:30 -07001060 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001061 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, obj);
1062 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001063 ScopedObjectAccess soa(env);
1064 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 }
1066
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 va_list ap;
1069 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001070 CHECK_NON_NULL_ARGUMENT(CallCharMethod, obj);
1071 CHECK_NON_NULL_ARGUMENT(CallCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001072 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001073 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001074 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001075 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 }
1077
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001079 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, obj);
1080 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001081 ScopedObjectAccess soa(env);
1082 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 }
1084
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001086 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, obj);
1087 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001088 ScopedObjectAccess soa(env);
1089 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 }
1091
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001093 va_list ap;
1094 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001095 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, obj);
1096 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001097 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001098 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001100 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 }
1102
Elliott Hughes72025e52011-08-23 17:50:30 -07001103 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001104 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, obj);
1105 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001106 ScopedObjectAccess soa(env);
1107 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 }
1109
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001111 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, obj);
1112 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001113 ScopedObjectAccess soa(env);
1114 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 }
1116
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 va_list ap;
1119 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001120 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, obj);
1121 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, mid);
1122 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001123 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001124 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001125 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 }
1127
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001129 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, obj);
1130 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001131 ScopedObjectAccess soa(env);
1132 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 }
1134
Elliott Hughes72025e52011-08-23 17:50:30 -07001135 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001136 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, obj);
1137 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001138 ScopedObjectAccess soa(env);
1139 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 }
1141
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001143 va_list ap;
1144 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001145 CHECK_NON_NULL_ARGUMENT(CallIntMethod, obj);
1146 CHECK_NON_NULL_ARGUMENT(CallIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001147 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001148 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001149 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001150 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 }
1152
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001154 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, obj);
1155 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001156 ScopedObjectAccess soa(env);
1157 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 }
1159
Elliott Hughes72025e52011-08-23 17:50:30 -07001160 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001161 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, obj);
1162 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001163 ScopedObjectAccess soa(env);
1164 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 }
1166
Elliott Hughes72025e52011-08-23 17:50:30 -07001167 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001168 va_list ap;
1169 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001170 CHECK_NON_NULL_ARGUMENT(CallLongMethod, obj);
1171 CHECK_NON_NULL_ARGUMENT(CallLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001172 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001173 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001175 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 }
1177
Elliott Hughes72025e52011-08-23 17:50:30 -07001178 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001179 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, obj);
1180 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001181 ScopedObjectAccess soa(env);
1182 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 }
1184
Elliott Hughes72025e52011-08-23 17:50:30 -07001185 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001186 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, obj);
1187 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001188 ScopedObjectAccess soa(env);
1189 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 }
1191
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001193 va_list ap;
1194 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001195 CHECK_NON_NULL_ARGUMENT(CallShortMethod, obj);
1196 CHECK_NON_NULL_ARGUMENT(CallShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001197 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001198 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001199 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001200 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 }
1202
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001204 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, obj);
1205 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001206 ScopedObjectAccess soa(env);
1207 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 }
1209
Elliott Hughes72025e52011-08-23 17:50:30 -07001210 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001211 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, obj);
1212 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001213 ScopedObjectAccess soa(env);
1214 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001215 }
1216
Elliott Hughes72025e52011-08-23 17:50:30 -07001217 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001218 va_list ap;
1219 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001220 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, obj);
1221 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001222 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001223 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001224 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 }
1226
Elliott Hughes72025e52011-08-23 17:50:30 -07001227 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001228 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, obj);
1229 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001230 ScopedObjectAccess soa(env);
1231 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 }
1233
Elliott Hughes72025e52011-08-23 17:50:30 -07001234 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001235 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, obj);
1236 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001237 ScopedObjectAccess soa(env);
1238 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 }
1240
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001241 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001243 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001244 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, obj);
1245 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001246 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001247 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1248 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001249 va_end(ap);
1250 return local_result;
1251 }
1252
Ian Rogersbc939662013-08-15 10:26:54 -07001253 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1254 va_list args) {
1255 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, obj);
1256 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001257 ScopedObjectAccess soa(env);
1258 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1259 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 }
1261
Ian Rogersbc939662013-08-15 10:26:54 -07001262 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1263 jvalue* args) {
1264 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, obj);
1265 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001266 ScopedObjectAccess soa(env);
1267 JValue result(InvokeWithJValues(soa, obj, mid, args));
1268 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 }
1270
Ian Rogersbc939662013-08-15 10:26:54 -07001271 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1272 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001274 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001275 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, obj);
1276 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001277 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001278 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001280 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 }
1282
Ian Rogersbc939662013-08-15 10:26:54 -07001283 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1284 va_list args) {
1285 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, obj);
1286 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001287 ScopedObjectAccess soa(env);
1288 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001289 }
1290
Ian Rogersbc939662013-08-15 10:26:54 -07001291 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1292 jvalue* args) {
1293 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, obj);
1294 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001295 ScopedObjectAccess soa(env);
1296 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001297 }
1298
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001299 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001301 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001302 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, obj);
1303 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001304 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001305 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001307 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 }
1309
Ian Rogersbc939662013-08-15 10:26:54 -07001310 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1311 va_list args) {
1312 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, obj);
1313 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001314 ScopedObjectAccess soa(env);
1315 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 }
1317
Ian Rogersbc939662013-08-15 10:26:54 -07001318 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1319 jvalue* args) {
1320 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, obj);
1321 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001322 ScopedObjectAccess soa(env);
1323 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001324 }
1325
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001326 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001328 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001329 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, obj);
1330 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, mid);
1331 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001332 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001334 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 }
1336
Ian Rogersbc939662013-08-15 10:26:54 -07001337 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1338 va_list args) {
1339 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, obj);
1340 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001341 ScopedObjectAccess soa(env);
1342 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001343 }
1344
Ian Rogersbc939662013-08-15 10:26:54 -07001345 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1346 jvalue* args) {
1347 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, obj);
1348 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001349 ScopedObjectAccess soa(env);
1350 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001351 }
1352
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001353 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001355 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001356 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, obj);
1357 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001358 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001359 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001361 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001362 }
1363
Ian Rogersbc939662013-08-15 10:26:54 -07001364 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1365 va_list args) {
1366 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, obj);
1367 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001368 ScopedObjectAccess soa(env);
1369 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 }
1371
Ian Rogersbc939662013-08-15 10:26:54 -07001372 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1373 jvalue* args) {
1374 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, obj);
1375 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001376 ScopedObjectAccess soa(env);
1377 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 }
1379
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001380 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001381 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001382 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001383 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, obj);
1384 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001385 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001386 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001388 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 }
1390
Ian Rogersbc939662013-08-15 10:26:54 -07001391 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1392 va_list args) {
1393 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, obj);
1394 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001395 ScopedObjectAccess soa(env);
1396 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001397 }
1398
Ian Rogersbc939662013-08-15 10:26:54 -07001399 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1400 jvalue* args) {
1401 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, obj);
1402 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001403 ScopedObjectAccess soa(env);
1404 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 }
1406
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001407 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001408 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001409 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001410 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, obj);
1411 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001412 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001413 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001414 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001415 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001416 }
1417
Ian Rogersbc939662013-08-15 10:26:54 -07001418 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1419 va_list args) {
1420 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, obj);
1421 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001422 ScopedObjectAccess soa(env);
1423 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 }
1425
Ian Rogersbc939662013-08-15 10:26:54 -07001426 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1427 jvalue* args) {
1428 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, obj);
1429 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001430 ScopedObjectAccess soa(env);
1431 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 }
1433
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001434 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001435 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001436 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001437 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, obj);
1438 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001439 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001440 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001442 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001443 }
1444
Ian Rogersbc939662013-08-15 10:26:54 -07001445 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1446 va_list args) {
1447 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, obj);
1448 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001449 ScopedObjectAccess soa(env);
1450 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001451 }
1452
Ian Rogersbc939662013-08-15 10:26:54 -07001453 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1454 jvalue* args) {
1455 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, obj);
1456 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001457 ScopedObjectAccess soa(env);
1458 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001459 }
1460
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001461 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001462 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001463 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001464 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, obj);
1465 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001466 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001467 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001469 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001470 }
1471
Ian Rogersbc939662013-08-15 10:26:54 -07001472 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1473 va_list args) {
1474 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, obj);
1475 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001476 ScopedObjectAccess soa(env);
1477 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001478 }
1479
Ian Rogersbc939662013-08-15 10:26:54 -07001480 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1481 jvalue* args) {
1482 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, obj);
1483 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001484 ScopedObjectAccess soa(env);
1485 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001486 }
1487
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001488 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001489 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001490 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001491 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, obj);
1492 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001493 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001494 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001495 va_end(ap);
1496 }
1497
Brian Carlstromea46f952013-07-30 01:26:50 -07001498 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1499 va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001500 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, obj);
1501 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001502 ScopedObjectAccess soa(env);
1503 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001504 }
1505
Ian Rogersbc939662013-08-15 10:26:54 -07001506 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1507 jvalue* args) {
1508 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, obj);
1509 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001510 ScopedObjectAccess soa(env);
1511 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001512 }
1513
Ian Rogersbc939662013-08-15 10:26:54 -07001514 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
1515 CHECK_NON_NULL_ARGUMENT(GetFieldID, java_class);
1516 CHECK_NON_NULL_ARGUMENT(GetFieldID, name);
1517 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001518 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001519 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001520 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001521
Ian Rogersbc939662013-08-15 10:26:54 -07001522 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1523 const char* sig) {
1524 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, java_class);
1525 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, name);
1526 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001527 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001528 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001529 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001530
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001531 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001532 CHECK_NON_NULL_ARGUMENT(GetObjectField, obj);
1533 CHECK_NON_NULL_ARGUMENT(GetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001534 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001535 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1536 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001537 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001538 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001539
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001540 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001541 CHECK_NON_NULL_ARGUMENT(GetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001542 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001543 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001544 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 }
1546
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001547 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001548 CHECK_NON_NULL_ARGUMENT(SetObjectField, java_object);
1549 CHECK_NON_NULL_ARGUMENT(SetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001550 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001551 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1552 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1553 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001554 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001555 }
1556
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001557 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001558 CHECK_NON_NULL_ARGUMENT(SetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001559 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001560 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1561 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001562 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 }
1564
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001565#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogersbc939662013-08-15 10:26:54 -07001566 CHECK_NON_NULL_ARGUMENT(Get #fn Field, instance); \
1567 CHECK_NON_NULL_ARGUMENT(Get #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001568 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001569 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1570 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001571 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001572
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001573#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogersbc939662013-08-15 10:26:54 -07001574 CHECK_NON_NULL_ARGUMENT(GetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001575 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001576 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001577 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001578
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001579#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001580 CHECK_NON_NULL_ARGUMENT(Set #fn Field, instance); \
1581 CHECK_NON_NULL_ARGUMENT(Set #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001582 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001583 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1584 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001585 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001586
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001587#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001588 CHECK_NON_NULL_ARGUMENT(SetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001589 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001590 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001591 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001592
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001593 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001594 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 }
1596
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001597 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001598 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 }
1600
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001601 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001602 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 }
1604
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001605 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001606 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 }
1608
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001609 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001610 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 }
1612
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001613 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001614 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001615 }
1616
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001617 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001618 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001619 }
1620
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001621 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001622 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001623 }
1624
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001625 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001626 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 }
1628
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001629 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001630 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001631 }
1632
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001633 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001634 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001635 }
1636
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001637 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001638 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 }
1640
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001641 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001642 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001643 }
1644
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001645 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001646 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001647 }
1648
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001649 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001650 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001651 }
1652
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001653 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001654 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001655 }
1656
1657 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001658 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001659 }
1660
1661 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001662 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001663 }
1664
1665 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001666 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001667 }
1668
1669 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001670 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001671 }
1672
1673 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001674 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001675 }
1676
1677 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001678 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001679 }
1680
1681 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001682 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001683 }
1684
1685 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001686 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001687 }
1688
1689 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001690 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001691 }
1692
1693 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001694 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001695 }
1696
1697 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001698 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001699 }
1700
1701 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001702 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001703 }
1704
1705 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001706 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001707 }
1708
1709 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001710 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001711 }
1712
1713 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001714 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001715 }
1716
1717 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001718 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 }
1720
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001721 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001722 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001723 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001724 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001725 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001726 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001727 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 va_end(ap);
1729 return local_result;
1730 }
1731
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001732 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001733 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001734 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001735 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001736 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001737 }
1738
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001739 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001740 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001741 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001742 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001743 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 }
1745
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001746 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001748 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001749 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001750 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001751 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001752 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001753 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001754 }
1755
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001756 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001757 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001758 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001759 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001760 }
1761
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001762 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001763 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001764 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001765 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001766 }
1767
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001768 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001770 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001771 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001772 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001773 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001775 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001776 }
1777
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001778 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001779 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001780 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001781 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001782 }
1783
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001784 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001785 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001786 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001787 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001788 }
1789
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001790 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001792 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001793 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001794 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001795 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001796 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001797 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001798 }
1799
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001800 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001801 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001802 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001803 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001804 }
1805
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001806 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001807 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001808 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001809 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001810 }
1811
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001812 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001814 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001815 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001816 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001817 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001818 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001819 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 }
1821
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001822 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001823 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001824 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001825 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001826 }
1827
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001828 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001829 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001830 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001831 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001832 }
1833
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001834 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001836 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001837 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001838 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001839 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001840 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001841 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001842 }
1843
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001844 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001845 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001846 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001847 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001848 }
1849
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001850 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001851 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001852 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001853 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001854 }
1855
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001856 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001857 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001858 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001859 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001860 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001861 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001862 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001863 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001864 }
1865
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001866 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001867 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001868 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001869 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001870 }
1871
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001872 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001873 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001874 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001875 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001876 }
1877
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001878 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001879 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001880 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001881 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001882 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001883 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001884 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001885 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001886 }
1887
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001888 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001889 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001890 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001891 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001892 }
1893
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001894 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001895 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001896 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001897 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001898 }
1899
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001900 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001901 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001902 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001903 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001904 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001905 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001906 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001907 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001908 }
1909
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001910 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001911 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001912 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001913 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001914 }
1915
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001916 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001917 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001918 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001919 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001920 }
1921
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001922 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001923 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001924 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001925 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001926 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001927 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001928 va_end(ap);
1929 }
1930
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001931 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001932 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001933 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001934 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001935 }
1936
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001937 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001938 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001939 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001940 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001941 }
1942
Elliott Hughes814e4032011-08-23 12:07:56 -07001943 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001944 if (UNLIKELY(char_count < 0)) {
1945 JniAbortF("NewString", "char_count < 0: %d", char_count);
1946 return nullptr;
1947 }
1948 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1949 JniAbortF("NewString", "chars == null && char_count > 0");
1950 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001951 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001952 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001953 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001954 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001955 }
1956
1957 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001958 if (utf == nullptr) {
1959 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001960 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001961 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001962 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001963 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001964 }
1965
Elliott Hughes814e4032011-08-23 12:07:56 -07001966 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001967 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001968 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001969 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001970 }
1971
1972 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001973 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001974 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001975 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001976 }
1977
Ian Rogersbc939662013-08-15 10:26:54 -07001978 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1979 jchar* buf) {
1980 CHECK_NON_NULL_ARGUMENT(GetStringRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001981 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001982 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001983 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001984 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001985 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07001986 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001987 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1988 memcpy(buf, chars + start, length * sizeof(jchar));
1989 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001990 }
1991
Ian Rogersbc939662013-08-15 10:26:54 -07001992 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1993 char* buf) {
1994 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001995 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001996 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001997 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001998 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001999 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002000 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringUTFRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002001 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
2002 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
2003 }
Elliott Hughes814e4032011-08-23 12:07:56 -07002004 }
2005
Elliott Hughes75770752011-08-24 17:52:38 -07002006 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Narayan Kamath8e611d32014-02-10 18:20:06 +00002007 CHECK_NON_NULL_ARGUMENT(GetStringChars, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002008 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002009 mirror::String* s = soa.Decode<mirror::String*>(java_string);
2010 mirror::CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002011 PinPrimitiveArray(soa, chars);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002012 if (is_copy != nullptr) {
2013 *is_copy = JNI_TRUE;
Elliott Hughes75770752011-08-24 17:52:38 -07002014 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002015 int32_t char_count = s->GetLength();
2016 int32_t offset = s->GetOffset();
2017 jchar* bytes = new jchar[char_count + 1];
2018 for (int32_t i = 0; i < char_count; i++) {
2019 bytes[i] = chars->Get(i + offset);
2020 }
2021 bytes[char_count] = '\0';
2022 return bytes;
Elliott Hughes814e4032011-08-23 12:07:56 -07002023 }
2024
Mathieu Chartier590fee92013-09-13 13:46:47 -07002025 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Narayan Kamath8e611d32014-02-10 18:20:06 +00002026 CHECK_NON_NULL_ARGUMENT(ReleaseStringChars, java_string);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002027 delete[] chars;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002028 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002029 UnpinPrimitiveArray(soa, soa.Decode<mirror::String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 }
2031
Elliott Hughes75770752011-08-24 17:52:38 -07002032 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07002033 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 }
2035
Elliott Hughes75770752011-08-24 17:52:38 -07002036 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002037 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07002038 }
2039
Elliott Hughes75770752011-08-24 17:52:38 -07002040 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002041 if (java_string == nullptr) {
2042 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07002043 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002044 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07002045 *is_copy = JNI_TRUE;
2046 }
Ian Rogersef28b142012-11-30 14:22:18 -08002047 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002048 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002049 size_t byte_count = s->GetUtfLength();
2050 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002051 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07002052 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
2053 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
2054 bytes[byte_count] = '\0';
2055 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002056 }
2057
Elliott Hughes75770752011-08-24 17:52:38 -07002058 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002059 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002060 }
2061
Elliott Hughesbd935992011-08-22 11:59:34 -07002062 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogersbc939662013-08-15 10:26:54 -07002063 CHECK_NON_NULL_ARGUMENT(GetArrayLength, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002064 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002065 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07002066 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002067 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
2068 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002069 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07002070 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 }
2072
Elliott Hughes814e4032011-08-23 12:07:56 -07002073 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogersbc939662013-08-15 10:26:54 -07002074 CHECK_NON_NULL_ARGUMENT(GetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002075 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002076 mirror::ObjectArray<mirror::Object>* array =
2077 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002078 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 }
2080
Ian Rogersbc939662013-08-15 10:26:54 -07002081 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2082 jobject java_value) {
2083 CHECK_NON_NULL_ARGUMENT(SetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002084 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002085 mirror::ObjectArray<mirror::Object>* array =
2086 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
2087 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002088 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 }
2090
2091 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002092 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002093 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 }
2095
2096 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002097 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002098 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 }
2100
2101 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002102 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002103 return NewPrimitiveArray<jcharArray, mirror::CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 }
2105
2106 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002107 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002108 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 }
2110
2111 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002112 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002113 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 }
2115
2116 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002117 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002118 return NewPrimitiveArray<jintArray, mirror::IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 }
2120
2121 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002122 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002123 return NewPrimitiveArray<jlongArray, mirror::LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 }
2125
Ian Rogers1d99e452014-01-02 17:36:41 -08002126 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2127 jobject initial_element) {
2128 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002129 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002130 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002131 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002132
2133 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002134 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002135 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002136 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002137 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08002138 if (UNLIKELY(element_class->IsPrimitive())) {
2139 JniAbortF("NewObjectArray", "not an object type: %s",
2140 PrettyDescriptor(element_class).c_str());
2141 return nullptr;
2142 }
Ian Rogers1d99e452014-01-02 17:36:41 -08002143 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers98379392014-02-24 16:53:16 -08002144 array_class = class_linker->FindArrayClass(soa.Self(), element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08002145 if (UNLIKELY(array_class == nullptr)) {
2146 return nullptr;
2147 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002148 }
2149
Elliott Hughes75770752011-08-24 17:52:38 -07002150 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002151 mirror::ObjectArray<mirror::Object>* result =
2152 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002153 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002154 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002155 if (initial_object != nullptr) {
2156 mirror::Class* element_class = result->GetClass()->GetComponentType();
2157 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2158 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2159 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2160 PrettyDescriptor(element_class).c_str());
2161
2162 } else {
2163 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002164 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08002165 }
2166 }
Elliott Hughes75770752011-08-24 17:52:38 -07002167 }
2168 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002169 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002170 }
2171
2172 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002173 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002174 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002175 }
2176
Ian Rogersa15e67d2012-02-28 13:51:55 -08002177 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002178 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayCritical, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002179 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002180 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
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 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002187 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002188 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002189 *is_copy = JNI_FALSE;
2190 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002191 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002192 }
2193
Mathieu Chartier590fee92013-09-13 13:46:47 -07002194 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* elements, jint mode) {
Ian Rogersbc939662013-08-15 10:26:54 -07002195 CHECK_NON_NULL_ARGUMENT(ReleasePrimitiveArrayCritical, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002196 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002197 }
2198
Elliott Hughes75770752011-08-24 17:52:38 -07002199 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002200 CHECK_NON_NULL_ARGUMENT(GetBooleanArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002201 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002202 return GetPrimitiveArray<jbooleanArray, jboolean*, mirror::BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002203 }
2204
Elliott Hughes75770752011-08-24 17:52:38 -07002205 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002206 CHECK_NON_NULL_ARGUMENT(GetByteArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002207 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002208 return GetPrimitiveArray<jbyteArray, jbyte*, mirror::ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002209 }
2210
Elliott Hughes75770752011-08-24 17:52:38 -07002211 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002212 CHECK_NON_NULL_ARGUMENT(GetCharArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002213 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002214 return GetPrimitiveArray<jcharArray, jchar*, mirror::CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002215 }
2216
Elliott Hughes75770752011-08-24 17:52:38 -07002217 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002218 CHECK_NON_NULL_ARGUMENT(GetDoubleArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002219 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002220 return GetPrimitiveArray<jdoubleArray, jdouble*, mirror::DoubleArray>(soa, 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 Rogersbc939662013-08-15 10:26:54 -07002224 CHECK_NON_NULL_ARGUMENT(GetFloatArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002225 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002226 return GetPrimitiveArray<jfloatArray, jfloat*, mirror::FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002227 }
2228
Elliott Hughes75770752011-08-24 17:52:38 -07002229 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002230 CHECK_NON_NULL_ARGUMENT(GetIntArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002231 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002232 return GetPrimitiveArray<jintArray, jint*, mirror::IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002233 }
2234
Elliott Hughes75770752011-08-24 17:52:38 -07002235 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002236 CHECK_NON_NULL_ARGUMENT(GetLongArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002237 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002238 return GetPrimitiveArray<jlongArray, jlong*, mirror::LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002239 }
2240
Elliott Hughes75770752011-08-24 17:52:38 -07002241 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002242 CHECK_NON_NULL_ARGUMENT(GetShortArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002243 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002244 return GetPrimitiveArray<jshortArray, jshort*, mirror::ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002245 }
2246
Mathieu Chartier590fee92013-09-13 13:46:47 -07002247 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2248 jint mode) {
2249 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002250 }
2251
Mathieu Chartier590fee92013-09-13 13:46:47 -07002252 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
2253 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002254 }
2255
Mathieu Chartier590fee92013-09-13 13:46:47 -07002256 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
2257 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002258 }
2259
Mathieu Chartier590fee92013-09-13 13:46:47 -07002260 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2261 jint mode) {
2262 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002263 }
2264
Mathieu Chartier590fee92013-09-13 13:46:47 -07002265 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2266 jint mode) {
2267 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002268 }
2269
Mathieu Chartier590fee92013-09-13 13:46:47 -07002270 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
2271 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002272 }
2273
Mathieu Chartier590fee92013-09-13 13:46:47 -07002274 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
2275 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002276 }
2277
Mathieu Chartier590fee92013-09-13 13:46:47 -07002278 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2279 jint mode) {
2280 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002281 }
2282
Ian Rogersbc939662013-08-15 10:26:54 -07002283 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2284 jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002285 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002286 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(soa, array, start,
2287 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002288 }
2289
Ian Rogersbc939662013-08-15 10:26:54 -07002290 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2291 jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002292 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002293 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002294 }
2295
Ian Rogersbc939662013-08-15 10:26:54 -07002296 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2297 jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002298 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002299 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002300 }
2301
Ian Rogersbc939662013-08-15 10:26:54 -07002302 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2303 jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002304 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002305 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(soa, array, start, length,
2306 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002307 }
2308
Ian Rogersbc939662013-08-15 10:26:54 -07002309 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2310 jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002311 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002312 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(soa, array, start, length,
2313 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002314 }
2315
Ian Rogersbc939662013-08-15 10:26:54 -07002316 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2317 jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002318 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002319 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002320 }
2321
Ian Rogersbc939662013-08-15 10:26:54 -07002322 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2323 jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002324 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002325 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002326 }
2327
Ian Rogersbc939662013-08-15 10:26:54 -07002328 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2329 jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002330 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002331 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(soa, array, start, length,
2332 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002333 }
2334
Ian Rogersbc939662013-08-15 10:26:54 -07002335 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2336 const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002337 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002338 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002339 }
2340
Ian Rogersbc939662013-08-15 10:26:54 -07002341 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2342 const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002343 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002344 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002345 }
2346
Ian Rogersbc939662013-08-15 10:26:54 -07002347 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2348 const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002349 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002350 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002351 }
2352
Ian Rogersbc939662013-08-15 10:26:54 -07002353 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2354 const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002355 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002356 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002357 }
2358
Ian Rogersbc939662013-08-15 10:26:54 -07002359 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2360 const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002361 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002362 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002363 }
2364
Ian Rogersbc939662013-08-15 10:26:54 -07002365 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2366 const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002367 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002368 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002369 }
2370
Ian Rogersbc939662013-08-15 10:26:54 -07002371 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2372 const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002373 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002374 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002375 }
2376
Ian Rogersbc939662013-08-15 10:26:54 -07002377 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2378 const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002379 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002380 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(soa, array, start, length,
2381 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002382 }
2383
Ian Rogersbc939662013-08-15 10:26:54 -07002384 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2385 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002386 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2387 }
2388
Ian Rogersbc939662013-08-15 10:26:54 -07002389 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2390 jint method_count, bool return_errors) {
2391 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002392 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002393 return JNI_ERR; // Not reached.
2394 }
2395 CHECK_NON_NULL_ARGUMENT(RegisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002396 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002397 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002398 if (UNLIKELY(method_count == 0)) {
2399 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2400 << PrettyDescriptor(c);
2401 return JNI_OK;
2402 }
2403 CHECK_NON_NULL_ARGUMENT(RegisterNatives, methods);
2404 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002405 const char* name = methods[i].name;
2406 const char* sig = methods[i].signature;
Ian Rogers1eb512d2013-10-18 15:42:20 -07002407 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002408 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002409 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002410 ++sig;
2411 }
2412
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002413 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2414 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002415 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002416 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002417 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002418 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002419 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002420 << PrettyDescriptor(c) << "." << name << sig << " in "
2421 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002422 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002423 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002424 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002425 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002426 << PrettyDescriptor(c) << "." << name << sig
2427 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002428 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002429 return JNI_ERR;
2430 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002431
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002432 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002433
Ian Rogers1eb512d2013-10-18 15:42:20 -07002434 m->RegisterNative(soa.Self(), methods[i].fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002435 }
2436 return JNI_OK;
2437 }
2438
Elliott Hughes5174fe62011-08-23 15:12:35 -07002439 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -07002440 CHECK_NON_NULL_ARGUMENT(UnregisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002441 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002442 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002443
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002444 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002445
2446 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002447 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002448 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002449 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002450 }
2451 }
2452 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002453 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002454 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002455 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002456 }
2457 }
2458
2459 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002460 }
2461
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002462 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2463 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002464 CHECK_NON_NULL_ARGUMENT(MonitorEnter, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002465 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002466 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2467 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002468 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002469 return JNI_ERR;
2470 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002471 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002472 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002473 }
2474
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002475 static jint MonitorExit(JNIEnv* env, jobject java_object)
2476 UNLOCK_FUNCTION(monitor_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002477 CHECK_NON_NULL_ARGUMENT(MonitorExit, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002478 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002479 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002480 o->MonitorExit(soa.Self());
2481 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002482 return JNI_ERR;
2483 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002484 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002485 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002486 }
2487
2488 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogersbc939662013-08-15 10:26:54 -07002489 CHECK_NON_NULL_ARGUMENT(GetJavaVM, vm);
Elliott Hughescdf53122011-08-19 15:46:09 -07002490 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002491 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002492 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002493 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002494 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002495 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002496 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002497 }
2498
Elliott Hughescdf53122011-08-19 15:46:09 -07002499 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002500 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002501 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002502 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002503 if (address == nullptr && capacity != 0) {
2504 JniAbortF("NewDirectByteBuffer", "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002505 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002506
Ian Rogers936b37f2014-02-14 00:52:24 -08002507 // At the moment, the capacity is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002508 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002509 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002510 jint capacity_arg = static_cast<jint>(capacity);
2511
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002512 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2513 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002514 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002515 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002516 }
2517
Elliott Hughesb465ab02011-08-24 11:21:21 -07002518 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002519 return reinterpret_cast<void*>(env->GetLongField(
2520 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002521 }
2522
Elliott Hughesb465ab02011-08-24 11:21:21 -07002523 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002524 return static_cast<jlong>(env->GetIntField(
2525 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002526 }
2527
Elliott Hughesb465ab02011-08-24 11:21:21 -07002528 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -07002529 CHECK_NON_NULL_ARGUMENT(GetObjectRefType, java_object);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002530
2531 // Do we definitely know what kind of reference this is?
2532 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2533 IndirectRefKind kind = GetIndirectRefKind(ref);
2534 switch (kind) {
2535 case kLocal:
Ian Rogersef28b142012-11-30 14:22:18 -08002536 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002537 return JNILocalRefType;
2538 }
2539 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002540 case kGlobal:
2541 return JNIGlobalRefType;
2542 case kWeakGlobal:
2543 return JNIWeakGlobalRefType;
2544 case kSirtOrInvalid:
2545 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002546 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002547 return JNILocalRefType;
2548 }
2549
Ian Rogersef28b142012-11-30 14:22:18 -08002550 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002551 return JNIInvalidRefType;
2552 }
2553
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002554 // If we're handing out direct pointers, check whether it's a direct pointer to a local
2555 // reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002556 {
2557 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002558 if (soa.Decode<mirror::Object*>(java_object) ==
2559 reinterpret_cast<mirror::Object*>(java_object)) {
2560 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<mirror::Object*>(java_object))) {
Ian Rogersef28b142012-11-30 14:22:18 -08002561 return JNILocalRefType;
2562 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002563 }
2564 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002565 return JNIInvalidRefType;
2566 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002567 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2568 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002569 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002570
2571 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002572 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2573 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002574 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002575 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002576 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2577 return JNI_ERR;
2578 }
2579 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002580 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002581 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2582 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002583 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002584 soa.Self()->ThrowOutOfMemoryError(caller);
2585 }
2586 return okay ? JNI_OK : JNI_ERR;
2587 }
2588
2589 template<typename JniT, typename ArtT>
2590 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002591 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d99e452014-01-02 17:36:41 -08002592 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002593 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002594 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002595 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002596 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002597 return soa.AddLocalReference<JniT>(result);
2598 }
2599
2600 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2601 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2602 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002603 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002604 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2605 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002606 // Only make a copy if necessary.
2607 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2608 if (is_copy != nullptr) {
2609 *is_copy = JNI_TRUE;
2610 }
2611 static const size_t component_size = array->GetClass()->GetComponentSize();
2612 size_t size = array->GetLength() * component_size;
2613 void* data = new uint64_t[RoundUp(size, 8) / 8];
2614 memcpy(data, array->GetData(), size);
2615 return reinterpret_cast<CArrayT>(data);
2616 } else {
2617 if (is_copy != nullptr) {
2618 *is_copy = JNI_FALSE;
2619 }
2620 return reinterpret_cast<CArrayT>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002621 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002622 }
2623
Mathieu Chartier590fee92013-09-13 13:46:47 -07002624 template <typename ArrayT, typename ElementT>
2625 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
2626 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002627 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002628 size_t component_size = array->GetClass()->GetComponentSize();
Ian Rogersef7d42f2014-01-06 12:55:46 -08002629 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002630 gc::Heap* heap = Runtime::Current()->GetHeap();
2631 bool is_copy = array_data != reinterpret_cast<void*>(elements);
2632 size_t bytes = array->GetLength() * component_size;
2633 VLOG(heap) << "Release primitive array " << env << " array_data " << array_data
2634 << " elements " << reinterpret_cast<void*>(elements);
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002635 if (is_copy) {
2636 // Sanity check: If elements is not the same as the java array's data, it better not be a
2637 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2638 // copies we make?
2639 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
2640 JniAbortF("ReleaseArrayElements", "invalid element pointer %p, array elements are %p",
2641 reinterpret_cast<void*>(elements), array_data);
2642 return;
2643 }
2644 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002645 // Don't need to copy if we had a direct pointer.
2646 if (mode != JNI_ABORT && is_copy) {
2647 memcpy(array_data, elements, bytes);
2648 }
2649 if (mode != JNI_COMMIT) {
2650 if (is_copy) {
2651 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002652 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002653 // Non copy to a movable object must means that we had disabled the moving GC.
2654 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002655 }
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002656 UnpinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002657 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002658 }
2659
2660 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2661 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2662 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002663 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002664 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002665 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2666 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2667 ThrowAIOOBE(soa, array, start, length, "src");
2668 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002669 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002670 JavaT* data = array->GetData();
2671 memcpy(buf, data + start, length * sizeof(JavaT));
2672 }
2673 }
2674
2675 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2676 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2677 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002678 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002679 CHECK_NON_NULL_ARGUMENT(SetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002680 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2681 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2682 ThrowAIOOBE(soa, array, start, length, "dst");
2683 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002684 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002685 JavaT* data = array->GetData();
2686 memcpy(data + start, buf, length * sizeof(JavaT));
2687 }
2688 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002689};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002690
Elliott Hughes88c5c352012-03-15 18:49:48 -07002691const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002692 nullptr, // reserved0.
2693 nullptr, // reserved1.
2694 nullptr, // reserved2.
2695 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002696 JNI::GetVersion,
2697 JNI::DefineClass,
2698 JNI::FindClass,
2699 JNI::FromReflectedMethod,
2700 JNI::FromReflectedField,
2701 JNI::ToReflectedMethod,
2702 JNI::GetSuperclass,
2703 JNI::IsAssignableFrom,
2704 JNI::ToReflectedField,
2705 JNI::Throw,
2706 JNI::ThrowNew,
2707 JNI::ExceptionOccurred,
2708 JNI::ExceptionDescribe,
2709 JNI::ExceptionClear,
2710 JNI::FatalError,
2711 JNI::PushLocalFrame,
2712 JNI::PopLocalFrame,
2713 JNI::NewGlobalRef,
2714 JNI::DeleteGlobalRef,
2715 JNI::DeleteLocalRef,
2716 JNI::IsSameObject,
2717 JNI::NewLocalRef,
2718 JNI::EnsureLocalCapacity,
2719 JNI::AllocObject,
2720 JNI::NewObject,
2721 JNI::NewObjectV,
2722 JNI::NewObjectA,
2723 JNI::GetObjectClass,
2724 JNI::IsInstanceOf,
2725 JNI::GetMethodID,
2726 JNI::CallObjectMethod,
2727 JNI::CallObjectMethodV,
2728 JNI::CallObjectMethodA,
2729 JNI::CallBooleanMethod,
2730 JNI::CallBooleanMethodV,
2731 JNI::CallBooleanMethodA,
2732 JNI::CallByteMethod,
2733 JNI::CallByteMethodV,
2734 JNI::CallByteMethodA,
2735 JNI::CallCharMethod,
2736 JNI::CallCharMethodV,
2737 JNI::CallCharMethodA,
2738 JNI::CallShortMethod,
2739 JNI::CallShortMethodV,
2740 JNI::CallShortMethodA,
2741 JNI::CallIntMethod,
2742 JNI::CallIntMethodV,
2743 JNI::CallIntMethodA,
2744 JNI::CallLongMethod,
2745 JNI::CallLongMethodV,
2746 JNI::CallLongMethodA,
2747 JNI::CallFloatMethod,
2748 JNI::CallFloatMethodV,
2749 JNI::CallFloatMethodA,
2750 JNI::CallDoubleMethod,
2751 JNI::CallDoubleMethodV,
2752 JNI::CallDoubleMethodA,
2753 JNI::CallVoidMethod,
2754 JNI::CallVoidMethodV,
2755 JNI::CallVoidMethodA,
2756 JNI::CallNonvirtualObjectMethod,
2757 JNI::CallNonvirtualObjectMethodV,
2758 JNI::CallNonvirtualObjectMethodA,
2759 JNI::CallNonvirtualBooleanMethod,
2760 JNI::CallNonvirtualBooleanMethodV,
2761 JNI::CallNonvirtualBooleanMethodA,
2762 JNI::CallNonvirtualByteMethod,
2763 JNI::CallNonvirtualByteMethodV,
2764 JNI::CallNonvirtualByteMethodA,
2765 JNI::CallNonvirtualCharMethod,
2766 JNI::CallNonvirtualCharMethodV,
2767 JNI::CallNonvirtualCharMethodA,
2768 JNI::CallNonvirtualShortMethod,
2769 JNI::CallNonvirtualShortMethodV,
2770 JNI::CallNonvirtualShortMethodA,
2771 JNI::CallNonvirtualIntMethod,
2772 JNI::CallNonvirtualIntMethodV,
2773 JNI::CallNonvirtualIntMethodA,
2774 JNI::CallNonvirtualLongMethod,
2775 JNI::CallNonvirtualLongMethodV,
2776 JNI::CallNonvirtualLongMethodA,
2777 JNI::CallNonvirtualFloatMethod,
2778 JNI::CallNonvirtualFloatMethodV,
2779 JNI::CallNonvirtualFloatMethodA,
2780 JNI::CallNonvirtualDoubleMethod,
2781 JNI::CallNonvirtualDoubleMethodV,
2782 JNI::CallNonvirtualDoubleMethodA,
2783 JNI::CallNonvirtualVoidMethod,
2784 JNI::CallNonvirtualVoidMethodV,
2785 JNI::CallNonvirtualVoidMethodA,
2786 JNI::GetFieldID,
2787 JNI::GetObjectField,
2788 JNI::GetBooleanField,
2789 JNI::GetByteField,
2790 JNI::GetCharField,
2791 JNI::GetShortField,
2792 JNI::GetIntField,
2793 JNI::GetLongField,
2794 JNI::GetFloatField,
2795 JNI::GetDoubleField,
2796 JNI::SetObjectField,
2797 JNI::SetBooleanField,
2798 JNI::SetByteField,
2799 JNI::SetCharField,
2800 JNI::SetShortField,
2801 JNI::SetIntField,
2802 JNI::SetLongField,
2803 JNI::SetFloatField,
2804 JNI::SetDoubleField,
2805 JNI::GetStaticMethodID,
2806 JNI::CallStaticObjectMethod,
2807 JNI::CallStaticObjectMethodV,
2808 JNI::CallStaticObjectMethodA,
2809 JNI::CallStaticBooleanMethod,
2810 JNI::CallStaticBooleanMethodV,
2811 JNI::CallStaticBooleanMethodA,
2812 JNI::CallStaticByteMethod,
2813 JNI::CallStaticByteMethodV,
2814 JNI::CallStaticByteMethodA,
2815 JNI::CallStaticCharMethod,
2816 JNI::CallStaticCharMethodV,
2817 JNI::CallStaticCharMethodA,
2818 JNI::CallStaticShortMethod,
2819 JNI::CallStaticShortMethodV,
2820 JNI::CallStaticShortMethodA,
2821 JNI::CallStaticIntMethod,
2822 JNI::CallStaticIntMethodV,
2823 JNI::CallStaticIntMethodA,
2824 JNI::CallStaticLongMethod,
2825 JNI::CallStaticLongMethodV,
2826 JNI::CallStaticLongMethodA,
2827 JNI::CallStaticFloatMethod,
2828 JNI::CallStaticFloatMethodV,
2829 JNI::CallStaticFloatMethodA,
2830 JNI::CallStaticDoubleMethod,
2831 JNI::CallStaticDoubleMethodV,
2832 JNI::CallStaticDoubleMethodA,
2833 JNI::CallStaticVoidMethod,
2834 JNI::CallStaticVoidMethodV,
2835 JNI::CallStaticVoidMethodA,
2836 JNI::GetStaticFieldID,
2837 JNI::GetStaticObjectField,
2838 JNI::GetStaticBooleanField,
2839 JNI::GetStaticByteField,
2840 JNI::GetStaticCharField,
2841 JNI::GetStaticShortField,
2842 JNI::GetStaticIntField,
2843 JNI::GetStaticLongField,
2844 JNI::GetStaticFloatField,
2845 JNI::GetStaticDoubleField,
2846 JNI::SetStaticObjectField,
2847 JNI::SetStaticBooleanField,
2848 JNI::SetStaticByteField,
2849 JNI::SetStaticCharField,
2850 JNI::SetStaticShortField,
2851 JNI::SetStaticIntField,
2852 JNI::SetStaticLongField,
2853 JNI::SetStaticFloatField,
2854 JNI::SetStaticDoubleField,
2855 JNI::NewString,
2856 JNI::GetStringLength,
2857 JNI::GetStringChars,
2858 JNI::ReleaseStringChars,
2859 JNI::NewStringUTF,
2860 JNI::GetStringUTFLength,
2861 JNI::GetStringUTFChars,
2862 JNI::ReleaseStringUTFChars,
2863 JNI::GetArrayLength,
2864 JNI::NewObjectArray,
2865 JNI::GetObjectArrayElement,
2866 JNI::SetObjectArrayElement,
2867 JNI::NewBooleanArray,
2868 JNI::NewByteArray,
2869 JNI::NewCharArray,
2870 JNI::NewShortArray,
2871 JNI::NewIntArray,
2872 JNI::NewLongArray,
2873 JNI::NewFloatArray,
2874 JNI::NewDoubleArray,
2875 JNI::GetBooleanArrayElements,
2876 JNI::GetByteArrayElements,
2877 JNI::GetCharArrayElements,
2878 JNI::GetShortArrayElements,
2879 JNI::GetIntArrayElements,
2880 JNI::GetLongArrayElements,
2881 JNI::GetFloatArrayElements,
2882 JNI::GetDoubleArrayElements,
2883 JNI::ReleaseBooleanArrayElements,
2884 JNI::ReleaseByteArrayElements,
2885 JNI::ReleaseCharArrayElements,
2886 JNI::ReleaseShortArrayElements,
2887 JNI::ReleaseIntArrayElements,
2888 JNI::ReleaseLongArrayElements,
2889 JNI::ReleaseFloatArrayElements,
2890 JNI::ReleaseDoubleArrayElements,
2891 JNI::GetBooleanArrayRegion,
2892 JNI::GetByteArrayRegion,
2893 JNI::GetCharArrayRegion,
2894 JNI::GetShortArrayRegion,
2895 JNI::GetIntArrayRegion,
2896 JNI::GetLongArrayRegion,
2897 JNI::GetFloatArrayRegion,
2898 JNI::GetDoubleArrayRegion,
2899 JNI::SetBooleanArrayRegion,
2900 JNI::SetByteArrayRegion,
2901 JNI::SetCharArrayRegion,
2902 JNI::SetShortArrayRegion,
2903 JNI::SetIntArrayRegion,
2904 JNI::SetLongArrayRegion,
2905 JNI::SetFloatArrayRegion,
2906 JNI::SetDoubleArrayRegion,
2907 JNI::RegisterNatives,
2908 JNI::UnregisterNatives,
2909 JNI::MonitorEnter,
2910 JNI::MonitorExit,
2911 JNI::GetJavaVM,
2912 JNI::GetStringRegion,
2913 JNI::GetStringUTFRegion,
2914 JNI::GetPrimitiveArrayCritical,
2915 JNI::ReleasePrimitiveArrayCritical,
2916 JNI::GetStringCritical,
2917 JNI::ReleaseStringCritical,
2918 JNI::NewWeakGlobalRef,
2919 JNI::DeleteWeakGlobalRef,
2920 JNI::ExceptionCheck,
2921 JNI::NewDirectByteBuffer,
2922 JNI::GetDirectBufferAddress,
2923 JNI::GetDirectBufferCapacity,
2924 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002925};
2926
Elliott Hughes75770752011-08-24 17:52:38 -07002927JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002928 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002929 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002930 local_ref_cookie(IRT_FIRST_SEGMENT),
2931 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002932 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002933 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002934 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002935 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002936 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002937 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002938 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002939}
2940
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002941JNIEnvExt::~JNIEnvExt() {
2942}
2943
Mathieu Chartier590fee92013-09-13 13:46:47 -07002944jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2945 if (obj == nullptr) {
2946 return nullptr;
2947 }
2948 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2949}
2950
2951void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2952 if (obj != nullptr) {
2953 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
2954 }
2955}
Elliott Hughes88c5c352012-03-15 18:49:48 -07002956void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2957 check_jni = enabled;
2958 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002959}
2960
Elliott Hughes73e66f72012-05-09 09:34:45 -07002961void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2962 locals.Dump(os);
2963 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002964}
2965
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002966void JNIEnvExt::PushFrame(int /*capacity*/) {
2967 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002968 stacked_local_ref_cookies.push_back(local_ref_cookie);
2969 local_ref_cookie = locals.GetSegmentState();
2970}
2971
2972void JNIEnvExt::PopFrame() {
2973 locals.SetSegmentState(local_ref_cookie);
2974 local_ref_cookie = stacked_local_ref_cookies.back();
2975 stacked_local_ref_cookies.pop_back();
2976}
2977
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002978Offset JNIEnvExt::SegmentStateOffset() {
2979 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2980 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2981}
2982
Carl Shapiroea4dca82011-08-01 13:45:38 -07002983// JNI Invocation interface.
2984
Brian Carlstrombddf9762013-05-14 11:35:37 -07002985extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002986 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002987 if (IsBadJniVersion(args->version)) {
2988 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002989 return JNI_EVERSION;
2990 }
2991 Runtime::Options options;
2992 for (int i = 0; i < args->nOptions; ++i) {
2993 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002994 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002995 }
2996 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002997 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002998 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002999 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003000 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07003001 bool started = runtime->Start();
3002 if (!started) {
3003 delete Thread::Current()->GetJniEnv();
3004 delete runtime->GetJavaVM();
3005 LOG(WARNING) << "CreateJavaVM failed";
3006 return JNI_ERR;
3007 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07003008 *p_env = Thread::Current()->GetJniEnv();
3009 *p_vm = runtime->GetJavaVM();
3010 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003011}
3012
Elliott Hughesf2682d52011-08-15 16:37:04 -07003013extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003014 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003015 if (runtime == nullptr) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003016 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003017 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003018 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003019 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003020 }
3021 return JNI_OK;
3022}
3023
3024// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003025extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003026 return JNI_ERR;
3027}
3028
Elliott Hughescdf53122011-08-19 15:46:09 -07003029class JII {
3030 public:
3031 static jint DestroyJavaVM(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003032 if (vm == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003033 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003034 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003035 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3036 delete raw_vm->runtime;
3037 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003038 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003039
Elliott Hughescdf53122011-08-19 15:46:09 -07003040 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003041 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07003042 }
3043
3044 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003045 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07003046 }
3047
3048 static jint DetachCurrentThread(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003049 if (vm == nullptr || Thread::Current() == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003050 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003051 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003052 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3053 Runtime* runtime = raw_vm->runtime;
3054 runtime->DetachCurrentThread();
3055 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07003056 }
3057
3058 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07003059 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
3060 // and unlike other calls that take a JNI version doesn't care if you supply
3061 // JNI_VERSION_1_1, which we don't otherwise support.
3062 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07003063 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07003064 return JNI_EVERSION;
3065 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003066 if (vm == nullptr || env == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003067 return JNI_ERR;
3068 }
3069 Thread* thread = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003070 if (thread == nullptr) {
3071 *env = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07003072 return JNI_EDETACHED;
3073 }
3074 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003075 return JNI_OK;
3076 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003077};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003078
Elliott Hughes88c5c352012-03-15 18:49:48 -07003079const JNIInvokeInterface gJniInvokeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003080 nullptr, // reserved0
3081 nullptr, // reserved1
3082 nullptr, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07003083 JII::DestroyJavaVM,
3084 JII::AttachCurrentThread,
3085 JII::DetachCurrentThread,
3086 JII::GetEnv,
3087 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07003088};
3089
Elliott Hughesa0957642011-09-02 14:27:33 -07003090JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003091 : runtime(runtime),
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003092 check_jni_abort_hook(nullptr),
3093 check_jni_abort_hook_data(nullptr),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003094 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003095 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07003096 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08003097 work_around_app_jni_bugs(false),
Ian Rogers62d6c772013-02-27 08:32:07 -08003098 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003099 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07003100 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07003101 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003102 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003103 libraries(new Libraries),
3104 weak_globals_lock_("JNI weak global reference table lock"),
3105 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
3106 allow_new_weak_globals_(true),
3107 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003108 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003109 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003110 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003111 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003112}
3113
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003114JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003115 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003116}
3117
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003118jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3119 if (obj == nullptr) {
3120 return nullptr;
3121 }
3122 MutexLock mu(self, weak_globals_lock_);
3123 while (UNLIKELY(!allow_new_weak_globals_)) {
3124 weak_globals_add_condition_.WaitHoldingLocks(self);
3125 }
3126 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3127 return reinterpret_cast<jweak>(ref);
3128}
3129
3130void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3131 MutexLock mu(self, weak_globals_lock_);
3132 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3133 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3134 << "failed to find entry";
3135 }
3136}
3137
Elliott Hughes88c5c352012-03-15 18:49:48 -07003138void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3139 check_jni = enabled;
3140 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003141}
3142
Elliott Hughesae80b492012-04-24 10:43:17 -07003143void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3144 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3145 if (force_copy) {
3146 os << " (with forcecopy)";
3147 }
3148 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07003149 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003150 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003151 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003152 os << "; pins=" << pin_table.Size();
3153 }
3154 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003155 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003156 os << "; globals=" << globals.Capacity();
3157 }
3158 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003159 MutexLock mu(self, weak_globals_lock_);
3160 if (weak_globals_.Capacity() > 0) {
3161 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003162 }
3163 }
3164 os << '\n';
3165
3166 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003167 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003168 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3169 }
3170}
3171
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003172void JavaVMExt::DisallowNewWeakGlobals() {
3173 MutexLock mu(Thread::Current(), weak_globals_lock_);
3174 allow_new_weak_globals_ = false;
3175}
3176
3177void JavaVMExt::AllowNewWeakGlobals() {
3178 Thread* self = Thread::Current();
3179 MutexLock mu(self, weak_globals_lock_);
3180 allow_new_weak_globals_ = true;
3181 weak_globals_add_condition_.Broadcast(self);
3182}
3183
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003184mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3185 MutexLock mu(self, weak_globals_lock_);
3186 while (UNLIKELY(!allow_new_weak_globals_)) {
3187 weak_globals_add_condition_.WaitHoldingLocks(self);
3188 }
3189 return const_cast<mirror::Object*>(weak_globals_.Get(ref));
3190}
3191
Elliott Hughes73e66f72012-05-09 09:34:45 -07003192void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003193 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003194 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003195 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003196 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003197 }
3198 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003199 MutexLock mu(self, weak_globals_lock_);
3200 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003201 }
3202 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003203 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003204 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003205 }
3206}
3207
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003208bool JavaVMExt::LoadNativeLibrary(const std::string& path,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003209 const SirtRef<mirror::ClassLoader>& class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003210 std::string* detail) {
3211 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003212
3213 // See if we've already loaded this library. If we have, and the class loader
3214 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003215 // TODO: for better results we should canonicalize the pathname (or even compare
3216 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003217 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003218 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003219 {
3220 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003221 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003222 library = libraries->Get(path);
3223 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003224 if (library != nullptr) {
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003225 if (library->GetClassLoader() != class_loader.get()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003226 // The library will be associated with class_loader. The JNI
3227 // spec says we can't load the same library into more than one
3228 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003229 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003230 "ClassLoader %p; can't open in ClassLoader %p",
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003231 path.c_str(), library->GetClassLoader(), class_loader.get());
Elliott Hughes75770752011-08-24 17:52:38 -07003232 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003233 return false;
3234 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003235 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003236 << "ClassLoader " << class_loader.get() << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003237 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003238 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003239 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003240 return false;
3241 }
3242 return true;
3243 }
3244
3245 // Open the shared library. Because we're using a full path, the system
3246 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3247 // resolve this library's dependencies though.)
3248
3249 // Failures here are expected when java.library.path has several entries
3250 // and we have to hunt for the lib.
3251
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003252 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3253 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3254 // dlopen) becomes zero from dlclose.
3255
Elliott Hughescdf53122011-08-19 15:46:09 -07003256 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003257 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003258 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003259 void* handle = dlopen(path.empty() ? nullptr : path.c_str(), RTLD_LAZY);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003260 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003261
Elliott Hughes84b2f142012-09-27 09:16:28 -07003262 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003263
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003264 if (handle == nullptr) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003265 *detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07003266 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003267 return false;
3268 }
3269
3270 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003271 // TODO: move the locking (and more of this logic) into Libraries.
3272 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003273 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003274 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003275 library = libraries->Get(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003276 if (library == nullptr) { // We won race to get libraries_lock
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003277 library = new SharedLibrary(path, handle, class_loader.get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003278 libraries->Put(path, library);
3279 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003280 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003281 }
3282 if (!created_library) {
3283 LOG(INFO) << "WOW: we lost a race to add shared library: "
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003284 << "\"" << path << "\" ClassLoader=" << class_loader.get();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003285 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003286 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003287
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003288 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader.get()
3289 << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003290
Elliott Hughes79353722013-08-02 16:52:18 -07003291 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003292 void* sym = dlsym(handle, "JNI_OnLoad");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003293 if (sym == nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003294 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003295 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003296 } else {
3297 // Call JNI_OnLoad. We have to override the current class
3298 // loader, which will always be "null" since the stuff at the
3299 // top of the stack is around Runtime.loadLibrary(). (See
3300 // the comments in the JNI FindClass function.)
3301 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3302 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003303 SirtRef<mirror::ClassLoader> old_class_loader(self, self->GetClassLoaderOverride());
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003304 self->SetClassLoaderOverride(class_loader.get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003305
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003306 int version = 0;
3307 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003308 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003309 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003310 version = (*jni_on_load)(this, nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -07003311 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003312
Mathieu Chartier590fee92013-09-13 13:46:47 -07003313 self->SetClassLoaderOverride(old_class_loader.get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003314
Elliott Hughes79353722013-08-02 16:52:18 -07003315 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003316 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003317 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003318 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003319 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003320 // It's unwise to call dlclose() here, but we can mark it
3321 // as bad and ensure that future load attempts will fail.
3322 // We don't know how far JNI_OnLoad got, so there could
3323 // be some partially-initialized stuff accessible through
3324 // newly-registered native method calls. We could try to
3325 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003326 } else {
3327 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003328 }
Elliott Hughes79353722013-08-02 16:52:18 -07003329 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003330 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003331 }
3332
Elliott Hughes79353722013-08-02 16:52:18 -07003333 library->SetResult(was_successful);
3334 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003335}
3336
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003337void* JavaVMExt::FindCodeForNativeMethod(mirror::ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003338 CHECK(m->IsNative());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003339 mirror::Class* c = m->GetDeclaringClass();
Elliott Hughes79082e32011-08-25 12:07:32 -07003340 // If this is a static method, it could be called before the class
3341 // has been initialized.
3342 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003343 c = EnsureInitialized(Thread::Current(), c);
3344 if (c == nullptr) {
3345 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003346 }
3347 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003348 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003349 }
Brian Carlstrom16192862011-09-12 17:50:06 -07003350 std::string detail;
3351 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003352 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003353 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003354 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003355 native_method = libraries->FindNativeMethod(m, detail);
3356 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003357 // Throwing can cause libraries_lock to be reacquired.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003358 if (native_method == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003359 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3360 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003361 }
3362 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003363}
3364
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003365void JavaVMExt::SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003366 MutexLock mu(Thread::Current(), weak_globals_lock_);
3367 for (mirror::Object** entry : weak_globals_) {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003368 mirror::Object* obj = *entry;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003369 mirror::Object* new_obj = callback(obj, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003370 if (new_obj == nullptr) {
3371 new_obj = kClearedJniWeakGlobal;
3372 }
3373 *entry = new_obj;
3374 }
3375}
3376
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003377void JavaVMExt::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003378 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003379 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003380 ReaderMutexLock mu(self, globals_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003381 globals.VisitRoots(callback, arg, 0, kRootJNIGlobal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003382 }
3383 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003384 MutexLock mu(self, pins_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003385 pin_table.VisitRoots(callback, arg, 0, kRootVMInternal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003386 }
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003387 {
3388 MutexLock mu(self, libraries_lock);
3389 // Libraries contains shared libraries which hold a pointer to a class loader.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003390 libraries->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003391 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07003392 // The weak_globals table is visited by the GC itself (because it mutates the table).
3393}
3394
Elliott Hughesc8fece32013-01-02 11:27:23 -08003395void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003396 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003397 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003398 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003399 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3400 }
3401 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3402}
3403
Ian Rogersdf20fe02011-07-20 20:34:16 -07003404} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003405
3406std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3407 switch (rhs) {
3408 case JNIInvalidRefType:
3409 os << "JNIInvalidRefType";
3410 return os;
3411 case JNILocalRefType:
3412 os << "JNILocalRefType";
3413 return os;
3414 case JNIGlobalRefType:
3415 os << "JNIGlobalRefType";
3416 return os;
3417 case JNIWeakGlobalRefType:
3418 os << "JNIWeakGlobalRefType";
3419 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003420 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003421 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003422 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003423 }
3424}