blob: 4fad5c9c11b2ba6e312ff2475935fafd6ad29d4d [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"
Ian Rogers98379392014-02-24 16:53:16 -080029#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070031#include "gc/accounting/card_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070032#include "interpreter/interpreter.h"
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070033#include "invoke_arg_array_builder.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070034#include "jni.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070035#include "mirror/art_field-inl.h"
36#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/class-inl.h"
38#include "mirror/class_loader.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/object-inl.h"
40#include "mirror/object_array-inl.h"
41#include "mirror/throwable.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080042#include "object_utils.h"
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080043#include "parsed_options.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 "
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800108 << PrettyTypeOf(argument) << " as argument " << (i + 1)
109 << " to " << PrettyMethod(m);
Elliott Hughesb264f082012-04-06 17:10:10 -0700110 ++error_count;
111 }
Jeff Hao5d917302013-02-27 17:57:33 -0800112 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
113 offset++;
Elliott Hughesb264f082012-04-06 17:10:10 -0700114 }
115 }
116 if (error_count > 0) {
117 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
118 // with an argument.
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800119 JniAbortF(nullptr, "bad arguments passed to %s (see above for details)",
120 PrettyMethod(m).c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -0700121 }
122}
Elliott Hughesb264f082012-04-06 17:10:10 -0700123
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800124void InvokeWithArgArray(const ScopedObjectAccess& soa, mirror::ArtMethod* method,
Ian Rogers0177e532014-02-11 16:30:46 -0800125 ArgArray* arg_array, JValue* result, const char* shorty)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700126 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700127 uint32_t* args = arg_array->GetArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700128 if (UNLIKELY(soa.Env()->check_jni)) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700129 CheckMethodArguments(method, args);
Elliott Hughes4cacde82012-04-11 18:32:27 -0700130 }
Ian Rogers0177e532014-02-11 16:30:46 -0800131 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, shorty);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700132}
133
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700134static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
135 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700136 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800137 mirror::ArtMethod* method = soa.DecodeMethod(mid);
138 mirror::Object* receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700139 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800140 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700141 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800142 arg_array.BuildArgArray(soa, receiver, args);
Ian Rogers0177e532014-02-11 16:30:46 -0800143 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty());
Jeff Hao6474d192013-03-26 14:08:09 -0700144 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700145}
146
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800147static mirror::ArtMethod* FindVirtualMethod(mirror::Object* receiver, mirror::ArtMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700148 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700149 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700150}
151
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700152static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
153 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700154 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800155 mirror::Object* receiver = soa.Decode<mirror::Object*>(obj);
156 mirror::ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700157 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800158 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700159 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800160 arg_array.BuildArgArray(soa, receiver, args);
Ian Rogers0177e532014-02-11 16:30:46 -0800161 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty());
Jeff Hao6474d192013-03-26 14:08:09 -0700162 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700163}
164
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700165static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
166 jobject obj, jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700167 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800168 mirror::Object* receiver = soa.Decode<mirror::Object*>(obj);
169 mirror::ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700170 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800171 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700172 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800173 arg_array.BuildArgArray(soa, receiver, args);
Ian Rogers0177e532014-02-11 16:30:46 -0800174 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty());
Jeff Hao6474d192013-03-26 14:08:09 -0700175 return result;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700176}
177
Elliott Hughes6b436852011-08-12 10:16:44 -0700178// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
179// separated with slashes but aren't wrapped with "L;" like regular descriptors
180// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
181// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
182// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700183static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700184 std::string result;
185 // Add the missing "L;" if necessary.
186 if (name[0] == '[') {
187 result = name;
188 } else {
189 result += 'L';
190 result += name;
191 result += ';';
192 }
193 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700194 if (result.find('.') != std::string::npos) {
195 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
196 << "\"" << name << "\"";
197 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700198 }
199 return result;
200}
201
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800202static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, mirror::Class* c,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700203 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700204 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800205 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
206 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
207 "no %s method \"%s.%s%s\"",
208 kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700209}
210
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800211static mirror::Class* EnsureInitialized(Thread* self, mirror::Class* klass)
212 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
213 if (LIKELY(klass->IsInitialized())) {
214 return klass;
215 }
216 SirtRef<mirror::Class> sirt_klass(self, klass);
217 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(sirt_klass, true, true)) {
218 return nullptr;
219 }
220 return sirt_klass.get();
221}
222
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700223static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
224 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700225 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800226 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(jni_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800227 if (c == nullptr) {
228 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700229 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800230 mirror::ArtMethod* method = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700231 if (is_static) {
232 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700233 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700234 method = c->FindVirtualMethod(name, sig);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800235 if (method == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700236 // No virtual method matching the signature. Search declared
237 // private methods and constructors.
238 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700239 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700240 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800241 if (method == nullptr || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700242 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800243 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700244 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700245 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700246}
247
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800248static mirror::ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700249 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800250 mirror::ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700251 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
252 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogersef28b142012-11-30 14:22:18 -0800253 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700254 }
Brian Carlstromce888532013-10-10 00:32:58 -0700255 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800256 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700257 return method->GetDeclaringClass()->GetClassLoader();
258 }
259 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800260 mirror::ClassLoader* class_loader =
261 soa.Decode<mirror::ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
262 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700263 return class_loader;
264 }
265 // See if the override ClassLoader is set for gtests.
266 class_loader = soa.Self()->GetClassLoaderOverride();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800267 if (class_loader != nullptr) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800268 // If so, CommonCompilerTest should have set UseCompileTimeClassPath.
Brian Carlstromce888532013-10-10 00:32:58 -0700269 CHECK(Runtime::Current()->UseCompileTimeClassPath());
270 return class_loader;
271 }
272 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800273 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700274}
275
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700276static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
277 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700278 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800279 SirtRef<mirror::Class> c(soa.Self(), EnsureInitialized(soa.Self(),
280 soa.Decode<mirror::Class*>(jni_class)));
281 if (c.get() == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800282 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700283 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800284 mirror::ArtField* field = nullptr;
285 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700286 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
287 if (sig[1] != '\0') {
Jeff Hao62509b62013-12-10 17:44:56 -0800288 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), c->GetClassLoader());
Ian Rogers98379392014-02-24 16:53:16 -0800289 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700290 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700291 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700292 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800293 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700294 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700295 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800296 ThrowLocation throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800297 SirtRef<mirror::Throwable> cause(soa.Self(), soa.Self()->GetException(&throw_location));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700298 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800299 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800300 "no type \"%s\" found and so no field \"%s\" "
301 "could be found in class \"%s\" or its superclasses", sig, name,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800302 ClassHelper(c.get()).GetDescriptor());
303 soa.Self()->GetException(nullptr)->SetCause(cause.get());
304 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700305 }
306 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800307 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700308 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800309 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700310 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800311 if (field == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800312 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
313 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
314 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800315 sig, name, ClassHelper(c.get()).GetDescriptor());
316 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700317 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700318 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700319}
320
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800321static void PinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700322 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700323 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700324 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700325 vm->pin_table.Add(array);
326}
327
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800328static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, mirror::Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700329 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700330 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700331 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700332 vm->pin_table.Remove(array);
333}
334
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800335static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700336 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700337 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700338 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800339 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
340 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
341 "%s offset=%d length=%d %s.length=%d",
342 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700343}
Ian Rogers0571d352011-11-03 19:51:38 -0700344
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700345static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
346 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700347 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800348 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
349 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
350 "offset=%d length=%d string.length()=%d", start, length,
351 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700352}
Elliott Hughes814e4032011-08-23 12:07:56 -0700353
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700354int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700355 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700356 // Turn the const char* into a java.lang.String.
357 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800358 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700359 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700360 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700361
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700362 // Choose an appropriate constructor and set up the arguments.
363 jvalue args[2];
364 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800365 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700366 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800367 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700368 signature = "(Ljava/lang/String;)V";
369 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800370 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700371 signature = "(Ljava/lang/Throwable;)V";
372 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700373 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700374 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
375 args[0].l = s.get();
376 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700377 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700378 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800379 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800380 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700381 LOG(ERROR) << "No <init>" << signature << " in "
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800382 << PrettyClass(soa.Decode<mirror::Class*>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700383 return JNI_ERR;
384 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700385
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800386 ScopedLocalRef<jthrowable> exception(
387 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
388 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700389 return JNI_ERR;
390 }
Ian Rogersef28b142012-11-30 14:22:18 -0800391 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800392 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800393 soa.Self()->SetException(throw_location, soa.Decode<mirror::Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700394 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700395}
396
Elliott Hughes462c9442012-03-23 18:47:50 -0700397static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800398 if (vm == nullptr || p_env == nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -0700399 return JNI_ERR;
400 }
401
Elliott Hughes462c9442012-03-23 18:47:50 -0700402 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700403 Thread* self = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800404 if (self != nullptr) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700405 *p_env = self->GetJniEnv();
406 return JNI_OK;
407 }
408
409 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
410
411 // No threads allowed in zygote mode.
412 if (runtime->IsZygote()) {
413 LOG(ERROR) << "Attempt to attach a thread in the zygote";
414 return JNI_ERR;
415 }
416
Elliott Hughes462c9442012-03-23 18:47:50 -0700417 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800418 const char* thread_name = nullptr;
419 jobject thread_group = nullptr;
420 if (args != nullptr) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700421 if (IsBadJniVersion(args->version)) {
422 LOG(ERROR) << "Bad JNI version passed to "
423 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
424 << args->version;
425 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700426 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700427 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700428 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700429 }
Elliott Hughes75770752011-08-24 17:52:38 -0700430
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800431 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800432 *p_env = nullptr;
Ian Rogers120f1c72012-09-28 17:17:10 -0700433 return JNI_ERR;
434 } else {
435 *p_env = Thread::Current()->GetJniEnv();
436 return JNI_OK;
437 }
Elliott Hughes75770752011-08-24 17:52:38 -0700438}
439
Elliott Hughes79082e32011-08-25 12:07:32 -0700440class SharedLibrary {
441 public:
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800442 SharedLibrary(const std::string& path, void* handle, mirror::Object* class_loader)
Elliott Hughes79082e32011-08-25 12:07:32 -0700443 : path_(path),
444 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700445 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700446 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700447 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700448 jni_on_load_thread_id_(Thread::Current()->GetThreadId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700449 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700450 }
451
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800452 mirror::Object* GetClassLoader() {
Elliott Hughes79082e32011-08-25 12:07:32 -0700453 return class_loader_;
454 }
455
456 std::string GetPath() {
457 return path_;
458 }
459
460 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700461 * Check the result of an earlier call to JNI_OnLoad on this library.
462 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700463 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700464 bool CheckOnLoadResult()
465 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700466 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700467 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700468 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
469 bool okay;
470 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700471 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700472
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700473 if (jni_on_load_thread_id_ == self->GetThreadId()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700474 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
475 // caller can continue.
476 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
477 okay = true;
478 } else {
479 while (jni_on_load_result_ == kPending) {
480 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700481 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700482 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700483
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700484 okay = (jni_on_load_result_ == kOkay);
485 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
486 << (okay ? "succeeded" : "failed") << "]";
487 }
488 }
489 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700490 return okay;
491 }
492
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700493 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700494 Thread* self = Thread::Current();
495 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700496
Elliott Hughes79082e32011-08-25 12:07:32 -0700497 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700498 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700499
500 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700501 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700502 }
503
504 void* FindSymbol(const std::string& symbol_name) {
505 return dlsym(handle_, symbol_name.c_str());
506 }
507
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800508 void VisitRoots(RootCallback* visitor, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800509 if (class_loader_ != nullptr) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800510 visitor(&class_loader_, arg, 0, kRootVMInternal);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800511 }
512 }
513
Elliott Hughes79082e32011-08-25 12:07:32 -0700514 private:
515 enum JNI_OnLoadState {
516 kPending,
517 kFailed,
518 kOkay,
519 };
520
521 // Path to library "/system/lib/libjni.so".
522 std::string path_;
523
524 // The void* returned by dlopen(3).
525 void* handle_;
526
527 // The ClassLoader this library is associated with.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800528 mirror::Object* class_loader_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700529
530 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700531 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700532 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700533 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700534 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700535 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700536 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700537 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700538};
539
Elliott Hughes79082e32011-08-25 12:07:32 -0700540// This exists mainly to keep implementation details out of the header file.
541class Libraries {
542 public:
543 Libraries() {
544 }
545
546 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700547 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700548 }
549
Elliott Hughesae80b492012-04-24 10:43:17 -0700550 void Dump(std::ostream& os) const {
551 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700552 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700553 if (!first) {
554 os << ' ';
555 }
556 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700557 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700558 }
559 }
560
561 size_t size() const {
562 return libraries_.size();
563 }
564
Elliott Hughes79082e32011-08-25 12:07:32 -0700565 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700566 auto it = libraries_.find(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800567 return (it == libraries_.end()) ? nullptr : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700568 }
569
570 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700571 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700572 }
573
574 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800575 void* FindNativeMethod(mirror::ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700576 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700577 std::string jni_short_name(JniShortName(m));
578 std::string jni_long_name(JniLongName(m));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800579 const mirror::ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700580 for (const auto& lib : libraries_) {
581 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700582 if (library->GetClassLoader() != declaring_class_loader) {
583 // We only search libraries loaded by the appropriate ClassLoader.
584 continue;
585 }
586 // Try the short name then the long name...
587 void* fn = library->FindSymbol(jni_short_name);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800588 if (fn == nullptr) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700589 fn = library->FindSymbol(jni_long_name);
590 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800591 if (fn != nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800592 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
593 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700594 return fn;
595 }
596 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700597 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700598 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700599 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700600 LOG(ERROR) << detail;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800601 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -0700602 }
603
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800604 void VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800605 for (auto& lib_pair : libraries_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800606 lib_pair.second->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -0800607 }
608 }
609
Elliott Hughes79082e32011-08-25 12:07:32 -0700610 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700611 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700612};
613
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700614JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
615 jvalue* args) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800616 mirror::ArtMethod* method = soa.DecodeMethod(mid);
617 mirror::Object* receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700618 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800619 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700620 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800621 arg_array.BuildArgArray(soa, receiver, args);
Ian Rogers0177e532014-02-11 16:30:46 -0800622 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty());
Jeff Hao6474d192013-03-26 14:08:09 -0700623 return result;
Elliott Hughesd07986f2011-12-06 18:27:45 -0800624}
625
Ian Rogersbc939662013-08-15 10:26:54 -0700626#define CHECK_NON_NULL_ARGUMENT(fn, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800627 if (UNLIKELY(value == nullptr)) { \
Ian Rogersbc939662013-08-15 10:26:54 -0700628 JniAbortF(#fn, #value " == null"); \
629 }
630
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700631#define CHECK_NON_NULL_MEMCPY_ARGUMENT(fn, length, value) \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800632 if (UNLIKELY(length != 0 && value == nullptr)) { \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700633 JniAbortF(#fn, #value " == null"); \
634 }
635
Elliott Hughescdf53122011-08-19 15:46:09 -0700636class JNI {
637 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700638 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700639 return JNI_VERSION_1_6;
640 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700641
Ian Rogers25e8b912012-09-07 11:31:36 -0700642 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700643 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800644 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700645 }
646
Elliott Hughescdf53122011-08-19 15:46:09 -0700647 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogersbc939662013-08-15 10:26:54 -0700648 CHECK_NON_NULL_ARGUMENT(FindClass, name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700649 Runtime* runtime = Runtime::Current();
650 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700651 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700652 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800653 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700654 if (runtime->IsStarted()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700655 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), GetClassLoader(soa));
Ian Rogers98379392014-02-24 16:53:16 -0800656 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700657 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800658 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700659 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700660 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700661 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700662
Elliott Hughescdf53122011-08-19 15:46:09 -0700663 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogersbc939662013-08-15 10:26:54 -0700664 CHECK_NON_NULL_ARGUMENT(FromReflectedMethod, java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700665 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800666 jobject art_method = env->GetObjectField(
667 java_method, WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod);
668 mirror::ArtMethod* method = soa.Decode<mirror::ArtMethod*>(art_method);
669 DCHECK(method != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700670 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700671 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700672
Elliott Hughescdf53122011-08-19 15:46:09 -0700673 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogersbc939662013-08-15 10:26:54 -0700674 CHECK_NON_NULL_ARGUMENT(FromReflectedField, java_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700675 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700676 jobject art_field = env->GetObjectField(java_field,
677 WellKnownClasses::java_lang_reflect_Field_artField);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800678 mirror::ArtField* field = soa.Decode<mirror::ArtField*>(art_field);
679 DCHECK(field != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700680 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700681 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700682
Elliott Hughescdf53122011-08-19 15:46:09 -0700683 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700684 CHECK_NON_NULL_ARGUMENT(ToReflectedMethod, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700685 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800686 mirror::ArtMethod* m = soa.DecodeMethod(mid);
687 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -0700688 jobject art_method = soa.AddLocalReference<jobject>(m);
689 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
690 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800691 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700692 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800693 SetObjectField(env, reflect_method,
694 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod, art_method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700695 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700696 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700697
Elliott Hughescdf53122011-08-19 15:46:09 -0700698 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700699 CHECK_NON_NULL_ARGUMENT(ToReflectedField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700700 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800701 mirror::ArtField* f = soa.DecodeField(fid);
Brian Carlstromea46f952013-07-30 01:26:50 -0700702 jobject art_field = soa.AddLocalReference<jobject>(f);
703 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
704 if (env->ExceptionCheck()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800705 return nullptr;
Brian Carlstromea46f952013-07-30 01:26:50 -0700706 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800707 SetObjectField(env, reflect_field,
708 WellKnownClasses::java_lang_reflect_Field_artField, art_field);
Brian Carlstromea46f952013-07-30 01:26:50 -0700709 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700710 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700711
Elliott Hughes37f7a402011-08-22 18:56:01 -0700712 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -0700713 CHECK_NON_NULL_ARGUMENT(GetObjectClass, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700714 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800715 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700716 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700717 }
718
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700719 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700720 CHECK_NON_NULL_ARGUMENT(GetSuperclass, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700721 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800722 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700723 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700724 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700725
Elliott Hughes37f7a402011-08-22 18:56:01 -0700726 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogersbc939662013-08-15 10:26:54 -0700727 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class1);
728 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class2);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700729 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800730 mirror::Class* c1 = soa.Decode<mirror::Class*>(java_class1);
731 mirror::Class* c2 = soa.Decode<mirror::Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700732 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700733 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700734
Elliott Hughese84278b2012-03-22 10:06:53 -0700735 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700736 CHECK_NON_NULL_ARGUMENT(IsInstanceOf, java_class);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800737 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700738 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700739 return JNI_TRUE;
740 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700741 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800742 mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
743 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700744 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700745 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700746 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700747
Elliott Hughes37f7a402011-08-22 18:56:01 -0700748 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700749 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800750 mirror::Throwable* exception = soa.Decode<mirror::Throwable*>(java_exception);
751 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700752 return JNI_ERR;
753 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800754 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
755 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700756 return JNI_OK;
757 }
758
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700759 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogersbc939662013-08-15 10:26:54 -0700760 CHECK_NON_NULL_ARGUMENT(ThrowNew, c);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800761 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700762 }
763
764 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700765 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700766 }
767
768 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700769 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700770 }
771
772 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700773 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700774
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800775 SirtRef<mirror::Object> old_throw_this_object(soa.Self(), nullptr);
776 SirtRef<mirror::ArtMethod> old_throw_method(soa.Self(), nullptr);
777 SirtRef<mirror::Throwable> old_exception(soa.Self(), nullptr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800778 uint32_t old_throw_dex_pc;
779 {
780 ThrowLocation old_throw_location;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800781 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Ian Rogers62d6c772013-02-27 08:32:07 -0800782 old_throw_this_object.reset(old_throw_location.GetThis());
783 old_throw_method.reset(old_throw_location.GetMethod());
784 old_exception.reset(old_exception_obj);
785 old_throw_dex_pc = old_throw_location.GetDexPc();
786 soa.Self()->ClearException();
787 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800788 ScopedLocalRef<jthrowable> exception(env,
789 soa.AddLocalReference<jthrowable>(old_exception.get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700790 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
791 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800792 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700793 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Ian Rogers62d6c772013-02-27 08:32:07 -0800794 << PrettyTypeOf(old_exception.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700795 } else {
796 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800797 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800798 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(nullptr))
Elliott Hughes72025e52011-08-23 17:50:30 -0700799 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800800 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700801 }
802 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800803 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
804 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700805
Ian Rogers62d6c772013-02-27 08:32:07 -0800806 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700807 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700808
Elliott Hughescdf53122011-08-19 15:46:09 -0700809 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700810 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800811 mirror::Object* exception = soa.Self()->GetException(nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700812 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700813 }
814
Ian Rogers25e8b912012-09-07 11:31:36 -0700815 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700816 LOG(FATAL) << "JNI FatalError called: " << msg;
817 }
818
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700819 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800820 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700821 return JNI_ERR;
822 }
Ian Rogersef28b142012-11-30 14:22:18 -0800823 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700824 return JNI_OK;
825 }
826
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700827 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700828 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800829 mirror::Object* survivor = soa.Decode<mirror::Object*>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700830 soa.Env()->PopFrame();
831 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700832 }
833
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700834 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800835 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700836 }
837
Elliott Hughescdf53122011-08-19 15:46:09 -0700838 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700839 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800840 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800841 // Check for null after decoding the object to handle cleared weak globals.
842 if (decoded_obj == nullptr) {
843 return nullptr;
844 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700845 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700846 IndirectReferenceTable& globals = vm->globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700847 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700848 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700849 return reinterpret_cast<jobject>(ref);
850 }
851
852 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800853 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700854 return;
855 }
Ian Rogersef28b142012-11-30 14:22:18 -0800856 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700857 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800858 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700859 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700860
861 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
862 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
863 << "failed to find entry";
864 }
865 }
866
867 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700868 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800869 return AddWeakGlobalReference(soa, soa.Decode<mirror::Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700870 }
871
872 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700873 if (obj != nullptr) {
874 ScopedObjectAccess soa(env);
875 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700876 }
877 }
878
879 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700880 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800881 mirror::Object* decoded_obj = soa.Decode<mirror::Object*>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800882 // Check for null after decoding the object to handle cleared weak globals.
883 if (decoded_obj == nullptr) {
884 return nullptr;
885 }
886 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700887 }
888
889 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800890 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700891 return;
892 }
Ian Rogersef28b142012-11-30 14:22:18 -0800893 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700894
Ian Rogersef28b142012-11-30 14:22:18 -0800895 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700896 if (!locals.Remove(cookie, obj)) {
897 // Attempting to delete a local reference that is not in the
898 // topmost local reference frame is a no-op. DeleteLocalRef returns
899 // void and doesn't throw any exceptions, but we should probably
900 // complain about it so the user will notice that things aren't
901 // going quite the way they expect.
902 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
903 << "failed to find entry";
904 }
905 }
906
907 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700908 if (obj1 == obj2) {
909 return JNI_TRUE;
910 } else {
911 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800912 return (soa.Decode<mirror::Object*>(obj1) == soa.Decode<mirror::Object*>(obj2))
913 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700914 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700915 }
916
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700917 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700918 CHECK_NON_NULL_ARGUMENT(AllocObject, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700919 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800920 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800921 if (c == nullptr) {
922 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700923 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700924 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700925 }
926
Ian Rogersbc939662013-08-15 10:26:54 -0700927 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700928 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700929 va_start(args, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700930 CHECK_NON_NULL_ARGUMENT(NewObject, java_class);
931 CHECK_NON_NULL_ARGUMENT(NewObject, mid);
932 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700933 va_end(args);
934 return result;
935 }
936
Elliott Hughes72025e52011-08-23 17:50:30 -0700937 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700938 CHECK_NON_NULL_ARGUMENT(NewObjectV, java_class);
939 CHECK_NON_NULL_ARGUMENT(NewObjectV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700940 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800941 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800942 if (c == nullptr) {
943 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700944 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800945 mirror::Object* result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800946 if (result == nullptr) {
947 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700948 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700949 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700950 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800951 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800952 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700953 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800954 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700955 }
956
Elliott Hughes72025e52011-08-23 17:50:30 -0700957 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700958 CHECK_NON_NULL_ARGUMENT(NewObjectA, java_class);
959 CHECK_NON_NULL_ARGUMENT(NewObjectA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700960 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800961 mirror::Class* c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class*>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800962 if (c == nullptr) {
963 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700964 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800965 mirror::Object* result = c->AllocObject(soa.Self());
966 if (result == nullptr) {
967 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700968 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700969 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700970 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800971 if (soa.Self()->IsExceptionPending()) {
972 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700973 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800974 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700975 }
976
Ian Rogersbc939662013-08-15 10:26:54 -0700977 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
978 CHECK_NON_NULL_ARGUMENT(GetMethodID, java_class);
979 CHECK_NON_NULL_ARGUMENT(GetMethodID, name);
980 CHECK_NON_NULL_ARGUMENT(GetMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700981 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700982 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700983 }
984
Ian Rogersbc939662013-08-15 10:26:54 -0700985 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
986 const char* sig) {
987 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, java_class);
988 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, name);
989 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700990 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700991 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700992 }
993
Elliott Hughes72025e52011-08-23 17:50:30 -0700994 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700995 va_list ap;
996 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700997 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, obj);
998 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700999 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001000 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001001 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001002 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001003 }
1004
Elliott Hughes72025e52011-08-23 17:50:30 -07001005 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001006 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, obj);
1007 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001008 ScopedObjectAccess soa(env);
1009 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
1010 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001011 }
1012
Elliott Hughes72025e52011-08-23 17:50:30 -07001013 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001014 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, obj);
1015 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001016 ScopedObjectAccess soa(env);
1017 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
1018 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 }
1020
Elliott Hughes72025e52011-08-23 17:50:30 -07001021 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 va_list ap;
1023 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001024 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, obj);
1025 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001026 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001027 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001029 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001030 }
1031
Elliott Hughes72025e52011-08-23 17:50:30 -07001032 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001033 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, obj);
1034 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001035 ScopedObjectAccess soa(env);
1036 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001037 }
1038
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001040 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, obj);
1041 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001042 ScopedObjectAccess soa(env);
1043 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001044 }
1045
Elliott Hughes72025e52011-08-23 17:50:30 -07001046 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 va_list ap;
1048 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001049 CHECK_NON_NULL_ARGUMENT(CallByteMethod, obj);
1050 CHECK_NON_NULL_ARGUMENT(CallByteMethod, mid);
1051 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001052 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001054 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001055 }
1056
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001058 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, obj);
1059 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001060 ScopedObjectAccess soa(env);
1061 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 }
1063
Elliott Hughes72025e52011-08-23 17:50:30 -07001064 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001065 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, obj);
1066 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001067 ScopedObjectAccess soa(env);
1068 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001069 }
1070
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001072 va_list ap;
1073 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001074 CHECK_NON_NULL_ARGUMENT(CallCharMethod, obj);
1075 CHECK_NON_NULL_ARGUMENT(CallCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001076 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001077 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001079 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 }
1081
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001083 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, obj);
1084 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001085 ScopedObjectAccess soa(env);
1086 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 }
1088
Elliott Hughes72025e52011-08-23 17:50:30 -07001089 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001090 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, obj);
1091 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001092 ScopedObjectAccess soa(env);
1093 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 }
1095
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 va_list ap;
1098 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001099 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, obj);
1100 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001101 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001102 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001103 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001104 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 }
1106
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001108 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, obj);
1109 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001110 ScopedObjectAccess soa(env);
1111 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 }
1113
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001115 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, obj);
1116 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001117 ScopedObjectAccess soa(env);
1118 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 }
1120
Elliott Hughes72025e52011-08-23 17:50:30 -07001121 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 va_list ap;
1123 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001124 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, obj);
1125 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, mid);
1126 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001127 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001129 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 }
1131
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001133 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, obj);
1134 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001135 ScopedObjectAccess soa(env);
1136 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 }
1138
Elliott Hughes72025e52011-08-23 17:50:30 -07001139 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001140 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, obj);
1141 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001142 ScopedObjectAccess soa(env);
1143 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 }
1145
Elliott Hughes72025e52011-08-23 17:50:30 -07001146 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 va_list ap;
1148 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001149 CHECK_NON_NULL_ARGUMENT(CallIntMethod, obj);
1150 CHECK_NON_NULL_ARGUMENT(CallIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001151 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001152 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001153 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001154 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 }
1156
Elliott Hughes72025e52011-08-23 17:50:30 -07001157 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001158 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, obj);
1159 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001160 ScopedObjectAccess soa(env);
1161 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 }
1163
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001165 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, obj);
1166 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001167 ScopedObjectAccess soa(env);
1168 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 }
1170
Elliott Hughes72025e52011-08-23 17:50:30 -07001171 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001172 va_list ap;
1173 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001174 CHECK_NON_NULL_ARGUMENT(CallLongMethod, obj);
1175 CHECK_NON_NULL_ARGUMENT(CallLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001176 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001177 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001178 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001179 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 }
1181
Elliott Hughes72025e52011-08-23 17:50:30 -07001182 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001183 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, obj);
1184 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001185 ScopedObjectAccess soa(env);
1186 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 }
1188
Elliott Hughes72025e52011-08-23 17:50:30 -07001189 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001190 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, obj);
1191 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001192 ScopedObjectAccess soa(env);
1193 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 }
1195
Elliott Hughes72025e52011-08-23 17:50:30 -07001196 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001197 va_list ap;
1198 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001199 CHECK_NON_NULL_ARGUMENT(CallShortMethod, obj);
1200 CHECK_NON_NULL_ARGUMENT(CallShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001201 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001202 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001203 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001204 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 }
1206
Elliott Hughes72025e52011-08-23 17:50:30 -07001207 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001208 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, obj);
1209 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001210 ScopedObjectAccess soa(env);
1211 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001212 }
1213
Elliott Hughes72025e52011-08-23 17:50:30 -07001214 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001215 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, obj);
1216 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001217 ScopedObjectAccess soa(env);
1218 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 }
1220
Elliott Hughes72025e52011-08-23 17:50:30 -07001221 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001222 va_list ap;
1223 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001224 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, obj);
1225 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001226 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001227 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001228 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 }
1230
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001232 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, obj);
1233 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001234 ScopedObjectAccess soa(env);
1235 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 }
1237
Elliott Hughes72025e52011-08-23 17:50:30 -07001238 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001239 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, obj);
1240 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001241 ScopedObjectAccess soa(env);
1242 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001243 }
1244
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001245 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001247 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001248 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, obj);
1249 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001250 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001251 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1252 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 va_end(ap);
1254 return local_result;
1255 }
1256
Ian Rogersbc939662013-08-15 10:26:54 -07001257 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1258 va_list args) {
1259 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, obj);
1260 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001261 ScopedObjectAccess soa(env);
1262 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1263 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001264 }
1265
Ian Rogersbc939662013-08-15 10:26:54 -07001266 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1267 jvalue* args) {
1268 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, obj);
1269 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001270 ScopedObjectAccess soa(env);
1271 JValue result(InvokeWithJValues(soa, obj, mid, args));
1272 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 }
1274
Ian Rogersbc939662013-08-15 10:26:54 -07001275 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1276 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001277 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001278 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001279 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, obj);
1280 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001281 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001282 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001284 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 }
1286
Ian Rogersbc939662013-08-15 10:26:54 -07001287 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1288 va_list args) {
1289 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, obj);
1290 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001291 ScopedObjectAccess soa(env);
1292 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 }
1294
Ian Rogersbc939662013-08-15 10:26:54 -07001295 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1296 jvalue* args) {
1297 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, obj);
1298 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001299 ScopedObjectAccess soa(env);
1300 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 }
1302
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001303 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001305 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001306 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, obj);
1307 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001308 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001309 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001311 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 }
1313
Ian Rogersbc939662013-08-15 10:26:54 -07001314 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1315 va_list args) {
1316 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, obj);
1317 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001318 ScopedObjectAccess soa(env);
1319 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 }
1321
Ian Rogersbc939662013-08-15 10:26:54 -07001322 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1323 jvalue* args) {
1324 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, obj);
1325 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001326 ScopedObjectAccess soa(env);
1327 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 }
1329
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001330 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001332 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001333 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, obj);
1334 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, mid);
1335 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001336 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001337 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001338 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 }
1340
Ian Rogersbc939662013-08-15 10:26:54 -07001341 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1342 va_list args) {
1343 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, obj);
1344 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001345 ScopedObjectAccess soa(env);
1346 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001347 }
1348
Ian Rogersbc939662013-08-15 10:26:54 -07001349 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1350 jvalue* args) {
1351 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, obj);
1352 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001353 ScopedObjectAccess soa(env);
1354 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 }
1356
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001357 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001359 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001360 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, obj);
1361 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001362 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001363 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001364 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001365 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001366 }
1367
Ian Rogersbc939662013-08-15 10:26:54 -07001368 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1369 va_list args) {
1370 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, obj);
1371 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001372 ScopedObjectAccess soa(env);
1373 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001374 }
1375
Ian Rogersbc939662013-08-15 10:26:54 -07001376 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1377 jvalue* args) {
1378 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, obj);
1379 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001380 ScopedObjectAccess soa(env);
1381 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001382 }
1383
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001384 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001386 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001387 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, obj);
1388 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001389 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001390 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001391 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001392 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001393 }
1394
Ian Rogersbc939662013-08-15 10:26:54 -07001395 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1396 va_list args) {
1397 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, obj);
1398 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001399 ScopedObjectAccess soa(env);
1400 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001401 }
1402
Ian Rogersbc939662013-08-15 10:26:54 -07001403 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1404 jvalue* args) {
1405 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, obj);
1406 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001407 ScopedObjectAccess soa(env);
1408 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 }
1410
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001411 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001413 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001414 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, obj);
1415 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001416 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001417 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001419 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 }
1421
Ian Rogersbc939662013-08-15 10:26:54 -07001422 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1423 va_list args) {
1424 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, obj);
1425 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001426 ScopedObjectAccess soa(env);
1427 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 }
1429
Ian Rogersbc939662013-08-15 10:26:54 -07001430 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1431 jvalue* args) {
1432 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, obj);
1433 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001434 ScopedObjectAccess soa(env);
1435 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 }
1437
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001438 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001439 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001440 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001441 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, obj);
1442 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001443 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001444 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001445 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001446 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001447 }
1448
Ian Rogersbc939662013-08-15 10:26:54 -07001449 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1450 va_list args) {
1451 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, obj);
1452 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001453 ScopedObjectAccess soa(env);
1454 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001455 }
1456
Ian Rogersbc939662013-08-15 10:26:54 -07001457 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1458 jvalue* args) {
1459 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, obj);
1460 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001461 ScopedObjectAccess soa(env);
1462 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001463 }
1464
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001465 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001467 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001468 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, obj);
1469 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001470 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001471 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001472 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001473 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001474 }
1475
Ian Rogersbc939662013-08-15 10:26:54 -07001476 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1477 va_list args) {
1478 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, obj);
1479 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001480 ScopedObjectAccess soa(env);
1481 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001482 }
1483
Ian Rogersbc939662013-08-15 10:26:54 -07001484 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1485 jvalue* args) {
1486 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, obj);
1487 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001488 ScopedObjectAccess soa(env);
1489 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001490 }
1491
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001492 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001494 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001495 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, obj);
1496 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001497 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001498 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001499 va_end(ap);
1500 }
1501
Brian Carlstromea46f952013-07-30 01:26:50 -07001502 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1503 va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001504 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, obj);
1505 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001506 ScopedObjectAccess soa(env);
1507 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001508 }
1509
Ian Rogersbc939662013-08-15 10:26:54 -07001510 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1511 jvalue* args) {
1512 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, obj);
1513 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001514 ScopedObjectAccess soa(env);
1515 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001516 }
1517
Ian Rogersbc939662013-08-15 10:26:54 -07001518 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
1519 CHECK_NON_NULL_ARGUMENT(GetFieldID, java_class);
1520 CHECK_NON_NULL_ARGUMENT(GetFieldID, name);
1521 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001522 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001523 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001524 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001525
Ian Rogersbc939662013-08-15 10:26:54 -07001526 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1527 const char* sig) {
1528 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, java_class);
1529 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, name);
1530 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001531 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001532 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001533 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001534
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001535 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001536 CHECK_NON_NULL_ARGUMENT(GetObjectField, obj);
1537 CHECK_NON_NULL_ARGUMENT(GetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001538 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001539 mirror::Object* o = soa.Decode<mirror::Object*>(obj);
1540 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001541 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001543
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001544 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001545 CHECK_NON_NULL_ARGUMENT(GetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001546 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001547 mirror::ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001548 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001549 }
1550
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001551 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001552 CHECK_NON_NULL_ARGUMENT(SetObjectField, java_object);
1553 CHECK_NON_NULL_ARGUMENT(SetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001554 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001555 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
1556 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1557 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001558 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001559 }
1560
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001561 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001562 CHECK_NON_NULL_ARGUMENT(SetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001563 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001564 mirror::Object* v = soa.Decode<mirror::Object*>(java_value);
1565 mirror::ArtField* f = soa.DecodeField(fid);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001566 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001567 }
1568
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001569#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogersbc939662013-08-15 10:26:54 -07001570 CHECK_NON_NULL_ARGUMENT(Get #fn Field, instance); \
1571 CHECK_NON_NULL_ARGUMENT(Get #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001572 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001573 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1574 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001575 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001576
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001577#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogersbc939662013-08-15 10:26:54 -07001578 CHECK_NON_NULL_ARGUMENT(GetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001579 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001580 mirror::ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001581 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001582
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001583#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001584 CHECK_NON_NULL_ARGUMENT(Set #fn Field, instance); \
1585 CHECK_NON_NULL_ARGUMENT(Set #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001586 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001587 mirror::Object* o = soa.Decode<mirror::Object*>(instance); \
1588 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001589 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001590
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001591#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001592 CHECK_NON_NULL_ARGUMENT(SetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001593 ScopedObjectAccess soa(env); \
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001594 mirror::ArtField* f = soa.DecodeField(fid); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001595 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001596
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001597 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001598 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 }
1600
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001601 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001602 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 }
1604
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001605 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001606 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 }
1608
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001609 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001610 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 }
1612
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001613 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001614 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001615 }
1616
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001617 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001618 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001619 }
1620
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001621 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001622 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001623 }
1624
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001625 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001626 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 }
1628
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001629 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001630 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001631 }
1632
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001633 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001634 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001635 }
1636
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001637 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001638 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 }
1640
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001641 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001642 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001643 }
1644
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001645 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001646 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 }
1648
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001649 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001650 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001651 }
1652
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001653 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001654 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001655 }
1656
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001657 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001658 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001659 }
1660
1661 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001662 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001663 }
1664
1665 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001666 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001667 }
1668
1669 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001670 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001671 }
1672
1673 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001674 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001675 }
1676
1677 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001678 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001679 }
1680
1681 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001682 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001683 }
1684
1685 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001686 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001687 }
1688
1689 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001690 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001691 }
1692
1693 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001694 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001695 }
1696
1697 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001698 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001699 }
1700
1701 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001702 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001703 }
1704
1705 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001706 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001707 }
1708
1709 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001710 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001711 }
1712
1713 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001714 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001715 }
1716
1717 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001718 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001719 }
1720
1721 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001722 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 }
1724
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001725 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001726 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001727 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001728 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001729 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001730 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001731 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 va_end(ap);
1733 return local_result;
1734 }
1735
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001736 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001737 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001738 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001739 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001740 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 }
1742
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001743 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001744 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001745 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001746 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001747 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 }
1749
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001750 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001751 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001752 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001753 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001754 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001755 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001756 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001757 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001758 }
1759
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001760 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001761 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001762 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001763 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001764 }
1765
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001766 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001767 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001768 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001769 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001770 }
1771
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001772 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001773 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001774 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001775 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001776 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001777 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001778 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001779 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001780 }
1781
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001782 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001783 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001784 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001785 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 }
1787
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001788 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001789 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001790 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001791 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001792 }
1793
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001794 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001795 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001796 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001797 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001798 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001799 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001800 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001801 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001802 }
1803
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001804 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001805 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001806 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001807 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001808 }
1809
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001810 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001811 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001812 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001813 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001814 }
1815
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001816 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001817 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001818 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001819 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001820 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001821 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001822 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001823 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 }
1825
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001826 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001827 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001828 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001829 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001830 }
1831
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001832 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001833 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001834 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001835 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001836 }
1837
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001838 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001839 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001840 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001841 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001842 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001843 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001844 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001845 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001846 }
1847
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001848 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001849 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001850 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001851 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001852 }
1853
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001854 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001855 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001856 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001857 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001858 }
1859
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001860 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001861 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001862 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001863 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001864 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001865 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001866 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001867 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001868 }
1869
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001870 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001871 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001872 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001873 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001874 }
1875
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001876 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001877 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001878 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001879 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001880 }
1881
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001882 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001883 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001884 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001885 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001886 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001887 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001888 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001889 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001890 }
1891
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001892 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001893 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001894 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001895 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001896 }
1897
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001898 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001899 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001900 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001901 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001902 }
1903
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001904 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001905 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001906 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001907 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001908 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001909 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001910 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001911 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001912 }
1913
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001914 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001915 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001916 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001917 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001918 }
1919
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001920 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001921 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001922 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001923 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001924 }
1925
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001926 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001927 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001928 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001929 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001930 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001931 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001932 va_end(ap);
1933 }
1934
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001935 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001936 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001937 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001938 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001939 }
1940
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001941 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001942 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001943 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001944 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001945 }
1946
Elliott Hughes814e4032011-08-23 12:07:56 -07001947 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001948 if (UNLIKELY(char_count < 0)) {
1949 JniAbortF("NewString", "char_count < 0: %d", char_count);
1950 return nullptr;
1951 }
1952 if (UNLIKELY(chars == nullptr && char_count > 0)) {
1953 JniAbortF("NewString", "chars == null && char_count > 0");
1954 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001955 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001956 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001957 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001958 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001959 }
1960
1961 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001962 if (utf == nullptr) {
1963 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001964 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001965 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001966 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001967 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001968 }
1969
Elliott Hughes814e4032011-08-23 12:07:56 -07001970 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001971 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001972 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001973 return soa.Decode<mirror::String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001974 }
1975
1976 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001977 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001978 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001979 return soa.Decode<mirror::String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001980 }
1981
Ian Rogersbc939662013-08-15 10:26:54 -07001982 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1983 jchar* buf) {
1984 CHECK_NON_NULL_ARGUMENT(GetStringRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001985 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001986 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001987 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001988 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001989 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07001990 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001991 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1992 memcpy(buf, chars + start, length * sizeof(jchar));
1993 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001994 }
1995
Ian Rogersbc939662013-08-15 10:26:54 -07001996 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1997 char* buf) {
1998 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001999 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002000 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002001 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002002 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002003 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002004 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringUTFRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002005 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
2006 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
2007 }
Elliott Hughes814e4032011-08-23 12:07:56 -07002008 }
2009
Elliott Hughes75770752011-08-24 17:52:38 -07002010 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Narayan Kamath8e611d32014-02-10 18:20:06 +00002011 CHECK_NON_NULL_ARGUMENT(GetStringChars, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002012 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002013 mirror::String* s = soa.Decode<mirror::String*>(java_string);
2014 mirror::CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002015 PinPrimitiveArray(soa, chars);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002016 if (is_copy != nullptr) {
2017 *is_copy = JNI_TRUE;
Elliott Hughes75770752011-08-24 17:52:38 -07002018 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002019 int32_t char_count = s->GetLength();
2020 int32_t offset = s->GetOffset();
2021 jchar* bytes = new jchar[char_count + 1];
2022 for (int32_t i = 0; i < char_count; i++) {
2023 bytes[i] = chars->Get(i + offset);
2024 }
2025 bytes[char_count] = '\0';
2026 return bytes;
Elliott Hughes814e4032011-08-23 12:07:56 -07002027 }
2028
Mathieu Chartier590fee92013-09-13 13:46:47 -07002029 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Narayan Kamath8e611d32014-02-10 18:20:06 +00002030 CHECK_NON_NULL_ARGUMENT(ReleaseStringChars, java_string);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002031 delete[] chars;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002032 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002033 UnpinPrimitiveArray(soa, soa.Decode<mirror::String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 }
2035
Elliott Hughes75770752011-08-24 17:52:38 -07002036 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07002037 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002038 }
2039
Elliott Hughes75770752011-08-24 17:52:38 -07002040 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002041 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 }
2043
Elliott Hughes75770752011-08-24 17:52:38 -07002044 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002045 if (java_string == nullptr) {
2046 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07002047 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002048 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07002049 *is_copy = JNI_TRUE;
2050 }
Ian Rogersef28b142012-11-30 14:22:18 -08002051 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002052 mirror::String* s = soa.Decode<mirror::String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002053 size_t byte_count = s->GetUtfLength();
2054 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002055 CHECK(bytes != nullptr); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07002056 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
2057 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
2058 bytes[byte_count] = '\0';
2059 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002060 }
2061
Elliott Hughes75770752011-08-24 17:52:38 -07002062 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002063 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002064 }
2065
Elliott Hughesbd935992011-08-22 11:59:34 -07002066 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogersbc939662013-08-15 10:26:54 -07002067 CHECK_NON_NULL_ARGUMENT(GetArrayLength, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002068 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002069 mirror::Object* obj = soa.Decode<mirror::Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07002070 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002071 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
2072 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002073 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07002074 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 }
2076
Elliott Hughes814e4032011-08-23 12:07:56 -07002077 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogersbc939662013-08-15 10:26:54 -07002078 CHECK_NON_NULL_ARGUMENT(GetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002079 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002080 mirror::ObjectArray<mirror::Object>* array =
2081 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002082 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07002083 }
2084
Ian Rogersbc939662013-08-15 10:26:54 -07002085 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2086 jobject java_value) {
2087 CHECK_NON_NULL_ARGUMENT(SetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002088 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002089 mirror::ObjectArray<mirror::Object>* array =
2090 soa.Decode<mirror::ObjectArray<mirror::Object>*>(java_array);
2091 mirror::Object* value = soa.Decode<mirror::Object*>(java_value);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002092 array->Set<false>(index, value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002093 }
2094
2095 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002096 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002097 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 }
2099
2100 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002101 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002102 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 }
2104
2105 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002106 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002107 return NewPrimitiveArray<jcharArray, mirror::CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002108 }
2109
2110 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002111 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002112 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002113 }
2114
2115 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002116 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002117 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002118 }
2119
2120 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002121 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002122 return NewPrimitiveArray<jintArray, mirror::IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 }
2124
2125 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002126 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002127 return NewPrimitiveArray<jlongArray, mirror::LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002128 }
2129
Ian Rogers1d99e452014-01-02 17:36:41 -08002130 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2131 jobject initial_element) {
2132 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002133 JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002134 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002135 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002136
2137 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002138 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002139 mirror::Class* array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002140 {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002141 mirror::Class* element_class = soa.Decode<mirror::Class*>(element_jclass);
Ian Rogers1d99e452014-01-02 17:36:41 -08002142 if (UNLIKELY(element_class->IsPrimitive())) {
2143 JniAbortF("NewObjectArray", "not an object type: %s",
2144 PrettyDescriptor(element_class).c_str());
2145 return nullptr;
2146 }
Ian Rogers1d99e452014-01-02 17:36:41 -08002147 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers98379392014-02-24 16:53:16 -08002148 array_class = class_linker->FindArrayClass(soa.Self(), element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08002149 if (UNLIKELY(array_class == nullptr)) {
2150 return nullptr;
2151 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002152 }
2153
Elliott Hughes75770752011-08-24 17:52:38 -07002154 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002155 mirror::ObjectArray<mirror::Object>* result =
2156 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002157 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002158 mirror::Object* initial_object = soa.Decode<mirror::Object*>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002159 if (initial_object != nullptr) {
2160 mirror::Class* element_class = result->GetClass()->GetComponentType();
2161 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2162 JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with element "
2163 "type of '%s'", PrettyDescriptor(initial_object->GetClass()).c_str(),
2164 PrettyDescriptor(element_class).c_str());
2165
2166 } else {
2167 for (jsize i = 0; i < length; ++i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002168 result->SetWithoutChecks<false>(i, initial_object);
Ian Rogers1d99e452014-01-02 17:36:41 -08002169 }
2170 }
Elliott Hughes75770752011-08-24 17:52:38 -07002171 }
2172 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002173 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002174 }
2175
2176 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002177 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002178 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 }
2180
Ian Rogersa15e67d2012-02-28 13:51:55 -08002181 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002182 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayCritical, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002183 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002184 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002185 gc::Heap* heap = Runtime::Current()->GetHeap();
2186 if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002187 heap->IncrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002188 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002189 array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002190 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002191 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002192 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002193 *is_copy = JNI_FALSE;
2194 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002195 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002196 }
2197
Mathieu Chartier590fee92013-09-13 13:46:47 -07002198 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* elements, jint mode) {
Ian Rogersbc939662013-08-15 10:26:54 -07002199 CHECK_NON_NULL_ARGUMENT(ReleasePrimitiveArrayCritical, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002200 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002201 }
2202
Elliott Hughes75770752011-08-24 17:52:38 -07002203 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002204 CHECK_NON_NULL_ARGUMENT(GetBooleanArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002205 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002206 return GetPrimitiveArray<jbooleanArray, jboolean*, mirror::BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002207 }
2208
Elliott Hughes75770752011-08-24 17:52:38 -07002209 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002210 CHECK_NON_NULL_ARGUMENT(GetByteArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002211 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002212 return GetPrimitiveArray<jbyteArray, jbyte*, mirror::ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002213 }
2214
Elliott Hughes75770752011-08-24 17:52:38 -07002215 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002216 CHECK_NON_NULL_ARGUMENT(GetCharArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002217 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002218 return GetPrimitiveArray<jcharArray, jchar*, mirror::CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002219 }
2220
Elliott Hughes75770752011-08-24 17:52:38 -07002221 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002222 CHECK_NON_NULL_ARGUMENT(GetDoubleArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002223 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002224 return GetPrimitiveArray<jdoubleArray, jdouble*, mirror::DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002225 }
2226
Elliott Hughes75770752011-08-24 17:52:38 -07002227 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002228 CHECK_NON_NULL_ARGUMENT(GetFloatArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002229 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002230 return GetPrimitiveArray<jfloatArray, jfloat*, mirror::FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002231 }
2232
Elliott Hughes75770752011-08-24 17:52:38 -07002233 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002234 CHECK_NON_NULL_ARGUMENT(GetIntArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002235 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002236 return GetPrimitiveArray<jintArray, jint*, mirror::IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002237 }
2238
Elliott Hughes75770752011-08-24 17:52:38 -07002239 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002240 CHECK_NON_NULL_ARGUMENT(GetLongArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002241 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002242 return GetPrimitiveArray<jlongArray, jlong*, mirror::LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002243 }
2244
Elliott Hughes75770752011-08-24 17:52:38 -07002245 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002246 CHECK_NON_NULL_ARGUMENT(GetShortArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002247 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002248 return GetPrimitiveArray<jshortArray, jshort*, mirror::ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002249 }
2250
Mathieu Chartier590fee92013-09-13 13:46:47 -07002251 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2252 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 ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* 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 ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
2261 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002262 }
2263
Mathieu Chartier590fee92013-09-13 13:46:47 -07002264 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2265 jint mode) {
2266 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002267 }
2268
Mathieu Chartier590fee92013-09-13 13:46:47 -07002269 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2270 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 ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* 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 ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
2279 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002280 }
2281
Mathieu Chartier590fee92013-09-13 13:46:47 -07002282 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2283 jint mode) {
2284 ReleasePrimitiveArray(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002285 }
2286
Ian Rogersbc939662013-08-15 10:26:54 -07002287 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2288 jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002289 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002290 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(soa, array, start,
2291 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002292 }
2293
Ian Rogersbc939662013-08-15 10:26:54 -07002294 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2295 jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002296 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002297 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002298 }
2299
Ian Rogersbc939662013-08-15 10:26:54 -07002300 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2301 jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002302 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002303 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002304 }
2305
Ian Rogersbc939662013-08-15 10:26:54 -07002306 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2307 jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002308 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002309 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(soa, array, start, length,
2310 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002311 }
2312
Ian Rogersbc939662013-08-15 10:26:54 -07002313 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2314 jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002315 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002316 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(soa, array, start, length,
2317 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002318 }
2319
Ian Rogersbc939662013-08-15 10:26:54 -07002320 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2321 jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002322 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002323 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002324 }
2325
Ian Rogersbc939662013-08-15 10:26:54 -07002326 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2327 jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002328 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002329 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002330 }
2331
Ian Rogersbc939662013-08-15 10:26:54 -07002332 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2333 jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002334 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002335 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(soa, array, start, length,
2336 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002337 }
2338
Ian Rogersbc939662013-08-15 10:26:54 -07002339 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2340 const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002341 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002342 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(soa, array, start,
2343 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002344 }
2345
Ian Rogersbc939662013-08-15 10:26:54 -07002346 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2347 const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002348 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002349 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002350 }
2351
Ian Rogersbc939662013-08-15 10:26:54 -07002352 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2353 const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002354 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002355 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002356 }
2357
Ian Rogersbc939662013-08-15 10:26:54 -07002358 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2359 const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002360 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002361 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(soa, array, start, length,
2362 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002363 }
2364
Ian Rogersbc939662013-08-15 10:26:54 -07002365 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2366 const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002367 ScopedObjectAccess soa(env);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002368 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(soa, array, start, length,
2369 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002370 }
2371
Ian Rogersbc939662013-08-15 10:26:54 -07002372 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2373 const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002374 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002375 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002376 }
2377
Ian Rogersbc939662013-08-15 10:26:54 -07002378 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2379 const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002380 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002381 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002382 }
2383
Ian Rogersbc939662013-08-15 10:26:54 -07002384 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2385 const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002386 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002387 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(soa, array, start, length,
2388 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002389 }
2390
Ian Rogersbc939662013-08-15 10:26:54 -07002391 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2392 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002393 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2394 }
2395
Ian Rogersbc939662013-08-15 10:26:54 -07002396 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2397 jint method_count, bool return_errors) {
2398 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002399 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002400 return JNI_ERR; // Not reached.
2401 }
2402 CHECK_NON_NULL_ARGUMENT(RegisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002403 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002404 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002405 if (UNLIKELY(method_count == 0)) {
2406 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2407 << PrettyDescriptor(c);
2408 return JNI_OK;
2409 }
2410 CHECK_NON_NULL_ARGUMENT(RegisterNatives, methods);
2411 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002412 const char* name = methods[i].name;
2413 const char* sig = methods[i].signature;
Ian Rogers1eb512d2013-10-18 15:42:20 -07002414 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002415 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002416 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002417 ++sig;
2418 }
2419
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002420 mirror::ArtMethod* m = c->FindDirectMethod(name, sig);
2421 if (m == nullptr) {
Elliott Hughes5174fe62011-08-23 15:12:35 -07002422 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002423 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002424 if (m == nullptr) {
Ian Rogers0177e532014-02-11 16:30:46 -08002425 c->DumpClass(LOG(ERROR), mirror::Class::kDumpClassFullDetail);
Elliott Hughesc8fece32013-01-02 11:27:23 -08002426 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogers0177e532014-02-11 16:30:46 -08002427 << PrettyDescriptor(c) << "." << name << sig << " in "
2428 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002429 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002430 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002431 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002432 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002433 << PrettyDescriptor(c) << "." << name << sig
2434 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002435 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002436 return JNI_ERR;
2437 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002438
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002439 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002440
Ian Rogers1eb512d2013-10-18 15:42:20 -07002441 m->RegisterNative(soa.Self(), methods[i].fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002442 }
2443 return JNI_OK;
2444 }
2445
Elliott Hughes5174fe62011-08-23 15:12:35 -07002446 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -07002447 CHECK_NON_NULL_ARGUMENT(UnregisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002448 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002449 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002450
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002451 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002452
2453 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002454 mirror::ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002455 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002456 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002457 }
2458 }
2459 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002460 mirror::ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002461 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002462 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002463 }
2464 }
2465
2466 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002467 }
2468
Ian Rogers719d1a32014-03-06 12:13:39 -08002469 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogersbc939662013-08-15 10:26:54 -07002470 CHECK_NON_NULL_ARGUMENT(MonitorEnter, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002471 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002472 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
2473 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002474 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002475 return JNI_ERR;
2476 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002477 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002478 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002479 }
2480
Ian Rogers719d1a32014-03-06 12:13:39 -08002481 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogersbc939662013-08-15 10:26:54 -07002482 CHECK_NON_NULL_ARGUMENT(MonitorExit, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002483 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002484 mirror::Object* o = soa.Decode<mirror::Object*>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002485 o->MonitorExit(soa.Self());
2486 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002487 return JNI_ERR;
2488 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002489 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002490 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002491 }
2492
2493 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogersbc939662013-08-15 10:26:54 -07002494 CHECK_NON_NULL_ARGUMENT(GetJavaVM, vm);
Elliott Hughescdf53122011-08-19 15:46:09 -07002495 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002496 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002497 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002498 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002499 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002500 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002501 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002502 }
2503
Elliott Hughescdf53122011-08-19 15:46:09 -07002504 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002505 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002506 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002507 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002508 if (address == nullptr && capacity != 0) {
2509 JniAbortF("NewDirectByteBuffer", "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002510 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002511
Ian Rogers936b37f2014-02-14 00:52:24 -08002512 // At the moment, the capacity is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002513 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002514 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002515 jint capacity_arg = static_cast<jint>(capacity);
2516
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002517 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2518 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002519 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002520 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002521 }
2522
Elliott Hughesb465ab02011-08-24 11:21:21 -07002523 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002524 return reinterpret_cast<void*>(env->GetLongField(
2525 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002526 }
2527
Elliott Hughesb465ab02011-08-24 11:21:21 -07002528 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002529 return static_cast<jlong>(env->GetIntField(
2530 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002531 }
2532
Elliott Hughesb465ab02011-08-24 11:21:21 -07002533 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -07002534 CHECK_NON_NULL_ARGUMENT(GetObjectRefType, java_object);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002535
2536 // Do we definitely know what kind of reference this is?
2537 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2538 IndirectRefKind kind = GetIndirectRefKind(ref);
2539 switch (kind) {
2540 case kLocal:
Ian Rogersef28b142012-11-30 14:22:18 -08002541 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002542 return JNILocalRefType;
2543 }
2544 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002545 case kGlobal:
2546 return JNIGlobalRefType;
2547 case kWeakGlobal:
2548 return JNIWeakGlobalRefType;
2549 case kSirtOrInvalid:
2550 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002551 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002552 return JNILocalRefType;
2553 }
2554
Ian Rogersef28b142012-11-30 14:22:18 -08002555 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002556 return JNIInvalidRefType;
2557 }
2558
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002559 // If we're handing out direct pointers, check whether it's a direct pointer to a local
2560 // reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002561 {
2562 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002563 if (soa.Decode<mirror::Object*>(java_object) ==
2564 reinterpret_cast<mirror::Object*>(java_object)) {
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002565 mirror::Object* object = reinterpret_cast<mirror::Object*>(java_object);
2566 if (soa.Env()->locals.ContainsDirectPointer(object)) {
Ian Rogersef28b142012-11-30 14:22:18 -08002567 return JNILocalRefType;
2568 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002569 }
2570 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002571 return JNIInvalidRefType;
2572 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002573 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2574 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002575 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002576
2577 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002578 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2579 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002580 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002581 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002582 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2583 return JNI_ERR;
2584 }
2585 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002586 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002587 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2588 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002589 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002590 soa.Self()->ThrowOutOfMemoryError(caller);
2591 }
2592 return okay ? JNI_OK : JNI_ERR;
2593 }
2594
2595 template<typename JniT, typename ArtT>
2596 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002597 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d99e452014-01-02 17:36:41 -08002598 if (UNLIKELY(length < 0)) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002599 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002600 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002601 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002602 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002603 return soa.AddLocalReference<JniT>(result);
2604 }
2605
2606 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2607 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2608 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002609 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002610 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2611 PinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002612 // Only make a copy if necessary.
2613 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2614 if (is_copy != nullptr) {
2615 *is_copy = JNI_TRUE;
2616 }
2617 static const size_t component_size = array->GetClass()->GetComponentSize();
2618 size_t size = array->GetLength() * component_size;
2619 void* data = new uint64_t[RoundUp(size, 8) / 8];
2620 memcpy(data, array->GetData(), size);
2621 return reinterpret_cast<CArrayT>(data);
2622 } else {
2623 if (is_copy != nullptr) {
2624 *is_copy = JNI_FALSE;
2625 }
2626 return reinterpret_cast<CArrayT>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002627 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002628 }
2629
Mathieu Chartier590fee92013-09-13 13:46:47 -07002630 template <typename ArrayT, typename ElementT>
2631 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
2632 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002633 mirror::Array* array = soa.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002634 size_t component_size = array->GetClass()->GetComponentSize();
Ian Rogersef7d42f2014-01-06 12:55:46 -08002635 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002636 gc::Heap* heap = Runtime::Current()->GetHeap();
2637 bool is_copy = array_data != reinterpret_cast<void*>(elements);
2638 size_t bytes = array->GetLength() * component_size;
2639 VLOG(heap) << "Release primitive array " << env << " array_data " << array_data
2640 << " elements " << reinterpret_cast<void*>(elements);
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002641 if (is_copy) {
2642 // Sanity check: If elements is not the same as the java array's data, it better not be a
2643 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2644 // copies we make?
2645 if (heap->IsNonDiscontinuousSpaceHeapAddress(reinterpret_cast<mirror::Object*>(elements))) {
2646 JniAbortF("ReleaseArrayElements", "invalid element pointer %p, array elements are %p",
2647 reinterpret_cast<void*>(elements), array_data);
2648 return;
2649 }
2650 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002651 // Don't need to copy if we had a direct pointer.
2652 if (mode != JNI_ABORT && is_copy) {
2653 memcpy(array_data, elements, bytes);
2654 }
2655 if (mode != JNI_COMMIT) {
2656 if (is_copy) {
2657 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002658 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002659 // Non copy to a movable object must means that we had disabled the moving GC.
2660 heap->DecrementDisableMovingGC(soa.Self());
Mathieu Chartier590fee92013-09-13 13:46:47 -07002661 }
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002662 UnpinPrimitiveArray(soa, array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002663 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002664 }
2665
2666 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2667 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2668 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002669 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002670 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002671 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2672 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2673 ThrowAIOOBE(soa, array, start, length, "src");
2674 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002675 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002676 JavaT* data = array->GetData();
2677 memcpy(buf, data + start, length * sizeof(JavaT));
2678 }
2679 }
2680
2681 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2682 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2683 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002684 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002685 CHECK_NON_NULL_ARGUMENT(SetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002686 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2687 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2688 ThrowAIOOBE(soa, array, start, length, "dst");
2689 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002690 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002691 JavaT* data = array->GetData();
2692 memcpy(data + start, buf, length * sizeof(JavaT));
2693 }
2694 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002695};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002696
Elliott Hughes88c5c352012-03-15 18:49:48 -07002697const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002698 nullptr, // reserved0.
2699 nullptr, // reserved1.
2700 nullptr, // reserved2.
2701 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002702 JNI::GetVersion,
2703 JNI::DefineClass,
2704 JNI::FindClass,
2705 JNI::FromReflectedMethod,
2706 JNI::FromReflectedField,
2707 JNI::ToReflectedMethod,
2708 JNI::GetSuperclass,
2709 JNI::IsAssignableFrom,
2710 JNI::ToReflectedField,
2711 JNI::Throw,
2712 JNI::ThrowNew,
2713 JNI::ExceptionOccurred,
2714 JNI::ExceptionDescribe,
2715 JNI::ExceptionClear,
2716 JNI::FatalError,
2717 JNI::PushLocalFrame,
2718 JNI::PopLocalFrame,
2719 JNI::NewGlobalRef,
2720 JNI::DeleteGlobalRef,
2721 JNI::DeleteLocalRef,
2722 JNI::IsSameObject,
2723 JNI::NewLocalRef,
2724 JNI::EnsureLocalCapacity,
2725 JNI::AllocObject,
2726 JNI::NewObject,
2727 JNI::NewObjectV,
2728 JNI::NewObjectA,
2729 JNI::GetObjectClass,
2730 JNI::IsInstanceOf,
2731 JNI::GetMethodID,
2732 JNI::CallObjectMethod,
2733 JNI::CallObjectMethodV,
2734 JNI::CallObjectMethodA,
2735 JNI::CallBooleanMethod,
2736 JNI::CallBooleanMethodV,
2737 JNI::CallBooleanMethodA,
2738 JNI::CallByteMethod,
2739 JNI::CallByteMethodV,
2740 JNI::CallByteMethodA,
2741 JNI::CallCharMethod,
2742 JNI::CallCharMethodV,
2743 JNI::CallCharMethodA,
2744 JNI::CallShortMethod,
2745 JNI::CallShortMethodV,
2746 JNI::CallShortMethodA,
2747 JNI::CallIntMethod,
2748 JNI::CallIntMethodV,
2749 JNI::CallIntMethodA,
2750 JNI::CallLongMethod,
2751 JNI::CallLongMethodV,
2752 JNI::CallLongMethodA,
2753 JNI::CallFloatMethod,
2754 JNI::CallFloatMethodV,
2755 JNI::CallFloatMethodA,
2756 JNI::CallDoubleMethod,
2757 JNI::CallDoubleMethodV,
2758 JNI::CallDoubleMethodA,
2759 JNI::CallVoidMethod,
2760 JNI::CallVoidMethodV,
2761 JNI::CallVoidMethodA,
2762 JNI::CallNonvirtualObjectMethod,
2763 JNI::CallNonvirtualObjectMethodV,
2764 JNI::CallNonvirtualObjectMethodA,
2765 JNI::CallNonvirtualBooleanMethod,
2766 JNI::CallNonvirtualBooleanMethodV,
2767 JNI::CallNonvirtualBooleanMethodA,
2768 JNI::CallNonvirtualByteMethod,
2769 JNI::CallNonvirtualByteMethodV,
2770 JNI::CallNonvirtualByteMethodA,
2771 JNI::CallNonvirtualCharMethod,
2772 JNI::CallNonvirtualCharMethodV,
2773 JNI::CallNonvirtualCharMethodA,
2774 JNI::CallNonvirtualShortMethod,
2775 JNI::CallNonvirtualShortMethodV,
2776 JNI::CallNonvirtualShortMethodA,
2777 JNI::CallNonvirtualIntMethod,
2778 JNI::CallNonvirtualIntMethodV,
2779 JNI::CallNonvirtualIntMethodA,
2780 JNI::CallNonvirtualLongMethod,
2781 JNI::CallNonvirtualLongMethodV,
2782 JNI::CallNonvirtualLongMethodA,
2783 JNI::CallNonvirtualFloatMethod,
2784 JNI::CallNonvirtualFloatMethodV,
2785 JNI::CallNonvirtualFloatMethodA,
2786 JNI::CallNonvirtualDoubleMethod,
2787 JNI::CallNonvirtualDoubleMethodV,
2788 JNI::CallNonvirtualDoubleMethodA,
2789 JNI::CallNonvirtualVoidMethod,
2790 JNI::CallNonvirtualVoidMethodV,
2791 JNI::CallNonvirtualVoidMethodA,
2792 JNI::GetFieldID,
2793 JNI::GetObjectField,
2794 JNI::GetBooleanField,
2795 JNI::GetByteField,
2796 JNI::GetCharField,
2797 JNI::GetShortField,
2798 JNI::GetIntField,
2799 JNI::GetLongField,
2800 JNI::GetFloatField,
2801 JNI::GetDoubleField,
2802 JNI::SetObjectField,
2803 JNI::SetBooleanField,
2804 JNI::SetByteField,
2805 JNI::SetCharField,
2806 JNI::SetShortField,
2807 JNI::SetIntField,
2808 JNI::SetLongField,
2809 JNI::SetFloatField,
2810 JNI::SetDoubleField,
2811 JNI::GetStaticMethodID,
2812 JNI::CallStaticObjectMethod,
2813 JNI::CallStaticObjectMethodV,
2814 JNI::CallStaticObjectMethodA,
2815 JNI::CallStaticBooleanMethod,
2816 JNI::CallStaticBooleanMethodV,
2817 JNI::CallStaticBooleanMethodA,
2818 JNI::CallStaticByteMethod,
2819 JNI::CallStaticByteMethodV,
2820 JNI::CallStaticByteMethodA,
2821 JNI::CallStaticCharMethod,
2822 JNI::CallStaticCharMethodV,
2823 JNI::CallStaticCharMethodA,
2824 JNI::CallStaticShortMethod,
2825 JNI::CallStaticShortMethodV,
2826 JNI::CallStaticShortMethodA,
2827 JNI::CallStaticIntMethod,
2828 JNI::CallStaticIntMethodV,
2829 JNI::CallStaticIntMethodA,
2830 JNI::CallStaticLongMethod,
2831 JNI::CallStaticLongMethodV,
2832 JNI::CallStaticLongMethodA,
2833 JNI::CallStaticFloatMethod,
2834 JNI::CallStaticFloatMethodV,
2835 JNI::CallStaticFloatMethodA,
2836 JNI::CallStaticDoubleMethod,
2837 JNI::CallStaticDoubleMethodV,
2838 JNI::CallStaticDoubleMethodA,
2839 JNI::CallStaticVoidMethod,
2840 JNI::CallStaticVoidMethodV,
2841 JNI::CallStaticVoidMethodA,
2842 JNI::GetStaticFieldID,
2843 JNI::GetStaticObjectField,
2844 JNI::GetStaticBooleanField,
2845 JNI::GetStaticByteField,
2846 JNI::GetStaticCharField,
2847 JNI::GetStaticShortField,
2848 JNI::GetStaticIntField,
2849 JNI::GetStaticLongField,
2850 JNI::GetStaticFloatField,
2851 JNI::GetStaticDoubleField,
2852 JNI::SetStaticObjectField,
2853 JNI::SetStaticBooleanField,
2854 JNI::SetStaticByteField,
2855 JNI::SetStaticCharField,
2856 JNI::SetStaticShortField,
2857 JNI::SetStaticIntField,
2858 JNI::SetStaticLongField,
2859 JNI::SetStaticFloatField,
2860 JNI::SetStaticDoubleField,
2861 JNI::NewString,
2862 JNI::GetStringLength,
2863 JNI::GetStringChars,
2864 JNI::ReleaseStringChars,
2865 JNI::NewStringUTF,
2866 JNI::GetStringUTFLength,
2867 JNI::GetStringUTFChars,
2868 JNI::ReleaseStringUTFChars,
2869 JNI::GetArrayLength,
2870 JNI::NewObjectArray,
2871 JNI::GetObjectArrayElement,
2872 JNI::SetObjectArrayElement,
2873 JNI::NewBooleanArray,
2874 JNI::NewByteArray,
2875 JNI::NewCharArray,
2876 JNI::NewShortArray,
2877 JNI::NewIntArray,
2878 JNI::NewLongArray,
2879 JNI::NewFloatArray,
2880 JNI::NewDoubleArray,
2881 JNI::GetBooleanArrayElements,
2882 JNI::GetByteArrayElements,
2883 JNI::GetCharArrayElements,
2884 JNI::GetShortArrayElements,
2885 JNI::GetIntArrayElements,
2886 JNI::GetLongArrayElements,
2887 JNI::GetFloatArrayElements,
2888 JNI::GetDoubleArrayElements,
2889 JNI::ReleaseBooleanArrayElements,
2890 JNI::ReleaseByteArrayElements,
2891 JNI::ReleaseCharArrayElements,
2892 JNI::ReleaseShortArrayElements,
2893 JNI::ReleaseIntArrayElements,
2894 JNI::ReleaseLongArrayElements,
2895 JNI::ReleaseFloatArrayElements,
2896 JNI::ReleaseDoubleArrayElements,
2897 JNI::GetBooleanArrayRegion,
2898 JNI::GetByteArrayRegion,
2899 JNI::GetCharArrayRegion,
2900 JNI::GetShortArrayRegion,
2901 JNI::GetIntArrayRegion,
2902 JNI::GetLongArrayRegion,
2903 JNI::GetFloatArrayRegion,
2904 JNI::GetDoubleArrayRegion,
2905 JNI::SetBooleanArrayRegion,
2906 JNI::SetByteArrayRegion,
2907 JNI::SetCharArrayRegion,
2908 JNI::SetShortArrayRegion,
2909 JNI::SetIntArrayRegion,
2910 JNI::SetLongArrayRegion,
2911 JNI::SetFloatArrayRegion,
2912 JNI::SetDoubleArrayRegion,
2913 JNI::RegisterNatives,
2914 JNI::UnregisterNatives,
2915 JNI::MonitorEnter,
2916 JNI::MonitorExit,
2917 JNI::GetJavaVM,
2918 JNI::GetStringRegion,
2919 JNI::GetStringUTFRegion,
2920 JNI::GetPrimitiveArrayCritical,
2921 JNI::ReleasePrimitiveArrayCritical,
2922 JNI::GetStringCritical,
2923 JNI::ReleaseStringCritical,
2924 JNI::NewWeakGlobalRef,
2925 JNI::DeleteWeakGlobalRef,
2926 JNI::ExceptionCheck,
2927 JNI::NewDirectByteBuffer,
2928 JNI::GetDirectBufferAddress,
2929 JNI::GetDirectBufferCapacity,
2930 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002931};
2932
Elliott Hughes75770752011-08-24 17:52:38 -07002933JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002934 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002935 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002936 local_ref_cookie(IRT_FIRST_SEGMENT),
2937 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002938 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002939 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002940 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002941 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002942 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002943 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002944 }
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002945}
2946
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002947JNIEnvExt::~JNIEnvExt() {
2948}
2949
Mathieu Chartier590fee92013-09-13 13:46:47 -07002950jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2951 if (obj == nullptr) {
2952 return nullptr;
2953 }
2954 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
2955}
2956
2957void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2958 if (obj != nullptr) {
2959 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
2960 }
2961}
Elliott Hughes88c5c352012-03-15 18:49:48 -07002962void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2963 check_jni = enabled;
2964 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002965}
2966
Elliott Hughes73e66f72012-05-09 09:34:45 -07002967void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2968 locals.Dump(os);
2969 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002970}
2971
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002972void JNIEnvExt::PushFrame(int /*capacity*/) {
2973 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002974 stacked_local_ref_cookies.push_back(local_ref_cookie);
2975 local_ref_cookie = locals.GetSegmentState();
2976}
2977
2978void JNIEnvExt::PopFrame() {
2979 locals.SetSegmentState(local_ref_cookie);
2980 local_ref_cookie = stacked_local_ref_cookies.back();
2981 stacked_local_ref_cookies.pop_back();
2982}
2983
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002984Offset JNIEnvExt::SegmentStateOffset() {
2985 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2986 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2987}
2988
Carl Shapiroea4dca82011-08-01 13:45:38 -07002989// JNI Invocation interface.
2990
Brian Carlstrombddf9762013-05-14 11:35:37 -07002991extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002992 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002993 if (IsBadJniVersion(args->version)) {
2994 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002995 return JNI_EVERSION;
2996 }
2997 Runtime::Options options;
2998 for (int i = 0; i < args->nOptions; ++i) {
2999 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08003000 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003001 }
3002 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003003 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003004 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003005 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003006 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07003007 bool started = runtime->Start();
3008 if (!started) {
3009 delete Thread::Current()->GetJniEnv();
3010 delete runtime->GetJavaVM();
3011 LOG(WARNING) << "CreateJavaVM failed";
3012 return JNI_ERR;
3013 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07003014 *p_env = Thread::Current()->GetJniEnv();
3015 *p_vm = runtime->GetJavaVM();
3016 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003017}
3018
Elliott Hughesf2682d52011-08-15 16:37:04 -07003019extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003020 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003021 if (runtime == nullptr) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003022 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003023 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07003024 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003025 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003026 }
3027 return JNI_OK;
3028}
3029
3030// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003031extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003032 return JNI_ERR;
3033}
3034
Elliott Hughescdf53122011-08-19 15:46:09 -07003035class JII {
3036 public:
3037 static jint DestroyJavaVM(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003038 if (vm == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003039 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003040 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003041 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3042 delete raw_vm->runtime;
3043 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003044 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003045
Elliott Hughescdf53122011-08-19 15:46:09 -07003046 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003047 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07003048 }
3049
3050 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07003051 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07003052 }
3053
3054 static jint DetachCurrentThread(JavaVM* vm) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003055 if (vm == nullptr || Thread::Current() == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003056 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07003057 }
Elliott Hughes6a144332012-04-03 13:07:11 -07003058 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
3059 Runtime* runtime = raw_vm->runtime;
3060 runtime->DetachCurrentThread();
3061 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07003062 }
3063
3064 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07003065 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
3066 // and unlike other calls that take a JNI version doesn't care if you supply
3067 // JNI_VERSION_1_1, which we don't otherwise support.
3068 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07003069 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07003070 return JNI_EVERSION;
3071 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003072 if (vm == nullptr || env == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -07003073 return JNI_ERR;
3074 }
3075 Thread* thread = Thread::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003076 if (thread == nullptr) {
3077 *env = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07003078 return JNI_EDETACHED;
3079 }
3080 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003081 return JNI_OK;
3082 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003083};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003084
Elliott Hughes88c5c352012-03-15 18:49:48 -07003085const JNIInvokeInterface gJniInvokeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003086 nullptr, // reserved0
3087 nullptr, // reserved1
3088 nullptr, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07003089 JII::DestroyJavaVM,
3090 JII::AttachCurrentThread,
3091 JII::DetachCurrentThread,
3092 JII::GetEnv,
3093 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07003094};
3095
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08003096JavaVMExt::JavaVMExt(Runtime* runtime, ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07003097 : runtime(runtime),
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003098 check_jni_abort_hook(nullptr),
3099 check_jni_abort_hook_data(nullptr),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003100 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003101 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07003102 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08003103 work_around_app_jni_bugs(false),
Ian Rogers62d6c772013-02-27 08:32:07 -08003104 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003105 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07003106 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07003107 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003108 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003109 libraries(new Libraries),
3110 weak_globals_lock_("JNI weak global reference table lock"),
3111 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
3112 allow_new_weak_globals_(true),
3113 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003114 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003115 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003116 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003117 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003118}
3119
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003120JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003121 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003122}
3123
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003124jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3125 if (obj == nullptr) {
3126 return nullptr;
3127 }
3128 MutexLock mu(self, weak_globals_lock_);
3129 while (UNLIKELY(!allow_new_weak_globals_)) {
3130 weak_globals_add_condition_.WaitHoldingLocks(self);
3131 }
3132 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3133 return reinterpret_cast<jweak>(ref);
3134}
3135
3136void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3137 MutexLock mu(self, weak_globals_lock_);
3138 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3139 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3140 << "failed to find entry";
3141 }
3142}
3143
Elliott Hughes88c5c352012-03-15 18:49:48 -07003144void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3145 check_jni = enabled;
3146 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003147}
3148
Elliott Hughesae80b492012-04-24 10:43:17 -07003149void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3150 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3151 if (force_copy) {
3152 os << " (with forcecopy)";
3153 }
3154 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07003155 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003156 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003157 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003158 os << "; pins=" << pin_table.Size();
3159 }
3160 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003161 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003162 os << "; globals=" << globals.Capacity();
3163 }
3164 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003165 MutexLock mu(self, weak_globals_lock_);
3166 if (weak_globals_.Capacity() > 0) {
3167 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003168 }
3169 }
3170 os << '\n';
3171
3172 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003173 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003174 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3175 }
3176}
3177
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003178void JavaVMExt::DisallowNewWeakGlobals() {
3179 MutexLock mu(Thread::Current(), weak_globals_lock_);
3180 allow_new_weak_globals_ = false;
3181}
3182
3183void JavaVMExt::AllowNewWeakGlobals() {
3184 Thread* self = Thread::Current();
3185 MutexLock mu(self, weak_globals_lock_);
3186 allow_new_weak_globals_ = true;
3187 weak_globals_add_condition_.Broadcast(self);
3188}
3189
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003190mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3191 MutexLock mu(self, weak_globals_lock_);
3192 while (UNLIKELY(!allow_new_weak_globals_)) {
3193 weak_globals_add_condition_.WaitHoldingLocks(self);
3194 }
3195 return const_cast<mirror::Object*>(weak_globals_.Get(ref));
3196}
3197
Elliott Hughes73e66f72012-05-09 09:34:45 -07003198void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003199 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003200 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003201 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003202 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003203 }
3204 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003205 MutexLock mu(self, weak_globals_lock_);
3206 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003207 }
3208 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003209 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003210 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003211 }
3212}
3213
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003214bool JavaVMExt::LoadNativeLibrary(const std::string& path,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003215 const SirtRef<mirror::ClassLoader>& class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003216 std::string* detail) {
3217 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003218
3219 // See if we've already loaded this library. If we have, and the class loader
3220 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003221 // TODO: for better results we should canonicalize the pathname (or even compare
3222 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003223 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003224 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003225 {
3226 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003227 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003228 library = libraries->Get(path);
3229 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003230 if (library != nullptr) {
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003231 if (library->GetClassLoader() != class_loader.get()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003232 // The library will be associated with class_loader. The JNI
3233 // spec says we can't load the same library into more than one
3234 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003235 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003236 "ClassLoader %p; can't open in ClassLoader %p",
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003237 path.c_str(), library->GetClassLoader(), class_loader.get());
Elliott Hughes75770752011-08-24 17:52:38 -07003238 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003239 return false;
3240 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003241 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003242 << "ClassLoader " << class_loader.get() << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003243 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003244 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003245 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003246 return false;
3247 }
3248 return true;
3249 }
3250
3251 // Open the shared library. Because we're using a full path, the system
3252 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3253 // resolve this library's dependencies though.)
3254
3255 // Failures here are expected when java.library.path has several entries
3256 // and we have to hunt for the lib.
3257
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003258 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3259 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3260 // dlopen) becomes zero from dlclose.
3261
Elliott Hughescdf53122011-08-19 15:46:09 -07003262 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003263 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003264 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003265 void* handle = dlopen(path.empty() ? nullptr : path.c_str(), RTLD_LAZY);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003266 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003267
Elliott Hughes84b2f142012-09-27 09:16:28 -07003268 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003269
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003270 if (handle == nullptr) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003271 *detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07003272 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003273 return false;
3274 }
3275
3276 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003277 // TODO: move the locking (and more of this logic) into Libraries.
3278 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003279 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003280 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003281 library = libraries->Get(path);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003282 if (library == nullptr) { // We won race to get libraries_lock
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003283 library = new SharedLibrary(path, handle, class_loader.get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003284 libraries->Put(path, library);
3285 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003286 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003287 }
3288 if (!created_library) {
3289 LOG(INFO) << "WOW: we lost a race to add shared library: "
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003290 << "\"" << path << "\" ClassLoader=" << class_loader.get();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003291 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003292 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003293
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003294 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader.get()
3295 << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003296
Elliott Hughes79353722013-08-02 16:52:18 -07003297 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003298 void* sym = dlsym(handle, "JNI_OnLoad");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003299 if (sym == nullptr) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003300 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003301 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003302 } else {
3303 // Call JNI_OnLoad. We have to override the current class
3304 // loader, which will always be "null" since the stuff at the
3305 // top of the stack is around Runtime.loadLibrary(). (See
3306 // the comments in the JNI FindClass function.)
3307 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3308 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003309 SirtRef<mirror::ClassLoader> old_class_loader(self, self->GetClassLoaderOverride());
Mathieu Chartier055d46c2014-02-06 11:22:17 -08003310 self->SetClassLoaderOverride(class_loader.get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003311
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003312 int version = 0;
3313 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003314 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003315 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003316 version = (*jni_on_load)(this, nullptr);
Elliott Hughes79082e32011-08-25 12:07:32 -07003317 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003318
Mathieu Chartier590fee92013-09-13 13:46:47 -07003319 self->SetClassLoaderOverride(old_class_loader.get());
Elliott Hughes79082e32011-08-25 12:07:32 -07003320
Elliott Hughes79353722013-08-02 16:52:18 -07003321 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003322 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003323 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003324 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003325 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003326 // It's unwise to call dlclose() here, but we can mark it
3327 // as bad and ensure that future load attempts will fail.
3328 // We don't know how far JNI_OnLoad got, so there could
3329 // be some partially-initialized stuff accessible through
3330 // newly-registered native method calls. We could try to
3331 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003332 } else {
3333 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003334 }
Elliott Hughes79353722013-08-02 16:52:18 -07003335 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003336 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003337 }
3338
Elliott Hughes79353722013-08-02 16:52:18 -07003339 library->SetResult(was_successful);
3340 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003341}
3342
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003343void* JavaVMExt::FindCodeForNativeMethod(mirror::ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003344 CHECK(m->IsNative());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003345 mirror::Class* c = m->GetDeclaringClass();
Elliott Hughes79082e32011-08-25 12:07:32 -07003346 // If this is a static method, it could be called before the class
3347 // has been initialized.
3348 if (m->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -08003349 c = EnsureInitialized(Thread::Current(), c);
3350 if (c == nullptr) {
3351 return nullptr;
Elliott Hughes79082e32011-08-25 12:07:32 -07003352 }
3353 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003354 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003355 }
Brian Carlstrom16192862011-09-12 17:50:06 -07003356 std::string detail;
3357 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003358 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003359 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003360 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003361 native_method = libraries->FindNativeMethod(m, detail);
3362 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003363 // Throwing can cause libraries_lock to be reacquired.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003364 if (native_method == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003365 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3366 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003367 }
3368 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003369}
3370
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003371void JavaVMExt::SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003372 MutexLock mu(Thread::Current(), weak_globals_lock_);
3373 for (mirror::Object** entry : weak_globals_) {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003374 mirror::Object* obj = *entry;
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003375 mirror::Object* new_obj = callback(obj, arg);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003376 if (new_obj == nullptr) {
3377 new_obj = kClearedJniWeakGlobal;
3378 }
3379 *entry = new_obj;
3380 }
3381}
3382
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003383void JavaVMExt::VisitRoots(RootCallback* callback, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003384 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003385 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003386 ReaderMutexLock mu(self, globals_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003387 globals.VisitRoots(callback, arg, 0, kRootJNIGlobal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003388 }
3389 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003390 MutexLock mu(self, pins_lock);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003391 pin_table.VisitRoots(callback, arg, 0, kRootVMInternal);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003392 }
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003393 {
3394 MutexLock mu(self, libraries_lock);
3395 // Libraries contains shared libraries which hold a pointer to a class loader.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08003396 libraries->VisitRoots(callback, arg);
Mathieu Chartier8f4be932014-01-28 15:25:19 -08003397 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07003398 // The weak_globals table is visited by the GC itself (because it mutates the table).
3399}
3400
Elliott Hughesc8fece32013-01-02 11:27:23 -08003401void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003402 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003403 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08003404 if (c.get() == nullptr) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003405 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3406 }
3407 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3408}
3409
Ian Rogersdf20fe02011-07-20 20:34:16 -07003410} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003411
3412std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3413 switch (rhs) {
3414 case JNIInvalidRefType:
3415 os << "JNIInvalidRefType";
3416 return os;
3417 case JNILocalRefType:
3418 os << "JNILocalRefType";
3419 return os;
3420 case JNIGlobalRefType:
3421 os << "JNIGlobalRefType";
3422 return os;
3423 case JNIWeakGlobalRefType:
3424 os << "JNIWeakGlobalRefType";
3425 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003426 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003427 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003428 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003429 }
3430}