blob: ceed8662098be74b4681134870040b8405eb33ac [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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080026#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080027#include "base/stl_util.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080028#include "base/stringpiece.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070029#include "class_linker.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"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070043#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070044#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070045#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070046#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070047#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048#include "utf.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070049#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070050#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070051
Brian Carlstromea46f952013-07-30 01:26:50 -070052using ::art::mirror::ArtField;
53using ::art::mirror::ArtMethod;
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070054using ::art::mirror::Array;
55using ::art::mirror::BooleanArray;
56using ::art::mirror::ByteArray;
57using ::art::mirror::CharArray;
58using ::art::mirror::Class;
59using ::art::mirror::ClassLoader;
60using ::art::mirror::DoubleArray;
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070061using ::art::mirror::FloatArray;
62using ::art::mirror::IntArray;
63using ::art::mirror::LongArray;
64using ::art::mirror::Object;
65using ::art::mirror::ObjectArray;
66using ::art::mirror::ShortArray;
67using ::art::mirror::String;
68using ::art::mirror::Throwable;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070070namespace art {
71
Brian Carlstrom7934ac22013-07-26 10:54:15 -070072static const size_t kMonitorsInitial = 32; // Arbitrary.
73static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070074
Brian Carlstrom7934ac22013-07-26 10:54:15 -070075static const size_t kLocalsInitial = 64; // Arbitrary.
76static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070077
Brian Carlstrom7934ac22013-07-26 10:54:15 -070078static const size_t kPinTableInitial = 16; // Arbitrary.
79static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070080
Brian Carlstrom7934ac22013-07-26 10:54:15 -070081static size_t gGlobalsInitial = 512; // Arbitrary.
82static size_t gGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Elliott Hughes2ced6a52011-10-16 18:44:48 -070083
Brian Carlstrom7934ac22013-07-26 10:54:15 -070084static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
85static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Ian Rogersdf20fe02011-07-20 20:34:16 -070086
Ian Rogers00f7d0e2012-07-19 15:28:27 -070087static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070088 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescdf53122011-08-19 15:46:09 -070089 if (obj == NULL) {
90 return NULL;
91 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070092 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -070093 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -070094 WriterMutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -070095 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
96 return reinterpret_cast<jweak>(ref);
97}
98
Jeff Hao19c5d372013-03-15 14:33:43 -070099static bool IsBadJniVersion(int version) {
100 // We don't support JNI_VERSION_1_1. These are the only other valid versions.
101 return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
102}
103
Brian Carlstromea46f952013-07-30 01:26:50 -0700104static void CheckMethodArguments(ArtMethod* m, uint32_t* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700105 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700106 MethodHelper mh(m);
Ian Rogers50b35e22012-10-04 10:09:15 -0700107 const DexFile::TypeList* params = mh.GetParameterTypeList();
108 if (params == NULL) {
109 return; // No arguments so nothing to check.
110 }
Jeff Hao5d917302013-02-27 17:57:33 -0800111 uint32_t offset = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -0700112 uint32_t num_params = params->Size();
Elliott Hughesb264f082012-04-06 17:10:10 -0700113 size_t error_count = 0;
Jeff Hao5d917302013-02-27 17:57:33 -0800114 if (!m->IsStatic()) {
115 offset = 1;
116 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700117 for (uint32_t i = 0; i < num_params; i++) {
118 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
119 Class* param_type = mh.GetClassFromTypeIdx(type_idx);
120 if (param_type == NULL) {
121 Thread* self = Thread::Current();
122 CHECK(self->IsExceptionPending());
123 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
124 << mh.GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
Ian Rogers62d6c772013-02-27 08:32:07 -0800125 << self->GetException(NULL)->Dump();
Ian Rogers50b35e22012-10-04 10:09:15 -0700126 self->ClearException();
127 ++error_count;
128 } else if (!param_type->IsPrimitive()) {
129 // TODO: check primitives are in range.
Jeff Hao5d917302013-02-27 17:57:33 -0800130 Object* argument = reinterpret_cast<Object*>(args[i + offset]);
Ian Rogers50b35e22012-10-04 10:09:15 -0700131 if (argument != NULL && !argument->InstanceOf(param_type)) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700132 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
133 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
134 ++error_count;
135 }
Jeff Hao5d917302013-02-27 17:57:33 -0800136 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
137 offset++;
Elliott Hughesb264f082012-04-06 17:10:10 -0700138 }
139 }
140 if (error_count > 0) {
141 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
142 // with an argument.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700143 JniAbortF(NULL, "bad arguments passed to %s (see above for details)", PrettyMethod(m).c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -0700144 }
145}
Elliott Hughesb264f082012-04-06 17:10:10 -0700146
Brian Carlstromea46f952013-07-30 01:26:50 -0700147void InvokeWithArgArray(const ScopedObjectAccess& soa, ArtMethod* method,
Jeff Hao6474d192013-03-26 14:08:09 -0700148 ArgArray* arg_array, JValue* result, char result_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700150 uint32_t* args = arg_array->GetArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700151 if (UNLIKELY(soa.Env()->check_jni)) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700152 CheckMethodArguments(method, args);
Elliott Hughes4cacde82012-04-11 18:32:27 -0700153 }
Sebastien Hertz7d658cf2013-07-09 10:56:11 +0200154 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, result_type);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700155}
156
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700157static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
158 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700159 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700160 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700161 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700162 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800163 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700164 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800165 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700166 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
167 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700168}
169
Brian Carlstromea46f952013-07-30 01:26:50 -0700170static ArtMethod* FindVirtualMethod(Object* receiver, ArtMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700171 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700172 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700173}
174
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700175static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
176 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700177 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700178 Object* receiver = soa.Decode<Object*>(obj);
Brian Carlstromea46f952013-07-30 01:26:50 -0700179 ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700180 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800181 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700182 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800183 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700184 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
185 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700186}
187
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700188static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
189 jobject obj, jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700190 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700191 Object* receiver = soa.Decode<Object*>(obj);
Brian Carlstromea46f952013-07-30 01:26:50 -0700192 ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700193 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800194 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700195 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800196 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700197 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
198 return result;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700199}
200
Elliott Hughes6b436852011-08-12 10:16:44 -0700201// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
202// separated with slashes but aren't wrapped with "L;" like regular descriptors
203// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
204// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
205// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700206static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700207 std::string result;
208 // Add the missing "L;" if necessary.
209 if (name[0] == '[') {
210 result = name;
211 } else {
212 result += 'L';
213 result += name;
214 result += ';';
215 }
216 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700217 if (result.find('.') != std::string::npos) {
218 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
219 << "\"" << name << "\"";
220 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700221 }
222 return result;
223}
224
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700225static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, Class* c,
226 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700227 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800228 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
229 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
230 "no %s method \"%s.%s%s\"",
231 kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700232}
233
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700234static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
235 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700236 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700237 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700238 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700239 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700240 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700241
Brian Carlstromea46f952013-07-30 01:26:50 -0700242 ArtMethod* method = NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700243 if (is_static) {
244 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700245 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700246 method = c->FindVirtualMethod(name, sig);
247 if (method == NULL) {
248 // No virtual method matching the signature. Search declared
249 // private methods and constructors.
250 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700251 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700252 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700253
Elliott Hughescdf53122011-08-19 15:46:09 -0700254 if (method == NULL || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700255 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700256 return NULL;
257 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700258
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700259 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700260}
261
Ian Rogersef28b142012-11-30 14:22:18 -0800262static ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700263 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700264 ArtMethod* method = soa.Self()->GetCurrentMethod(NULL);
Ian Rogersef28b142012-11-30 14:22:18 -0800265 if (method == NULL ||
266 method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
267 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700268 }
269 return method->GetDeclaringClass()->GetClassLoader();
270}
271
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
273 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700274 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700275 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700276 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700277 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700278 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700279
Brian Carlstromea46f952013-07-30 01:26:50 -0700280 ArtField* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700281 Class* field_type;
282 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
283 if (sig[1] != '\0') {
Ian Rogersef28b142012-11-30 14:22:18 -0800284 ClassLoader* cl = GetClassLoader(soa);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700285 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700286 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700287 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700288 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700289 if (field_type == NULL) {
290 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700291 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800292 ThrowLocation throw_location;
Brian Carlstromea46f952013-07-30 01:26:50 -0700293 SirtRef<Throwable> cause(soa.Self(), soa.Self()->GetException(&throw_location));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700294 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800295 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
296 "no type \"%s\" found and so no field \"%s\" could be found in class "
297 "\"%s\" or its superclasses", sig, name,
298 ClassHelper(c).GetDescriptor());
299 soa.Self()->GetException(NULL)->SetCause(cause.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700300 return NULL;
301 }
302 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800303 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700304 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800305 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700306 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700307 if (field == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800308 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
309 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
310 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
311 sig, name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700312 return NULL;
313 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700314 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700315}
316
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700317static void PinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700318 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700319 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700320 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700321 vm->pin_table.Add(array);
322}
323
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700324static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700325 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700326 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700327 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700328 vm->pin_table.Remove(array);
329}
330
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700331static void ThrowAIOOBE(ScopedObjectAccess& soa, Array* array, jsize start,
332 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700333 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700334 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800335 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
336 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
337 "%s offset=%d length=%d %s.length=%d",
338 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700339}
Ian Rogers0571d352011-11-03 19:51:38 -0700340
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700341static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
342 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700343 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800344 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
345 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
346 "offset=%d length=%d string.length()=%d", start, length,
347 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700348}
Elliott Hughes814e4032011-08-23 12:07:56 -0700349
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700350int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700351 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700352 // Turn the const char* into a java.lang.String.
353 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
354 if (msg != NULL && s.get() == NULL) {
355 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700356 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700357
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700358 // Choose an appropriate constructor and set up the arguments.
359 jvalue args[2];
360 const char* signature;
361 if (msg == NULL && cause == NULL) {
362 signature = "()V";
363 } else if (msg != NULL && cause == NULL) {
364 signature = "(Ljava/lang/String;)V";
365 args[0].l = s.get();
366 } else if (msg == NULL && cause != NULL) {
367 signature = "(Ljava/lang/Throwable;)V";
368 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700369 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700370 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
371 args[0].l = s.get();
372 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700373 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700374 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
375 if (mid == NULL) {
Ian Rogersef28b142012-11-30 14:22:18 -0800376 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700377 LOG(ERROR) << "No <init>" << signature << " in "
378 << PrettyClass(soa.Decode<Class*>(exception_class));
379 return JNI_ERR;
380 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700381
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700382 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
383 if (exception.get() == NULL) {
384 return JNI_ERR;
385 }
Ian Rogersef28b142012-11-30 14:22:18 -0800386 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800387 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
388 soa.Self()->SetException(throw_location, soa.Decode<Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700389 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700390}
391
Elliott Hughes462c9442012-03-23 18:47:50 -0700392static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700393 if (vm == NULL || p_env == NULL) {
394 return JNI_ERR;
395 }
396
Elliott Hughes462c9442012-03-23 18:47:50 -0700397 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700398 Thread* self = Thread::Current();
399 if (self != NULL) {
400 *p_env = self->GetJniEnv();
401 return JNI_OK;
402 }
403
404 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
405
406 // No threads allowed in zygote mode.
407 if (runtime->IsZygote()) {
408 LOG(ERROR) << "Attempt to attach a thread in the zygote";
409 return JNI_ERR;
410 }
411
Elliott Hughes462c9442012-03-23 18:47:50 -0700412 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
413 const char* thread_name = NULL;
Ian Rogers365c1022012-06-22 15:05:28 -0700414 jobject thread_group = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -0700415 if (args != NULL) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700416 if (IsBadJniVersion(args->version)) {
417 LOG(ERROR) << "Bad JNI version passed to "
418 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
419 << args->version;
420 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700421 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700422 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700423 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700424 }
Elliott Hughes75770752011-08-24 17:52:38 -0700425
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800426 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700427 *p_env = NULL;
428 return JNI_ERR;
429 } else {
430 *p_env = Thread::Current()->GetJniEnv();
431 return JNI_OK;
432 }
Elliott Hughes75770752011-08-24 17:52:38 -0700433}
434
Elliott Hughes79082e32011-08-25 12:07:32 -0700435class SharedLibrary {
436 public:
437 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
438 : path_(path),
439 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700440 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700441 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700442 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700443 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700444 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700445 }
446
Elliott Hughes79082e32011-08-25 12:07:32 -0700447 Object* GetClassLoader() {
448 return class_loader_;
449 }
450
451 std::string GetPath() {
452 return path_;
453 }
454
455 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700456 * Check the result of an earlier call to JNI_OnLoad on this library.
457 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700458 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700459 bool CheckOnLoadResult()
460 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700461 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700462 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700463 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
464 bool okay;
465 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700466 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700467
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700468 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
469 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
470 // caller can continue.
471 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
472 okay = true;
473 } else {
474 while (jni_on_load_result_ == kPending) {
475 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700476 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700477 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700478
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700479 okay = (jni_on_load_result_ == kOkay);
480 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
481 << (okay ? "succeeded" : "failed") << "]";
482 }
483 }
484 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700485 return okay;
486 }
487
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700488 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700489 Thread* self = Thread::Current();
490 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700491
Elliott Hughes79082e32011-08-25 12:07:32 -0700492 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700493 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700494
495 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700496 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700497 }
498
499 void* FindSymbol(const std::string& symbol_name) {
500 return dlsym(handle_, symbol_name.c_str());
501 }
502
503 private:
504 enum JNI_OnLoadState {
505 kPending,
506 kFailed,
507 kOkay,
508 };
509
510 // Path to library "/system/lib/libjni.so".
511 std::string path_;
512
513 // The void* returned by dlopen(3).
514 void* handle_;
515
516 // The ClassLoader this library is associated with.
517 Object* class_loader_;
518
519 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700520 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700521 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700522 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700523 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700524 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700525 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700526 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700527};
528
Elliott Hughes79082e32011-08-25 12:07:32 -0700529// This exists mainly to keep implementation details out of the header file.
530class Libraries {
531 public:
532 Libraries() {
533 }
534
535 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700536 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700537 }
538
Elliott Hughesae80b492012-04-24 10:43:17 -0700539 void Dump(std::ostream& os) const {
540 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700541 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700542 if (!first) {
543 os << ' ';
544 }
545 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700546 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700547 }
548 }
549
550 size_t size() const {
551 return libraries_.size();
552 }
553
Elliott Hughes79082e32011-08-25 12:07:32 -0700554 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700555 auto it = libraries_.find(path);
Ian Rogers712462a2012-04-12 16:32:29 -0700556 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700557 }
558
559 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700560 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700561 }
562
563 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstromea46f952013-07-30 01:26:50 -0700564 void* FindNativeMethod(const ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700565 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700566 std::string jni_short_name(JniShortName(m));
567 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700568 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700569 for (const auto& lib : libraries_) {
570 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700571 if (library->GetClassLoader() != declaring_class_loader) {
572 // We only search libraries loaded by the appropriate ClassLoader.
573 continue;
574 }
575 // Try the short name then the long name...
576 void* fn = library->FindSymbol(jni_short_name);
577 if (fn == NULL) {
578 fn = library->FindSymbol(jni_long_name);
579 }
580 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800581 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
582 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700583 return fn;
584 }
585 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700586 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700587 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700588 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700589 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700590 return NULL;
591 }
592
593 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700594 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700595};
596
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700597JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
598 jvalue* args) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700599 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700600 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700601 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800602 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700603 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800604 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700605 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
606 return result;
Elliott Hughesd07986f2011-12-06 18:27:45 -0800607}
608
Ian Rogersbc939662013-08-15 10:26:54 -0700609#define CHECK_NON_NULL_ARGUMENT(fn, value) \
610 if (UNLIKELY(value == NULL)) { \
611 JniAbortF(#fn, #value " == null"); \
612 }
613
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700614#define CHECK_NON_NULL_MEMCPY_ARGUMENT(fn, length, value) \
615 if (UNLIKELY(length != 0 && value == NULL)) { \
616 JniAbortF(#fn, #value " == null"); \
617 }
618
Elliott Hughescdf53122011-08-19 15:46:09 -0700619class JNI {
620 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700621 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700622 return JNI_VERSION_1_6;
623 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700624
Ian Rogers25e8b912012-09-07 11:31:36 -0700625 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700626 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700627 return NULL;
628 }
629
Elliott Hughescdf53122011-08-19 15:46:09 -0700630 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogersbc939662013-08-15 10:26:54 -0700631 CHECK_NON_NULL_ARGUMENT(FindClass, name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700632 Runtime* runtime = Runtime::Current();
633 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700634 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700635 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700636 Class* c = NULL;
637 if (runtime->IsStarted()) {
Ian Rogersef28b142012-11-30 14:22:18 -0800638 ClassLoader* cl = GetClassLoader(soa);
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800639 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700640 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800641 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700642 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700643 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700644 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700645
Elliott Hughescdf53122011-08-19 15:46:09 -0700646 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogersbc939662013-08-15 10:26:54 -0700647 CHECK_NON_NULL_ARGUMENT(FromReflectedMethod, java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700648 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700649 jobject art_method = env->GetObjectField(java_method,
650 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod);
651 ArtMethod* method = soa.Decode<ArtMethod*>(art_method);
652 DCHECK(method != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700653 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700654 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700655
Elliott Hughescdf53122011-08-19 15:46:09 -0700656 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogersbc939662013-08-15 10:26:54 -0700657 CHECK_NON_NULL_ARGUMENT(FromReflectedField, java_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700658 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700659 jobject art_field = env->GetObjectField(java_field,
660 WellKnownClasses::java_lang_reflect_Field_artField);
661 ArtField* field = soa.Decode<ArtField*>(art_field);
662 DCHECK(field != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700663 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700664 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700665
Elliott Hughescdf53122011-08-19 15:46:09 -0700666 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700667 CHECK_NON_NULL_ARGUMENT(ToReflectedMethod, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700668 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700669 ArtMethod* m = soa.DecodeMethod(mid);
670 jobject art_method = soa.AddLocalReference<jobject>(m);
671 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
672 if (env->ExceptionCheck()) {
673 return NULL;
674 }
675 SetObjectField(env,
676 reflect_method,
677 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod,
678 art_method);
679 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700680 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700681
Elliott Hughescdf53122011-08-19 15:46:09 -0700682 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700683 CHECK_NON_NULL_ARGUMENT(ToReflectedField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700684 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700685 ArtField* f = soa.DecodeField(fid);
686 jobject art_field = soa.AddLocalReference<jobject>(f);
687 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
688 if (env->ExceptionCheck()) {
689 return NULL;
690 }
691 SetObjectField(env,
692 reflect_field,
693 WellKnownClasses::java_lang_reflect_Field_artField,
694 art_field);
695 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700696 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700697
Elliott Hughes37f7a402011-08-22 18:56:01 -0700698 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -0700699 CHECK_NON_NULL_ARGUMENT(GetObjectClass, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700700 ScopedObjectAccess soa(env);
701 Object* o = soa.Decode<Object*>(java_object);
702 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700703 }
704
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700705 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700706 CHECK_NON_NULL_ARGUMENT(GetSuperclass, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700707 ScopedObjectAccess soa(env);
708 Class* c = soa.Decode<Class*>(java_class);
709 return soa.AddLocalReference<jclass>(c->GetSuperClass());
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 jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogersbc939662013-08-15 10:26:54 -0700713 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class1);
714 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class2);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700715 ScopedObjectAccess soa(env);
716 Class* c1 = soa.Decode<Class*>(java_class1);
717 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700718 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700719 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700720
Elliott Hughese84278b2012-03-22 10:06:53 -0700721 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700722 CHECK_NON_NULL_ARGUMENT(IsInstanceOf, java_class);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700723 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700724 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700725 return JNI_TRUE;
726 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700727 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700728 Object* obj = soa.Decode<Object*>(jobj);
729 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700730 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700731 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700732 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700733
Elliott Hughes37f7a402011-08-22 18:56:01 -0700734 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700735 ScopedObjectAccess soa(env);
736 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700737 if (exception == NULL) {
738 return JNI_ERR;
739 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800740 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
741 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700742 return JNI_OK;
743 }
744
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700745 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogersbc939662013-08-15 10:26:54 -0700746 CHECK_NON_NULL_ARGUMENT(ThrowNew, c);
Elliott Hughesa4f94742012-05-29 16:28:38 -0700747 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700748 }
749
750 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700751 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700752 }
753
754 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700755 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700756 }
757
758 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700759 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700760
Brian Carlstromea46f952013-07-30 01:26:50 -0700761 SirtRef<Object> old_throw_this_object(soa.Self(), NULL);
762 SirtRef<ArtMethod> old_throw_method(soa.Self(), NULL);
763 SirtRef<Throwable> old_exception(soa.Self(), NULL);
Ian Rogers62d6c772013-02-27 08:32:07 -0800764 uint32_t old_throw_dex_pc;
765 {
766 ThrowLocation old_throw_location;
Brian Carlstromea46f952013-07-30 01:26:50 -0700767 Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Ian Rogers62d6c772013-02-27 08:32:07 -0800768 old_throw_this_object.reset(old_throw_location.GetThis());
769 old_throw_method.reset(old_throw_location.GetMethod());
770 old_exception.reset(old_exception_obj);
771 old_throw_dex_pc = old_throw_location.GetDexPc();
772 soa.Self()->ClearException();
773 }
774 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(old_exception.get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700775 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
776 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
777 if (mid == NULL) {
778 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Ian Rogers62d6c772013-02-27 08:32:07 -0800779 << PrettyTypeOf(old_exception.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700780 } else {
781 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800782 if (soa.Self()->IsExceptionPending()) {
783 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(NULL))
Elliott Hughes72025e52011-08-23 17:50:30 -0700784 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800785 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700786 }
787 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800788 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
789 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700790
Ian Rogers62d6c772013-02-27 08:32:07 -0800791 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700792 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700793
Elliott Hughescdf53122011-08-19 15:46:09 -0700794 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700795 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800796 Object* exception = soa.Self()->GetException(NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700797 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700798 }
799
Ian Rogers25e8b912012-09-07 11:31:36 -0700800 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700801 LOG(FATAL) << "JNI FatalError called: " << msg;
802 }
803
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700804 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800805 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700806 return JNI_ERR;
807 }
Ian Rogersef28b142012-11-30 14:22:18 -0800808 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700809 return JNI_OK;
810 }
811
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700812 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700813 ScopedObjectAccess soa(env);
814 Object* survivor = soa.Decode<Object*>(java_survivor);
815 soa.Env()->PopFrame();
816 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700817 }
818
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700819 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800820 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700821 }
822
Elliott Hughescdf53122011-08-19 15:46:09 -0700823 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700824 if (obj == NULL) {
825 return NULL;
826 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700827 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700828 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700829 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700830 Object* decoded_obj = soa.Decode<Object*>(obj);
Ian Rogersb8a0b942013-08-20 18:09:52 -0700831 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700832 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700833 return reinterpret_cast<jobject>(ref);
834 }
835
836 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700837 if (obj == NULL) {
838 return;
839 }
Ian Rogersef28b142012-11-30 14:22:18 -0800840 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700841 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800842 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700843 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700844
845 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
846 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
847 << "failed to find entry";
848 }
849 }
850
851 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700852 ScopedObjectAccess soa(env);
853 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700854 }
855
856 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700857 if (obj == NULL) {
858 return;
859 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700860 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700861 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700862 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700863 WriterMutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700864
865 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
866 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
867 << "failed to find entry";
868 }
869 }
870
871 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700872 if (obj == NULL) {
873 return NULL;
874 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700875 ScopedObjectAccess soa(env);
Elliott Hughes9dcd45c2013-07-29 14:40:52 -0700876 return soa.AddLocalReference<jobject>(soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700877 }
878
879 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700880 if (obj == NULL) {
881 return;
882 }
Ian Rogersef28b142012-11-30 14:22:18 -0800883 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700884
Ian Rogersef28b142012-11-30 14:22:18 -0800885 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700886 if (!locals.Remove(cookie, obj)) {
887 // Attempting to delete a local reference that is not in the
888 // topmost local reference frame is a no-op. DeleteLocalRef returns
889 // void and doesn't throw any exceptions, but we should probably
890 // complain about it so the user will notice that things aren't
891 // going quite the way they expect.
892 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
893 << "failed to find entry";
894 }
895 }
896
897 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700898 if (obj1 == obj2) {
899 return JNI_TRUE;
900 } else {
901 ScopedObjectAccess soa(env);
902 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
903 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700904 }
905
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700906 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700907 CHECK_NON_NULL_ARGUMENT(AllocObject, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700908 ScopedObjectAccess soa(env);
909 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700910 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700911 return NULL;
912 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700913 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700914 }
915
Ian Rogersbc939662013-08-15 10:26:54 -0700916 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700917 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700918 va_start(args, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700919 CHECK_NON_NULL_ARGUMENT(NewObject, java_class);
920 CHECK_NON_NULL_ARGUMENT(NewObject, mid);
921 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700922 va_end(args);
923 return result;
924 }
925
Elliott Hughes72025e52011-08-23 17:50:30 -0700926 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700927 CHECK_NON_NULL_ARGUMENT(NewObjectV, java_class);
928 CHECK_NON_NULL_ARGUMENT(NewObjectV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700929 ScopedObjectAccess soa(env);
930 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700931 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700932 return NULL;
933 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700934 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700935 if (result == NULL) {
936 return NULL;
937 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700938 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700939 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700940 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700941 return local_result;
942 } else {
943 return NULL;
944 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700945 }
946
Elliott Hughes72025e52011-08-23 17:50:30 -0700947 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700948 CHECK_NON_NULL_ARGUMENT(NewObjectA, java_class);
949 CHECK_NON_NULL_ARGUMENT(NewObjectA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700950 ScopedObjectAccess soa(env);
951 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700952 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700953 return NULL;
954 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700955 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700956 if (result == NULL) {
957 return NULL;
958 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700959 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700960 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700961 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700962 return local_result;
963 } else {
964 return NULL;
965 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700966 }
967
Ian Rogersbc939662013-08-15 10:26:54 -0700968 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
969 CHECK_NON_NULL_ARGUMENT(GetMethodID, java_class);
970 CHECK_NON_NULL_ARGUMENT(GetMethodID, name);
971 CHECK_NON_NULL_ARGUMENT(GetMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700972 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700973 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700974 }
975
Ian Rogersbc939662013-08-15 10:26:54 -0700976 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
977 const char* sig) {
978 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, java_class);
979 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, name);
980 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, 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, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700983 }
984
Elliott Hughes72025e52011-08-23 17:50:30 -0700985 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700986 va_list ap;
987 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700988 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, obj);
989 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700990 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700991 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700992 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700993 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700994 }
995
Elliott Hughes72025e52011-08-23 17:50:30 -0700996 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700997 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, obj);
998 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700999 ScopedObjectAccess soa(env);
1000 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
1001 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 }
1003
Elliott Hughes72025e52011-08-23 17:50:30 -07001004 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001005 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, obj);
1006 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001007 ScopedObjectAccess soa(env);
1008 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
1009 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001010 }
1011
Elliott Hughes72025e52011-08-23 17:50:30 -07001012 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001013 va_list ap;
1014 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001015 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, obj);
1016 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001017 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001018 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001020 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 }
1022
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001024 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, obj);
1025 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001026 ScopedObjectAccess soa(env);
1027 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001028 }
1029
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001031 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, obj);
1032 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001033 ScopedObjectAccess soa(env);
1034 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001035 }
1036
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 va_list ap;
1039 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001040 CHECK_NON_NULL_ARGUMENT(CallByteMethod, obj);
1041 CHECK_NON_NULL_ARGUMENT(CallByteMethod, mid);
1042 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001043 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001044 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001045 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001049 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, obj);
1050 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001051 ScopedObjectAccess soa(env);
1052 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001053 }
1054
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001056 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, obj);
1057 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001058 ScopedObjectAccess soa(env);
1059 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001060 }
1061
Elliott Hughes72025e52011-08-23 17:50:30 -07001062 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 va_list ap;
1064 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001065 CHECK_NON_NULL_ARGUMENT(CallCharMethod, obj);
1066 CHECK_NON_NULL_ARGUMENT(CallCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001067 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001068 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001069 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001070 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 }
1072
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001074 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, obj);
1075 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001076 ScopedObjectAccess soa(env);
1077 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 }
1079
Elliott Hughes72025e52011-08-23 17:50:30 -07001080 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001081 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, obj);
1082 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001083 ScopedObjectAccess soa(env);
1084 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001085 }
1086
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001088 va_list ap;
1089 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001090 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, obj);
1091 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001092 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001093 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001095 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001096 }
1097
Elliott Hughes72025e52011-08-23 17:50:30 -07001098 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001099 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, obj);
1100 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001101 ScopedObjectAccess soa(env);
1102 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 }
1104
Elliott Hughes72025e52011-08-23 17:50:30 -07001105 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001106 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, obj);
1107 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001108 ScopedObjectAccess soa(env);
1109 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001110 }
1111
Elliott Hughes72025e52011-08-23 17:50:30 -07001112 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 va_list ap;
1114 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001115 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, obj);
1116 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, mid);
1117 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001118 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001119 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001120 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 }
1122
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001124 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, obj);
1125 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001126 ScopedObjectAccess soa(env);
1127 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 }
1129
Elliott Hughes72025e52011-08-23 17:50:30 -07001130 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001131 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, obj);
1132 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001133 ScopedObjectAccess soa(env);
1134 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 }
1136
Elliott Hughes72025e52011-08-23 17:50:30 -07001137 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001138 va_list ap;
1139 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001140 CHECK_NON_NULL_ARGUMENT(CallIntMethod, obj);
1141 CHECK_NON_NULL_ARGUMENT(CallIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001142 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001143 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001145 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 }
1147
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001149 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, obj);
1150 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001151 ScopedObjectAccess soa(env);
1152 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 }
1154
Elliott Hughes72025e52011-08-23 17:50:30 -07001155 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001156 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, obj);
1157 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001158 ScopedObjectAccess soa(env);
1159 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001160 }
1161
Elliott Hughes72025e52011-08-23 17:50:30 -07001162 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001163 va_list ap;
1164 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001165 CHECK_NON_NULL_ARGUMENT(CallLongMethod, obj);
1166 CHECK_NON_NULL_ARGUMENT(CallLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001167 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001168 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001169 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001170 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 }
1172
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001174 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, obj);
1175 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001176 ScopedObjectAccess soa(env);
1177 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 }
1179
Elliott Hughes72025e52011-08-23 17:50:30 -07001180 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001181 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, obj);
1182 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001183 ScopedObjectAccess soa(env);
1184 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 }
1186
Elliott Hughes72025e52011-08-23 17:50:30 -07001187 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001188 va_list ap;
1189 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001190 CHECK_NON_NULL_ARGUMENT(CallShortMethod, obj);
1191 CHECK_NON_NULL_ARGUMENT(CallShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001192 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001193 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001194 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001195 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 }
1197
Elliott Hughes72025e52011-08-23 17:50:30 -07001198 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001199 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, obj);
1200 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001201 ScopedObjectAccess soa(env);
1202 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 }
1204
Elliott Hughes72025e52011-08-23 17:50:30 -07001205 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001206 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, obj);
1207 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001208 ScopedObjectAccess soa(env);
1209 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 }
1211
Elliott Hughes72025e52011-08-23 17:50:30 -07001212 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001213 va_list ap;
1214 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001215 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, obj);
1216 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001217 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001218 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001219 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 }
1221
Elliott Hughes72025e52011-08-23 17:50:30 -07001222 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001223 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, obj);
1224 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001225 ScopedObjectAccess soa(env);
1226 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 }
1228
Elliott Hughes72025e52011-08-23 17:50:30 -07001229 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001230 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, obj);
1231 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001232 ScopedObjectAccess soa(env);
1233 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 }
1235
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001236 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001238 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001239 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, obj);
1240 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001241 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001242 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1243 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 va_end(ap);
1245 return local_result;
1246 }
1247
Ian Rogersbc939662013-08-15 10:26:54 -07001248 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1249 va_list args) {
1250 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, obj);
1251 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001252 ScopedObjectAccess soa(env);
1253 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1254 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 }
1256
Ian Rogersbc939662013-08-15 10:26:54 -07001257 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1258 jvalue* args) {
1259 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, obj);
1260 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001261 ScopedObjectAccess soa(env);
1262 JValue result(InvokeWithJValues(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 jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1267 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001269 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001270 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, obj);
1271 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001272 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001273 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001275 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 }
1277
Ian Rogersbc939662013-08-15 10:26:54 -07001278 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1279 va_list args) {
1280 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, obj);
1281 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001282 ScopedObjectAccess soa(env);
1283 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 }
1285
Ian Rogersbc939662013-08-15 10:26:54 -07001286 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1287 jvalue* args) {
1288 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, obj);
1289 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001290 ScopedObjectAccess soa(env);
1291 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 }
1293
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001294 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001296 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001297 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, obj);
1298 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001299 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001300 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001302 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001303 }
1304
Ian Rogersbc939662013-08-15 10:26:54 -07001305 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1306 va_list args) {
1307 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, obj);
1308 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001309 ScopedObjectAccess soa(env);
1310 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 }
1312
Ian Rogersbc939662013-08-15 10:26:54 -07001313 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1314 jvalue* args) {
1315 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, obj);
1316 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001317 ScopedObjectAccess soa(env);
1318 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 }
1320
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001321 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001322 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001323 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001324 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, obj);
1325 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, mid);
1326 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001327 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001329 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001330 }
1331
Ian Rogersbc939662013-08-15 10:26:54 -07001332 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1333 va_list args) {
1334 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, obj);
1335 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001336 ScopedObjectAccess soa(env);
1337 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001338 }
1339
Ian Rogersbc939662013-08-15 10:26:54 -07001340 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1341 jvalue* args) {
1342 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, obj);
1343 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001344 ScopedObjectAccess soa(env);
1345 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001346 }
1347
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001348 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001350 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001351 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, obj);
1352 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001353 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001354 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001356 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 }
1358
Ian Rogersbc939662013-08-15 10:26:54 -07001359 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1360 va_list args) {
1361 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, obj);
1362 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001363 ScopedObjectAccess soa(env);
1364 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 }
1366
Ian Rogersbc939662013-08-15 10:26:54 -07001367 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1368 jvalue* args) {
1369 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, obj);
1370 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001371 ScopedObjectAccess soa(env);
1372 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001373 }
1374
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001375 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001376 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001377 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001378 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, obj);
1379 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001380 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001381 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001382 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001383 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001384 }
1385
Ian Rogersbc939662013-08-15 10:26:54 -07001386 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1387 va_list args) {
1388 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, obj);
1389 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001390 ScopedObjectAccess soa(env);
1391 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 }
1393
Ian Rogersbc939662013-08-15 10:26:54 -07001394 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1395 jvalue* args) {
1396 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, obj);
1397 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001398 ScopedObjectAccess soa(env);
1399 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 }
1401
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001402 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001403 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001404 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001405 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, obj);
1406 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001407 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001408 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001409 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001410 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001411 }
1412
Ian Rogersbc939662013-08-15 10:26:54 -07001413 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1414 va_list args) {
1415 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, obj);
1416 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001417 ScopedObjectAccess soa(env);
1418 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001419 }
1420
Ian Rogersbc939662013-08-15 10:26:54 -07001421 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1422 jvalue* args) {
1423 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, obj);
1424 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001425 ScopedObjectAccess soa(env);
1426 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001427 }
1428
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001429 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001431 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001432 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, obj);
1433 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001434 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001435 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001437 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 }
1439
Ian Rogersbc939662013-08-15 10:26:54 -07001440 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1441 va_list args) {
1442 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, obj);
1443 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001444 ScopedObjectAccess soa(env);
1445 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001446 }
1447
Ian Rogersbc939662013-08-15 10:26:54 -07001448 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1449 jvalue* args) {
1450 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, obj);
1451 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001452 ScopedObjectAccess soa(env);
1453 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 }
1455
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001456 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001458 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001459 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, obj);
1460 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001461 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001462 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001463 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001464 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 }
1466
Ian Rogersbc939662013-08-15 10:26:54 -07001467 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1468 va_list args) {
1469 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, obj);
1470 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001471 ScopedObjectAccess soa(env);
1472 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001473 }
1474
Ian Rogersbc939662013-08-15 10:26:54 -07001475 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1476 jvalue* args) {
1477 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, obj);
1478 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001479 ScopedObjectAccess soa(env);
1480 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001481 }
1482
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001483 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001484 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001485 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001486 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, obj);
1487 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001488 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001489 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001490 va_end(ap);
1491 }
1492
Brian Carlstromea46f952013-07-30 01:26:50 -07001493 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1494 va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001495 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, obj);
1496 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001497 ScopedObjectAccess soa(env);
1498 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001499 }
1500
Ian Rogersbc939662013-08-15 10:26:54 -07001501 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1502 jvalue* args) {
1503 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, obj);
1504 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001505 ScopedObjectAccess soa(env);
1506 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001507 }
1508
Ian Rogersbc939662013-08-15 10:26:54 -07001509 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
1510 CHECK_NON_NULL_ARGUMENT(GetFieldID, java_class);
1511 CHECK_NON_NULL_ARGUMENT(GetFieldID, name);
1512 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001513 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001514 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001515 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001516
Ian Rogersbc939662013-08-15 10:26:54 -07001517 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1518 const char* sig) {
1519 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, java_class);
1520 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, 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, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001524 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001525
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001526 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001527 CHECK_NON_NULL_ARGUMENT(GetObjectField, obj);
1528 CHECK_NON_NULL_ARGUMENT(GetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001529 ScopedObjectAccess soa(env);
1530 Object* o = soa.Decode<Object*>(obj);
Brian Carlstromea46f952013-07-30 01:26:50 -07001531 ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001532 return soa.AddLocalReference<jobject>(f->GetObject(o));
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 GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001536 CHECK_NON_NULL_ARGUMENT(GetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001537 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -07001538 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001539 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001540 }
1541
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001542 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001543 CHECK_NON_NULL_ARGUMENT(SetObjectField, java_object);
1544 CHECK_NON_NULL_ARGUMENT(SetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001545 ScopedObjectAccess soa(env);
1546 Object* o = soa.Decode<Object*>(java_object);
1547 Object* v = soa.Decode<Object*>(java_value);
Brian Carlstromea46f952013-07-30 01:26:50 -07001548 ArtField* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001549 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001550 }
1551
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001552 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001553 CHECK_NON_NULL_ARGUMENT(SetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001554 ScopedObjectAccess soa(env);
1555 Object* v = soa.Decode<Object*>(java_value);
Brian Carlstromea46f952013-07-30 01:26:50 -07001556 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001557 f->SetObject(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001558 }
1559
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001560#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogersbc939662013-08-15 10:26:54 -07001561 CHECK_NON_NULL_ARGUMENT(Get #fn Field, instance); \
1562 CHECK_NON_NULL_ARGUMENT(Get #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001563 ScopedObjectAccess soa(env); \
1564 Object* o = soa.Decode<Object*>(instance); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001565 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001566 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001567
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001568#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogersbc939662013-08-15 10:26:54 -07001569 CHECK_NON_NULL_ARGUMENT(GetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001570 ScopedObjectAccess soa(env); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001571 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001572 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001573
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001574#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001575 CHECK_NON_NULL_ARGUMENT(Set #fn Field, instance); \
1576 CHECK_NON_NULL_ARGUMENT(Set #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001577 ScopedObjectAccess soa(env); \
1578 Object* o = soa.Decode<Object*>(instance); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001579 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001580 f->Set ##fn(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001581
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001582#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001583 CHECK_NON_NULL_ARGUMENT(SetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001584 ScopedObjectAccess soa(env); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001585 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001586 f->Set ##fn(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001587
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001588 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001589 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 }
1591
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001592 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001593 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001594 }
1595
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001596 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001597 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001598 }
1599
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001600 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001601 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001602 }
1603
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001604 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001605 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 }
1607
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001608 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001609 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 }
1611
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001612 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001613 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001614 }
1615
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001616 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001617 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 }
1619
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001620 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001621 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 }
1623
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001624 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001625 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001626 }
1627
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001628 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001629 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001630 }
1631
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001632 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001633 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001634 }
1635
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001636 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001637 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001638 }
1639
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001640 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001641 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001642 }
1643
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001644 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001645 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001646 }
1647
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001648 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001649 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001650 }
1651
1652 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001653 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001654 }
1655
1656 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001657 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001658 }
1659
1660 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001661 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001662 }
1663
1664 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001665 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001666 }
1667
1668 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001669 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001670 }
1671
1672 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001673 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001674 }
1675
1676 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001677 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001678 }
1679
1680 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001681 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001682 }
1683
1684 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001685 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001686 }
1687
1688 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001689 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001690 }
1691
1692 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001693 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001694 }
1695
1696 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001697 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001698 }
1699
1700 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001701 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001702 }
1703
1704 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001705 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001706 }
1707
1708 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001709 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001710 }
1711
1712 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001713 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001714 }
1715
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001716 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001718 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001719 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001720 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001721 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1722 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 va_end(ap);
1724 return local_result;
1725 }
1726
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001727 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001728 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001729 ScopedObjectAccess soa(env);
1730 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1731 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 }
1733
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001734 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001735 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001736 ScopedObjectAccess soa(env);
1737 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1738 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001739 }
1740
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001741 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001743 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001744 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001745 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001746 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001748 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001749 }
1750
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001751 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001752 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001753 ScopedObjectAccess soa(env);
1754 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 }
1756
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001757 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001758 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001759 ScopedObjectAccess soa(env);
1760 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001761 }
1762
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001763 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001764 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001765 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001766 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001767 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001768 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001769 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001770 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001771 }
1772
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001773 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001774 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001775 ScopedObjectAccess soa(env);
1776 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001777 }
1778
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001779 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001780 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001781 ScopedObjectAccess soa(env);
1782 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001783 }
1784
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001785 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001787 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001788 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001789 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001790 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001791 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001792 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 }
1794
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001795 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001796 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001797 ScopedObjectAccess soa(env);
1798 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001799 }
1800
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001801 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001802 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001803 ScopedObjectAccess soa(env);
1804 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001805 }
1806
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001807 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001808 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001809 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001810 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001811 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001812 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001814 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 }
1816
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001817 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001818 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001819 ScopedObjectAccess soa(env);
1820 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001821 }
1822
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001823 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001824 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001825 ScopedObjectAccess soa(env);
1826 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001827 }
1828
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001829 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001830 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001831 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001832 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001833 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001834 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001836 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001837 }
1838
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001839 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001840 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001841 ScopedObjectAccess soa(env);
1842 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001843 }
1844
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001845 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001846 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001847 ScopedObjectAccess soa(env);
1848 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001849 }
1850
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001851 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001852 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001853 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001854 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001855 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001856 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001857 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001858 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001859 }
1860
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001861 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001862 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001863 ScopedObjectAccess soa(env);
1864 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001865 }
1866
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001867 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001868 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001869 ScopedObjectAccess soa(env);
1870 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001871 }
1872
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001873 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001874 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001875 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001876 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001877 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001878 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001879 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001880 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001881 }
1882
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001883 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001884 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001885 ScopedObjectAccess soa(env);
1886 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001887 }
1888
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001889 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001890 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001891 ScopedObjectAccess soa(env);
1892 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001893 }
1894
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001895 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001896 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001897 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001898 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001899 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001900 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001901 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001902 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001903 }
1904
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001905 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001906 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001907 ScopedObjectAccess soa(env);
1908 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001909 }
1910
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001911 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001912 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001913 ScopedObjectAccess soa(env);
1914 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001915 }
1916
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001917 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001918 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001919 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001920 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001921 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001922 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001923 va_end(ap);
1924 }
1925
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001926 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001927 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001928 ScopedObjectAccess soa(env);
1929 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001930 }
1931
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001932 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001933 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001934 ScopedObjectAccess soa(env);
1935 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001936 }
1937
Elliott Hughes814e4032011-08-23 12:07:56 -07001938 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogersbc939662013-08-15 10:26:54 -07001939 if (UNLIKELY(chars == NULL && char_count > 0)) { \
1940 JniAbortF("NewString", "char == null && char_count > 0"); \
1941 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001942 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001943 String* result = String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001944 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001945 }
1946
1947 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001948 if (utf == NULL) {
1949 return NULL;
1950 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001951 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001952 String* result = String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001953 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001954 }
1955
Elliott Hughes814e4032011-08-23 12:07:56 -07001956 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001957 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001958 ScopedObjectAccess soa(env);
1959 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001960 }
1961
1962 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001963 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001964 ScopedObjectAccess soa(env);
1965 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001966 }
1967
Ian Rogersbc939662013-08-15 10:26:54 -07001968 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1969 jchar* buf) {
1970 CHECK_NON_NULL_ARGUMENT(GetStringRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001971 ScopedObjectAccess soa(env);
1972 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001973 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001974 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001975 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07001976 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001977 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1978 memcpy(buf, chars + start, length * sizeof(jchar));
1979 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001980 }
1981
Ian Rogersbc939662013-08-15 10:26:54 -07001982 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1983 char* buf) {
1984 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001985 ScopedObjectAccess soa(env);
1986 String* s = soa.Decode<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(GetStringUTFRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001991 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1992 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1993 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001994 }
1995
Elliott Hughes75770752011-08-24 17:52:38 -07001996 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07001997 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001998 ScopedObjectAccess soa(env);
1999 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002000 const CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002001 PinPrimitiveArray(soa, chars);
Elliott Hughes75770752011-08-24 17:52:38 -07002002 if (is_copy != NULL) {
2003 *is_copy = JNI_FALSE;
2004 }
2005 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07002006 }
2007
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002008 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Ian Rogersbc939662013-08-15 10:26:54 -07002009 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002010 ScopedObjectAccess soa(env);
2011 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 }
2013
Elliott Hughes75770752011-08-24 17:52:38 -07002014 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07002015 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 }
2017
Elliott Hughes75770752011-08-24 17:52:38 -07002018 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002019 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07002020 }
2021
Elliott Hughes75770752011-08-24 17:52:38 -07002022 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07002023 if (java_string == NULL) {
2024 return NULL;
2025 }
2026 if (is_copy != NULL) {
2027 *is_copy = JNI_TRUE;
2028 }
Ian Rogersef28b142012-11-30 14:22:18 -08002029 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002030 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002031 size_t byte_count = s->GetUtfLength();
2032 char* bytes = new char[byte_count + 1];
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002033 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07002034 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
2035 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
2036 bytes[byte_count] = '\0';
2037 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002038 }
2039
Elliott Hughes75770752011-08-24 17:52:38 -07002040 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002041 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002042 }
2043
Elliott Hughesbd935992011-08-22 11:59:34 -07002044 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogersbc939662013-08-15 10:26:54 -07002045 CHECK_NON_NULL_ARGUMENT(GetArrayLength, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002046 ScopedObjectAccess soa(env);
2047 Object* obj = soa.Decode<Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07002048 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002049 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
2050 }
Elliott Hughesbd935992011-08-22 11:59:34 -07002051 Array* array = obj->AsArray();
2052 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 }
2054
Elliott Hughes814e4032011-08-23 12:07:56 -07002055 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogersbc939662013-08-15 10:26:54 -07002056 CHECK_NON_NULL_ARGUMENT(GetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002057 ScopedObjectAccess soa(env);
2058 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
2059 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 }
2061
Ian Rogersbc939662013-08-15 10:26:54 -07002062 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2063 jobject java_value) {
2064 CHECK_NON_NULL_ARGUMENT(SetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002065 ScopedObjectAccess soa(env);
2066 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
2067 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 array->Set(index, value);
2069 }
2070
2071 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002072 ScopedObjectAccess soa(env);
2073 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 }
2075
2076 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002077 ScopedObjectAccess soa(env);
2078 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 }
2080
2081 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002082 ScopedObjectAccess soa(env);
2083 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002084 }
2085
2086 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002087 ScopedObjectAccess soa(env);
2088 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 }
2090
2091 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002092 ScopedObjectAccess soa(env);
2093 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 }
2095
2096 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002097 ScopedObjectAccess soa(env);
2098 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 }
2100
2101 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002102 ScopedObjectAccess soa(env);
2103 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002104 }
2105
2106 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002107 if (length < 0) {
2108 JniAbortF("NewObjectArray", "negative array length: %d", length);
2109 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002110
2111 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002112 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002113 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 std::string descriptor;
2115 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002116 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07002117
2118 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07002119 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
2120 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 return NULL;
2122 }
2123
Elliott Hughes75770752011-08-24 17:52:38 -07002124 // Allocate and initialize if necessary.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002125 Class* array_class = soa.Decode<Class*>(java_array_class.get());
Ian Rogers50b35e22012-10-04 10:09:15 -07002126 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07002127 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002128 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07002129 for (jsize i = 0; i < length; ++i) {
2130 result->Set(i, initial_object);
2131 }
2132 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002133 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 }
2135
2136 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002137 ScopedObjectAccess soa(env);
2138 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 }
2140
Ian Rogersa15e67d2012-02-28 13:51:55 -08002141 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002142 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayCritical, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002143 ScopedObjectAccess soa(env);
2144 Array* array = soa.Decode<Array*>(java_array);
2145 PinPrimitiveArray(soa, array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08002146 if (is_copy != NULL) {
2147 *is_copy = JNI_FALSE;
2148 }
2149 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002150 }
2151
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002152 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Ian Rogersbc939662013-08-15 10:26:54 -07002153 CHECK_NON_NULL_ARGUMENT(ReleasePrimitiveArrayCritical, array);
Ian Rogersef28b142012-11-30 14:22:18 -08002154 ReleasePrimitiveArray(env, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002155 }
2156
Elliott Hughes75770752011-08-24 17:52:38 -07002157 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002158 CHECK_NON_NULL_ARGUMENT(GetBooleanArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002159 ScopedObjectAccess soa(env);
2160 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 }
2162
Elliott Hughes75770752011-08-24 17:52:38 -07002163 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002164 CHECK_NON_NULL_ARGUMENT(GetByteArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002165 ScopedObjectAccess soa(env);
2166 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002167 }
2168
Elliott Hughes75770752011-08-24 17:52:38 -07002169 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002170 CHECK_NON_NULL_ARGUMENT(GetCharArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002171 ScopedObjectAccess soa(env);
2172 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002173 }
2174
Elliott Hughes75770752011-08-24 17:52:38 -07002175 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002176 CHECK_NON_NULL_ARGUMENT(GetDoubleArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002177 ScopedObjectAccess soa(env);
2178 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 }
2180
Elliott Hughes75770752011-08-24 17:52:38 -07002181 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002182 CHECK_NON_NULL_ARGUMENT(GetFloatArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002183 ScopedObjectAccess soa(env);
2184 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 }
2186
Elliott Hughes75770752011-08-24 17:52:38 -07002187 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002188 CHECK_NON_NULL_ARGUMENT(GetIntArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002189 ScopedObjectAccess soa(env);
2190 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002191 }
2192
Elliott Hughes75770752011-08-24 17:52:38 -07002193 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002194 CHECK_NON_NULL_ARGUMENT(GetLongArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002195 ScopedObjectAccess soa(env);
2196 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002197 }
2198
Elliott Hughes75770752011-08-24 17:52:38 -07002199 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002200 CHECK_NON_NULL_ARGUMENT(GetShortArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002201 ScopedObjectAccess soa(env);
2202 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002203 }
2204
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002205 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002206 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002207 }
2208
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002209 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002210 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002211 }
2212
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002213 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002214 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002215 }
2216
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002217 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002218 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002219 }
2220
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002221 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002222 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002223 }
2224
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002225 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002226 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002227 }
2228
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002229 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002230 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002231 }
2232
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002233 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002234 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002235 }
2236
Ian Rogersbc939662013-08-15 10:26:54 -07002237 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2238 jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002239 ScopedObjectAccess soa(env);
2240 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002241 }
2242
Ian Rogersbc939662013-08-15 10:26:54 -07002243 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2244 jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002245 ScopedObjectAccess soa(env);
2246 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002247 }
2248
Ian Rogersbc939662013-08-15 10:26:54 -07002249 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2250 jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002251 ScopedObjectAccess soa(env);
2252 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002253 }
2254
Ian Rogersbc939662013-08-15 10:26:54 -07002255 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2256 jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002257 ScopedObjectAccess soa(env);
2258 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002259 }
2260
Ian Rogersbc939662013-08-15 10:26:54 -07002261 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2262 jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002263 ScopedObjectAccess soa(env);
2264 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002265 }
2266
Ian Rogersbc939662013-08-15 10:26:54 -07002267 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2268 jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002269 ScopedObjectAccess soa(env);
2270 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002271 }
2272
Ian Rogersbc939662013-08-15 10:26:54 -07002273 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2274 jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002275 ScopedObjectAccess soa(env);
2276 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002277 }
2278
Ian Rogersbc939662013-08-15 10:26:54 -07002279 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2280 jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002281 ScopedObjectAccess soa(env);
2282 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002283 }
2284
Ian Rogersbc939662013-08-15 10:26:54 -07002285 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2286 const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002287 ScopedObjectAccess soa(env);
2288 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002289 }
2290
Ian Rogersbc939662013-08-15 10:26:54 -07002291 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2292 const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002293 ScopedObjectAccess soa(env);
2294 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002295 }
2296
Ian Rogersbc939662013-08-15 10:26:54 -07002297 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2298 const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002299 ScopedObjectAccess soa(env);
2300 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002301 }
2302
Ian Rogersbc939662013-08-15 10:26:54 -07002303 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2304 const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002305 ScopedObjectAccess soa(env);
2306 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002307 }
2308
Ian Rogersbc939662013-08-15 10:26:54 -07002309 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2310 const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002311 ScopedObjectAccess soa(env);
2312 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002313 }
2314
Ian Rogersbc939662013-08-15 10:26:54 -07002315 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2316 const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002317 ScopedObjectAccess soa(env);
2318 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002319 }
2320
Ian Rogersbc939662013-08-15 10:26:54 -07002321 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2322 const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002323 ScopedObjectAccess soa(env);
2324 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002325 }
2326
Ian Rogersbc939662013-08-15 10:26:54 -07002327 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2328 const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002329 ScopedObjectAccess soa(env);
2330 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002331 }
2332
Ian Rogersbc939662013-08-15 10:26:54 -07002333 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2334 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002335 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2336 }
2337
Ian Rogersbc939662013-08-15 10:26:54 -07002338 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2339 jint method_count, bool return_errors) {
2340 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002341 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002342 return JNI_ERR; // Not reached.
2343 }
2344 CHECK_NON_NULL_ARGUMENT(RegisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002345 ScopedObjectAccess soa(env);
2346 Class* c = soa.Decode<Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002347 if (UNLIKELY(method_count == 0)) {
2348 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2349 << PrettyDescriptor(c);
2350 return JNI_OK;
2351 }
2352 CHECK_NON_NULL_ARGUMENT(RegisterNatives, methods);
2353 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002354 const char* name = methods[i].name;
2355 const char* sig = methods[i].signature;
2356
2357 if (*sig == '!') {
2358 // TODO: fast jni. it's too noisy to log all these.
2359 ++sig;
2360 }
2361
Brian Carlstromea46f952013-07-30 01:26:50 -07002362 ArtMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002363 if (m == NULL) {
2364 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002365 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002366 if (m == NULL) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002367 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002368 << PrettyDescriptor(c) << "." << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002369 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002370 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002371 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002372 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002373 << PrettyDescriptor(c) << "." << name << sig
2374 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002375 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002376 return JNI_ERR;
2377 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002378
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002379 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002380
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002381 m->RegisterNative(soa.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002382 }
2383 return JNI_OK;
2384 }
2385
Elliott Hughes5174fe62011-08-23 15:12:35 -07002386 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -07002387 CHECK_NON_NULL_ARGUMENT(UnregisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002388 ScopedObjectAccess soa(env);
2389 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002390
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002391 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002392
2393 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07002394 ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002395 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002396 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002397 }
2398 }
2399 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07002400 ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002401 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002402 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002403 }
2404 }
2405
2406 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002407 }
2408
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002409 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2410 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002411 CHECK_NON_NULL_ARGUMENT(MonitorEnter, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002412 ScopedObjectAccess soa(env);
2413 Object* o = soa.Decode<Object*>(java_object);
2414 o->MonitorEnter(soa.Self());
2415 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002416 return JNI_ERR;
2417 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002418 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002419 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002420 }
2421
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002422 static jint MonitorExit(JNIEnv* env, jobject java_object)
2423 UNLOCK_FUNCTION(monitor_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002424 CHECK_NON_NULL_ARGUMENT(MonitorExit, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002425 ScopedObjectAccess soa(env);
2426 Object* o = soa.Decode<Object*>(java_object);
2427 o->MonitorExit(soa.Self());
2428 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002429 return JNI_ERR;
2430 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002431 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002432 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002433 }
2434
2435 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogersbc939662013-08-15 10:26:54 -07002436 CHECK_NON_NULL_ARGUMENT(GetJavaVM, vm);
Elliott Hughescdf53122011-08-19 15:46:09 -07002437 Runtime* runtime = Runtime::Current();
2438 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002439 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002440 } else {
2441 *vm = NULL;
2442 }
2443 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2444 }
2445
Elliott Hughescdf53122011-08-19 15:46:09 -07002446 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002447 if (capacity < 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002448 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002449 }
Elliott Hughes11a796e2012-12-19 14:42:57 -08002450 if (address == NULL && capacity != 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002451 JniAbortF("NewDirectByteBuffer", "non-zero capacity for NULL pointer: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002452 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002453
Elliott Hughes96a98872012-12-19 14:21:15 -08002454 // At the moment, the Java side is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002455 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2456 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002457 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002458 jint capacity_arg = static_cast<jint>(capacity);
2459
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002460 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2461 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002462 address_arg, capacity_arg);
Ian Rogersef28b142012-11-30 14:22:18 -08002463 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002464 }
2465
Elliott Hughesb465ab02011-08-24 11:21:21 -07002466 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Jeff Hao534f2b62013-07-10 15:29:36 -07002467 return reinterpret_cast<void*>(env->GetLongField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002468 }
2469
Elliott Hughesb465ab02011-08-24 11:21:21 -07002470 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002471 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002472 }
2473
Elliott Hughesb465ab02011-08-24 11:21:21 -07002474 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -07002475 CHECK_NON_NULL_ARGUMENT(GetObjectRefType, java_object);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002476
2477 // Do we definitely know what kind of reference this is?
2478 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2479 IndirectRefKind kind = GetIndirectRefKind(ref);
2480 switch (kind) {
2481 case kLocal:
Ian Rogersef28b142012-11-30 14:22:18 -08002482 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002483 return JNILocalRefType;
2484 }
2485 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002486 case kGlobal:
2487 return JNIGlobalRefType;
2488 case kWeakGlobal:
2489 return JNIWeakGlobalRefType;
2490 case kSirtOrInvalid:
2491 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002492 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002493 return JNILocalRefType;
2494 }
2495
Ian Rogersef28b142012-11-30 14:22:18 -08002496 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002497 return JNIInvalidRefType;
2498 }
2499
Elliott Hughesb465ab02011-08-24 11:21:21 -07002500 // If we're handing out direct pointers, check whether it's a direct pointer
2501 // to a local reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002502 {
2503 ScopedObjectAccess soa(env);
2504 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2505 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
2506 return JNILocalRefType;
2507 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002508 }
2509 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002510 return JNIInvalidRefType;
2511 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002512 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2513 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002514 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002515
2516 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002517 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2518 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002519 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002520 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002521 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2522 return JNI_ERR;
2523 }
2524 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002525 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002526 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2527 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002528 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002529 soa.Self()->ThrowOutOfMemoryError(caller);
2530 }
2531 return okay ? JNI_OK : JNI_ERR;
2532 }
2533
2534 template<typename JniT, typename ArtT>
2535 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002536 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002537 if (length < 0) {
2538 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
2539 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002540 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002541 return soa.AddLocalReference<JniT>(result);
2542 }
2543
2544 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2545 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2546 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002547 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002548 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2549 PinPrimitiveArray(soa, array);
2550 if (is_copy != NULL) {
2551 *is_copy = JNI_FALSE;
2552 }
2553 return array->GetData();
2554 }
2555
2556 template <typename ArrayT>
Ian Rogersef28b142012-11-30 14:22:18 -08002557 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002558 if (mode != JNI_COMMIT) {
Ian Rogersef28b142012-11-30 14:22:18 -08002559 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002560 Array* array = soa.Decode<Array*>(java_array);
2561 UnpinPrimitiveArray(soa, array);
2562 }
2563 }
2564
2565 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2566 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2567 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002568 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002569 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002570 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2571 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2572 ThrowAIOOBE(soa, array, start, length, "src");
2573 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002574 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002575 JavaT* data = array->GetData();
2576 memcpy(buf, data + start, length * sizeof(JavaT));
2577 }
2578 }
2579
2580 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2581 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2582 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002583 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002584 CHECK_NON_NULL_ARGUMENT(SetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002585 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2586 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2587 ThrowAIOOBE(soa, array, start, length, "dst");
2588 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002589 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002590 JavaT* data = array->GetData();
2591 memcpy(data + start, buf, length * sizeof(JavaT));
2592 }
2593 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002594};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002595
Elliott Hughes88c5c352012-03-15 18:49:48 -07002596const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002597 NULL, // reserved0.
2598 NULL, // reserved1.
2599 NULL, // reserved2.
2600 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002601 JNI::GetVersion,
2602 JNI::DefineClass,
2603 JNI::FindClass,
2604 JNI::FromReflectedMethod,
2605 JNI::FromReflectedField,
2606 JNI::ToReflectedMethod,
2607 JNI::GetSuperclass,
2608 JNI::IsAssignableFrom,
2609 JNI::ToReflectedField,
2610 JNI::Throw,
2611 JNI::ThrowNew,
2612 JNI::ExceptionOccurred,
2613 JNI::ExceptionDescribe,
2614 JNI::ExceptionClear,
2615 JNI::FatalError,
2616 JNI::PushLocalFrame,
2617 JNI::PopLocalFrame,
2618 JNI::NewGlobalRef,
2619 JNI::DeleteGlobalRef,
2620 JNI::DeleteLocalRef,
2621 JNI::IsSameObject,
2622 JNI::NewLocalRef,
2623 JNI::EnsureLocalCapacity,
2624 JNI::AllocObject,
2625 JNI::NewObject,
2626 JNI::NewObjectV,
2627 JNI::NewObjectA,
2628 JNI::GetObjectClass,
2629 JNI::IsInstanceOf,
2630 JNI::GetMethodID,
2631 JNI::CallObjectMethod,
2632 JNI::CallObjectMethodV,
2633 JNI::CallObjectMethodA,
2634 JNI::CallBooleanMethod,
2635 JNI::CallBooleanMethodV,
2636 JNI::CallBooleanMethodA,
2637 JNI::CallByteMethod,
2638 JNI::CallByteMethodV,
2639 JNI::CallByteMethodA,
2640 JNI::CallCharMethod,
2641 JNI::CallCharMethodV,
2642 JNI::CallCharMethodA,
2643 JNI::CallShortMethod,
2644 JNI::CallShortMethodV,
2645 JNI::CallShortMethodA,
2646 JNI::CallIntMethod,
2647 JNI::CallIntMethodV,
2648 JNI::CallIntMethodA,
2649 JNI::CallLongMethod,
2650 JNI::CallLongMethodV,
2651 JNI::CallLongMethodA,
2652 JNI::CallFloatMethod,
2653 JNI::CallFloatMethodV,
2654 JNI::CallFloatMethodA,
2655 JNI::CallDoubleMethod,
2656 JNI::CallDoubleMethodV,
2657 JNI::CallDoubleMethodA,
2658 JNI::CallVoidMethod,
2659 JNI::CallVoidMethodV,
2660 JNI::CallVoidMethodA,
2661 JNI::CallNonvirtualObjectMethod,
2662 JNI::CallNonvirtualObjectMethodV,
2663 JNI::CallNonvirtualObjectMethodA,
2664 JNI::CallNonvirtualBooleanMethod,
2665 JNI::CallNonvirtualBooleanMethodV,
2666 JNI::CallNonvirtualBooleanMethodA,
2667 JNI::CallNonvirtualByteMethod,
2668 JNI::CallNonvirtualByteMethodV,
2669 JNI::CallNonvirtualByteMethodA,
2670 JNI::CallNonvirtualCharMethod,
2671 JNI::CallNonvirtualCharMethodV,
2672 JNI::CallNonvirtualCharMethodA,
2673 JNI::CallNonvirtualShortMethod,
2674 JNI::CallNonvirtualShortMethodV,
2675 JNI::CallNonvirtualShortMethodA,
2676 JNI::CallNonvirtualIntMethod,
2677 JNI::CallNonvirtualIntMethodV,
2678 JNI::CallNonvirtualIntMethodA,
2679 JNI::CallNonvirtualLongMethod,
2680 JNI::CallNonvirtualLongMethodV,
2681 JNI::CallNonvirtualLongMethodA,
2682 JNI::CallNonvirtualFloatMethod,
2683 JNI::CallNonvirtualFloatMethodV,
2684 JNI::CallNonvirtualFloatMethodA,
2685 JNI::CallNonvirtualDoubleMethod,
2686 JNI::CallNonvirtualDoubleMethodV,
2687 JNI::CallNonvirtualDoubleMethodA,
2688 JNI::CallNonvirtualVoidMethod,
2689 JNI::CallNonvirtualVoidMethodV,
2690 JNI::CallNonvirtualVoidMethodA,
2691 JNI::GetFieldID,
2692 JNI::GetObjectField,
2693 JNI::GetBooleanField,
2694 JNI::GetByteField,
2695 JNI::GetCharField,
2696 JNI::GetShortField,
2697 JNI::GetIntField,
2698 JNI::GetLongField,
2699 JNI::GetFloatField,
2700 JNI::GetDoubleField,
2701 JNI::SetObjectField,
2702 JNI::SetBooleanField,
2703 JNI::SetByteField,
2704 JNI::SetCharField,
2705 JNI::SetShortField,
2706 JNI::SetIntField,
2707 JNI::SetLongField,
2708 JNI::SetFloatField,
2709 JNI::SetDoubleField,
2710 JNI::GetStaticMethodID,
2711 JNI::CallStaticObjectMethod,
2712 JNI::CallStaticObjectMethodV,
2713 JNI::CallStaticObjectMethodA,
2714 JNI::CallStaticBooleanMethod,
2715 JNI::CallStaticBooleanMethodV,
2716 JNI::CallStaticBooleanMethodA,
2717 JNI::CallStaticByteMethod,
2718 JNI::CallStaticByteMethodV,
2719 JNI::CallStaticByteMethodA,
2720 JNI::CallStaticCharMethod,
2721 JNI::CallStaticCharMethodV,
2722 JNI::CallStaticCharMethodA,
2723 JNI::CallStaticShortMethod,
2724 JNI::CallStaticShortMethodV,
2725 JNI::CallStaticShortMethodA,
2726 JNI::CallStaticIntMethod,
2727 JNI::CallStaticIntMethodV,
2728 JNI::CallStaticIntMethodA,
2729 JNI::CallStaticLongMethod,
2730 JNI::CallStaticLongMethodV,
2731 JNI::CallStaticLongMethodA,
2732 JNI::CallStaticFloatMethod,
2733 JNI::CallStaticFloatMethodV,
2734 JNI::CallStaticFloatMethodA,
2735 JNI::CallStaticDoubleMethod,
2736 JNI::CallStaticDoubleMethodV,
2737 JNI::CallStaticDoubleMethodA,
2738 JNI::CallStaticVoidMethod,
2739 JNI::CallStaticVoidMethodV,
2740 JNI::CallStaticVoidMethodA,
2741 JNI::GetStaticFieldID,
2742 JNI::GetStaticObjectField,
2743 JNI::GetStaticBooleanField,
2744 JNI::GetStaticByteField,
2745 JNI::GetStaticCharField,
2746 JNI::GetStaticShortField,
2747 JNI::GetStaticIntField,
2748 JNI::GetStaticLongField,
2749 JNI::GetStaticFloatField,
2750 JNI::GetStaticDoubleField,
2751 JNI::SetStaticObjectField,
2752 JNI::SetStaticBooleanField,
2753 JNI::SetStaticByteField,
2754 JNI::SetStaticCharField,
2755 JNI::SetStaticShortField,
2756 JNI::SetStaticIntField,
2757 JNI::SetStaticLongField,
2758 JNI::SetStaticFloatField,
2759 JNI::SetStaticDoubleField,
2760 JNI::NewString,
2761 JNI::GetStringLength,
2762 JNI::GetStringChars,
2763 JNI::ReleaseStringChars,
2764 JNI::NewStringUTF,
2765 JNI::GetStringUTFLength,
2766 JNI::GetStringUTFChars,
2767 JNI::ReleaseStringUTFChars,
2768 JNI::GetArrayLength,
2769 JNI::NewObjectArray,
2770 JNI::GetObjectArrayElement,
2771 JNI::SetObjectArrayElement,
2772 JNI::NewBooleanArray,
2773 JNI::NewByteArray,
2774 JNI::NewCharArray,
2775 JNI::NewShortArray,
2776 JNI::NewIntArray,
2777 JNI::NewLongArray,
2778 JNI::NewFloatArray,
2779 JNI::NewDoubleArray,
2780 JNI::GetBooleanArrayElements,
2781 JNI::GetByteArrayElements,
2782 JNI::GetCharArrayElements,
2783 JNI::GetShortArrayElements,
2784 JNI::GetIntArrayElements,
2785 JNI::GetLongArrayElements,
2786 JNI::GetFloatArrayElements,
2787 JNI::GetDoubleArrayElements,
2788 JNI::ReleaseBooleanArrayElements,
2789 JNI::ReleaseByteArrayElements,
2790 JNI::ReleaseCharArrayElements,
2791 JNI::ReleaseShortArrayElements,
2792 JNI::ReleaseIntArrayElements,
2793 JNI::ReleaseLongArrayElements,
2794 JNI::ReleaseFloatArrayElements,
2795 JNI::ReleaseDoubleArrayElements,
2796 JNI::GetBooleanArrayRegion,
2797 JNI::GetByteArrayRegion,
2798 JNI::GetCharArrayRegion,
2799 JNI::GetShortArrayRegion,
2800 JNI::GetIntArrayRegion,
2801 JNI::GetLongArrayRegion,
2802 JNI::GetFloatArrayRegion,
2803 JNI::GetDoubleArrayRegion,
2804 JNI::SetBooleanArrayRegion,
2805 JNI::SetByteArrayRegion,
2806 JNI::SetCharArrayRegion,
2807 JNI::SetShortArrayRegion,
2808 JNI::SetIntArrayRegion,
2809 JNI::SetLongArrayRegion,
2810 JNI::SetFloatArrayRegion,
2811 JNI::SetDoubleArrayRegion,
2812 JNI::RegisterNatives,
2813 JNI::UnregisterNatives,
2814 JNI::MonitorEnter,
2815 JNI::MonitorExit,
2816 JNI::GetJavaVM,
2817 JNI::GetStringRegion,
2818 JNI::GetStringUTFRegion,
2819 JNI::GetPrimitiveArrayCritical,
2820 JNI::ReleasePrimitiveArrayCritical,
2821 JNI::GetStringCritical,
2822 JNI::ReleaseStringCritical,
2823 JNI::NewWeakGlobalRef,
2824 JNI::DeleteWeakGlobalRef,
2825 JNI::ExceptionCheck,
2826 JNI::NewDirectByteBuffer,
2827 JNI::GetDirectBufferAddress,
2828 JNI::GetDirectBufferCapacity,
2829 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002830};
2831
Elliott Hughes75770752011-08-24 17:52:38 -07002832JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002833 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002834 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002835 local_ref_cookie(IRT_FIRST_SEGMENT),
2836 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002837 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002838 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002839 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002840 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002841 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002842 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002843 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002844 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2845 // errors will ensue.
2846 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2847 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002848}
2849
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002850JNIEnvExt::~JNIEnvExt() {
2851}
2852
Elliott Hughes88c5c352012-03-15 18:49:48 -07002853void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2854 check_jni = enabled;
2855 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002856}
2857
Elliott Hughes73e66f72012-05-09 09:34:45 -07002858void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2859 locals.Dump(os);
2860 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002861}
2862
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002863void JNIEnvExt::PushFrame(int /*capacity*/) {
2864 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002865 stacked_local_ref_cookies.push_back(local_ref_cookie);
2866 local_ref_cookie = locals.GetSegmentState();
2867}
2868
2869void JNIEnvExt::PopFrame() {
2870 locals.SetSegmentState(local_ref_cookie);
2871 local_ref_cookie = stacked_local_ref_cookies.back();
2872 stacked_local_ref_cookies.pop_back();
2873}
2874
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002875Offset JNIEnvExt::SegmentStateOffset() {
2876 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2877 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2878}
2879
Carl Shapiroea4dca82011-08-01 13:45:38 -07002880// JNI Invocation interface.
2881
Brian Carlstrombddf9762013-05-14 11:35:37 -07002882extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002883 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002884 if (IsBadJniVersion(args->version)) {
2885 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002886 return JNI_EVERSION;
2887 }
2888 Runtime::Options options;
2889 for (int i = 0; i < args->nOptions; ++i) {
2890 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002891 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002892 }
2893 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002894 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002895 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002896 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002897 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002898 bool started = runtime->Start();
2899 if (!started) {
2900 delete Thread::Current()->GetJniEnv();
2901 delete runtime->GetJavaVM();
2902 LOG(WARNING) << "CreateJavaVM failed";
2903 return JNI_ERR;
2904 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002905 *p_env = Thread::Current()->GetJniEnv();
2906 *p_vm = runtime->GetJavaVM();
2907 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002908}
2909
Elliott Hughesf2682d52011-08-15 16:37:04 -07002910extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002911 Runtime* runtime = Runtime::Current();
2912 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002913 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002914 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002915 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002916 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002917 }
2918 return JNI_OK;
2919}
2920
2921// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002922extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002923 return JNI_ERR;
2924}
2925
Elliott Hughescdf53122011-08-19 15:46:09 -07002926class JII {
2927 public:
2928 static jint DestroyJavaVM(JavaVM* vm) {
2929 if (vm == NULL) {
2930 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002931 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002932 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2933 delete raw_vm->runtime;
2934 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002935 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002936
Elliott Hughescdf53122011-08-19 15:46:09 -07002937 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002938 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002939 }
2940
2941 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002942 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002943 }
2944
2945 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002946 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002947 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002948 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002949 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2950 Runtime* runtime = raw_vm->runtime;
2951 runtime->DetachCurrentThread();
2952 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002953 }
2954
2955 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes83a25322013-03-14 11:18:53 -07002956 if (IsBadJniVersion(version)) {
2957 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07002958 return JNI_EVERSION;
2959 }
2960 if (vm == NULL || env == NULL) {
2961 return JNI_ERR;
2962 }
2963 Thread* thread = Thread::Current();
2964 if (thread == NULL) {
2965 *env = NULL;
2966 return JNI_EDETACHED;
2967 }
2968 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002969 return JNI_OK;
2970 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002971};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002972
Elliott Hughes88c5c352012-03-15 18:49:48 -07002973const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002974 NULL, // reserved0
2975 NULL, // reserved1
2976 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002977 JII::DestroyJavaVM,
2978 JII::AttachCurrentThread,
2979 JII::DetachCurrentThread,
2980 JII::GetEnv,
2981 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002982};
2983
Elliott Hughesa0957642011-09-02 14:27:33 -07002984JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002985 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002986 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002987 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002988 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002989 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002990 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002991 work_around_app_jni_bugs(false),
Ian Rogers62d6c772013-02-27 08:32:07 -08002992 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002993 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002994 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002995 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002996 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002997 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002998 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Elliott Hughes79082e32011-08-25 12:07:32 -07002999 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003000 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003001 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003002 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003003 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003004}
3005
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003006JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003007 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003008}
3009
Elliott Hughes88c5c352012-03-15 18:49:48 -07003010void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3011 check_jni = enabled;
3012 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003013}
3014
Elliott Hughesae80b492012-04-24 10:43:17 -07003015void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3016 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3017 if (force_copy) {
3018 os << " (with forcecopy)";
3019 }
3020 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07003021 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003022 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003023 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003024 os << "; pins=" << pin_table.Size();
3025 }
3026 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003027 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003028 os << "; globals=" << globals.Capacity();
3029 }
3030 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003031 ReaderMutexLock mu(self, weak_globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003032 if (weak_globals.Capacity() > 0) {
3033 os << " (plus " << weak_globals.Capacity() << " weak)";
3034 }
3035 }
3036 os << '\n';
3037
3038 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003039 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003040 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3041 }
3042}
3043
Elliott Hughes73e66f72012-05-09 09:34:45 -07003044void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003045 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003046 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003047 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003048 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003049 }
3050 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003051 ReaderMutexLock mu(self, weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003052 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003053 }
3054 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003055 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003056 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003057 }
3058}
3059
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003060bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
3061 std::string& detail) {
Elliott Hughes75770752011-08-24 17:52:38 -07003062 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003063
3064 // See if we've already loaded this library. If we have, and the class loader
3065 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003066 // TODO: for better results we should canonicalize the pathname (or even compare
3067 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003068 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003069 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003070 {
3071 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003072 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003073 library = libraries->Get(path);
3074 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003075 if (library != NULL) {
3076 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07003077 // The library will be associated with class_loader. The JNI
3078 // spec says we can't load the same library into more than one
3079 // class loader.
3080 StringAppendF(&detail, "Shared library \"%s\" already opened by "
3081 "ClassLoader %p; can't open in ClassLoader %p",
3082 path.c_str(), library->GetClassLoader(), class_loader);
3083 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003084 return false;
3085 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003086 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
3087 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003088 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003089 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
3090 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003091 return false;
3092 }
3093 return true;
3094 }
3095
3096 // Open the shared library. Because we're using a full path, the system
3097 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3098 // resolve this library's dependencies though.)
3099
3100 // Failures here are expected when java.library.path has several entries
3101 // and we have to hunt for the lib.
3102
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003103 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3104 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3105 // dlopen) becomes zero from dlclose.
3106
Elliott Hughescdf53122011-08-19 15:46:09 -07003107 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003108 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003109 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
3110 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
3111 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003112
Elliott Hughes84b2f142012-09-27 09:16:28 -07003113 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003114
3115 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07003116 detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07003117 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003118 return false;
3119 }
3120
3121 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003122 // TODO: move the locking (and more of this logic) into Libraries.
3123 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003124 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003125 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003126 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003127 if (library == NULL) { // We won race to get libraries_lock
3128 library = new SharedLibrary(path, handle, class_loader);
3129 libraries->Put(path, library);
3130 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003131 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003132 }
3133 if (!created_library) {
3134 LOG(INFO) << "WOW: we lost a race to add shared library: "
3135 << "\"" << path << "\" ClassLoader=" << class_loader;
3136 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003137 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003138
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003139 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003140
Elliott Hughes79353722013-08-02 16:52:18 -07003141 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003142 void* sym = dlsym(handle, "JNI_OnLoad");
3143 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003144 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003145 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003146 } else {
3147 // Call JNI_OnLoad. We have to override the current class
3148 // loader, which will always be "null" since the stuff at the
3149 // top of the stack is around Runtime.loadLibrary(). (See
3150 // the comments in the JNI FindClass function.)
3151 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3152 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07003153 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07003154 self->SetClassLoaderOverride(class_loader);
3155
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003156 int version = 0;
3157 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003158 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003159 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003160 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07003161 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003162
Brian Carlstromaded5f72011-10-07 17:15:04 -07003163 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07003164
Elliott Hughes79353722013-08-02 16:52:18 -07003165 if (version == JNI_ERR) {
3166 StringAppendF(&detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
3167 } else if (IsBadJniVersion(version)) {
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003168 StringAppendF(&detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
3169 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003170 // It's unwise to call dlclose() here, but we can mark it
3171 // as bad and ensure that future load attempts will fail.
3172 // We don't know how far JNI_OnLoad got, so there could
3173 // be some partially-initialized stuff accessible through
3174 // newly-registered native method calls. We could try to
3175 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003176 } else {
3177 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003178 }
Elliott Hughes79353722013-08-02 16:52:18 -07003179 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003180 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003181 }
3182
Elliott Hughes79353722013-08-02 16:52:18 -07003183 library->SetResult(was_successful);
3184 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003185}
3186
Brian Carlstromea46f952013-07-30 01:26:50 -07003187void* JavaVMExt::FindCodeForNativeMethod(ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003188 CHECK(m->IsNative());
3189
3190 Class* c = m->GetDeclaringClass();
3191
3192 // If this is a static method, it could be called before the class
3193 // has been initialized.
3194 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07003195 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003196 return NULL;
3197 }
3198 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003199 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003200 }
3201
Brian Carlstrom16192862011-09-12 17:50:06 -07003202 std::string detail;
3203 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003204 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003205 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003206 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003207 native_method = libraries->FindNativeMethod(m, detail);
3208 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003209 // Throwing can cause libraries_lock to be reacquired.
Brian Carlstrom16192862011-09-12 17:50:06 -07003210 if (native_method == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003211 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3212 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003213 }
3214 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003215}
3216
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003217void JavaVMExt::VisitRoots(RootVisitor* visitor, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003218 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003219 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003220 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003221 globals.VisitRoots(visitor, arg);
3222 }
3223 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003224 MutexLock mu(self, pins_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003225 pin_table.VisitRoots(visitor, arg);
3226 }
3227 // The weak_globals table is visited by the GC itself (because it mutates the table).
3228}
3229
Elliott Hughesc8fece32013-01-02 11:27:23 -08003230void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003231 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003232 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
3233 if (c.get() == NULL) {
3234 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3235 }
3236 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3237}
3238
Ian Rogersdf20fe02011-07-20 20:34:16 -07003239} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003240
3241std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3242 switch (rhs) {
3243 case JNIInvalidRefType:
3244 os << "JNIInvalidRefType";
3245 return os;
3246 case JNILocalRefType:
3247 os << "JNILocalRefType";
3248 return os;
3249 case JNIGlobalRefType:
3250 os << "JNIGlobalRefType";
3251 return os;
3252 case JNIWeakGlobalRefType:
3253 os << "JNIWeakGlobalRefType";
3254 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003255 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003256 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003257 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003258 }
3259}