blob: cbdf3cde4d173bca3e144c8cbee355096fd50532 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ian Rogersdf20fe02011-07-20 20:34:16 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070018
Elliott Hughes0af55432011-08-17 18:37:28 -070019#include <dlfcn.h>
Elliott Hughes79082e32011-08-25 12:07:32 -070020
21#include <cstdarg>
Elliott Hughes0af55432011-08-17 18:37:28 -070022#include <utility>
23#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080026#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080027#include "base/stl_util.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080028#include "base/stringpiece.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070029#include "class_linker.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070031#include "gc/accounting/card_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070032#include "interpreter/interpreter.h"
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070033#include "invoke_arg_array_builder.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070034#include "jni.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "mirror/class-inl.h"
36#include "mirror/class_loader.h"
37#include "mirror/field-inl.h"
38#include "mirror/abstract_method-inl.h"
39#include "mirror/object-inl.h"
40#include "mirror/object_array-inl.h"
41#include "mirror/throwable.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080042#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070043#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070044#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070045#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070046#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070047#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048#include "utf.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070049#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070050#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070051
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052using namespace art::mirror;
53
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070054namespace art {
55
Elliott Hughes2ced6a52011-10-16 18:44:48 -070056static const size_t kMonitorsInitial = 32; // Arbitrary.
57static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
58
59static const size_t kLocalsInitial = 64; // Arbitrary.
60static const size_t kLocalsMax = 512; // Arbitrary sanity check.
61
62static const size_t kPinTableInitial = 16; // Arbitrary.
63static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
64
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070065static size_t gGlobalsInitial = 512; // Arbitrary.
Elliott Hughes2010a602013-07-02 14:17:23 -070066static size_t gGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Elliott Hughes2ced6a52011-10-16 18:44:48 -070067
68static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
Elliott Hughes2010a602013-07-02 14:17:23 -070069static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Ian Rogersdf20fe02011-07-20 20:34:16 -070070
Ian Rogers00f7d0e2012-07-19 15:28:27 -070071static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070072 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescdf53122011-08-19 15:46:09 -070073 if (obj == NULL) {
74 return NULL;
75 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070076 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -070077 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -070078 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -070079 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
80 return reinterpret_cast<jweak>(ref);
81}
82
Jeff Hao19c5d372013-03-15 14:33:43 -070083static bool IsBadJniVersion(int version) {
84 // We don't support JNI_VERSION_1_1. These are the only other valid versions.
85 return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
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"
Ian Rogers62d6c772013-02-27 08:32:07 -0800109 << self->GetException(NULL)->Dump();
Ian Rogers50b35e22012-10-04 10:09:15 -0700110 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,
Jeff Hao6474d192013-03-26 14:08:09 -0700132 ArgArray* arg_array, JValue* result, char result_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700133 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700134 uint32_t* args = arg_array->GetArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700135 if (UNLIKELY(soa.Env()->check_jni)) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700136 CheckMethodArguments(method, args);
Elliott Hughes4cacde82012-04-11 18:32:27 -0700137 }
Sebastien Hertz7d658cf2013-07-09 10:56:11 +0200138 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, result_type);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700139}
140
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700141static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
142 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700143 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700144 AbstractMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700145 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700146 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800147 JValue 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);
Jeff Hao6474d192013-03-26 14:08:09 -0700150 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
151 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700152}
153
Mathieu Chartier66f19252012-09-18 08:57:04 -0700154static AbstractMethod* FindVirtualMethod(Object* receiver, AbstractMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700155 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700156 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700157}
158
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700159static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
160 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700161 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700162 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700163 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700164 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800165 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700166 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800167 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700168 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
169 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700170}
171
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700172static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
173 jobject obj, jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700174 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700175 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700176 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700177 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800178 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700179 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800180 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700181 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
182 return result;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700183}
184
Elliott Hughes6b436852011-08-12 10:16:44 -0700185// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
186// separated with slashes but aren't wrapped with "L;" like regular descriptors
187// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
188// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
189// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700190static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700191 std::string result;
192 // Add the missing "L;" if necessary.
193 if (name[0] == '[') {
194 result = name;
195 } else {
196 result += 'L';
197 result += name;
198 result += ';';
199 }
200 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700201 if (result.find('.') != std::string::npos) {
202 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
203 << "\"" << name << "\"";
204 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700205 }
206 return result;
207}
208
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700209static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, Class* c,
210 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700211 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800212 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
213 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
214 "no %s method \"%s.%s%s\"",
215 kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700216}
217
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700218static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
219 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700220 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700221 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700222 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700223 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700224 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700225
Mathieu Chartier66f19252012-09-18 08:57:04 -0700226 AbstractMethod* method = NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700227 if (is_static) {
228 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700229 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700230 method = c->FindVirtualMethod(name, sig);
231 if (method == NULL) {
232 // No virtual method matching the signature. Search declared
233 // private methods and constructors.
234 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700235 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700236 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700237
Elliott Hughescdf53122011-08-19 15:46:09 -0700238 if (method == NULL || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700239 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700240 return NULL;
241 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700242
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700243 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700244}
245
Ian Rogersef28b142012-11-30 14:22:18 -0800246static ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700247 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800248 AbstractMethod* method = soa.Self()->GetCurrentMethod(NULL);
Ian Rogersef28b142012-11-30 14:22:18 -0800249 if (method == NULL ||
250 method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
251 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700252 }
253 return method->GetDeclaringClass()->GetClassLoader();
254}
255
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700256static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
257 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700258 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700259 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700260 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700261 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700262 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700263
264 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700265 Class* field_type;
266 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
267 if (sig[1] != '\0') {
Ian Rogersef28b142012-11-30 14:22:18 -0800268 ClassLoader* cl = GetClassLoader(soa);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700269 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700270 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700271 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700272 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700273 if (field_type == NULL) {
274 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700275 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800276 ThrowLocation throw_location;
277 SirtRef<mirror::Throwable> cause(soa.Self(), soa.Self()->GetException(&throw_location));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700278 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800279 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
280 "no type \"%s\" found and so no field \"%s\" could be found in class "
281 "\"%s\" or its superclasses", sig, name,
282 ClassHelper(c).GetDescriptor());
283 soa.Self()->GetException(NULL)->SetCause(cause.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700284 return NULL;
285 }
286 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800287 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700288 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800289 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700290 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700291 if (field == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800292 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
293 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
294 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
295 sig, name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700296 return NULL;
297 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700298 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700299}
300
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700301static void PinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700302 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700303 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700304 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700305 vm->pin_table.Add(array);
306}
307
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700308static void UnpinPrimitiveArray(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.Remove(array);
313}
314
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700315static void ThrowAIOOBE(ScopedObjectAccess& soa, Array* array, jsize start,
316 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700317 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700318 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800319 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
320 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
321 "%s offset=%d length=%d %s.length=%d",
322 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700323}
Ian Rogers0571d352011-11-03 19:51:38 -0700324
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700325static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
326 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700327 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800328 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
329 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
330 "offset=%d length=%d string.length()=%d", start, length,
331 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700332}
Elliott Hughes814e4032011-08-23 12:07:56 -0700333
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700334int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700335 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700336 // Turn the const char* into a java.lang.String.
337 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
338 if (msg != NULL && s.get() == NULL) {
339 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700340 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700341
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700342 // Choose an appropriate constructor and set up the arguments.
343 jvalue args[2];
344 const char* signature;
345 if (msg == NULL && cause == NULL) {
346 signature = "()V";
347 } else if (msg != NULL && cause == NULL) {
348 signature = "(Ljava/lang/String;)V";
349 args[0].l = s.get();
350 } else if (msg == NULL && cause != NULL) {
351 signature = "(Ljava/lang/Throwable;)V";
352 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700353 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700354 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
355 args[0].l = s.get();
356 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700357 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700358 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
359 if (mid == NULL) {
Ian Rogersef28b142012-11-30 14:22:18 -0800360 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700361 LOG(ERROR) << "No <init>" << signature << " in "
362 << PrettyClass(soa.Decode<Class*>(exception_class));
363 return JNI_ERR;
364 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700365
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700366 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
367 if (exception.get() == NULL) {
368 return JNI_ERR;
369 }
Ian Rogersef28b142012-11-30 14:22:18 -0800370 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800371 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
372 soa.Self()->SetException(throw_location, soa.Decode<Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700373 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700374}
375
Elliott Hughes462c9442012-03-23 18:47:50 -0700376static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700377 if (vm == NULL || p_env == NULL) {
378 return JNI_ERR;
379 }
380
Elliott Hughes462c9442012-03-23 18:47:50 -0700381 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700382 Thread* self = Thread::Current();
383 if (self != NULL) {
384 *p_env = self->GetJniEnv();
385 return JNI_OK;
386 }
387
388 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
389
390 // No threads allowed in zygote mode.
391 if (runtime->IsZygote()) {
392 LOG(ERROR) << "Attempt to attach a thread in the zygote";
393 return JNI_ERR;
394 }
395
Elliott Hughes462c9442012-03-23 18:47:50 -0700396 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
397 const char* thread_name = NULL;
Ian Rogers365c1022012-06-22 15:05:28 -0700398 jobject thread_group = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -0700399 if (args != NULL) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700400 if (IsBadJniVersion(args->version)) {
401 LOG(ERROR) << "Bad JNI version passed to "
402 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
403 << args->version;
404 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700405 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700406 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700407 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700408 }
Elliott Hughes75770752011-08-24 17:52:38 -0700409
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800410 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700411 *p_env = NULL;
412 return JNI_ERR;
413 } else {
414 *p_env = Thread::Current()->GetJniEnv();
415 return JNI_OK;
416 }
Elliott Hughes75770752011-08-24 17:52:38 -0700417}
418
Elliott Hughes79082e32011-08-25 12:07:32 -0700419class SharedLibrary {
420 public:
421 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
422 : path_(path),
423 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700424 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700425 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700426 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700427 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700428 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700429 }
430
Elliott Hughes79082e32011-08-25 12:07:32 -0700431 Object* GetClassLoader() {
432 return class_loader_;
433 }
434
435 std::string GetPath() {
436 return path_;
437 }
438
439 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700440 * Check the result of an earlier call to JNI_OnLoad on this library.
441 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700442 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700443 bool CheckOnLoadResult()
444 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700445 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700446 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700447 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
448 bool okay;
449 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700450 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700451
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700452 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
453 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
454 // caller can continue.
455 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
456 okay = true;
457 } else {
458 while (jni_on_load_result_ == kPending) {
459 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700460 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700461 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700462
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700463 okay = (jni_on_load_result_ == kOkay);
464 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
465 << (okay ? "succeeded" : "failed") << "]";
466 }
467 }
468 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700469 return okay;
470 }
471
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700472 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700473 Thread* self = Thread::Current();
474 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700475
Elliott Hughes79082e32011-08-25 12:07:32 -0700476 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700477 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700478
479 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700480 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700481 }
482
483 void* FindSymbol(const std::string& symbol_name) {
484 return dlsym(handle_, symbol_name.c_str());
485 }
486
487 private:
488 enum JNI_OnLoadState {
489 kPending,
490 kFailed,
491 kOkay,
492 };
493
494 // Path to library "/system/lib/libjni.so".
495 std::string path_;
496
497 // The void* returned by dlopen(3).
498 void* handle_;
499
500 // The ClassLoader this library is associated with.
501 Object* class_loader_;
502
503 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700504 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700505 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700506 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700507 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700508 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700509 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700510 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700511};
512
Elliott Hughes79082e32011-08-25 12:07:32 -0700513// This exists mainly to keep implementation details out of the header file.
514class Libraries {
515 public:
516 Libraries() {
517 }
518
519 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700520 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700521 }
522
Elliott Hughesae80b492012-04-24 10:43:17 -0700523 void Dump(std::ostream& os) const {
524 bool first = true;
525 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
526 if (!first) {
527 os << ' ';
528 }
529 first = false;
530 os << it->first;
531 }
532 }
533
534 size_t size() const {
535 return libraries_.size();
536 }
537
Elliott Hughes79082e32011-08-25 12:07:32 -0700538 SharedLibrary* Get(const std::string& path) {
Ian Rogers712462a2012-04-12 16:32:29 -0700539 It it = libraries_.find(path);
540 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700541 }
542
543 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700544 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700545 }
546
547 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700548 void* FindNativeMethod(const AbstractMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700549 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700550 std::string jni_short_name(JniShortName(m));
551 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700552 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700553 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
554 SharedLibrary* library = it->second;
555 if (library->GetClassLoader() != declaring_class_loader) {
556 // We only search libraries loaded by the appropriate ClassLoader.
557 continue;
558 }
559 // Try the short name then the long name...
560 void* fn = library->FindSymbol(jni_short_name);
561 if (fn == NULL) {
562 fn = library->FindSymbol(jni_long_name);
563 }
564 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800565 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
566 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700567 return fn;
568 }
569 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700570 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700571 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700572 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700573 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700574 return NULL;
575 }
576
577 private:
Elliott Hughesae80b492012-04-24 10:43:17 -0700578 typedef SafeMap<std::string, SharedLibrary*>::const_iterator It; // TODO: C++0x auto
Elliott Hughes79082e32011-08-25 12:07:32 -0700579
Elliott Hughesa0e18062012-04-13 15:59:59 -0700580 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700581};
582
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700583JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
584 jvalue* args) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700585 AbstractMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700586 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700587 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800588 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700589 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800590 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700591 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
592 return result;
Elliott Hughesd07986f2011-12-06 18:27:45 -0800593}
594
Elliott Hughescdf53122011-08-19 15:46:09 -0700595class JNI {
596 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700597 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700598 return JNI_VERSION_1_6;
599 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700600
Ian Rogers25e8b912012-09-07 11:31:36 -0700601 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700602 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700603 return NULL;
604 }
605
Elliott Hughescdf53122011-08-19 15:46:09 -0700606 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700607 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700608 Runtime* runtime = Runtime::Current();
609 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700610 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700611 Class* c = NULL;
612 if (runtime->IsStarted()) {
Ian Rogersef28b142012-11-30 14:22:18 -0800613 ClassLoader* cl = GetClassLoader(soa);
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800614 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700615 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800616 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700617 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700618 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700619 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700620
Elliott Hughescdf53122011-08-19 15:46:09 -0700621 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700622 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700623 AbstractMethod* method = soa.Decode<AbstractMethod*>(java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700624 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700625 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700626
Elliott Hughescdf53122011-08-19 15:46:09 -0700627 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700628 ScopedObjectAccess soa(env);
629 Field* field = soa.Decode<Field*>(java_field);
630 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700631 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700632
Elliott Hughescdf53122011-08-19 15:46:09 -0700633 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700634 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700635 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700636 return soa.AddLocalReference<jobject>(method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700637 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700638
Elliott Hughescdf53122011-08-19 15:46:09 -0700639 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700640 ScopedObjectAccess soa(env);
641 Field* field = soa.DecodeField(fid);
642 return soa.AddLocalReference<jobject>(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700643 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700644
Elliott Hughes37f7a402011-08-22 18:56:01 -0700645 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700646 ScopedObjectAccess soa(env);
647 Object* o = soa.Decode<Object*>(java_object);
648 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700649 }
650
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700651 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700652 ScopedObjectAccess soa(env);
653 Class* c = soa.Decode<Class*>(java_class);
654 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700655 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700656
Elliott Hughes37f7a402011-08-22 18:56:01 -0700657 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700658 ScopedObjectAccess soa(env);
659 Class* c1 = soa.Decode<Class*>(java_class1);
660 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700661 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700662 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700663
Elliott Hughese84278b2012-03-22 10:06:53 -0700664 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700665 ScopedObjectAccess soa(env);
Elliott Hughes96a98872012-12-19 14:21:15 -0800666 if (java_class == NULL) {
667 JniAbortF("IsInstanceOf", "null class (second argument)");
668 }
Elliott Hughes37f7a402011-08-22 18:56:01 -0700669 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700670 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700671 return JNI_TRUE;
672 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700673 Object* obj = soa.Decode<Object*>(jobj);
674 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700675 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700676 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700677 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700678
Elliott Hughes37f7a402011-08-22 18:56:01 -0700679 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700680 ScopedObjectAccess soa(env);
681 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700682 if (exception == NULL) {
683 return JNI_ERR;
684 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800685 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
686 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700687 return JNI_OK;
688 }
689
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700690 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700691 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700692 }
693
694 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700695 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700696 }
697
698 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700699 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700700 }
701
702 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700703 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700704
Ian Rogers62d6c772013-02-27 08:32:07 -0800705 SirtRef<mirror::Object> old_throw_this_object(soa.Self(), NULL);
706 SirtRef<mirror::AbstractMethod> old_throw_method(soa.Self(), NULL);
707 SirtRef<mirror::Throwable> old_exception(soa.Self(), NULL);
708 uint32_t old_throw_dex_pc;
709 {
710 ThrowLocation old_throw_location;
711 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
712 old_throw_this_object.reset(old_throw_location.GetThis());
713 old_throw_method.reset(old_throw_location.GetMethod());
714 old_exception.reset(old_exception_obj);
715 old_throw_dex_pc = old_throw_location.GetDexPc();
716 soa.Self()->ClearException();
717 }
718 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(old_exception.get()));
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 "
Ian Rogers62d6c772013-02-27 08:32:07 -0800723 << PrettyTypeOf(old_exception.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700724 } else {
725 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800726 if (soa.Self()->IsExceptionPending()) {
727 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(NULL))
Elliott Hughes72025e52011-08-23 17:50:30 -0700728 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800729 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700730 }
731 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800732 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
733 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700734
Ian Rogers62d6c772013-02-27 08:32:07 -0800735 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700736 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700737
Elliott Hughescdf53122011-08-19 15:46:09 -0700738 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700739 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800740 Object* exception = soa.Self()->GetException(NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700741 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700742 }
743
Ian Rogers25e8b912012-09-07 11:31:36 -0700744 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700745 LOG(FATAL) << "JNI FatalError called: " << msg;
746 }
747
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700748 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800749 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700750 return JNI_ERR;
751 }
Ian Rogersef28b142012-11-30 14:22:18 -0800752 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700753 return JNI_OK;
754 }
755
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700756 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700757 ScopedObjectAccess soa(env);
758 Object* survivor = soa.Decode<Object*>(java_survivor);
759 soa.Env()->PopFrame();
760 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700761 }
762
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700763 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800764 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700765 }
766
Elliott Hughescdf53122011-08-19 15:46:09 -0700767 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700768 if (obj == NULL) {
769 return NULL;
770 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700771 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700772 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700773 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700774 Object* decoded_obj = soa.Decode<Object*>(obj);
Ian Rogers50b35e22012-10-04 10:09:15 -0700775 MutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700776 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700777 return reinterpret_cast<jobject>(ref);
778 }
779
780 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700781 if (obj == NULL) {
782 return;
783 }
Ian Rogersef28b142012-11-30 14:22:18 -0800784 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700785 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800786 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
787 MutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700788
789 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
790 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
791 << "failed to find entry";
792 }
793 }
794
795 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700796 ScopedObjectAccess soa(env);
797 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700798 }
799
800 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700801 if (obj == NULL) {
802 return;
803 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700804 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700805 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700806 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -0700807 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700808
809 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
810 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
811 << "failed to find entry";
812 }
813 }
814
815 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700816 if (obj == NULL) {
817 return NULL;
818 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700819 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700820 IndirectReferenceTable& locals = soa.Env()->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700821
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700822 uint32_t cookie = soa.Env()->local_ref_cookie;
823 IndirectRef ref = locals.Add(cookie, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700824 return reinterpret_cast<jobject>(ref);
825 }
826
827 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700828 if (obj == NULL) {
829 return;
830 }
Ian Rogersef28b142012-11-30 14:22:18 -0800831 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700832
Ian Rogersef28b142012-11-30 14:22:18 -0800833 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700834 if (!locals.Remove(cookie, obj)) {
835 // Attempting to delete a local reference that is not in the
836 // topmost local reference frame is a no-op. DeleteLocalRef returns
837 // void and doesn't throw any exceptions, but we should probably
838 // complain about it so the user will notice that things aren't
839 // going quite the way they expect.
840 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
841 << "failed to find entry";
842 }
843 }
844
845 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700846 ScopedObjectAccess soa(env);
Ian Rogers120f1c72012-09-28 17:17:10 -0700847 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700848 }
849
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700850 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700851 ScopedObjectAccess soa(env);
852 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700853 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700854 return NULL;
855 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700856 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700857 }
858
Elliott Hughese84278b2012-03-22 10:06:53 -0700859 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700860 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700861 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -0700862 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700863 va_end(args);
864 return result;
865 }
866
Elliott Hughes72025e52011-08-23 17:50:30 -0700867 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700868 ScopedObjectAccess soa(env);
869 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700870 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700871 return NULL;
872 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700873 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700874 if (result == NULL) {
875 return NULL;
876 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700877 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700878 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700879 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700880 return local_result;
881 } else {
882 return NULL;
883 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700884 }
885
Elliott Hughes72025e52011-08-23 17:50:30 -0700886 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700887 ScopedObjectAccess soa(env);
888 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700889 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700890 return NULL;
891 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700892 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700893 if (result == NULL) {
894 return NULL;
895 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700896 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700897 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700898 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700899 return local_result;
900 } else {
901 return NULL;
902 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700903 }
904
Elliott Hughescdf53122011-08-19 15:46:09 -0700905 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700906 ScopedObjectAccess soa(env);
907 return FindMethodID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700908 }
909
910 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700911 ScopedObjectAccess soa(env);
912 return FindMethodID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700913 }
914
Elliott Hughes72025e52011-08-23 17:50:30 -0700915 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700916 va_list ap;
917 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700918 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700919 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700920 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700921 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700922 }
923
Elliott Hughes72025e52011-08-23 17:50:30 -0700924 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700925 ScopedObjectAccess soa(env);
926 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
927 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700928 }
929
Elliott Hughes72025e52011-08-23 17:50:30 -0700930 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700931 ScopedObjectAccess soa(env);
932 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
933 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700934 }
935
Elliott Hughes72025e52011-08-23 17:50:30 -0700936 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700937 va_list ap;
938 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700939 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700940 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700941 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700942 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700943 }
944
Elliott Hughes72025e52011-08-23 17:50:30 -0700945 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700946 ScopedObjectAccess soa(env);
947 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700948 }
949
Elliott Hughes72025e52011-08-23 17:50:30 -0700950 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700951 ScopedObjectAccess soa(env);
952 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700953 }
954
Elliott Hughes72025e52011-08-23 17:50:30 -0700955 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700956 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700957 va_list ap;
958 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700959 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700960 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700961 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700962 }
963
Elliott Hughes72025e52011-08-23 17:50:30 -0700964 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700965 ScopedObjectAccess soa(env);
966 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700967 }
968
Elliott Hughes72025e52011-08-23 17:50:30 -0700969 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700970 ScopedObjectAccess soa(env);
971 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700972 }
973
Elliott Hughes72025e52011-08-23 17:50:30 -0700974 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700975 va_list ap;
976 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700977 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700978 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700979 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700980 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700981 }
982
Elliott Hughes72025e52011-08-23 17:50:30 -0700983 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700984 ScopedObjectAccess soa(env);
985 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 }
987
Elliott Hughes72025e52011-08-23 17:50:30 -0700988 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700989 ScopedObjectAccess soa(env);
990 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 }
992
Elliott Hughes72025e52011-08-23 17:50:30 -0700993 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700994 va_list ap;
995 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700996 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700997 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700999 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001000 }
1001
Elliott Hughes72025e52011-08-23 17:50:30 -07001002 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001003 ScopedObjectAccess soa(env);
1004 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 }
1006
Elliott Hughes72025e52011-08-23 17:50:30 -07001007 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001008 ScopedObjectAccess soa(env);
1009 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001010 }
1011
Elliott Hughes72025e52011-08-23 17:50:30 -07001012 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001013 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 va_list ap;
1015 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001016 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001018 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 }
1020
Elliott Hughes72025e52011-08-23 17:50:30 -07001021 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001022 ScopedObjectAccess soa(env);
1023 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 }
1025
Elliott Hughes72025e52011-08-23 17:50:30 -07001026 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001027 ScopedObjectAccess soa(env);
1028 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001029 }
1030
Elliott Hughes72025e52011-08-23 17:50:30 -07001031 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001032 va_list ap;
1033 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001034 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001035 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001036 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001037 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001038 }
1039
Elliott Hughes72025e52011-08-23 17:50:30 -07001040 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001041 ScopedObjectAccess soa(env);
1042 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 }
1044
Elliott Hughes72025e52011-08-23 17:50:30 -07001045 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001046 ScopedObjectAccess soa(env);
1047 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 }
1049
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001051 va_list ap;
1052 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001053 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001054 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001056 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 }
1058
Elliott Hughes72025e52011-08-23 17:50:30 -07001059 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001060 ScopedObjectAccess soa(env);
1061 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 }
1063
Elliott Hughes72025e52011-08-23 17:50:30 -07001064 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001065 ScopedObjectAccess soa(env);
1066 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001067 }
1068
Elliott Hughes72025e52011-08-23 17:50:30 -07001069 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001070 va_list ap;
1071 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001072 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001073 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001074 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001075 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 }
1077
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001079 ScopedObjectAccess soa(env);
1080 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 }
1082
Elliott Hughes72025e52011-08-23 17:50:30 -07001083 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001084 ScopedObjectAccess soa(env);
1085 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 }
1087
Elliott Hughes72025e52011-08-23 17:50:30 -07001088 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001089 va_list ap;
1090 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001091 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001092 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001093 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001094 }
1095
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001097 ScopedObjectAccess soa(env);
1098 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 }
1100
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001102 ScopedObjectAccess soa(env);
1103 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001104 }
1105
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001106 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001108 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001109 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001110 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1111 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 va_end(ap);
1113 return local_result;
1114 }
1115
1116 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001117 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001118 ScopedObjectAccess soa(env);
1119 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1120 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 }
1122
1123 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001124 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001125 ScopedObjectAccess soa(env);
1126 JValue result(InvokeWithJValues(soa, obj, mid, args));
1127 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 }
1129
1130 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001131 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001134 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001135 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001137 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 }
1139
1140 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001141 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001142 ScopedObjectAccess soa(env);
1143 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 }
1145
1146 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001147 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001148 ScopedObjectAccess soa(env);
1149 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 }
1151
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001152 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001153 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001154 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001155 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001156 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001157 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001158 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 }
1160
1161 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001162 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001163 ScopedObjectAccess soa(env);
1164 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 }
1166
1167 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001168 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001169 ScopedObjectAccess soa(env);
1170 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 }
1172
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001173 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001174 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001176 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001177 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001179 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 }
1181
1182 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001183 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001184 ScopedObjectAccess soa(env);
1185 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001186 }
1187
1188 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001189 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001190 ScopedObjectAccess soa(env);
1191 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 }
1193
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001194 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001196 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001197 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001198 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001200 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 }
1202
1203 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001204 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001205 ScopedObjectAccess soa(env);
1206 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001207 }
1208
1209 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001210 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001211 ScopedObjectAccess soa(env);
1212 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 }
1214
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001215 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001217 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001218 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001219 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001221 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 }
1223
1224 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001225 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001226 ScopedObjectAccess soa(env);
1227 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 }
1229
1230 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001231 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001232 ScopedObjectAccess soa(env);
1233 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 }
1235
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001236 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001238 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001239 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001240 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001242 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001243 }
1244
1245 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001246 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001247 ScopedObjectAccess soa(env);
1248 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001249 }
1250
1251 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001252 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001253 ScopedObjectAccess soa(env);
1254 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 }
1256
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001257 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001259 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001260 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001261 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001263 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001264 }
1265
1266 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001267 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001268 ScopedObjectAccess soa(env);
1269 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 }
1271
1272 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001273 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001274 ScopedObjectAccess soa(env);
1275 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 }
1277
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001278 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001280 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001281 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001282 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001284 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 }
1286
1287 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001288 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001289 ScopedObjectAccess soa(env);
1290 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 }
1292
1293 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001294 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001295 ScopedObjectAccess soa(env);
1296 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001297 }
1298
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001299 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001301 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001302 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001303 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 va_end(ap);
1305 }
1306
1307 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001308 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001309 ScopedObjectAccess soa(env);
1310 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 }
1312
1313 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001314 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001315 ScopedObjectAccess soa(env);
1316 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001317 }
1318
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001319 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001320 ScopedObjectAccess soa(env);
1321 return FindFieldID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001322 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001323
1324
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001325 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001326 ScopedObjectAccess soa(env);
1327 return FindFieldID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001329
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001330 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001331 ScopedObjectAccess soa(env);
1332 Object* o = soa.Decode<Object*>(obj);
1333 Field* f = soa.DecodeField(fid);
1334 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001336
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001337 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001338 ScopedObjectAccess soa(env);
1339 Field* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001340 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 }
1342
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001343 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001344 ScopedObjectAccess soa(env);
1345 Object* o = soa.Decode<Object*>(java_object);
1346 Object* v = soa.Decode<Object*>(java_value);
1347 Field* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001348 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 }
1350
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001351 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001352 ScopedObjectAccess soa(env);
1353 Object* v = soa.Decode<Object*>(java_value);
1354 Field* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001355 f->SetObject(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 }
1357
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001358#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001359 ScopedObjectAccess soa(env); \
1360 Object* o = soa.Decode<Object*>(instance); \
1361 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001362 return f->fn(o)
1363
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001364#define GET_STATIC_PRIMITIVE_FIELD(fn) \
1365 ScopedObjectAccess soa(env); \
1366 Field* f = soa.DecodeField(fid); \
1367 return f->fn(f->GetDeclaringClass())
1368
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001369#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001370 ScopedObjectAccess soa(env); \
1371 Object* o = soa.Decode<Object*>(instance); \
1372 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001373 f->fn(o, value)
1374
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001375#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
1376 ScopedObjectAccess soa(env); \
1377 Field* f = soa.DecodeField(fid); \
1378 f->fn(f->GetDeclaringClass(), value)
1379
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001380 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1381 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001382 }
1383
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001384 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1385 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 }
1387
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001388 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1389 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001390 }
1391
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001392 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1393 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 }
1395
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001396 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1397 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 }
1399
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001400 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1401 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001402 }
1403
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001404 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1405 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 }
1407
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001408 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1409 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001410 }
1411
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001412 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001413 GET_STATIC_PRIMITIVE_FIELD(GetBoolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001414 }
1415
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001416 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001417 GET_STATIC_PRIMITIVE_FIELD(GetByte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 }
1419
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001420 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001421 GET_STATIC_PRIMITIVE_FIELD(GetChar);
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 }
1423
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001424 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001425 GET_STATIC_PRIMITIVE_FIELD(GetShort);
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 }
1427
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001428 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001429 GET_STATIC_PRIMITIVE_FIELD(GetInt);
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 }
1431
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001432 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001433 GET_STATIC_PRIMITIVE_FIELD(GetLong);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001434 }
1435
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001436 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001437 GET_STATIC_PRIMITIVE_FIELD(GetFloat);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001438 }
1439
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001440 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001441 GET_STATIC_PRIMITIVE_FIELD(GetDouble);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001442 }
1443
1444 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1445 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1446 }
1447
1448 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1449 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1450 }
1451
1452 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1453 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1454 }
1455
1456 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1457 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1458 }
1459
1460 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1461 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1462 }
1463
1464 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1465 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1466 }
1467
1468 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1469 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1470 }
1471
1472 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1473 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1474 }
1475
1476 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001477 SET_STATIC_PRIMITIVE_FIELD(SetBoolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001478 }
1479
1480 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001481 SET_STATIC_PRIMITIVE_FIELD(SetByte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001482 }
1483
1484 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001485 SET_STATIC_PRIMITIVE_FIELD(SetChar, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001486 }
1487
1488 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001489 SET_STATIC_PRIMITIVE_FIELD(SetFloat, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001490 }
1491
1492 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001493 SET_STATIC_PRIMITIVE_FIELD(SetDouble, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001494 }
1495
1496 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001497 SET_STATIC_PRIMITIVE_FIELD(SetInt, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001498 }
1499
1500 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001501 SET_STATIC_PRIMITIVE_FIELD(SetLong, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001502 }
1503
1504 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001505 SET_STATIC_PRIMITIVE_FIELD(SetShort, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001506 }
1507
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001508 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001509 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001510 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001511 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001512 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1513 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001514 va_end(ap);
1515 return local_result;
1516 }
1517
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001518 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001519 ScopedObjectAccess soa(env);
1520 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1521 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001522 }
1523
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001524 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001525 ScopedObjectAccess soa(env);
1526 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1527 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001528 }
1529
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001530 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001531 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001532 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001533 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001534 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001535 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001536 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001537 }
1538
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001539 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001540 ScopedObjectAccess soa(env);
1541 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 }
1543
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001544 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001545 ScopedObjectAccess soa(env);
1546 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001547 }
1548
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001549 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001550 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001551 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001552 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001553 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001554 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001555 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001556 }
1557
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001558 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001559 ScopedObjectAccess soa(env);
1560 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001561 }
1562
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001563 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001564 ScopedObjectAccess soa(env);
1565 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 }
1567
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001568 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001569 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001570 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001571 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001572 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001574 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001575 }
1576
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001577 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001578 ScopedObjectAccess soa(env);
1579 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001580 }
1581
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001582 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001583 ScopedObjectAccess soa(env);
1584 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 }
1586
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001587 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001588 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001589 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001590 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001591 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001592 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001593 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001594 }
1595
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001596 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001597 ScopedObjectAccess soa(env);
1598 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 }
1600
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001601 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001602 ScopedObjectAccess soa(env);
1603 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001604 }
1605
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001606 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001608 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001609 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001610 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001612 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001613 }
1614
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001615 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001616 ScopedObjectAccess soa(env);
1617 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 }
1619
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001620 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001621 ScopedObjectAccess soa(env);
1622 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001623 }
1624
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001625 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001626 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001627 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001628 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001629 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001630 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001631 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 }
1633
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001634 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001635 ScopedObjectAccess soa(env);
1636 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 }
1638
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001639 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001640 ScopedObjectAccess soa(env);
1641 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 }
1643
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001644 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001645 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001646 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001647 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001648 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001650 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 }
1652
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001653 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001654 ScopedObjectAccess soa(env);
1655 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001656 }
1657
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001658 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001659 ScopedObjectAccess soa(env);
1660 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001661 }
1662
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001663 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001664 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001665 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001666 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001667 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001668 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001669 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 }
1671
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001672 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001673 ScopedObjectAccess soa(env);
1674 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 }
1676
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001677 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001678 ScopedObjectAccess soa(env);
1679 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001680 }
1681
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001682 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001683 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001684 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001685 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001686 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 va_end(ap);
1688 }
1689
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001690 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001691 ScopedObjectAccess soa(env);
1692 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001693 }
1694
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001695 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001696 ScopedObjectAccess soa(env);
1697 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 }
1699
Elliott Hughes814e4032011-08-23 12:07:56 -07001700 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001701 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001702 String* result = String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001703 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001704 }
1705
1706 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001707 if (utf == NULL) {
1708 return NULL;
1709 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001710 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001711 String* result = String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001712 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001713 }
1714
Elliott Hughes814e4032011-08-23 12:07:56 -07001715 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001716 ScopedObjectAccess soa(env);
1717 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001718 }
1719
1720 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001721 ScopedObjectAccess soa(env);
1722 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001723 }
1724
Elliott Hughesb465ab02011-08-24 11:21:21 -07001725 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001726 ScopedObjectAccess soa(env);
1727 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001728 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001729 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001730 } else {
1731 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1732 memcpy(buf, chars + start, length * sizeof(jchar));
1733 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001734 }
1735
Elliott Hughesb465ab02011-08-24 11:21:21 -07001736 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001737 ScopedObjectAccess soa(env);
1738 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001739 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001740 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001741 } else {
1742 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1743 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1744 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001745 }
1746
Elliott Hughes75770752011-08-24 17:52:38 -07001747 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001748 ScopedObjectAccess soa(env);
1749 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001750 const CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001751 PinPrimitiveArray(soa, chars);
Elliott Hughes75770752011-08-24 17:52:38 -07001752 if (is_copy != NULL) {
1753 *is_copy = JNI_FALSE;
1754 }
1755 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001756 }
1757
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001758 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001759 ScopedObjectAccess soa(env);
1760 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001761 }
1762
Elliott Hughes75770752011-08-24 17:52:38 -07001763 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001764 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001765 }
1766
Elliott Hughes75770752011-08-24 17:52:38 -07001767 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001768 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001769 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001770 }
1771
Elliott Hughes75770752011-08-24 17:52:38 -07001772 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001773 if (java_string == NULL) {
1774 return NULL;
1775 }
1776 if (is_copy != NULL) {
1777 *is_copy = JNI_TRUE;
1778 }
Ian Rogersef28b142012-11-30 14:22:18 -08001779 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001780 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001781 size_t byte_count = s->GetUtfLength();
1782 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001783 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001784 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1785 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1786 bytes[byte_count] = '\0';
1787 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001788 }
1789
Elliott Hughes75770752011-08-24 17:52:38 -07001790 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001791 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001792 }
1793
Elliott Hughesbd935992011-08-22 11:59:34 -07001794 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001795 ScopedObjectAccess soa(env);
1796 Object* obj = soa.Decode<Object*>(java_array);
Elliott Hughes96a98872012-12-19 14:21:15 -08001797 if (!obj->IsArrayInstance()) {
1798 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1799 }
Elliott Hughesbd935992011-08-22 11:59:34 -07001800 Array* array = obj->AsArray();
1801 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001802 }
1803
Elliott Hughes814e4032011-08-23 12:07:56 -07001804 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001805 ScopedObjectAccess soa(env);
1806 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1807 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001808 }
1809
1810 static void SetObjectArrayElement(JNIEnv* env,
1811 jobjectArray java_array, jsize index, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001812 ScopedObjectAccess soa(env);
1813 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1814 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 array->Set(index, value);
1816 }
1817
1818 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001819 ScopedObjectAccess soa(env);
1820 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001821 }
1822
1823 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001824 ScopedObjectAccess soa(env);
1825 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001826 }
1827
1828 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001829 ScopedObjectAccess soa(env);
1830 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 }
1832
1833 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001834 ScopedObjectAccess soa(env);
1835 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001836 }
1837
1838 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001839 ScopedObjectAccess soa(env);
1840 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001841 }
1842
1843 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001844 ScopedObjectAccess soa(env);
1845 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001846 }
1847
1848 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001849 ScopedObjectAccess soa(env);
1850 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001851 }
1852
1853 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001854 ScopedObjectAccess soa(env);
Elliott Hughes96a98872012-12-19 14:21:15 -08001855 if (length < 0) {
1856 JniAbortF("NewObjectArray", "negative array length: %d", length);
1857 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001858
1859 // Compute the array class corresponding to the given element class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001860 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001861 std::string descriptor;
1862 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001863 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001864
1865 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001866 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1867 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001868 return NULL;
1869 }
1870
Elliott Hughes75770752011-08-24 17:52:38 -07001871 // Allocate and initialize if necessary.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001872 Class* array_class = soa.Decode<Class*>(java_array_class.get());
Ian Rogers50b35e22012-10-04 10:09:15 -07001873 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001874 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001875 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07001876 for (jsize i = 0; i < length; ++i) {
1877 result->Set(i, initial_object);
1878 }
1879 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001880 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001881 }
1882
1883 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001884 ScopedObjectAccess soa(env);
1885 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001886 }
1887
Ian Rogersa15e67d2012-02-28 13:51:55 -08001888 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001889 ScopedObjectAccess soa(env);
1890 Array* array = soa.Decode<Array*>(java_array);
1891 PinPrimitiveArray(soa, array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001892 if (is_copy != NULL) {
1893 *is_copy = JNI_FALSE;
1894 }
1895 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001896 }
1897
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001898 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001899 ReleasePrimitiveArray(env, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001900 }
1901
Elliott Hughes75770752011-08-24 17:52:38 -07001902 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001903 ScopedObjectAccess soa(env);
1904 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001905 }
1906
Elliott Hughes75770752011-08-24 17:52:38 -07001907 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001908 ScopedObjectAccess soa(env);
1909 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001910 }
1911
Elliott Hughes75770752011-08-24 17:52:38 -07001912 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001913 ScopedObjectAccess soa(env);
1914 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001915 }
1916
Elliott Hughes75770752011-08-24 17:52:38 -07001917 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001918 ScopedObjectAccess soa(env);
1919 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001920 }
1921
Elliott Hughes75770752011-08-24 17:52:38 -07001922 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001923 ScopedObjectAccess soa(env);
1924 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001925 }
1926
Elliott Hughes75770752011-08-24 17:52:38 -07001927 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001928 ScopedObjectAccess soa(env);
1929 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001930 }
1931
Elliott Hughes75770752011-08-24 17:52:38 -07001932 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001933 ScopedObjectAccess soa(env);
1934 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001935 }
1936
Elliott Hughes75770752011-08-24 17:52:38 -07001937 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001938 ScopedObjectAccess soa(env);
1939 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001940 }
1941
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001942 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001943 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001944 }
1945
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001946 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001947 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001948 }
1949
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001950 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001951 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001952 }
1953
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001954 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001955 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001956 }
1957
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001958 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001959 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001960 }
1961
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001962 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001963 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001964 }
1965
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001966 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001967 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001968 }
1969
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001970 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001971 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001972 }
1973
Elliott Hughes814e4032011-08-23 12:07:56 -07001974 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001975 ScopedObjectAccess soa(env);
1976 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001977 }
1978
Elliott Hughes814e4032011-08-23 12:07:56 -07001979 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001980 ScopedObjectAccess soa(env);
1981 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 }
1983
Elliott Hughes814e4032011-08-23 12:07:56 -07001984 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001985 ScopedObjectAccess soa(env);
1986 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 }
1988
Elliott Hughes814e4032011-08-23 12:07:56 -07001989 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001990 ScopedObjectAccess soa(env);
1991 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 }
1993
Elliott Hughes814e4032011-08-23 12:07:56 -07001994 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001995 ScopedObjectAccess soa(env);
1996 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001997 }
1998
Elliott Hughes814e4032011-08-23 12:07:56 -07001999 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002000 ScopedObjectAccess soa(env);
2001 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 }
2003
Elliott Hughes814e4032011-08-23 12:07:56 -07002004 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002005 ScopedObjectAccess soa(env);
2006 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002007 }
2008
Elliott Hughes814e4032011-08-23 12:07:56 -07002009 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002010 ScopedObjectAccess soa(env);
2011 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 }
2013
Elliott Hughes814e4032011-08-23 12:07:56 -07002014 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002015 ScopedObjectAccess soa(env);
2016 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 }
2018
Elliott Hughes814e4032011-08-23 12:07:56 -07002019 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002020 ScopedObjectAccess soa(env);
2021 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 }
2023
Elliott Hughes814e4032011-08-23 12:07:56 -07002024 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002025 ScopedObjectAccess soa(env);
2026 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 }
2028
Elliott Hughes814e4032011-08-23 12:07:56 -07002029 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002030 ScopedObjectAccess soa(env);
2031 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 }
2033
Elliott Hughes814e4032011-08-23 12:07:56 -07002034 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002035 ScopedObjectAccess soa(env);
2036 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 }
2038
Elliott Hughes814e4032011-08-23 12:07:56 -07002039 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002040 ScopedObjectAccess soa(env);
2041 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 }
2043
Elliott Hughes814e4032011-08-23 12:07:56 -07002044 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002045 ScopedObjectAccess soa(env);
2046 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 }
2048
Elliott Hughes814e4032011-08-23 12:07:56 -07002049 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002050 ScopedObjectAccess soa(env);
2051 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 }
2053
Elliott Hughes5174fe62011-08-23 15:12:35 -07002054 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002055 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2056 }
2057
2058 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 -07002059 ScopedObjectAccess soa(env);
2060 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002061
Elliott Hughesc8fece32013-01-02 11:27:23 -08002062 for (size_t i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 const char* name = methods[i].name;
2064 const char* sig = methods[i].signature;
2065
2066 if (*sig == '!') {
2067 // TODO: fast jni. it's too noisy to log all these.
2068 ++sig;
2069 }
2070
Mathieu Chartier66f19252012-09-18 08:57:04 -07002071 AbstractMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002072 if (m == NULL) {
2073 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002074 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002075 if (m == NULL) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002076 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
2077 << PrettyDescriptor(c) << "." << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002078 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002080 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002081 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
2082 << PrettyDescriptor(c) << "." << name << sig
2083 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002084 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 return JNI_ERR;
2086 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002087
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002088 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002089
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002090 m->RegisterNative(soa.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 }
2092 return JNI_OK;
2093 }
2094
Elliott Hughes5174fe62011-08-23 15:12:35 -07002095 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002096 ScopedObjectAccess soa(env);
2097 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002098
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002099 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002100
2101 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002102 AbstractMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002103 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002104 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002105 }
2106 }
2107 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002108 AbstractMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002109 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002110 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002111 }
2112 }
2113
2114 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002115 }
2116
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002117 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2118 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
2119 ScopedObjectAccess soa(env);
2120 Object* o = soa.Decode<Object*>(java_object);
2121 o->MonitorEnter(soa.Self());
2122 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002123 return JNI_ERR;
2124 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002125 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002126 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 }
2128
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002129 static jint MonitorExit(JNIEnv* env, jobject java_object)
2130 UNLOCK_FUNCTION(monitor_lock_) {
2131 ScopedObjectAccess soa(env);
2132 Object* o = soa.Decode<Object*>(java_object);
2133 o->MonitorExit(soa.Self());
2134 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002135 return JNI_ERR;
2136 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002137 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002138 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 }
2140
2141 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 Runtime* runtime = Runtime::Current();
2143 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002144 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002145 } else {
2146 *vm = NULL;
2147 }
2148 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2149 }
2150
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002152 if (capacity < 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002153 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002154 }
Elliott Hughes11a796e2012-12-19 14:42:57 -08002155 if (address == NULL && capacity != 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002156 JniAbortF("NewDirectByteBuffer", "non-zero capacity for NULL pointer: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002157 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002158
Elliott Hughes96a98872012-12-19 14:21:15 -08002159 // At the moment, the Java side is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002160 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2161 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002162 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002163 jint capacity_arg = static_cast<jint>(capacity);
2164
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002165 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2166 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002167 address_arg, capacity_arg);
Ian Rogersef28b142012-11-30 14:22:18 -08002168 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 }
2170
Elliott Hughesb465ab02011-08-24 11:21:21 -07002171 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002172 return reinterpret_cast<void*>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002173 }
2174
Elliott Hughesb465ab02011-08-24 11:21:21 -07002175 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002176 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002177 }
2178
Elliott Hughesb465ab02011-08-24 11:21:21 -07002179 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002180 if (java_object == NULL) {
2181 JniAbortF("GetObjectRefType", "null object");
2182 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002183
2184 // Do we definitely know what kind of reference this is?
2185 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2186 IndirectRefKind kind = GetIndirectRefKind(ref);
2187 switch (kind) {
2188 case kLocal:
Ian Rogersef28b142012-11-30 14:22:18 -08002189 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002190 return JNILocalRefType;
2191 }
2192 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002193 case kGlobal:
2194 return JNIGlobalRefType;
2195 case kWeakGlobal:
2196 return JNIWeakGlobalRefType;
2197 case kSirtOrInvalid:
2198 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002199 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002200 return JNILocalRefType;
2201 }
2202
Ian Rogersef28b142012-11-30 14:22:18 -08002203 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002204 return JNIInvalidRefType;
2205 }
2206
Elliott Hughesb465ab02011-08-24 11:21:21 -07002207 // If we're handing out direct pointers, check whether it's a direct pointer
2208 // to a local reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002209 {
2210 ScopedObjectAccess soa(env);
2211 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2212 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
2213 return JNILocalRefType;
2214 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002215 }
2216 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002217 return JNIInvalidRefType;
2218 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002219 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2220 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002221 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002222
2223 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002224 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2225 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002226 // TODO: we should try to expand the table if necessary.
2227 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
2228 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2229 return JNI_ERR;
2230 }
2231 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002232 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002233 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2234 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002235 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002236 soa.Self()->ThrowOutOfMemoryError(caller);
2237 }
2238 return okay ? JNI_OK : JNI_ERR;
2239 }
2240
2241 template<typename JniT, typename ArtT>
2242 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002243 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002244 if (length < 0) {
2245 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
2246 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002247 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002248 return soa.AddLocalReference<JniT>(result);
2249 }
2250
2251 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2252 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2253 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002254 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002255 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2256 PinPrimitiveArray(soa, array);
2257 if (is_copy != NULL) {
2258 *is_copy = JNI_FALSE;
2259 }
2260 return array->GetData();
2261 }
2262
2263 template <typename ArrayT>
Ian Rogersef28b142012-11-30 14:22:18 -08002264 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002265 if (mode != JNI_COMMIT) {
Ian Rogersef28b142012-11-30 14:22:18 -08002266 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002267 Array* array = soa.Decode<Array*>(java_array);
2268 UnpinPrimitiveArray(soa, array);
2269 }
2270 }
2271
2272 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2273 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2274 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002275 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002276 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2277 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2278 ThrowAIOOBE(soa, array, start, length, "src");
2279 } else {
2280 JavaT* data = array->GetData();
2281 memcpy(buf, data + start, length * sizeof(JavaT));
2282 }
2283 }
2284
2285 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2286 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2287 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002288 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002289 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2290 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2291 ThrowAIOOBE(soa, array, start, length, "dst");
2292 } else {
2293 JavaT* data = array->GetData();
2294 memcpy(data + start, buf, length * sizeof(JavaT));
2295 }
2296 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002297};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002298
Elliott Hughes88c5c352012-03-15 18:49:48 -07002299const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002300 NULL, // reserved0.
2301 NULL, // reserved1.
2302 NULL, // reserved2.
2303 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002304 JNI::GetVersion,
2305 JNI::DefineClass,
2306 JNI::FindClass,
2307 JNI::FromReflectedMethod,
2308 JNI::FromReflectedField,
2309 JNI::ToReflectedMethod,
2310 JNI::GetSuperclass,
2311 JNI::IsAssignableFrom,
2312 JNI::ToReflectedField,
2313 JNI::Throw,
2314 JNI::ThrowNew,
2315 JNI::ExceptionOccurred,
2316 JNI::ExceptionDescribe,
2317 JNI::ExceptionClear,
2318 JNI::FatalError,
2319 JNI::PushLocalFrame,
2320 JNI::PopLocalFrame,
2321 JNI::NewGlobalRef,
2322 JNI::DeleteGlobalRef,
2323 JNI::DeleteLocalRef,
2324 JNI::IsSameObject,
2325 JNI::NewLocalRef,
2326 JNI::EnsureLocalCapacity,
2327 JNI::AllocObject,
2328 JNI::NewObject,
2329 JNI::NewObjectV,
2330 JNI::NewObjectA,
2331 JNI::GetObjectClass,
2332 JNI::IsInstanceOf,
2333 JNI::GetMethodID,
2334 JNI::CallObjectMethod,
2335 JNI::CallObjectMethodV,
2336 JNI::CallObjectMethodA,
2337 JNI::CallBooleanMethod,
2338 JNI::CallBooleanMethodV,
2339 JNI::CallBooleanMethodA,
2340 JNI::CallByteMethod,
2341 JNI::CallByteMethodV,
2342 JNI::CallByteMethodA,
2343 JNI::CallCharMethod,
2344 JNI::CallCharMethodV,
2345 JNI::CallCharMethodA,
2346 JNI::CallShortMethod,
2347 JNI::CallShortMethodV,
2348 JNI::CallShortMethodA,
2349 JNI::CallIntMethod,
2350 JNI::CallIntMethodV,
2351 JNI::CallIntMethodA,
2352 JNI::CallLongMethod,
2353 JNI::CallLongMethodV,
2354 JNI::CallLongMethodA,
2355 JNI::CallFloatMethod,
2356 JNI::CallFloatMethodV,
2357 JNI::CallFloatMethodA,
2358 JNI::CallDoubleMethod,
2359 JNI::CallDoubleMethodV,
2360 JNI::CallDoubleMethodA,
2361 JNI::CallVoidMethod,
2362 JNI::CallVoidMethodV,
2363 JNI::CallVoidMethodA,
2364 JNI::CallNonvirtualObjectMethod,
2365 JNI::CallNonvirtualObjectMethodV,
2366 JNI::CallNonvirtualObjectMethodA,
2367 JNI::CallNonvirtualBooleanMethod,
2368 JNI::CallNonvirtualBooleanMethodV,
2369 JNI::CallNonvirtualBooleanMethodA,
2370 JNI::CallNonvirtualByteMethod,
2371 JNI::CallNonvirtualByteMethodV,
2372 JNI::CallNonvirtualByteMethodA,
2373 JNI::CallNonvirtualCharMethod,
2374 JNI::CallNonvirtualCharMethodV,
2375 JNI::CallNonvirtualCharMethodA,
2376 JNI::CallNonvirtualShortMethod,
2377 JNI::CallNonvirtualShortMethodV,
2378 JNI::CallNonvirtualShortMethodA,
2379 JNI::CallNonvirtualIntMethod,
2380 JNI::CallNonvirtualIntMethodV,
2381 JNI::CallNonvirtualIntMethodA,
2382 JNI::CallNonvirtualLongMethod,
2383 JNI::CallNonvirtualLongMethodV,
2384 JNI::CallNonvirtualLongMethodA,
2385 JNI::CallNonvirtualFloatMethod,
2386 JNI::CallNonvirtualFloatMethodV,
2387 JNI::CallNonvirtualFloatMethodA,
2388 JNI::CallNonvirtualDoubleMethod,
2389 JNI::CallNonvirtualDoubleMethodV,
2390 JNI::CallNonvirtualDoubleMethodA,
2391 JNI::CallNonvirtualVoidMethod,
2392 JNI::CallNonvirtualVoidMethodV,
2393 JNI::CallNonvirtualVoidMethodA,
2394 JNI::GetFieldID,
2395 JNI::GetObjectField,
2396 JNI::GetBooleanField,
2397 JNI::GetByteField,
2398 JNI::GetCharField,
2399 JNI::GetShortField,
2400 JNI::GetIntField,
2401 JNI::GetLongField,
2402 JNI::GetFloatField,
2403 JNI::GetDoubleField,
2404 JNI::SetObjectField,
2405 JNI::SetBooleanField,
2406 JNI::SetByteField,
2407 JNI::SetCharField,
2408 JNI::SetShortField,
2409 JNI::SetIntField,
2410 JNI::SetLongField,
2411 JNI::SetFloatField,
2412 JNI::SetDoubleField,
2413 JNI::GetStaticMethodID,
2414 JNI::CallStaticObjectMethod,
2415 JNI::CallStaticObjectMethodV,
2416 JNI::CallStaticObjectMethodA,
2417 JNI::CallStaticBooleanMethod,
2418 JNI::CallStaticBooleanMethodV,
2419 JNI::CallStaticBooleanMethodA,
2420 JNI::CallStaticByteMethod,
2421 JNI::CallStaticByteMethodV,
2422 JNI::CallStaticByteMethodA,
2423 JNI::CallStaticCharMethod,
2424 JNI::CallStaticCharMethodV,
2425 JNI::CallStaticCharMethodA,
2426 JNI::CallStaticShortMethod,
2427 JNI::CallStaticShortMethodV,
2428 JNI::CallStaticShortMethodA,
2429 JNI::CallStaticIntMethod,
2430 JNI::CallStaticIntMethodV,
2431 JNI::CallStaticIntMethodA,
2432 JNI::CallStaticLongMethod,
2433 JNI::CallStaticLongMethodV,
2434 JNI::CallStaticLongMethodA,
2435 JNI::CallStaticFloatMethod,
2436 JNI::CallStaticFloatMethodV,
2437 JNI::CallStaticFloatMethodA,
2438 JNI::CallStaticDoubleMethod,
2439 JNI::CallStaticDoubleMethodV,
2440 JNI::CallStaticDoubleMethodA,
2441 JNI::CallStaticVoidMethod,
2442 JNI::CallStaticVoidMethodV,
2443 JNI::CallStaticVoidMethodA,
2444 JNI::GetStaticFieldID,
2445 JNI::GetStaticObjectField,
2446 JNI::GetStaticBooleanField,
2447 JNI::GetStaticByteField,
2448 JNI::GetStaticCharField,
2449 JNI::GetStaticShortField,
2450 JNI::GetStaticIntField,
2451 JNI::GetStaticLongField,
2452 JNI::GetStaticFloatField,
2453 JNI::GetStaticDoubleField,
2454 JNI::SetStaticObjectField,
2455 JNI::SetStaticBooleanField,
2456 JNI::SetStaticByteField,
2457 JNI::SetStaticCharField,
2458 JNI::SetStaticShortField,
2459 JNI::SetStaticIntField,
2460 JNI::SetStaticLongField,
2461 JNI::SetStaticFloatField,
2462 JNI::SetStaticDoubleField,
2463 JNI::NewString,
2464 JNI::GetStringLength,
2465 JNI::GetStringChars,
2466 JNI::ReleaseStringChars,
2467 JNI::NewStringUTF,
2468 JNI::GetStringUTFLength,
2469 JNI::GetStringUTFChars,
2470 JNI::ReleaseStringUTFChars,
2471 JNI::GetArrayLength,
2472 JNI::NewObjectArray,
2473 JNI::GetObjectArrayElement,
2474 JNI::SetObjectArrayElement,
2475 JNI::NewBooleanArray,
2476 JNI::NewByteArray,
2477 JNI::NewCharArray,
2478 JNI::NewShortArray,
2479 JNI::NewIntArray,
2480 JNI::NewLongArray,
2481 JNI::NewFloatArray,
2482 JNI::NewDoubleArray,
2483 JNI::GetBooleanArrayElements,
2484 JNI::GetByteArrayElements,
2485 JNI::GetCharArrayElements,
2486 JNI::GetShortArrayElements,
2487 JNI::GetIntArrayElements,
2488 JNI::GetLongArrayElements,
2489 JNI::GetFloatArrayElements,
2490 JNI::GetDoubleArrayElements,
2491 JNI::ReleaseBooleanArrayElements,
2492 JNI::ReleaseByteArrayElements,
2493 JNI::ReleaseCharArrayElements,
2494 JNI::ReleaseShortArrayElements,
2495 JNI::ReleaseIntArrayElements,
2496 JNI::ReleaseLongArrayElements,
2497 JNI::ReleaseFloatArrayElements,
2498 JNI::ReleaseDoubleArrayElements,
2499 JNI::GetBooleanArrayRegion,
2500 JNI::GetByteArrayRegion,
2501 JNI::GetCharArrayRegion,
2502 JNI::GetShortArrayRegion,
2503 JNI::GetIntArrayRegion,
2504 JNI::GetLongArrayRegion,
2505 JNI::GetFloatArrayRegion,
2506 JNI::GetDoubleArrayRegion,
2507 JNI::SetBooleanArrayRegion,
2508 JNI::SetByteArrayRegion,
2509 JNI::SetCharArrayRegion,
2510 JNI::SetShortArrayRegion,
2511 JNI::SetIntArrayRegion,
2512 JNI::SetLongArrayRegion,
2513 JNI::SetFloatArrayRegion,
2514 JNI::SetDoubleArrayRegion,
2515 JNI::RegisterNatives,
2516 JNI::UnregisterNatives,
2517 JNI::MonitorEnter,
2518 JNI::MonitorExit,
2519 JNI::GetJavaVM,
2520 JNI::GetStringRegion,
2521 JNI::GetStringUTFRegion,
2522 JNI::GetPrimitiveArrayCritical,
2523 JNI::ReleasePrimitiveArrayCritical,
2524 JNI::GetStringCritical,
2525 JNI::ReleaseStringCritical,
2526 JNI::NewWeakGlobalRef,
2527 JNI::DeleteWeakGlobalRef,
2528 JNI::ExceptionCheck,
2529 JNI::NewDirectByteBuffer,
2530 JNI::GetDirectBufferAddress,
2531 JNI::GetDirectBufferCapacity,
2532 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002533};
2534
Elliott Hughes75770752011-08-24 17:52:38 -07002535JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002536 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002537 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002538 local_ref_cookie(IRT_FIRST_SEGMENT),
2539 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002540 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002541 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002542 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002543 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002544 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002545 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002546 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002547 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2548 // errors will ensue.
2549 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2550 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002551}
2552
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002553JNIEnvExt::~JNIEnvExt() {
2554}
2555
Elliott Hughes88c5c352012-03-15 18:49:48 -07002556void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2557 check_jni = enabled;
2558 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002559}
2560
Elliott Hughes73e66f72012-05-09 09:34:45 -07002561void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2562 locals.Dump(os);
2563 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002564}
2565
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002566void JNIEnvExt::PushFrame(int /*capacity*/) {
2567 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002568 stacked_local_ref_cookies.push_back(local_ref_cookie);
2569 local_ref_cookie = locals.GetSegmentState();
2570}
2571
2572void JNIEnvExt::PopFrame() {
2573 locals.SetSegmentState(local_ref_cookie);
2574 local_ref_cookie = stacked_local_ref_cookies.back();
2575 stacked_local_ref_cookies.pop_back();
2576}
2577
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002578Offset JNIEnvExt::SegmentStateOffset() {
2579 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2580 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2581}
2582
Carl Shapiroea4dca82011-08-01 13:45:38 -07002583// JNI Invocation interface.
2584
Brian Carlstrombddf9762013-05-14 11:35:37 -07002585extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002586 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002587 if (IsBadJniVersion(args->version)) {
2588 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002589 return JNI_EVERSION;
2590 }
2591 Runtime::Options options;
2592 for (int i = 0; i < args->nOptions; ++i) {
2593 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002594 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002595 }
2596 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002597 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002598 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002599 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002600 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002601 bool started = runtime->Start();
2602 if (!started) {
2603 delete Thread::Current()->GetJniEnv();
2604 delete runtime->GetJavaVM();
2605 LOG(WARNING) << "CreateJavaVM failed";
2606 return JNI_ERR;
2607 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002608 *p_env = Thread::Current()->GetJniEnv();
2609 *p_vm = runtime->GetJavaVM();
2610 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002611}
2612
Elliott Hughesf2682d52011-08-15 16:37:04 -07002613extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002614 Runtime* runtime = Runtime::Current();
2615 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002616 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002617 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002618 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002619 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002620 }
2621 return JNI_OK;
2622}
2623
2624// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002625extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002626 return JNI_ERR;
2627}
2628
Elliott Hughescdf53122011-08-19 15:46:09 -07002629class JII {
2630 public:
2631 static jint DestroyJavaVM(JavaVM* vm) {
2632 if (vm == NULL) {
2633 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002634 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002635 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2636 delete raw_vm->runtime;
2637 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002638 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002639
Elliott Hughescdf53122011-08-19 15:46:09 -07002640 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002641 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002642 }
2643
2644 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002645 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002646 }
2647
2648 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002649 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002650 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002651 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002652 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2653 Runtime* runtime = raw_vm->runtime;
2654 runtime->DetachCurrentThread();
2655 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002656 }
2657
2658 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes83a25322013-03-14 11:18:53 -07002659 if (IsBadJniVersion(version)) {
2660 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07002661 return JNI_EVERSION;
2662 }
2663 if (vm == NULL || env == NULL) {
2664 return JNI_ERR;
2665 }
2666 Thread* thread = Thread::Current();
2667 if (thread == NULL) {
2668 *env = NULL;
2669 return JNI_EDETACHED;
2670 }
2671 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002672 return JNI_OK;
2673 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002674};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002675
Elliott Hughes88c5c352012-03-15 18:49:48 -07002676const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002677 NULL, // reserved0
2678 NULL, // reserved1
2679 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002680 JII::DestroyJavaVM,
2681 JII::AttachCurrentThread,
2682 JII::DetachCurrentThread,
2683 JII::GetEnv,
2684 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002685};
2686
Elliott Hughesa0957642011-09-02 14:27:33 -07002687JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002688 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002689 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002690 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002691 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002692 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002693 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002694 work_around_app_jni_bugs(false),
Ian Rogers62d6c772013-02-27 08:32:07 -08002695 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002696 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002697 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002698 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002699 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002700 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002701 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Elliott Hughes79082e32011-08-25 12:07:32 -07002702 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002703 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002704 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002705 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002706 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002707}
2708
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002709JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002710 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002711}
2712
Elliott Hughes88c5c352012-03-15 18:49:48 -07002713void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2714 check_jni = enabled;
2715 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002716}
2717
Elliott Hughesae80b492012-04-24 10:43:17 -07002718void JavaVMExt::DumpForSigQuit(std::ostream& os) {
2719 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
2720 if (force_copy) {
2721 os << " (with forcecopy)";
2722 }
2723 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07002724 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07002725 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002726 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002727 os << "; pins=" << pin_table.Size();
2728 }
2729 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002730 MutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002731 os << "; globals=" << globals.Capacity();
2732 }
2733 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002734 MutexLock mu(self, weak_globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002735 if (weak_globals.Capacity() > 0) {
2736 os << " (plus " << weak_globals.Capacity() << " weak)";
2737 }
2738 }
2739 os << '\n';
2740
2741 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002742 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002743 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
2744 }
2745}
2746
Elliott Hughes73e66f72012-05-09 09:34:45 -07002747void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002748 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002749 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002750 MutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002751 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002752 }
2753 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002754 MutexLock mu(self, weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002755 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002756 }
2757 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002758 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002759 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002760 }
2761}
2762
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002763bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
2764 std::string& detail) {
Elliott Hughes75770752011-08-24 17:52:38 -07002765 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002766
2767 // See if we've already loaded this library. If we have, and the class loader
2768 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002769 // TODO: for better results we should canonicalize the pathname (or even compare
2770 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002771 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07002772 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07002773 {
2774 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07002775 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002776 library = libraries->Get(path);
2777 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002778 if (library != NULL) {
2779 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002780 // The library will be associated with class_loader. The JNI
2781 // spec says we can't load the same library into more than one
2782 // class loader.
2783 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2784 "ClassLoader %p; can't open in ClassLoader %p",
2785 path.c_str(), library->GetClassLoader(), class_loader);
2786 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002787 return false;
2788 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002789 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2790 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002791 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002792 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2793 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002794 return false;
2795 }
2796 return true;
2797 }
2798
2799 // Open the shared library. Because we're using a full path, the system
2800 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2801 // resolve this library's dependencies though.)
2802
2803 // Failures here are expected when java.library.path has several entries
2804 // and we have to hunt for the lib.
2805
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002806 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
2807 // class unloading. Libraries will only be unloaded when the reference count (incremented by
2808 // dlopen) becomes zero from dlclose.
2809
Elliott Hughescdf53122011-08-19 15:46:09 -07002810 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002811 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002812 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
2813 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
2814 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07002815
Elliott Hughes84b2f142012-09-27 09:16:28 -07002816 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002817
2818 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002819 detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07002820 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002821 return false;
2822 }
2823
2824 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002825 // TODO: move the locking (and more of this logic) into Libraries.
2826 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002827 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002828 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002829 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002830 if (library == NULL) { // We won race to get libraries_lock
2831 library = new SharedLibrary(path, handle, class_loader);
2832 libraries->Put(path, library);
2833 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002834 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002835 }
2836 if (!created_library) {
2837 LOG(INFO) << "WOW: we lost a race to add shared library: "
2838 << "\"" << path << "\" ClassLoader=" << class_loader;
2839 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002840 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002841
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002842 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002843
2844 bool result = true;
2845 void* sym = dlsym(handle, "JNI_OnLoad");
2846 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002847 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002848 } else {
2849 // Call JNI_OnLoad. We have to override the current class
2850 // loader, which will always be "null" since the stuff at the
2851 // top of the stack is around Runtime.loadLibrary(). (See
2852 // the comments in the JNI FindClass function.)
2853 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2854 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07002855 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002856 self->SetClassLoaderOverride(class_loader);
2857
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002858 int version = 0;
2859 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002860 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002861 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002862 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002863 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002864
Brian Carlstromaded5f72011-10-07 17:15:04 -07002865 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002866
Elliott Hughes83a25322013-03-14 11:18:53 -07002867 if (IsBadJniVersion(version)) {
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07002868 StringAppendF(&detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
2869 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07002870 // It's unwise to call dlclose() here, but we can mark it
2871 // as bad and ensure that future load attempts will fail.
2872 // We don't know how far JNI_OnLoad got, so there could
2873 // be some partially-initialized stuff accessible through
2874 // newly-registered native method calls. We could try to
2875 // unregister them, but that doesn't seem worthwhile.
2876 result = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07002877 }
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07002878 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2879 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002880 }
2881
2882 library->SetResult(result);
2883 return result;
2884}
2885
Mathieu Chartier66f19252012-09-18 08:57:04 -07002886void* JavaVMExt::FindCodeForNativeMethod(AbstractMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002887 CHECK(m->IsNative());
2888
2889 Class* c = m->GetDeclaringClass();
2890
2891 // If this is a static method, it could be called before the class
2892 // has been initialized.
2893 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002894 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002895 return NULL;
2896 }
2897 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002898 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002899 }
2900
Brian Carlstrom16192862011-09-12 17:50:06 -07002901 std::string detail;
2902 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07002903 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07002904 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002905 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07002906 native_method = libraries->FindNativeMethod(m, detail);
2907 }
Ian Rogers62d6c772013-02-27 08:32:07 -08002908 // Throwing can cause libraries_lock to be reacquired.
Brian Carlstrom16192862011-09-12 17:50:06 -07002909 if (native_method == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002910 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
2911 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002912 }
2913 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002914}
2915
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002916void JavaVMExt::VisitRoots(RootVisitor* visitor, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002917 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07002918 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002919 MutexLock mu(self, globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002920 globals.VisitRoots(visitor, arg);
2921 }
2922 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002923 MutexLock mu(self, pins_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002924 pin_table.VisitRoots(visitor, arg);
2925 }
2926 // The weak_globals table is visited by the GC itself (because it mutates the table).
2927}
2928
Elliott Hughesc8fece32013-01-02 11:27:23 -08002929void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
2930 size_t method_count) {
2931 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
2932 if (c.get() == NULL) {
2933 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
2934 }
2935 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
2936}
2937
Ian Rogersdf20fe02011-07-20 20:34:16 -07002938} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002939
2940std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2941 switch (rhs) {
2942 case JNIInvalidRefType:
2943 os << "JNIInvalidRefType";
2944 return os;
2945 case JNILocalRefType:
2946 os << "JNILocalRefType";
2947 return os;
2948 case JNIGlobalRefType:
2949 os << "JNIGlobalRefType";
2950 return os;
2951 case JNIWeakGlobalRefType:
2952 os << "JNIWeakGlobalRefType";
2953 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002954 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002955 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002956 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002957 }
2958}