blob: eabce2cb72b08c5c0a87a8e3198671ca53cd2956 [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 Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "gc/card_table-inl.h"
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070031#include "invoke_arg_array_builder.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070032#include "jni.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "mirror/class-inl.h"
34#include "mirror/class_loader.h"
35#include "mirror/field-inl.h"
36#include "mirror/abstract_method-inl.h"
37#include "mirror/object-inl.h"
38#include "mirror/object_array-inl.h"
39#include "mirror/throwable.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080040#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070041#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070042#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070043#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070044#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070045#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046#include "utf.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070047#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070048#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070049
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050using namespace art::mirror;
51
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070052namespace art {
53
Elliott Hughes2ced6a52011-10-16 18:44:48 -070054static const size_t kMonitorsInitial = 32; // Arbitrary.
55static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
56
57static const size_t kLocalsInitial = 64; // Arbitrary.
58static const size_t kLocalsMax = 512; // Arbitrary sanity check.
59
60static const size_t kPinTableInitial = 16; // Arbitrary.
61static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
62
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070063static size_t gGlobalsInitial = 512; // Arbitrary.
64static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070065
66static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
67static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
68
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070069void SetJniGlobalsMax(size_t max) {
70 if (max != 0) {
71 gGlobalsMax = max;
72 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
73 }
74}
Ian Rogersdf20fe02011-07-20 20:34:16 -070075
Ian Rogers00f7d0e2012-07-19 15:28:27 -070076static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070077 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescdf53122011-08-19 15:46:09 -070078 if (obj == NULL) {
79 return NULL;
80 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070081 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -070082 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -070083 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -070084 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
85 return reinterpret_cast<jweak>(ref);
86}
87
Jeff Hao5d917302013-02-27 17:57:33 -080088static void CheckMethodArguments(AbstractMethod* m, uint32_t* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -070089 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesb264f082012-04-06 17:10:10 -070090 MethodHelper mh(m);
Ian Rogers50b35e22012-10-04 10:09:15 -070091 const DexFile::TypeList* params = mh.GetParameterTypeList();
92 if (params == NULL) {
93 return; // No arguments so nothing to check.
94 }
Jeff Hao5d917302013-02-27 17:57:33 -080095 uint32_t offset = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -070096 uint32_t num_params = params->Size();
Elliott Hughesb264f082012-04-06 17:10:10 -070097 size_t error_count = 0;
Jeff Hao5d917302013-02-27 17:57:33 -080098 if (!m->IsStatic()) {
99 offset = 1;
100 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700101 for (uint32_t i = 0; i < num_params; i++) {
102 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
103 Class* param_type = mh.GetClassFromTypeIdx(type_idx);
104 if (param_type == NULL) {
105 Thread* self = Thread::Current();
106 CHECK(self->IsExceptionPending());
107 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
108 << mh.GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
109 << self->GetException()->Dump();
110 self->ClearException();
111 ++error_count;
112 } else if (!param_type->IsPrimitive()) {
113 // TODO: check primitives are in range.
Jeff Hao5d917302013-02-27 17:57:33 -0800114 Object* argument = reinterpret_cast<Object*>(args[i + offset]);
Ian Rogers50b35e22012-10-04 10:09:15 -0700115 if (argument != NULL && !argument->InstanceOf(param_type)) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700116 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
117 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
118 ++error_count;
119 }
Jeff Hao5d917302013-02-27 17:57:33 -0800120 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
121 offset++;
Elliott Hughesb264f082012-04-06 17:10:10 -0700122 }
123 }
124 if (error_count > 0) {
125 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
126 // with an argument.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700127 JniAbortF(NULL, "bad arguments passed to %s (see above for details)", PrettyMethod(m).c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -0700128 }
129}
Elliott Hughesb264f082012-04-06 17:10:10 -0700130
Jeff Hao5d917302013-02-27 17:57:33 -0800131void InvokeWithArgArray(const ScopedObjectAccess& soa, AbstractMethod* method,
132 ArgArray* arg_array, JValue* result, JValue* float_result)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700133 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700134 if (UNLIKELY(soa.Env()->check_jni)) {
Jeff Hao5d917302013-02-27 17:57:33 -0800135 CheckMethodArguments(method, arg_array->GetArray());
Elliott Hughes4cacde82012-04-11 18:32:27 -0700136 }
Jeff Hao5d917302013-02-27 17:57:33 -0800137 method->Invoke(soa.Self(), arg_array->GetArray(), arg_array->GetNumBytes(), result, float_result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700138}
139
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700140static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
141 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700142 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700143 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700144 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700145 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800146 JValue result;
147 JValue float_result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700148 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800149 arg_array.BuildArgArray(soa, receiver, args);
150 InvokeWithArgArray(soa, method, &arg_array, &result, &float_result);
151 if (mh.IsReturnFloatOrDouble()) {
152 return float_result;
153 } else {
154 return result;
155 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700156}
157
Mathieu Chartier66f19252012-09-18 08:57:04 -0700158static AbstractMethod* FindVirtualMethod(Object* receiver, AbstractMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700159 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700160 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700161}
162
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700163static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
164 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700165 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700166 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700167 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700168 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800169 JValue result;
170 JValue float_result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700171 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800172 arg_array.BuildArgArray(soa, receiver, args);
173 InvokeWithArgArray(soa, method, &arg_array, &result, &float_result);
174 if (mh.IsReturnFloatOrDouble()) {
175 return float_result;
176 } else {
177 return result;
178 }
Elliott Hughes72025e52011-08-23 17:50:30 -0700179}
180
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700181static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
182 jobject obj, jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700183 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700184 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700185 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700186 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800187 JValue result;
188 JValue float_result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700189 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800190 arg_array.BuildArgArray(soa, receiver, args);
191 InvokeWithArgArray(soa, method, &arg_array, &result, &float_result);
192 if (mh.IsReturnFloatOrDouble()) {
193 return float_result;
194 } else {
195 return result;
196 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700197}
198
Elliott Hughes6b436852011-08-12 10:16:44 -0700199// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
200// separated with slashes but aren't wrapped with "L;" like regular descriptors
201// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
202// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
203// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700204static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700205 std::string result;
206 // Add the missing "L;" if necessary.
207 if (name[0] == '[') {
208 result = name;
209 } else {
210 result += 'L';
211 result += name;
212 result += ';';
213 }
214 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700215 if (result.find('.') != std::string::npos) {
216 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
217 << "\"" << name << "\"";
218 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700219 }
220 return result;
221}
222
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700223static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, Class* c,
224 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700225 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700226 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes91250e02011-12-13 22:30:35 -0800227 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700228}
229
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700230static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
231 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700232 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700233 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700234 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700235 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700236 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700237
Mathieu Chartier66f19252012-09-18 08:57:04 -0700238 AbstractMethod* method = NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700239 if (is_static) {
240 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700241 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700242 method = c->FindVirtualMethod(name, sig);
243 if (method == NULL) {
244 // No virtual method matching the signature. Search declared
245 // private methods and constructors.
246 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700247 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700248 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700249
Elliott Hughescdf53122011-08-19 15:46:09 -0700250 if (method == NULL || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700251 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700252 return NULL;
253 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700254
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700255 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700256}
257
Ian Rogersef28b142012-11-30 14:22:18 -0800258static ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700259 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef28b142012-11-30 14:22:18 -0800260 AbstractMethod* method = soa.Self()->GetCurrentMethod();
261 if (method == NULL ||
262 method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
263 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700264 }
265 return method->GetDeclaringClass()->GetClassLoader();
266}
267
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700268static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
269 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700270 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700271 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700272 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700273 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700274 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700275
276 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700277 Class* field_type;
278 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
279 if (sig[1] != '\0') {
Ian Rogersef28b142012-11-30 14:22:18 -0800280 ClassLoader* cl = GetClassLoader(soa);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700281 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700282 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700283 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700284 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700285 if (field_type == NULL) {
286 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700287 DCHECK(soa.Self()->IsExceptionPending());
288 soa.Self()->ClearException();
289 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700290 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800291 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700292 return NULL;
293 }
294 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800295 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700296 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800297 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700298 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700299 if (field == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700300 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700301 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800302 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700303 return NULL;
304 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700305 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700306}
307
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700308static void PinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700309 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700310 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700311 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700312 vm->pin_table.Add(array);
313}
314
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700315static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700316 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700317 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700318 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700319 vm->pin_table.Remove(array);
320}
321
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700322static void ThrowAIOOBE(ScopedObjectAccess& soa, Array* array, jsize start,
323 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700324 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700325 std::string type(PrettyTypeOf(array));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700326 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700327 "%s offset=%d length=%d %s.length=%d",
328 type.c_str(), start, length, identifier, array->GetLength());
329}
Ian Rogers0571d352011-11-03 19:51:38 -0700330
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700331static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
332 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700333 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700334 soa.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700335 "offset=%d length=%d string.length()=%d", start, length, array_length);
336}
Elliott Hughes814e4032011-08-23 12:07:56 -0700337
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700338int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700339 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700340 // Turn the const char* into a java.lang.String.
341 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
342 if (msg != NULL && s.get() == NULL) {
343 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700344 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700345
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700346 // Choose an appropriate constructor and set up the arguments.
347 jvalue args[2];
348 const char* signature;
349 if (msg == NULL && cause == NULL) {
350 signature = "()V";
351 } else if (msg != NULL && cause == NULL) {
352 signature = "(Ljava/lang/String;)V";
353 args[0].l = s.get();
354 } else if (msg == NULL && cause != NULL) {
355 signature = "(Ljava/lang/Throwable;)V";
356 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700357 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700358 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
359 args[0].l = s.get();
360 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700361 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700362 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
363 if (mid == NULL) {
Ian Rogersef28b142012-11-30 14:22:18 -0800364 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700365 LOG(ERROR) << "No <init>" << signature << " in "
366 << PrettyClass(soa.Decode<Class*>(exception_class));
367 return JNI_ERR;
368 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700369
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700370 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
371 if (exception.get() == NULL) {
372 return JNI_ERR;
373 }
Elliott Hughesa4f94742012-05-29 16:28:38 -0700374
Ian Rogersef28b142012-11-30 14:22:18 -0800375 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700376 soa.Self()->SetException(soa.Decode<Throwable*>(exception.get()));
Elliott Hughesa4f94742012-05-29 16:28:38 -0700377
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700378 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700379}
380
Elliott Hughes462c9442012-03-23 18:47:50 -0700381static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700382 if (vm == NULL || p_env == NULL) {
383 return JNI_ERR;
384 }
385
Elliott Hughes462c9442012-03-23 18:47:50 -0700386 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700387 Thread* self = Thread::Current();
388 if (self != NULL) {
389 *p_env = self->GetJniEnv();
390 return JNI_OK;
391 }
392
393 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
394
395 // No threads allowed in zygote mode.
396 if (runtime->IsZygote()) {
397 LOG(ERROR) << "Attempt to attach a thread in the zygote";
398 return JNI_ERR;
399 }
400
Elliott Hughes462c9442012-03-23 18:47:50 -0700401 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
402 const char* thread_name = NULL;
Ian Rogers365c1022012-06-22 15:05:28 -0700403 jobject thread_group = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -0700404 if (args != NULL) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700405 if (IsBadJniVersion(args->version)) {
406 LOG(ERROR) << "Bad JNI version passed to "
407 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
408 << args->version;
409 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700410 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700411 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700412 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700413 }
Elliott Hughes75770752011-08-24 17:52:38 -0700414
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800415 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700416 *p_env = NULL;
417 return JNI_ERR;
418 } else {
419 *p_env = Thread::Current()->GetJniEnv();
420 return JNI_OK;
421 }
Elliott Hughes75770752011-08-24 17:52:38 -0700422}
423
Elliott Hughes79082e32011-08-25 12:07:32 -0700424class SharedLibrary {
425 public:
426 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
427 : path_(path),
428 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700429 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700430 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700431 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700432 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700433 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700434 }
435
Elliott Hughes79082e32011-08-25 12:07:32 -0700436 Object* GetClassLoader() {
437 return class_loader_;
438 }
439
440 std::string GetPath() {
441 return path_;
442 }
443
444 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700445 * Check the result of an earlier call to JNI_OnLoad on this library.
446 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700447 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700448 bool CheckOnLoadResult()
449 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700450 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700451 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700452 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
453 bool okay;
454 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700455 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700456
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700457 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
458 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
459 // caller can continue.
460 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
461 okay = true;
462 } else {
463 while (jni_on_load_result_ == kPending) {
464 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700465 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700466 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700467
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700468 okay = (jni_on_load_result_ == kOkay);
469 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
470 << (okay ? "succeeded" : "failed") << "]";
471 }
472 }
473 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700474 return okay;
475 }
476
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700477 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700478 Thread* self = Thread::Current();
479 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700480
Elliott Hughes79082e32011-08-25 12:07:32 -0700481 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700482 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700483
484 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700485 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700486 }
487
488 void* FindSymbol(const std::string& symbol_name) {
489 return dlsym(handle_, symbol_name.c_str());
490 }
491
492 private:
493 enum JNI_OnLoadState {
494 kPending,
495 kFailed,
496 kOkay,
497 };
498
499 // Path to library "/system/lib/libjni.so".
500 std::string path_;
501
502 // The void* returned by dlopen(3).
503 void* handle_;
504
505 // The ClassLoader this library is associated with.
506 Object* class_loader_;
507
508 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700509 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700510 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700511 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700512 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700513 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700514 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700515 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700516};
517
Elliott Hughes79082e32011-08-25 12:07:32 -0700518// This exists mainly to keep implementation details out of the header file.
519class Libraries {
520 public:
521 Libraries() {
522 }
523
524 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700525 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700526 }
527
Elliott Hughesae80b492012-04-24 10:43:17 -0700528 void Dump(std::ostream& os) const {
529 bool first = true;
530 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
531 if (!first) {
532 os << ' ';
533 }
534 first = false;
535 os << it->first;
536 }
537 }
538
539 size_t size() const {
540 return libraries_.size();
541 }
542
Elliott Hughes79082e32011-08-25 12:07:32 -0700543 SharedLibrary* Get(const std::string& path) {
Ian Rogers712462a2012-04-12 16:32:29 -0700544 It it = libraries_.find(path);
545 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700546 }
547
548 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700549 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700550 }
551
552 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700553 void* FindNativeMethod(const AbstractMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700554 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700555 std::string jni_short_name(JniShortName(m));
556 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700557 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700558 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
559 SharedLibrary* library = it->second;
560 if (library->GetClassLoader() != declaring_class_loader) {
561 // We only search libraries loaded by the appropriate ClassLoader.
562 continue;
563 }
564 // Try the short name then the long name...
565 void* fn = library->FindSymbol(jni_short_name);
566 if (fn == NULL) {
567 fn = library->FindSymbol(jni_long_name);
568 }
569 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800570 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
571 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700572 return fn;
573 }
574 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700575 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700576 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700577 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700578 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700579 return NULL;
580 }
581
582 private:
Elliott Hughesae80b492012-04-24 10:43:17 -0700583 typedef SafeMap<std::string, SharedLibrary*>::const_iterator It; // TODO: C++0x auto
Elliott Hughes79082e32011-08-25 12:07:32 -0700584
Elliott Hughesa0e18062012-04-13 15:59:59 -0700585 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700586};
587
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700588JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
589 jvalue* args) {
590 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700591 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700592 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800593 JValue result;
594 JValue float_result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700595 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800596 arg_array.BuildArgArray(soa, receiver, args);
597 InvokeWithArgArray(soa, method, &arg_array, &result, &float_result);
598 if (mh.IsReturnFloatOrDouble()) {
599 return float_result;
600 } else {
601 return result;
602 }
Elliott Hughesd07986f2011-12-06 18:27:45 -0800603}
604
Elliott Hughescdf53122011-08-19 15:46:09 -0700605class JNI {
606 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700607 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700608 return JNI_VERSION_1_6;
609 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700610
Ian Rogers25e8b912012-09-07 11:31:36 -0700611 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700612 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700613 return NULL;
614 }
615
Elliott Hughescdf53122011-08-19 15:46:09 -0700616 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700617 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700618 Runtime* runtime = Runtime::Current();
619 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700620 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700621 Class* c = NULL;
622 if (runtime->IsStarted()) {
Ian Rogersef28b142012-11-30 14:22:18 -0800623 ClassLoader* cl = GetClassLoader(soa);
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800624 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700625 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800626 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700627 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700628 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700629 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700630
Elliott Hughescdf53122011-08-19 15:46:09 -0700631 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700632 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700633 AbstractMethod* method = soa.Decode<AbstractMethod*>(java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700634 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700635 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700636
Elliott Hughescdf53122011-08-19 15:46:09 -0700637 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700638 ScopedObjectAccess soa(env);
639 Field* field = soa.Decode<Field*>(java_field);
640 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700641 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700642
Elliott Hughescdf53122011-08-19 15:46:09 -0700643 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700644 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700645 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700646 return soa.AddLocalReference<jobject>(method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700647 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700648
Elliott Hughescdf53122011-08-19 15:46:09 -0700649 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700650 ScopedObjectAccess soa(env);
651 Field* field = soa.DecodeField(fid);
652 return soa.AddLocalReference<jobject>(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700653 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700654
Elliott Hughes37f7a402011-08-22 18:56:01 -0700655 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700656 ScopedObjectAccess soa(env);
657 Object* o = soa.Decode<Object*>(java_object);
658 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700659 }
660
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700661 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700662 ScopedObjectAccess soa(env);
663 Class* c = soa.Decode<Class*>(java_class);
664 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700665 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700666
Elliott Hughes37f7a402011-08-22 18:56:01 -0700667 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700668 ScopedObjectAccess soa(env);
669 Class* c1 = soa.Decode<Class*>(java_class1);
670 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700671 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700672 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700673
Elliott Hughese84278b2012-03-22 10:06:53 -0700674 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700675 ScopedObjectAccess soa(env);
Elliott Hughes96a98872012-12-19 14:21:15 -0800676 if (java_class == NULL) {
677 JniAbortF("IsInstanceOf", "null class (second argument)");
678 }
Elliott Hughes37f7a402011-08-22 18:56:01 -0700679 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700680 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700681 return JNI_TRUE;
682 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700683 Object* obj = soa.Decode<Object*>(jobj);
684 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700685 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700686 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700687 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700688
Elliott Hughes37f7a402011-08-22 18:56:01 -0700689 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700690 ScopedObjectAccess soa(env);
691 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700692 if (exception == NULL) {
693 return JNI_ERR;
694 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700695 soa.Self()->SetException(exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700696 return JNI_OK;
697 }
698
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700699 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700700 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700701 }
702
703 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700704 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700705 }
706
707 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700708 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700709 }
710
711 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700712 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700713
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700714 Thread* self = soa.Self();
Elliott Hughes72025e52011-08-23 17:50:30 -0700715 Throwable* original_exception = self->GetException();
716 self->ClearException();
717
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700718 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700719 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
720 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
721 if (mid == NULL) {
722 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700723 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700724 } else {
725 env->CallVoidMethod(exception.get(), mid);
726 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700727 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700728 << " thrown while calling printStackTrace";
729 self->ClearException();
730 }
731 }
732
733 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700734 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700735
Elliott Hughescdf53122011-08-19 15:46:09 -0700736 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700737 ScopedObjectAccess soa(env);
738 Object* exception = soa.Self()->GetException();
739 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700740 }
741
Ian Rogers25e8b912012-09-07 11:31:36 -0700742 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700743 LOG(FATAL) << "JNI FatalError called: " << msg;
744 }
745
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700746 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800747 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700748 return JNI_ERR;
749 }
Ian Rogersef28b142012-11-30 14:22:18 -0800750 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700751 return JNI_OK;
752 }
753
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700754 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700755 ScopedObjectAccess soa(env);
756 Object* survivor = soa.Decode<Object*>(java_survivor);
757 soa.Env()->PopFrame();
758 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700759 }
760
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700761 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800762 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700763 }
764
Elliott Hughescdf53122011-08-19 15:46:09 -0700765 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700766 if (obj == NULL) {
767 return NULL;
768 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700769 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700770 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700771 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700772 Object* decoded_obj = soa.Decode<Object*>(obj);
Ian Rogers50b35e22012-10-04 10:09:15 -0700773 MutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700774 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700775 return reinterpret_cast<jobject>(ref);
776 }
777
778 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700779 if (obj == NULL) {
780 return;
781 }
Ian Rogersef28b142012-11-30 14:22:18 -0800782 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700783 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800784 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
785 MutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700786
787 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
788 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
789 << "failed to find entry";
790 }
791 }
792
793 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700794 ScopedObjectAccess soa(env);
795 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700796 }
797
798 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700799 if (obj == NULL) {
800 return;
801 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700802 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700803 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700804 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -0700805 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700806
807 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
808 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
809 << "failed to find entry";
810 }
811 }
812
813 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700814 if (obj == NULL) {
815 return NULL;
816 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700817 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700818 IndirectReferenceTable& locals = soa.Env()->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700819
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700820 uint32_t cookie = soa.Env()->local_ref_cookie;
821 IndirectRef ref = locals.Add(cookie, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700822 return reinterpret_cast<jobject>(ref);
823 }
824
825 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700826 if (obj == NULL) {
827 return;
828 }
Ian Rogersef28b142012-11-30 14:22:18 -0800829 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700830
Ian Rogersef28b142012-11-30 14:22:18 -0800831 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700832 if (!locals.Remove(cookie, obj)) {
833 // Attempting to delete a local reference that is not in the
834 // topmost local reference frame is a no-op. DeleteLocalRef returns
835 // void and doesn't throw any exceptions, but we should probably
836 // complain about it so the user will notice that things aren't
837 // going quite the way they expect.
838 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
839 << "failed to find entry";
840 }
841 }
842
843 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700844 ScopedObjectAccess soa(env);
Ian Rogers120f1c72012-09-28 17:17:10 -0700845 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700846 }
847
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700848 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700849 ScopedObjectAccess soa(env);
850 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700851 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700852 return NULL;
853 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700854 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700855 }
856
Elliott Hughese84278b2012-03-22 10:06:53 -0700857 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700858 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700859 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -0700860 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700861 va_end(args);
862 return result;
863 }
864
Elliott Hughes72025e52011-08-23 17:50:30 -0700865 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700866 ScopedObjectAccess soa(env);
867 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700868 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700869 return NULL;
870 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700871 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700872 if (result == NULL) {
873 return NULL;
874 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700875 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700876 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700877 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700878 return local_result;
879 } else {
880 return NULL;
881 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700882 }
883
Elliott Hughes72025e52011-08-23 17:50:30 -0700884 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700885 ScopedObjectAccess soa(env);
886 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700887 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700888 return NULL;
889 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700890 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700891 if (result == NULL) {
892 return NULL;
893 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700894 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700895 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700896 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700897 return local_result;
898 } else {
899 return NULL;
900 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700901 }
902
Elliott Hughescdf53122011-08-19 15:46:09 -0700903 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700904 ScopedObjectAccess soa(env);
905 return FindMethodID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700906 }
907
908 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700909 ScopedObjectAccess soa(env);
910 return FindMethodID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700911 }
912
Elliott Hughes72025e52011-08-23 17:50:30 -0700913 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700914 va_list ap;
915 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700916 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700917 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700918 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700919 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700920 }
921
Elliott Hughes72025e52011-08-23 17:50:30 -0700922 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700923 ScopedObjectAccess soa(env);
924 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
925 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700926 }
927
Elliott Hughes72025e52011-08-23 17:50:30 -0700928 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700929 ScopedObjectAccess soa(env);
930 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
931 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700932 }
933
Elliott Hughes72025e52011-08-23 17:50:30 -0700934 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700935 va_list ap;
936 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700937 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700938 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700939 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700940 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700941 }
942
Elliott Hughes72025e52011-08-23 17:50:30 -0700943 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700944 ScopedObjectAccess soa(env);
945 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700946 }
947
Elliott Hughes72025e52011-08-23 17:50:30 -0700948 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700949 ScopedObjectAccess soa(env);
950 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700951 }
952
Elliott Hughes72025e52011-08-23 17:50:30 -0700953 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700954 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700955 va_list ap;
956 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700957 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700958 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700959 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700960 }
961
Elliott Hughes72025e52011-08-23 17:50:30 -0700962 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700963 ScopedObjectAccess soa(env);
964 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700965 }
966
Elliott Hughes72025e52011-08-23 17:50:30 -0700967 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700968 ScopedObjectAccess soa(env);
969 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700970 }
971
Elliott Hughes72025e52011-08-23 17:50:30 -0700972 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700973 va_list ap;
974 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700975 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700976 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700977 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700978 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700979 }
980
Elliott Hughes72025e52011-08-23 17:50:30 -0700981 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700982 ScopedObjectAccess soa(env);
983 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700984 }
985
Elliott Hughes72025e52011-08-23 17:50:30 -0700986 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700987 ScopedObjectAccess soa(env);
988 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700989 }
990
Elliott Hughes72025e52011-08-23 17:50:30 -0700991 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700992 va_list ap;
993 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700994 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700995 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700996 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700997 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700998 }
999
Elliott Hughes72025e52011-08-23 17:50:30 -07001000 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001001 ScopedObjectAccess soa(env);
1002 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001003 }
1004
Elliott Hughes72025e52011-08-23 17:50:30 -07001005 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001006 ScopedObjectAccess soa(env);
1007 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001008 }
1009
Elliott Hughes72025e52011-08-23 17:50:30 -07001010 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001011 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001012 va_list ap;
1013 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001014 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001016 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001017 }
1018
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001020 ScopedObjectAccess soa(env);
1021 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001022 }
1023
Elliott Hughes72025e52011-08-23 17:50:30 -07001024 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001025 ScopedObjectAccess soa(env);
1026 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 }
1028
Elliott Hughes72025e52011-08-23 17:50:30 -07001029 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 va_list ap;
1031 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001032 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001033 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001035 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001036 }
1037
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001039 ScopedObjectAccess soa(env);
1040 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001041 }
1042
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001044 ScopedObjectAccess soa(env);
1045 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001046 }
1047
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001049 va_list ap;
1050 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001051 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001052 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001054 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001055 }
1056
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001058 ScopedObjectAccess soa(env);
1059 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001060 }
1061
Elliott Hughes72025e52011-08-23 17:50:30 -07001062 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001063 ScopedObjectAccess soa(env);
1064 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001065 }
1066
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001068 va_list ap;
1069 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001070 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001071 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001072 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001073 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 }
1075
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001077 ScopedObjectAccess soa(env);
1078 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 }
1080
Elliott Hughes72025e52011-08-23 17:50:30 -07001081 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001082 ScopedObjectAccess soa(env);
1083 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001084 }
1085
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001087 va_list ap;
1088 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001089 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001090 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 }
1093
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001095 ScopedObjectAccess soa(env);
1096 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 }
1098
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001100 ScopedObjectAccess soa(env);
1101 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 }
1103
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001104 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001106 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001107 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001108 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1109 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001110 va_end(ap);
1111 return local_result;
1112 }
1113
1114 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001115 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001116 ScopedObjectAccess soa(env);
1117 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1118 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 }
1120
1121 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001122 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001123 ScopedObjectAccess soa(env);
1124 JValue result(InvokeWithJValues(soa, obj, mid, args));
1125 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 }
1127
1128 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001129 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001131 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001132 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001133 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001135 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 }
1137
1138 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001139 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001140 ScopedObjectAccess soa(env);
1141 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 }
1143
1144 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001145 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001146 ScopedObjectAccess soa(env);
1147 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 }
1149
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001150 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001152 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001153 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001154 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001156 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 }
1158
1159 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001160 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001161 ScopedObjectAccess soa(env);
1162 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 }
1164
1165 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001166 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001167 ScopedObjectAccess soa(env);
1168 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 }
1170
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001171 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001172 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001175 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001177 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 }
1179
1180 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001181 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001182 ScopedObjectAccess soa(env);
1183 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 }
1185
1186 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001187 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001188 ScopedObjectAccess soa(env);
1189 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 }
1191
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001192 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001193 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001194 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001195 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001196 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001198 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 }
1200
1201 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001202 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001203 ScopedObjectAccess soa(env);
1204 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 }
1206
1207 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001208 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001209 ScopedObjectAccess soa(env);
1210 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 }
1212
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001213 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001215 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001216 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001217 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001219 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 }
1221
1222 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001223 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001224 ScopedObjectAccess soa(env);
1225 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 }
1227
1228 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001229 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001230 ScopedObjectAccess soa(env);
1231 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 }
1233
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001234 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001235 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001236 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001237 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001238 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001240 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 }
1242
1243 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001244 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001245 ScopedObjectAccess soa(env);
1246 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 }
1248
1249 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001250 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001251 ScopedObjectAccess soa(env);
1252 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 }
1254
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001255 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001257 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001258 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001259 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001261 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 }
1263
1264 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001265 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001266 ScopedObjectAccess soa(env);
1267 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 }
1269
1270 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001271 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001272 ScopedObjectAccess soa(env);
1273 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 }
1275
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001276 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001277 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001278 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001279 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001280 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001282 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 }
1284
1285 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001286 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001287 ScopedObjectAccess soa(env);
1288 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001289 }
1290
1291 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001292 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001293 ScopedObjectAccess soa(env);
1294 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 }
1296
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001297 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001299 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001300 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001301 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 va_end(ap);
1303 }
1304
1305 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001306 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001307 ScopedObjectAccess soa(env);
1308 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001309 }
1310
1311 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001312 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001313 ScopedObjectAccess soa(env);
1314 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 }
1316
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001317 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001318 ScopedObjectAccess soa(env);
1319 return FindFieldID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001321
1322
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001323 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001324 ScopedObjectAccess soa(env);
1325 return FindFieldID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001326 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001327
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001328 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001329 ScopedObjectAccess soa(env);
1330 Object* o = soa.Decode<Object*>(obj);
1331 Field* f = soa.DecodeField(fid);
1332 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001334
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001335 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001336 ScopedObjectAccess soa(env);
1337 Field* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001338 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001339 }
1340
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001341 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001342 ScopedObjectAccess soa(env);
1343 Object* o = soa.Decode<Object*>(java_object);
1344 Object* v = soa.Decode<Object*>(java_value);
1345 Field* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001346 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001347 }
1348
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001349 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001350 ScopedObjectAccess soa(env);
1351 Object* v = soa.Decode<Object*>(java_value);
1352 Field* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001353 f->SetObject(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 }
1355
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001356#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001357 ScopedObjectAccess soa(env); \
1358 Object* o = soa.Decode<Object*>(instance); \
1359 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001360 return f->fn(o)
1361
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001362#define GET_STATIC_PRIMITIVE_FIELD(fn) \
1363 ScopedObjectAccess soa(env); \
1364 Field* f = soa.DecodeField(fid); \
1365 return f->fn(f->GetDeclaringClass())
1366
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001367#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001368 ScopedObjectAccess soa(env); \
1369 Object* o = soa.Decode<Object*>(instance); \
1370 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001371 f->fn(o, value)
1372
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001373#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
1374 ScopedObjectAccess soa(env); \
1375 Field* f = soa.DecodeField(fid); \
1376 f->fn(f->GetDeclaringClass(), value)
1377
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001378 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1379 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001380 }
1381
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001382 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1383 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001384 }
1385
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001386 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1387 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001388 }
1389
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001390 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1391 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 }
1393
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001394 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1395 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 }
1397
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001398 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1399 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 }
1401
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001402 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1403 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001404 }
1405
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001406 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1407 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001408 }
1409
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001410 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001411 GET_STATIC_PRIMITIVE_FIELD(GetBoolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 }
1413
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001414 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001415 GET_STATIC_PRIMITIVE_FIELD(GetByte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001416 }
1417
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001418 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001419 GET_STATIC_PRIMITIVE_FIELD(GetChar);
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 }
1421
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001422 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001423 GET_STATIC_PRIMITIVE_FIELD(GetShort);
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 }
1425
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001426 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001427 GET_STATIC_PRIMITIVE_FIELD(GetInt);
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 }
1429
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001430 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001431 GET_STATIC_PRIMITIVE_FIELD(GetLong);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001432 }
1433
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001434 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001435 GET_STATIC_PRIMITIVE_FIELD(GetFloat);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001436 }
1437
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001438 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001439 GET_STATIC_PRIMITIVE_FIELD(GetDouble);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001440 }
1441
1442 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1443 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1444 }
1445
1446 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1447 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1448 }
1449
1450 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1451 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1452 }
1453
1454 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1455 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1456 }
1457
1458 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1459 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1460 }
1461
1462 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1463 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1464 }
1465
1466 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1467 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1468 }
1469
1470 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1471 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1472 }
1473
1474 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001475 SET_STATIC_PRIMITIVE_FIELD(SetBoolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001476 }
1477
1478 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001479 SET_STATIC_PRIMITIVE_FIELD(SetByte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001480 }
1481
1482 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001483 SET_STATIC_PRIMITIVE_FIELD(SetChar, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001484 }
1485
1486 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001487 SET_STATIC_PRIMITIVE_FIELD(SetFloat, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001488 }
1489
1490 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001491 SET_STATIC_PRIMITIVE_FIELD(SetDouble, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001492 }
1493
1494 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001495 SET_STATIC_PRIMITIVE_FIELD(SetInt, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001496 }
1497
1498 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001499 SET_STATIC_PRIMITIVE_FIELD(SetLong, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001500 }
1501
1502 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001503 SET_STATIC_PRIMITIVE_FIELD(SetShort, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001504 }
1505
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001506 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001507 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001508 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001509 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001510 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1511 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001512 va_end(ap);
1513 return local_result;
1514 }
1515
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001516 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001517 ScopedObjectAccess soa(env);
1518 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1519 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001520 }
1521
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001522 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001523 ScopedObjectAccess soa(env);
1524 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1525 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001526 }
1527
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001528 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001529 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001530 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001531 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001532 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001533 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001534 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001535 }
1536
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001537 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001538 ScopedObjectAccess soa(env);
1539 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001540 }
1541
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001542 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001543 ScopedObjectAccess soa(env);
1544 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 }
1546
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001547 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001548 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001549 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001550 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001551 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001552 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001553 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001554 }
1555
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001556 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001557 ScopedObjectAccess soa(env);
1558 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001559 }
1560
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001561 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001562 ScopedObjectAccess soa(env);
1563 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001564 }
1565
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001566 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001567 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001568 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001569 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001570 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001571 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001572 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 }
1574
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001575 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001576 ScopedObjectAccess soa(env);
1577 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001578 }
1579
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001580 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001581 ScopedObjectAccess soa(env);
1582 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001583 }
1584
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001585 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001586 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001587 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001588 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001589 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001591 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001592 }
1593
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001594 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001595 ScopedObjectAccess soa(env);
1596 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001597 }
1598
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001599 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001600 ScopedObjectAccess soa(env);
1601 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001602 }
1603
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001604 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001605 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001606 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001607 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001608 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001609 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001610 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 }
1612
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001613 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001614 ScopedObjectAccess soa(env);
1615 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 }
1617
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001618 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001619 ScopedObjectAccess soa(env);
1620 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001621 }
1622
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001623 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001625 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001626 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001627 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001628 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001629 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001630 }
1631
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001632 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001633 ScopedObjectAccess soa(env);
1634 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001635 }
1636
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001637 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001638 ScopedObjectAccess soa(env);
1639 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001640 }
1641
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001642 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001643 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001644 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001645 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001646 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001648 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 }
1650
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001651 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001652 ScopedObjectAccess soa(env);
1653 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001654 }
1655
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001656 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001657 ScopedObjectAccess soa(env);
1658 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001659 }
1660
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001661 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001663 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001664 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001665 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001666 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001667 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001668 }
1669
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001670 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001671 ScopedObjectAccess soa(env);
1672 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 }
1674
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001675 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001676 ScopedObjectAccess soa(env);
1677 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001678 }
1679
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001680 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001682 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001683 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001684 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001685 va_end(ap);
1686 }
1687
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001688 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001689 ScopedObjectAccess soa(env);
1690 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 }
1692
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001693 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001694 ScopedObjectAccess soa(env);
1695 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 }
1697
Elliott Hughes814e4032011-08-23 12:07:56 -07001698 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001699 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001700 String* result = String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001701 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001702 }
1703
1704 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 if (utf == NULL) {
1706 return NULL;
1707 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001708 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001709 String* result = String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001710 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 }
1712
Elliott Hughes814e4032011-08-23 12:07:56 -07001713 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001714 ScopedObjectAccess soa(env);
1715 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001716 }
1717
1718 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001719 ScopedObjectAccess soa(env);
1720 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001721 }
1722
Elliott Hughesb465ab02011-08-24 11:21:21 -07001723 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001724 ScopedObjectAccess soa(env);
1725 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001726 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001727 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001728 } else {
1729 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1730 memcpy(buf, chars + start, length * sizeof(jchar));
1731 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001732 }
1733
Elliott Hughesb465ab02011-08-24 11:21:21 -07001734 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001735 ScopedObjectAccess soa(env);
1736 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001737 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001738 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001739 } else {
1740 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1741 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1742 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001743 }
1744
Elliott Hughes75770752011-08-24 17:52:38 -07001745 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001746 ScopedObjectAccess soa(env);
1747 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001748 const CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001749 PinPrimitiveArray(soa, chars);
Elliott Hughes75770752011-08-24 17:52:38 -07001750 if (is_copy != NULL) {
1751 *is_copy = JNI_FALSE;
1752 }
1753 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001754 }
1755
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001756 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001757 ScopedObjectAccess soa(env);
1758 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001759 }
1760
Elliott Hughes75770752011-08-24 17:52:38 -07001761 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001762 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001763 }
1764
Elliott Hughes75770752011-08-24 17:52:38 -07001765 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001766 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001767 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001768 }
1769
Elliott Hughes75770752011-08-24 17:52:38 -07001770 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001771 if (java_string == NULL) {
1772 return NULL;
1773 }
1774 if (is_copy != NULL) {
1775 *is_copy = JNI_TRUE;
1776 }
Ian Rogersef28b142012-11-30 14:22:18 -08001777 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001778 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001779 size_t byte_count = s->GetUtfLength();
1780 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001781 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001782 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1783 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1784 bytes[byte_count] = '\0';
1785 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001786 }
1787
Elliott Hughes75770752011-08-24 17:52:38 -07001788 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001789 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001790 }
1791
Elliott Hughesbd935992011-08-22 11:59:34 -07001792 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001793 ScopedObjectAccess soa(env);
1794 Object* obj = soa.Decode<Object*>(java_array);
Elliott Hughes96a98872012-12-19 14:21:15 -08001795 if (!obj->IsArrayInstance()) {
1796 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1797 }
Elliott Hughesbd935992011-08-22 11:59:34 -07001798 Array* array = obj->AsArray();
1799 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001800 }
1801
Elliott Hughes814e4032011-08-23 12:07:56 -07001802 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001803 ScopedObjectAccess soa(env);
1804 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1805 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001806 }
1807
1808 static void SetObjectArrayElement(JNIEnv* env,
1809 jobjectArray java_array, jsize index, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001810 ScopedObjectAccess soa(env);
1811 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1812 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001813 array->Set(index, value);
1814 }
1815
1816 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001817 ScopedObjectAccess soa(env);
1818 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001819 }
1820
1821 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001822 ScopedObjectAccess soa(env);
1823 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 }
1825
1826 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001827 ScopedObjectAccess soa(env);
1828 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 }
1830
1831 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001832 ScopedObjectAccess soa(env);
1833 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001834 }
1835
1836 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001837 ScopedObjectAccess soa(env);
1838 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001839 }
1840
1841 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001842 ScopedObjectAccess soa(env);
1843 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001844 }
1845
1846 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001847 ScopedObjectAccess soa(env);
1848 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001849 }
1850
1851 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001852 ScopedObjectAccess soa(env);
Elliott Hughes96a98872012-12-19 14:21:15 -08001853 if (length < 0) {
1854 JniAbortF("NewObjectArray", "negative array length: %d", length);
1855 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001856
1857 // Compute the array class corresponding to the given element class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001858 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001859 std::string descriptor;
1860 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001861 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001862
1863 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001864 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1865 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001866 return NULL;
1867 }
1868
Elliott Hughes75770752011-08-24 17:52:38 -07001869 // Allocate and initialize if necessary.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001870 Class* array_class = soa.Decode<Class*>(java_array_class.get());
Ian Rogers50b35e22012-10-04 10:09:15 -07001871 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001872 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001873 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07001874 for (jsize i = 0; i < length; ++i) {
1875 result->Set(i, initial_object);
1876 }
1877 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001878 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001879 }
1880
1881 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001882 ScopedObjectAccess soa(env);
1883 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001884 }
1885
Ian Rogersa15e67d2012-02-28 13:51:55 -08001886 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001887 ScopedObjectAccess soa(env);
1888 Array* array = soa.Decode<Array*>(java_array);
1889 PinPrimitiveArray(soa, array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001890 if (is_copy != NULL) {
1891 *is_copy = JNI_FALSE;
1892 }
1893 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001894 }
1895
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001896 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001897 ReleasePrimitiveArray(env, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001898 }
1899
Elliott Hughes75770752011-08-24 17:52:38 -07001900 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001901 ScopedObjectAccess soa(env);
1902 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001903 }
1904
Elliott Hughes75770752011-08-24 17:52:38 -07001905 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001906 ScopedObjectAccess soa(env);
1907 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001908 }
1909
Elliott Hughes75770752011-08-24 17:52:38 -07001910 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001911 ScopedObjectAccess soa(env);
1912 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001913 }
1914
Elliott Hughes75770752011-08-24 17:52:38 -07001915 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001916 ScopedObjectAccess soa(env);
1917 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001918 }
1919
Elliott Hughes75770752011-08-24 17:52:38 -07001920 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001921 ScopedObjectAccess soa(env);
1922 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001923 }
1924
Elliott Hughes75770752011-08-24 17:52:38 -07001925 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001926 ScopedObjectAccess soa(env);
1927 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001928 }
1929
Elliott Hughes75770752011-08-24 17:52:38 -07001930 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001931 ScopedObjectAccess soa(env);
1932 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001933 }
1934
Elliott Hughes75770752011-08-24 17:52:38 -07001935 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001936 ScopedObjectAccess soa(env);
1937 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001938 }
1939
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001940 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001941 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001942 }
1943
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001944 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001945 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001946 }
1947
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001948 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001949 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001950 }
1951
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001952 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001953 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001954 }
1955
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001956 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001957 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001958 }
1959
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001960 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001961 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001962 }
1963
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001964 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001965 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001966 }
1967
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001968 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001969 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001970 }
1971
Elliott Hughes814e4032011-08-23 12:07:56 -07001972 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001973 ScopedObjectAccess soa(env);
1974 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001975 }
1976
Elliott Hughes814e4032011-08-23 12:07:56 -07001977 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001978 ScopedObjectAccess soa(env);
1979 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001980 }
1981
Elliott Hughes814e4032011-08-23 12:07:56 -07001982 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001983 ScopedObjectAccess soa(env);
1984 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001985 }
1986
Elliott Hughes814e4032011-08-23 12:07:56 -07001987 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001988 ScopedObjectAccess soa(env);
1989 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001990 }
1991
Elliott Hughes814e4032011-08-23 12:07:56 -07001992 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001993 ScopedObjectAccess soa(env);
1994 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001995 }
1996
Elliott Hughes814e4032011-08-23 12:07:56 -07001997 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001998 ScopedObjectAccess soa(env);
1999 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002000 }
2001
Elliott Hughes814e4032011-08-23 12:07:56 -07002002 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002003 ScopedObjectAccess soa(env);
2004 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002005 }
2006
Elliott Hughes814e4032011-08-23 12:07:56 -07002007 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002008 ScopedObjectAccess soa(env);
2009 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002010 }
2011
Elliott Hughes814e4032011-08-23 12:07:56 -07002012 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002013 ScopedObjectAccess soa(env);
2014 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002015 }
2016
Elliott Hughes814e4032011-08-23 12:07:56 -07002017 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002018 ScopedObjectAccess soa(env);
2019 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002020 }
2021
Elliott Hughes814e4032011-08-23 12:07:56 -07002022 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002023 ScopedObjectAccess soa(env);
2024 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002025 }
2026
Elliott Hughes814e4032011-08-23 12:07:56 -07002027 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002028 ScopedObjectAccess soa(env);
2029 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 }
2031
Elliott Hughes814e4032011-08-23 12:07:56 -07002032 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002033 ScopedObjectAccess soa(env);
2034 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002035 }
2036
Elliott Hughes814e4032011-08-23 12:07:56 -07002037 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002038 ScopedObjectAccess soa(env);
2039 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002040 }
2041
Elliott Hughes814e4032011-08-23 12:07:56 -07002042 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002043 ScopedObjectAccess soa(env);
2044 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002045 }
2046
Elliott Hughes814e4032011-08-23 12:07:56 -07002047 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002048 ScopedObjectAccess soa(env);
2049 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 }
2051
Elliott Hughes5174fe62011-08-23 15:12:35 -07002052 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002053 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2054 }
2055
2056 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, size_t method_count, bool return_errors) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002057 ScopedObjectAccess soa(env);
2058 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002059
Elliott Hughesc8fece32013-01-02 11:27:23 -08002060 for (size_t i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 const char* name = methods[i].name;
2062 const char* sig = methods[i].signature;
2063
2064 if (*sig == '!') {
2065 // TODO: fast jni. it's too noisy to log all these.
2066 ++sig;
2067 }
2068
Mathieu Chartier66f19252012-09-18 08:57:04 -07002069 AbstractMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002070 if (m == NULL) {
2071 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002073 if (m == NULL) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002074 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
2075 << PrettyDescriptor(c) << "." << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002076 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002078 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002079 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
2080 << PrettyDescriptor(c) << "." << name << sig
2081 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002082 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002083 return JNI_ERR;
2084 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002085
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002086 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002087
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002088 m->RegisterNative(soa.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002089 }
2090 return JNI_OK;
2091 }
2092
Elliott Hughes5174fe62011-08-23 15:12:35 -07002093 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002094 ScopedObjectAccess soa(env);
2095 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002096
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002097 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002098
2099 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002100 AbstractMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002101 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002102 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002103 }
2104 }
2105 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002106 AbstractMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002107 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002108 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002109 }
2110 }
2111
2112 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002113 }
2114
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002115 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2116 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
2117 ScopedObjectAccess soa(env);
2118 Object* o = soa.Decode<Object*>(java_object);
2119 o->MonitorEnter(soa.Self());
2120 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002121 return JNI_ERR;
2122 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002123 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002124 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 }
2126
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002127 static jint MonitorExit(JNIEnv* env, jobject java_object)
2128 UNLOCK_FUNCTION(monitor_lock_) {
2129 ScopedObjectAccess soa(env);
2130 Object* o = soa.Decode<Object*>(java_object);
2131 o->MonitorExit(soa.Self());
2132 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002133 return JNI_ERR;
2134 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002135 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002136 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002137 }
2138
2139 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002140 Runtime* runtime = Runtime::Current();
2141 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002142 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 } else {
2144 *vm = NULL;
2145 }
2146 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2147 }
2148
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002150 if (capacity < 0) {
2151 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %d", capacity);
2152 }
Elliott Hughes11a796e2012-12-19 14:42:57 -08002153 if (address == NULL && capacity != 0) {
2154 JniAbortF("NewDirectByteBuffer", "non-zero capacity for NULL pointer: %d", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002155 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002156
Elliott Hughes96a98872012-12-19 14:21:15 -08002157 // At the moment, the Java side is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002158 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2159 CHECK_LE(capacity, 0xffffffff);
2160 jint address_arg = reinterpret_cast<jint>(address);
2161 jint capacity_arg = static_cast<jint>(capacity);
2162
Elliott Hugheseac76672012-05-24 21:56:51 -07002163 jobject result = env->NewObject(WellKnownClasses::java_nio_ReadWriteDirectByteBuffer,
2164 WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_init,
2165 address_arg, capacity_arg);
Ian Rogersef28b142012-11-30 14:22:18 -08002166 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002167 }
2168
Elliott Hughesb465ab02011-08-24 11:21:21 -07002169 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hugheseac76672012-05-24 21:56:51 -07002170 return reinterpret_cast<void*>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002171 }
2172
Elliott Hughesb465ab02011-08-24 11:21:21 -07002173 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hugheseac76672012-05-24 21:56:51 -07002174 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002175 }
2176
Elliott Hughesb465ab02011-08-24 11:21:21 -07002177 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002178 if (java_object == NULL) {
2179 JniAbortF("GetObjectRefType", "null object");
2180 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002181
2182 // Do we definitely know what kind of reference this is?
2183 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2184 IndirectRefKind kind = GetIndirectRefKind(ref);
2185 switch (kind) {
2186 case kLocal:
Ian Rogersef28b142012-11-30 14:22:18 -08002187 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002188 return JNILocalRefType;
2189 }
2190 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002191 case kGlobal:
2192 return JNIGlobalRefType;
2193 case kWeakGlobal:
2194 return JNIWeakGlobalRefType;
2195 case kSirtOrInvalid:
2196 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002197 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002198 return JNILocalRefType;
2199 }
2200
Ian Rogersef28b142012-11-30 14:22:18 -08002201 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002202 return JNIInvalidRefType;
2203 }
2204
Elliott Hughesb465ab02011-08-24 11:21:21 -07002205 // If we're handing out direct pointers, check whether it's a direct pointer
2206 // to a local reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002207 {
2208 ScopedObjectAccess soa(env);
2209 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2210 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
2211 return JNILocalRefType;
2212 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002213 }
2214 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002215 return JNIInvalidRefType;
2216 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002217 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2218 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002219 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002220
2221 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002222 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2223 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002224 // TODO: we should try to expand the table if necessary.
2225 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
2226 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2227 return JNI_ERR;
2228 }
2229 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002230 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002231 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2232 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002233 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002234 soa.Self()->ThrowOutOfMemoryError(caller);
2235 }
2236 return okay ? JNI_OK : JNI_ERR;
2237 }
2238
2239 template<typename JniT, typename ArtT>
2240 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002241 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002242 if (length < 0) {
2243 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
2244 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002245 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002246 return soa.AddLocalReference<JniT>(result);
2247 }
2248
2249 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2250 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2251 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002252 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002253 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2254 PinPrimitiveArray(soa, array);
2255 if (is_copy != NULL) {
2256 *is_copy = JNI_FALSE;
2257 }
2258 return array->GetData();
2259 }
2260
2261 template <typename ArrayT>
Ian Rogersef28b142012-11-30 14:22:18 -08002262 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002263 if (mode != JNI_COMMIT) {
Ian Rogersef28b142012-11-30 14:22:18 -08002264 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002265 Array* array = soa.Decode<Array*>(java_array);
2266 UnpinPrimitiveArray(soa, array);
2267 }
2268 }
2269
2270 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2271 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2272 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002273 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002274 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2275 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2276 ThrowAIOOBE(soa, array, start, length, "src");
2277 } else {
2278 JavaT* data = array->GetData();
2279 memcpy(buf, data + start, length * sizeof(JavaT));
2280 }
2281 }
2282
2283 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2284 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2285 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002286 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002287 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2288 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2289 ThrowAIOOBE(soa, array, start, length, "dst");
2290 } else {
2291 JavaT* data = array->GetData();
2292 memcpy(data + start, buf, length * sizeof(JavaT));
2293 }
2294 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002295};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002296
Elliott Hughes88c5c352012-03-15 18:49:48 -07002297const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002298 NULL, // reserved0.
2299 NULL, // reserved1.
2300 NULL, // reserved2.
2301 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002302 JNI::GetVersion,
2303 JNI::DefineClass,
2304 JNI::FindClass,
2305 JNI::FromReflectedMethod,
2306 JNI::FromReflectedField,
2307 JNI::ToReflectedMethod,
2308 JNI::GetSuperclass,
2309 JNI::IsAssignableFrom,
2310 JNI::ToReflectedField,
2311 JNI::Throw,
2312 JNI::ThrowNew,
2313 JNI::ExceptionOccurred,
2314 JNI::ExceptionDescribe,
2315 JNI::ExceptionClear,
2316 JNI::FatalError,
2317 JNI::PushLocalFrame,
2318 JNI::PopLocalFrame,
2319 JNI::NewGlobalRef,
2320 JNI::DeleteGlobalRef,
2321 JNI::DeleteLocalRef,
2322 JNI::IsSameObject,
2323 JNI::NewLocalRef,
2324 JNI::EnsureLocalCapacity,
2325 JNI::AllocObject,
2326 JNI::NewObject,
2327 JNI::NewObjectV,
2328 JNI::NewObjectA,
2329 JNI::GetObjectClass,
2330 JNI::IsInstanceOf,
2331 JNI::GetMethodID,
2332 JNI::CallObjectMethod,
2333 JNI::CallObjectMethodV,
2334 JNI::CallObjectMethodA,
2335 JNI::CallBooleanMethod,
2336 JNI::CallBooleanMethodV,
2337 JNI::CallBooleanMethodA,
2338 JNI::CallByteMethod,
2339 JNI::CallByteMethodV,
2340 JNI::CallByteMethodA,
2341 JNI::CallCharMethod,
2342 JNI::CallCharMethodV,
2343 JNI::CallCharMethodA,
2344 JNI::CallShortMethod,
2345 JNI::CallShortMethodV,
2346 JNI::CallShortMethodA,
2347 JNI::CallIntMethod,
2348 JNI::CallIntMethodV,
2349 JNI::CallIntMethodA,
2350 JNI::CallLongMethod,
2351 JNI::CallLongMethodV,
2352 JNI::CallLongMethodA,
2353 JNI::CallFloatMethod,
2354 JNI::CallFloatMethodV,
2355 JNI::CallFloatMethodA,
2356 JNI::CallDoubleMethod,
2357 JNI::CallDoubleMethodV,
2358 JNI::CallDoubleMethodA,
2359 JNI::CallVoidMethod,
2360 JNI::CallVoidMethodV,
2361 JNI::CallVoidMethodA,
2362 JNI::CallNonvirtualObjectMethod,
2363 JNI::CallNonvirtualObjectMethodV,
2364 JNI::CallNonvirtualObjectMethodA,
2365 JNI::CallNonvirtualBooleanMethod,
2366 JNI::CallNonvirtualBooleanMethodV,
2367 JNI::CallNonvirtualBooleanMethodA,
2368 JNI::CallNonvirtualByteMethod,
2369 JNI::CallNonvirtualByteMethodV,
2370 JNI::CallNonvirtualByteMethodA,
2371 JNI::CallNonvirtualCharMethod,
2372 JNI::CallNonvirtualCharMethodV,
2373 JNI::CallNonvirtualCharMethodA,
2374 JNI::CallNonvirtualShortMethod,
2375 JNI::CallNonvirtualShortMethodV,
2376 JNI::CallNonvirtualShortMethodA,
2377 JNI::CallNonvirtualIntMethod,
2378 JNI::CallNonvirtualIntMethodV,
2379 JNI::CallNonvirtualIntMethodA,
2380 JNI::CallNonvirtualLongMethod,
2381 JNI::CallNonvirtualLongMethodV,
2382 JNI::CallNonvirtualLongMethodA,
2383 JNI::CallNonvirtualFloatMethod,
2384 JNI::CallNonvirtualFloatMethodV,
2385 JNI::CallNonvirtualFloatMethodA,
2386 JNI::CallNonvirtualDoubleMethod,
2387 JNI::CallNonvirtualDoubleMethodV,
2388 JNI::CallNonvirtualDoubleMethodA,
2389 JNI::CallNonvirtualVoidMethod,
2390 JNI::CallNonvirtualVoidMethodV,
2391 JNI::CallNonvirtualVoidMethodA,
2392 JNI::GetFieldID,
2393 JNI::GetObjectField,
2394 JNI::GetBooleanField,
2395 JNI::GetByteField,
2396 JNI::GetCharField,
2397 JNI::GetShortField,
2398 JNI::GetIntField,
2399 JNI::GetLongField,
2400 JNI::GetFloatField,
2401 JNI::GetDoubleField,
2402 JNI::SetObjectField,
2403 JNI::SetBooleanField,
2404 JNI::SetByteField,
2405 JNI::SetCharField,
2406 JNI::SetShortField,
2407 JNI::SetIntField,
2408 JNI::SetLongField,
2409 JNI::SetFloatField,
2410 JNI::SetDoubleField,
2411 JNI::GetStaticMethodID,
2412 JNI::CallStaticObjectMethod,
2413 JNI::CallStaticObjectMethodV,
2414 JNI::CallStaticObjectMethodA,
2415 JNI::CallStaticBooleanMethod,
2416 JNI::CallStaticBooleanMethodV,
2417 JNI::CallStaticBooleanMethodA,
2418 JNI::CallStaticByteMethod,
2419 JNI::CallStaticByteMethodV,
2420 JNI::CallStaticByteMethodA,
2421 JNI::CallStaticCharMethod,
2422 JNI::CallStaticCharMethodV,
2423 JNI::CallStaticCharMethodA,
2424 JNI::CallStaticShortMethod,
2425 JNI::CallStaticShortMethodV,
2426 JNI::CallStaticShortMethodA,
2427 JNI::CallStaticIntMethod,
2428 JNI::CallStaticIntMethodV,
2429 JNI::CallStaticIntMethodA,
2430 JNI::CallStaticLongMethod,
2431 JNI::CallStaticLongMethodV,
2432 JNI::CallStaticLongMethodA,
2433 JNI::CallStaticFloatMethod,
2434 JNI::CallStaticFloatMethodV,
2435 JNI::CallStaticFloatMethodA,
2436 JNI::CallStaticDoubleMethod,
2437 JNI::CallStaticDoubleMethodV,
2438 JNI::CallStaticDoubleMethodA,
2439 JNI::CallStaticVoidMethod,
2440 JNI::CallStaticVoidMethodV,
2441 JNI::CallStaticVoidMethodA,
2442 JNI::GetStaticFieldID,
2443 JNI::GetStaticObjectField,
2444 JNI::GetStaticBooleanField,
2445 JNI::GetStaticByteField,
2446 JNI::GetStaticCharField,
2447 JNI::GetStaticShortField,
2448 JNI::GetStaticIntField,
2449 JNI::GetStaticLongField,
2450 JNI::GetStaticFloatField,
2451 JNI::GetStaticDoubleField,
2452 JNI::SetStaticObjectField,
2453 JNI::SetStaticBooleanField,
2454 JNI::SetStaticByteField,
2455 JNI::SetStaticCharField,
2456 JNI::SetStaticShortField,
2457 JNI::SetStaticIntField,
2458 JNI::SetStaticLongField,
2459 JNI::SetStaticFloatField,
2460 JNI::SetStaticDoubleField,
2461 JNI::NewString,
2462 JNI::GetStringLength,
2463 JNI::GetStringChars,
2464 JNI::ReleaseStringChars,
2465 JNI::NewStringUTF,
2466 JNI::GetStringUTFLength,
2467 JNI::GetStringUTFChars,
2468 JNI::ReleaseStringUTFChars,
2469 JNI::GetArrayLength,
2470 JNI::NewObjectArray,
2471 JNI::GetObjectArrayElement,
2472 JNI::SetObjectArrayElement,
2473 JNI::NewBooleanArray,
2474 JNI::NewByteArray,
2475 JNI::NewCharArray,
2476 JNI::NewShortArray,
2477 JNI::NewIntArray,
2478 JNI::NewLongArray,
2479 JNI::NewFloatArray,
2480 JNI::NewDoubleArray,
2481 JNI::GetBooleanArrayElements,
2482 JNI::GetByteArrayElements,
2483 JNI::GetCharArrayElements,
2484 JNI::GetShortArrayElements,
2485 JNI::GetIntArrayElements,
2486 JNI::GetLongArrayElements,
2487 JNI::GetFloatArrayElements,
2488 JNI::GetDoubleArrayElements,
2489 JNI::ReleaseBooleanArrayElements,
2490 JNI::ReleaseByteArrayElements,
2491 JNI::ReleaseCharArrayElements,
2492 JNI::ReleaseShortArrayElements,
2493 JNI::ReleaseIntArrayElements,
2494 JNI::ReleaseLongArrayElements,
2495 JNI::ReleaseFloatArrayElements,
2496 JNI::ReleaseDoubleArrayElements,
2497 JNI::GetBooleanArrayRegion,
2498 JNI::GetByteArrayRegion,
2499 JNI::GetCharArrayRegion,
2500 JNI::GetShortArrayRegion,
2501 JNI::GetIntArrayRegion,
2502 JNI::GetLongArrayRegion,
2503 JNI::GetFloatArrayRegion,
2504 JNI::GetDoubleArrayRegion,
2505 JNI::SetBooleanArrayRegion,
2506 JNI::SetByteArrayRegion,
2507 JNI::SetCharArrayRegion,
2508 JNI::SetShortArrayRegion,
2509 JNI::SetIntArrayRegion,
2510 JNI::SetLongArrayRegion,
2511 JNI::SetFloatArrayRegion,
2512 JNI::SetDoubleArrayRegion,
2513 JNI::RegisterNatives,
2514 JNI::UnregisterNatives,
2515 JNI::MonitorEnter,
2516 JNI::MonitorExit,
2517 JNI::GetJavaVM,
2518 JNI::GetStringRegion,
2519 JNI::GetStringUTFRegion,
2520 JNI::GetPrimitiveArrayCritical,
2521 JNI::ReleasePrimitiveArrayCritical,
2522 JNI::GetStringCritical,
2523 JNI::ReleaseStringCritical,
2524 JNI::NewWeakGlobalRef,
2525 JNI::DeleteWeakGlobalRef,
2526 JNI::ExceptionCheck,
2527 JNI::NewDirectByteBuffer,
2528 JNI::GetDirectBufferAddress,
2529 JNI::GetDirectBufferCapacity,
2530 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002531};
2532
Elliott Hughes75770752011-08-24 17:52:38 -07002533JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002534 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002535 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002536 local_ref_cookie(IRT_FIRST_SEGMENT),
2537 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002538 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002539 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002540 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002541 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002542 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002543 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002544 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002545 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2546 // errors will ensue.
2547 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2548 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002549}
2550
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002551JNIEnvExt::~JNIEnvExt() {
2552}
2553
Elliott Hughes88c5c352012-03-15 18:49:48 -07002554void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2555 check_jni = enabled;
2556 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002557}
2558
Elliott Hughes73e66f72012-05-09 09:34:45 -07002559void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2560 locals.Dump(os);
2561 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002562}
2563
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002564void JNIEnvExt::PushFrame(int /*capacity*/) {
2565 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002566 stacked_local_ref_cookies.push_back(local_ref_cookie);
2567 local_ref_cookie = locals.GetSegmentState();
2568}
2569
2570void JNIEnvExt::PopFrame() {
2571 locals.SetSegmentState(local_ref_cookie);
2572 local_ref_cookie = stacked_local_ref_cookies.back();
2573 stacked_local_ref_cookies.pop_back();
2574}
2575
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002576Offset JNIEnvExt::SegmentStateOffset() {
2577 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2578 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2579}
2580
Carl Shapiroea4dca82011-08-01 13:45:38 -07002581// JNI Invocation interface.
2582
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002583extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2584 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002585 if (IsBadJniVersion(args->version)) {
2586 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002587 return JNI_EVERSION;
2588 }
2589 Runtime::Options options;
2590 for (int i = 0; i < args->nOptions; ++i) {
2591 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002592 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002593 }
2594 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002595 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002596 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002597 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002598 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002599 bool started = runtime->Start();
2600 if (!started) {
2601 delete Thread::Current()->GetJniEnv();
2602 delete runtime->GetJavaVM();
2603 LOG(WARNING) << "CreateJavaVM failed";
2604 return JNI_ERR;
2605 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002606 *p_env = Thread::Current()->GetJniEnv();
2607 *p_vm = runtime->GetJavaVM();
2608 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002609}
2610
Elliott Hughesf2682d52011-08-15 16:37:04 -07002611extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002612 Runtime* runtime = Runtime::Current();
2613 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002614 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002615 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002616 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002617 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002618 }
2619 return JNI_OK;
2620}
2621
2622// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002623extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002624 return JNI_ERR;
2625}
2626
Elliott Hughescdf53122011-08-19 15:46:09 -07002627class JII {
2628 public:
2629 static jint DestroyJavaVM(JavaVM* vm) {
2630 if (vm == NULL) {
2631 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002632 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002633 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2634 delete raw_vm->runtime;
2635 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002636 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002637
Elliott Hughescdf53122011-08-19 15:46:09 -07002638 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002639 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002640 }
2641
2642 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002643 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002644 }
2645
2646 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002647 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002648 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002649 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002650 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2651 Runtime* runtime = raw_vm->runtime;
2652 runtime->DetachCurrentThread();
2653 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002654 }
2655
2656 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes83a25322013-03-14 11:18:53 -07002657 if (IsBadJniVersion(version)) {
2658 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07002659 return JNI_EVERSION;
2660 }
2661 if (vm == NULL || env == NULL) {
2662 return JNI_ERR;
2663 }
2664 Thread* thread = Thread::Current();
2665 if (thread == NULL) {
2666 *env = NULL;
2667 return JNI_EDETACHED;
2668 }
2669 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002670 return JNI_OK;
2671 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002672};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002673
Elliott Hughes88c5c352012-03-15 18:49:48 -07002674const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002675 NULL, // reserved0
2676 NULL, // reserved1
2677 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002678 JII::DestroyJavaVM,
2679 JII::AttachCurrentThread,
2680 JII::DetachCurrentThread,
2681 JII::GetEnv,
2682 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002683};
2684
Elliott Hughesa0957642011-09-02 14:27:33 -07002685JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002686 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002687 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002688 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002689 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002690 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002691 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002692 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002693 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002694 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002695 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002696 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002697 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002698 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002699 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Elliott Hughes79082e32011-08-25 12:07:32 -07002700 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002701 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002702 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002703 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002704 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002705}
2706
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002707JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002708 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002709}
2710
Elliott Hughes88c5c352012-03-15 18:49:48 -07002711void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2712 check_jni = enabled;
2713 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002714}
2715
Elliott Hughesae80b492012-04-24 10:43:17 -07002716void JavaVMExt::DumpForSigQuit(std::ostream& os) {
2717 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
2718 if (force_copy) {
2719 os << " (with forcecopy)";
2720 }
2721 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07002722 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07002723 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002724 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002725 os << "; pins=" << pin_table.Size();
2726 }
2727 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002728 MutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002729 os << "; globals=" << globals.Capacity();
2730 }
2731 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002732 MutexLock mu(self, weak_globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002733 if (weak_globals.Capacity() > 0) {
2734 os << " (plus " << weak_globals.Capacity() << " weak)";
2735 }
2736 }
2737 os << '\n';
2738
2739 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002740 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002741 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
2742 }
2743}
2744
Elliott Hughes73e66f72012-05-09 09:34:45 -07002745void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002746 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002747 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002748 MutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002749 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002750 }
2751 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002752 MutexLock mu(self, weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002753 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002754 }
2755 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002756 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002757 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002758 }
2759}
2760
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002761bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
2762 std::string& detail) {
Elliott Hughes75770752011-08-24 17:52:38 -07002763 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002764
2765 // See if we've already loaded this library. If we have, and the class loader
2766 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002767 // TODO: for better results we should canonicalize the pathname (or even compare
2768 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002769 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07002770 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07002771 {
2772 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07002773 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002774 library = libraries->Get(path);
2775 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002776 if (library != NULL) {
2777 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002778 // The library will be associated with class_loader. The JNI
2779 // spec says we can't load the same library into more than one
2780 // class loader.
2781 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2782 "ClassLoader %p; can't open in ClassLoader %p",
2783 path.c_str(), library->GetClassLoader(), class_loader);
2784 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002785 return false;
2786 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002787 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2788 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002789 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002790 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2791 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002792 return false;
2793 }
2794 return true;
2795 }
2796
2797 // Open the shared library. Because we're using a full path, the system
2798 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2799 // resolve this library's dependencies though.)
2800
2801 // Failures here are expected when java.library.path has several entries
2802 // and we have to hunt for the lib.
2803
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002804 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
2805 // class unloading. Libraries will only be unloaded when the reference count (incremented by
2806 // dlopen) becomes zero from dlclose.
2807
Elliott Hughescdf53122011-08-19 15:46:09 -07002808 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002809 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002810 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
2811 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
2812 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07002813
Elliott Hughes84b2f142012-09-27 09:16:28 -07002814 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002815
2816 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002817 detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07002818 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002819 return false;
2820 }
2821
2822 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002823 // TODO: move the locking (and more of this logic) into Libraries.
2824 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002825 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002826 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002827 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002828 if (library == NULL) { // We won race to get libraries_lock
2829 library = new SharedLibrary(path, handle, class_loader);
2830 libraries->Put(path, library);
2831 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002832 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002833 }
2834 if (!created_library) {
2835 LOG(INFO) << "WOW: we lost a race to add shared library: "
2836 << "\"" << path << "\" ClassLoader=" << class_loader;
2837 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002838 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002839
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002840 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002841
2842 bool result = true;
2843 void* sym = dlsym(handle, "JNI_OnLoad");
2844 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002845 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002846 } else {
2847 // Call JNI_OnLoad. We have to override the current class
2848 // loader, which will always be "null" since the stuff at the
2849 // top of the stack is around Runtime.loadLibrary(). (See
2850 // the comments in the JNI FindClass function.)
2851 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2852 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07002853 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002854 self->SetClassLoaderOverride(class_loader);
2855
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002856 int version = 0;
2857 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002858 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002859 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002860 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002861 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002862
Brian Carlstromaded5f72011-10-07 17:15:04 -07002863 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002864
Elliott Hughes83a25322013-03-14 11:18:53 -07002865 if (IsBadJniVersion(version)) {
2866 LOG(ERROR) << "Bad JNI version returned from JNI_OnLoad in \"" << path << "\": " << version;
Elliott Hughes79082e32011-08-25 12:07:32 -07002867 // It's unwise to call dlclose() here, but we can mark it
2868 // as bad and ensure that future load attempts will fail.
2869 // We don't know how far JNI_OnLoad got, so there could
2870 // be some partially-initialized stuff accessible through
2871 // newly-registered native method calls. We could try to
2872 // unregister them, but that doesn't seem worthwhile.
2873 result = false;
2874 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002875 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2876 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002877 }
2878 }
2879
2880 library->SetResult(result);
2881 return result;
2882}
2883
Mathieu Chartier66f19252012-09-18 08:57:04 -07002884void* JavaVMExt::FindCodeForNativeMethod(AbstractMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002885 CHECK(m->IsNative());
2886
2887 Class* c = m->GetDeclaringClass();
2888
2889 // If this is a static method, it could be called before the class
2890 // has been initialized.
2891 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002892 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002893 return NULL;
2894 }
2895 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002896 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002897 }
2898
Brian Carlstrom16192862011-09-12 17:50:06 -07002899 std::string detail;
2900 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07002901 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07002902 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002903 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07002904 native_method = libraries->FindNativeMethod(m, detail);
2905 }
2906 // throwing can cause libraries_lock to be reacquired
2907 if (native_method == NULL) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002908 self->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002909 }
2910 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002911}
2912
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002913void JavaVMExt::VisitRoots(RootVisitor* visitor, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002914 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07002915 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002916 MutexLock mu(self, globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002917 globals.VisitRoots(visitor, arg);
2918 }
2919 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002920 MutexLock mu(self, pins_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002921 pin_table.VisitRoots(visitor, arg);
2922 }
2923 // The weak_globals table is visited by the GC itself (because it mutates the table).
2924}
2925
Elliott Hughesc8fece32013-01-02 11:27:23 -08002926void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
2927 size_t method_count) {
2928 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
2929 if (c.get() == NULL) {
2930 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
2931 }
2932 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
2933}
2934
Ian Rogersdf20fe02011-07-20 20:34:16 -07002935} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002936
2937std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2938 switch (rhs) {
2939 case JNIInvalidRefType:
2940 os << "JNIInvalidRefType";
2941 return os;
2942 case JNILocalRefType:
2943 os << "JNILocalRefType";
2944 return os;
2945 case JNIGlobalRefType:
2946 os << "JNIGlobalRefType";
2947 return os;
2948 case JNIWeakGlobalRefType:
2949 os << "JNIWeakGlobalRefType";
2950 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002951 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002952 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002953 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002954 }
2955}