blob: 5ea1f7717265b762aa97401e960d898ff584e15c [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 }
Jeff Hao3dd9f762013-07-08 13:09:25 -0700138 // Check interpreter only mode for methods invoked through JNI.
139 // TODO: Check that this catches all remaining paths to invoke.
140 // TODO: Move check up a level to avoid building arg array and then shadow frame?
141 if (Runtime::Current()->GetInstrumentation()->InterpretOnly()) {
142 Object* receiver = method->IsStatic() ? NULL : reinterpret_cast<Object*>(args[0]);
143 if (!method->IsStatic()) {
144 args++;
145 }
146 ManagedStack fragment;
147 soa.Self()->PushManagedStackFragment(&fragment);
148 art::interpreter::EnterInterpreterFromInvoke(soa.Self(), method, receiver, args, result);
149 soa.Self()->PopManagedStackFragment(fragment);
150 } else {
151 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, result_type);
152 }
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700153}
154
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700155static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
156 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700157 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700158 AbstractMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700159 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700160 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800161 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700162 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800163 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700164 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
165 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700166}
167
Mathieu Chartier66f19252012-09-18 08:57:04 -0700168static AbstractMethod* FindVirtualMethod(Object* receiver, AbstractMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700169 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700170 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700171}
172
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700173static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
174 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700175 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700176 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700177 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700178 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800179 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700180 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800181 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700182 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
183 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700184}
185
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700186static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
187 jobject obj, jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700188 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700189 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700190 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700191 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800192 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700193 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800194 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700195 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
196 return result;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700197}
198
Elliott Hughes6b436852011-08-12 10:16:44 -0700199// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
200// separated with slashes but aren't wrapped with "L;" like regular descriptors
201// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
202// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
203// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700204static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700205 std::string result;
206 // Add the missing "L;" if necessary.
207 if (name[0] == '[') {
208 result = name;
209 } else {
210 result += 'L';
211 result += name;
212 result += ';';
213 }
214 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700215 if (result.find('.') != std::string::npos) {
216 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
217 << "\"" << name << "\"";
218 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700219 }
220 return result;
221}
222
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700223static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, Class* c,
224 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700225 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800226 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
227 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
228 "no %s method \"%s.%s%s\"",
229 kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700230}
231
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700232static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
233 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700234 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700235 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700236 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700237 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700238 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700239
Mathieu Chartier66f19252012-09-18 08:57:04 -0700240 AbstractMethod* method = NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700241 if (is_static) {
242 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700243 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700244 method = c->FindVirtualMethod(name, sig);
245 if (method == NULL) {
246 // No virtual method matching the signature. Search declared
247 // private methods and constructors.
248 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700249 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700250 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700251
Elliott Hughescdf53122011-08-19 15:46:09 -0700252 if (method == NULL || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700253 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700254 return NULL;
255 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700256
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700257 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700258}
259
Ian Rogersef28b142012-11-30 14:22:18 -0800260static ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700261 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800262 AbstractMethod* method = soa.Self()->GetCurrentMethod(NULL);
Ian Rogersef28b142012-11-30 14:22:18 -0800263 if (method == NULL ||
264 method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
265 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700266 }
267 return method->GetDeclaringClass()->GetClassLoader();
268}
269
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700270static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
271 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700272 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700273 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700274 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700275 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700276 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700277
278 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700279 Class* field_type;
280 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
281 if (sig[1] != '\0') {
Ian Rogersef28b142012-11-30 14:22:18 -0800282 ClassLoader* cl = GetClassLoader(soa);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700283 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700284 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700285 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700286 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700287 if (field_type == NULL) {
288 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700289 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800290 ThrowLocation throw_location;
291 SirtRef<mirror::Throwable> cause(soa.Self(), soa.Self()->GetException(&throw_location));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700292 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800293 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
294 "no type \"%s\" found and so no field \"%s\" could be found in class "
295 "\"%s\" or its superclasses", sig, name,
296 ClassHelper(c).GetDescriptor());
297 soa.Self()->GetException(NULL)->SetCause(cause.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700298 return NULL;
299 }
300 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800301 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700302 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800303 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700304 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700305 if (field == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800306 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
307 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
308 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
309 sig, name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700310 return NULL;
311 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700312 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700313}
314
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700315static void PinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700316 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700317 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700318 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700319 vm->pin_table.Add(array);
320}
321
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700322static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700323 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700324 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700325 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700326 vm->pin_table.Remove(array);
327}
328
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700329static void ThrowAIOOBE(ScopedObjectAccess& soa, Array* array, jsize start,
330 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700331 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700332 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800333 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
334 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
335 "%s offset=%d length=%d %s.length=%d",
336 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700337}
Ian Rogers0571d352011-11-03 19:51:38 -0700338
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700339static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
340 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700341 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800342 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
343 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
344 "offset=%d length=%d string.length()=%d", start, length,
345 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700346}
Elliott Hughes814e4032011-08-23 12:07:56 -0700347
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700348int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700349 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700350 // Turn the const char* into a java.lang.String.
351 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
352 if (msg != NULL && s.get() == NULL) {
353 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700354 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700355
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700356 // Choose an appropriate constructor and set up the arguments.
357 jvalue args[2];
358 const char* signature;
359 if (msg == NULL && cause == NULL) {
360 signature = "()V";
361 } else if (msg != NULL && cause == NULL) {
362 signature = "(Ljava/lang/String;)V";
363 args[0].l = s.get();
364 } else if (msg == NULL && cause != NULL) {
365 signature = "(Ljava/lang/Throwable;)V";
366 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700367 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700368 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
369 args[0].l = s.get();
370 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700371 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700372 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
373 if (mid == NULL) {
Ian Rogersef28b142012-11-30 14:22:18 -0800374 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700375 LOG(ERROR) << "No <init>" << signature << " in "
376 << PrettyClass(soa.Decode<Class*>(exception_class));
377 return JNI_ERR;
378 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700379
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700380 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
381 if (exception.get() == NULL) {
382 return JNI_ERR;
383 }
Ian Rogersef28b142012-11-30 14:22:18 -0800384 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800385 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
386 soa.Self()->SetException(throw_location, soa.Decode<Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700387 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700388}
389
Elliott Hughes462c9442012-03-23 18:47:50 -0700390static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700391 if (vm == NULL || p_env == NULL) {
392 return JNI_ERR;
393 }
394
Elliott Hughes462c9442012-03-23 18:47:50 -0700395 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700396 Thread* self = Thread::Current();
397 if (self != NULL) {
398 *p_env = self->GetJniEnv();
399 return JNI_OK;
400 }
401
402 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
403
404 // No threads allowed in zygote mode.
405 if (runtime->IsZygote()) {
406 LOG(ERROR) << "Attempt to attach a thread in the zygote";
407 return JNI_ERR;
408 }
409
Elliott Hughes462c9442012-03-23 18:47:50 -0700410 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
411 const char* thread_name = NULL;
Ian Rogers365c1022012-06-22 15:05:28 -0700412 jobject thread_group = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -0700413 if (args != NULL) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700414 if (IsBadJniVersion(args->version)) {
415 LOG(ERROR) << "Bad JNI version passed to "
416 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
417 << args->version;
418 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700419 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700420 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700421 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700422 }
Elliott Hughes75770752011-08-24 17:52:38 -0700423
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800424 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700425 *p_env = NULL;
426 return JNI_ERR;
427 } else {
428 *p_env = Thread::Current()->GetJniEnv();
429 return JNI_OK;
430 }
Elliott Hughes75770752011-08-24 17:52:38 -0700431}
432
Elliott Hughes79082e32011-08-25 12:07:32 -0700433class SharedLibrary {
434 public:
435 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
436 : path_(path),
437 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700438 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700439 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700440 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700441 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700442 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700443 }
444
Elliott Hughes79082e32011-08-25 12:07:32 -0700445 Object* GetClassLoader() {
446 return class_loader_;
447 }
448
449 std::string GetPath() {
450 return path_;
451 }
452
453 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700454 * Check the result of an earlier call to JNI_OnLoad on this library.
455 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700456 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700457 bool CheckOnLoadResult()
458 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700459 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700460 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700461 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
462 bool okay;
463 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700464 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700465
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700466 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
467 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
468 // caller can continue.
469 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
470 okay = true;
471 } else {
472 while (jni_on_load_result_ == kPending) {
473 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700474 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700475 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700476
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700477 okay = (jni_on_load_result_ == kOkay);
478 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
479 << (okay ? "succeeded" : "failed") << "]";
480 }
481 }
482 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700483 return okay;
484 }
485
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700486 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700487 Thread* self = Thread::Current();
488 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700489
Elliott Hughes79082e32011-08-25 12:07:32 -0700490 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700491 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700492
493 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700494 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700495 }
496
497 void* FindSymbol(const std::string& symbol_name) {
498 return dlsym(handle_, symbol_name.c_str());
499 }
500
501 private:
502 enum JNI_OnLoadState {
503 kPending,
504 kFailed,
505 kOkay,
506 };
507
508 // Path to library "/system/lib/libjni.so".
509 std::string path_;
510
511 // The void* returned by dlopen(3).
512 void* handle_;
513
514 // The ClassLoader this library is associated with.
515 Object* class_loader_;
516
517 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700518 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700519 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700520 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700521 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700522 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700523 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700524 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700525};
526
Elliott Hughes79082e32011-08-25 12:07:32 -0700527// This exists mainly to keep implementation details out of the header file.
528class Libraries {
529 public:
530 Libraries() {
531 }
532
533 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700534 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700535 }
536
Elliott Hughesae80b492012-04-24 10:43:17 -0700537 void Dump(std::ostream& os) const {
538 bool first = true;
539 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
540 if (!first) {
541 os << ' ';
542 }
543 first = false;
544 os << it->first;
545 }
546 }
547
548 size_t size() const {
549 return libraries_.size();
550 }
551
Elliott Hughes79082e32011-08-25 12:07:32 -0700552 SharedLibrary* Get(const std::string& path) {
Ian Rogers712462a2012-04-12 16:32:29 -0700553 It it = libraries_.find(path);
554 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700555 }
556
557 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700558 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700559 }
560
561 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700562 void* FindNativeMethod(const AbstractMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700563 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700564 std::string jni_short_name(JniShortName(m));
565 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700566 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700567 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
568 SharedLibrary* library = it->second;
569 if (library->GetClassLoader() != declaring_class_loader) {
570 // We only search libraries loaded by the appropriate ClassLoader.
571 continue;
572 }
573 // Try the short name then the long name...
574 void* fn = library->FindSymbol(jni_short_name);
575 if (fn == NULL) {
576 fn = library->FindSymbol(jni_long_name);
577 }
578 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800579 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
580 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700581 return fn;
582 }
583 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700584 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700585 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700586 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700587 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700588 return NULL;
589 }
590
591 private:
Elliott Hughesae80b492012-04-24 10:43:17 -0700592 typedef SafeMap<std::string, SharedLibrary*>::const_iterator It; // TODO: C++0x auto
Elliott Hughes79082e32011-08-25 12:07:32 -0700593
Elliott Hughesa0e18062012-04-13 15:59:59 -0700594 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700595};
596
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700597JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
598 jvalue* args) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700599 AbstractMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700600 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700601 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800602 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700603 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800604 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700605 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
606 return result;
Elliott Hughesd07986f2011-12-06 18:27:45 -0800607}
608
Elliott Hughescdf53122011-08-19 15:46:09 -0700609class JNI {
610 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700611 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700612 return JNI_VERSION_1_6;
613 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700614
Ian Rogers25e8b912012-09-07 11:31:36 -0700615 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700616 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700617 return NULL;
618 }
619
Elliott Hughescdf53122011-08-19 15:46:09 -0700620 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700621 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700622 Runtime* runtime = Runtime::Current();
623 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700624 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700625 Class* c = NULL;
626 if (runtime->IsStarted()) {
Ian Rogersef28b142012-11-30 14:22:18 -0800627 ClassLoader* cl = GetClassLoader(soa);
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800628 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700629 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800630 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700631 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700632 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700633 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700634
Elliott Hughescdf53122011-08-19 15:46:09 -0700635 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700636 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700637 AbstractMethod* method = soa.Decode<AbstractMethod*>(java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700638 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700639 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700640
Elliott Hughescdf53122011-08-19 15:46:09 -0700641 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700642 ScopedObjectAccess soa(env);
643 Field* field = soa.Decode<Field*>(java_field);
644 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700645 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700646
Elliott Hughescdf53122011-08-19 15:46:09 -0700647 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700648 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700649 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700650 return soa.AddLocalReference<jobject>(method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700651 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700652
Elliott Hughescdf53122011-08-19 15:46:09 -0700653 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700654 ScopedObjectAccess soa(env);
655 Field* field = soa.DecodeField(fid);
656 return soa.AddLocalReference<jobject>(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700657 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700658
Elliott Hughes37f7a402011-08-22 18:56:01 -0700659 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700660 ScopedObjectAccess soa(env);
661 Object* o = soa.Decode<Object*>(java_object);
662 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700663 }
664
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700665 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700666 ScopedObjectAccess soa(env);
667 Class* c = soa.Decode<Class*>(java_class);
668 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700669 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700670
Elliott Hughes37f7a402011-08-22 18:56:01 -0700671 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700672 ScopedObjectAccess soa(env);
673 Class* c1 = soa.Decode<Class*>(java_class1);
674 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700675 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700676 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700677
Elliott Hughese84278b2012-03-22 10:06:53 -0700678 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700679 ScopedObjectAccess soa(env);
Elliott Hughes96a98872012-12-19 14:21:15 -0800680 if (java_class == NULL) {
681 JniAbortF("IsInstanceOf", "null class (second argument)");
682 }
Elliott Hughes37f7a402011-08-22 18:56:01 -0700683 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700684 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700685 return JNI_TRUE;
686 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700687 Object* obj = soa.Decode<Object*>(jobj);
688 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700689 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700690 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700691 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700692
Elliott Hughes37f7a402011-08-22 18:56:01 -0700693 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700694 ScopedObjectAccess soa(env);
695 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700696 if (exception == NULL) {
697 return JNI_ERR;
698 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800699 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
700 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700701 return JNI_OK;
702 }
703
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700704 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700705 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700706 }
707
708 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700709 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700710 }
711
712 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700713 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700714 }
715
716 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700717 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700718
Ian Rogers62d6c772013-02-27 08:32:07 -0800719 SirtRef<mirror::Object> old_throw_this_object(soa.Self(), NULL);
720 SirtRef<mirror::AbstractMethod> old_throw_method(soa.Self(), NULL);
721 SirtRef<mirror::Throwable> old_exception(soa.Self(), NULL);
722 uint32_t old_throw_dex_pc;
723 {
724 ThrowLocation old_throw_location;
725 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
726 old_throw_this_object.reset(old_throw_location.GetThis());
727 old_throw_method.reset(old_throw_location.GetMethod());
728 old_exception.reset(old_exception_obj);
729 old_throw_dex_pc = old_throw_location.GetDexPc();
730 soa.Self()->ClearException();
731 }
732 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(old_exception.get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700733 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
734 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
735 if (mid == NULL) {
736 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Ian Rogers62d6c772013-02-27 08:32:07 -0800737 << PrettyTypeOf(old_exception.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700738 } else {
739 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800740 if (soa.Self()->IsExceptionPending()) {
741 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(NULL))
Elliott Hughes72025e52011-08-23 17:50:30 -0700742 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800743 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700744 }
745 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800746 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
747 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700748
Ian Rogers62d6c772013-02-27 08:32:07 -0800749 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700750 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700751
Elliott Hughescdf53122011-08-19 15:46:09 -0700752 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700753 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800754 Object* exception = soa.Self()->GetException(NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700755 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700756 }
757
Ian Rogers25e8b912012-09-07 11:31:36 -0700758 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700759 LOG(FATAL) << "JNI FatalError called: " << msg;
760 }
761
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700762 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800763 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700764 return JNI_ERR;
765 }
Ian Rogersef28b142012-11-30 14:22:18 -0800766 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700767 return JNI_OK;
768 }
769
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700770 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700771 ScopedObjectAccess soa(env);
772 Object* survivor = soa.Decode<Object*>(java_survivor);
773 soa.Env()->PopFrame();
774 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700775 }
776
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700777 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800778 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700779 }
780
Elliott Hughescdf53122011-08-19 15:46:09 -0700781 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700782 if (obj == NULL) {
783 return NULL;
784 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700785 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700786 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700787 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700788 Object* decoded_obj = soa.Decode<Object*>(obj);
Ian Rogers50b35e22012-10-04 10:09:15 -0700789 MutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700790 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700791 return reinterpret_cast<jobject>(ref);
792 }
793
794 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700795 if (obj == NULL) {
796 return;
797 }
Ian Rogersef28b142012-11-30 14:22:18 -0800798 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700799 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800800 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
801 MutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700802
803 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
804 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
805 << "failed to find entry";
806 }
807 }
808
809 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700810 ScopedObjectAccess soa(env);
811 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700812 }
813
814 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700815 if (obj == NULL) {
816 return;
817 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700818 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700819 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700820 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -0700821 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700822
823 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
824 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
825 << "failed to find entry";
826 }
827 }
828
829 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700830 if (obj == NULL) {
831 return NULL;
832 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700833 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700834 IndirectReferenceTable& locals = soa.Env()->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700835
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700836 uint32_t cookie = soa.Env()->local_ref_cookie;
837 IndirectRef ref = locals.Add(cookie, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700838 return reinterpret_cast<jobject>(ref);
839 }
840
841 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700842 if (obj == NULL) {
843 return;
844 }
Ian Rogersef28b142012-11-30 14:22:18 -0800845 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700846
Ian Rogersef28b142012-11-30 14:22:18 -0800847 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700848 if (!locals.Remove(cookie, obj)) {
849 // Attempting to delete a local reference that is not in the
850 // topmost local reference frame is a no-op. DeleteLocalRef returns
851 // void and doesn't throw any exceptions, but we should probably
852 // complain about it so the user will notice that things aren't
853 // going quite the way they expect.
854 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
855 << "failed to find entry";
856 }
857 }
858
859 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700860 ScopedObjectAccess soa(env);
Ian Rogers120f1c72012-09-28 17:17:10 -0700861 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700862 }
863
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700864 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700865 ScopedObjectAccess soa(env);
866 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700867 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700868 return NULL;
869 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700870 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700871 }
872
Elliott Hughese84278b2012-03-22 10:06:53 -0700873 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700874 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700875 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -0700876 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700877 va_end(args);
878 return result;
879 }
880
Elliott Hughes72025e52011-08-23 17:50:30 -0700881 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700882 ScopedObjectAccess soa(env);
883 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700884 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700885 return NULL;
886 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700887 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700888 if (result == NULL) {
889 return NULL;
890 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700891 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700892 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700893 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700894 return local_result;
895 } else {
896 return NULL;
897 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700898 }
899
Elliott Hughes72025e52011-08-23 17:50:30 -0700900 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700901 ScopedObjectAccess soa(env);
902 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700903 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700904 return NULL;
905 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700906 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700907 if (result == NULL) {
908 return NULL;
909 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700910 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700911 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700912 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700913 return local_result;
914 } else {
915 return NULL;
916 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700917 }
918
Elliott Hughescdf53122011-08-19 15:46:09 -0700919 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700920 ScopedObjectAccess soa(env);
921 return FindMethodID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700922 }
923
924 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700925 ScopedObjectAccess soa(env);
926 return FindMethodID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700927 }
928
Elliott Hughes72025e52011-08-23 17:50:30 -0700929 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700930 va_list ap;
931 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700932 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700933 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700934 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700935 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700936 }
937
Elliott Hughes72025e52011-08-23 17:50:30 -0700938 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700939 ScopedObjectAccess soa(env);
940 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
941 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700942 }
943
Elliott Hughes72025e52011-08-23 17:50:30 -0700944 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700945 ScopedObjectAccess soa(env);
946 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
947 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700948 }
949
Elliott Hughes72025e52011-08-23 17:50:30 -0700950 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700951 va_list ap;
952 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700953 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700954 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700955 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700956 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700957 }
958
Elliott Hughes72025e52011-08-23 17:50:30 -0700959 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700960 ScopedObjectAccess soa(env);
961 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700962 }
963
Elliott Hughes72025e52011-08-23 17:50:30 -0700964 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700965 ScopedObjectAccess soa(env);
966 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700967 }
968
Elliott Hughes72025e52011-08-23 17:50:30 -0700969 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700970 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700971 va_list ap;
972 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700973 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700974 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700975 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700976 }
977
Elliott Hughes72025e52011-08-23 17:50:30 -0700978 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700979 ScopedObjectAccess soa(env);
980 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700981 }
982
Elliott Hughes72025e52011-08-23 17:50:30 -0700983 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700984 ScopedObjectAccess soa(env);
985 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 }
987
Elliott Hughes72025e52011-08-23 17:50:30 -0700988 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700989 va_list ap;
990 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700991 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700992 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700993 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700994 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 }
996
Elliott Hughes72025e52011-08-23 17:50:30 -0700997 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700998 ScopedObjectAccess soa(env);
999 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001000 }
1001
Elliott Hughes72025e52011-08-23 17:50:30 -07001002 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001003 ScopedObjectAccess soa(env);
1004 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 }
1006
Elliott Hughes72025e52011-08-23 17:50:30 -07001007 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001008 va_list ap;
1009 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001010 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001011 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001012 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001013 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001014 }
1015
Elliott Hughes72025e52011-08-23 17:50:30 -07001016 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001017 ScopedObjectAccess soa(env);
1018 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 }
1020
Elliott Hughes72025e52011-08-23 17:50:30 -07001021 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001022 ScopedObjectAccess soa(env);
1023 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 }
1025
Elliott Hughes72025e52011-08-23 17:50:30 -07001026 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001027 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 va_list ap;
1029 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001030 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001031 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001032 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001033 }
1034
Elliott Hughes72025e52011-08-23 17:50:30 -07001035 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001036 ScopedObjectAccess soa(env);
1037 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001038 }
1039
Elliott Hughes72025e52011-08-23 17:50:30 -07001040 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001041 ScopedObjectAccess soa(env);
1042 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 }
1044
Elliott Hughes72025e52011-08-23 17:50:30 -07001045 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001046 va_list ap;
1047 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001048 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001049 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001051 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001052 }
1053
Elliott Hughes72025e52011-08-23 17:50:30 -07001054 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001055 ScopedObjectAccess soa(env);
1056 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 }
1058
Elliott Hughes72025e52011-08-23 17:50:30 -07001059 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001060 ScopedObjectAccess soa(env);
1061 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 }
1063
Elliott Hughes72025e52011-08-23 17:50:30 -07001064 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001065 va_list ap;
1066 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001067 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001068 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001069 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001070 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001071 }
1072
Elliott Hughes72025e52011-08-23 17:50:30 -07001073 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001074 ScopedObjectAccess soa(env);
1075 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 }
1077
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001079 ScopedObjectAccess soa(env);
1080 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 }
1082
Elliott Hughes72025e52011-08-23 17:50:30 -07001083 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001084 va_list ap;
1085 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001086 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001087 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001088 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001089 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 }
1091
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001093 ScopedObjectAccess soa(env);
1094 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 }
1096
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001098 ScopedObjectAccess soa(env);
1099 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 }
1101
Elliott Hughes72025e52011-08-23 17:50:30 -07001102 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001103 va_list ap;
1104 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001105 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001106 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 }
1109
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001111 ScopedObjectAccess soa(env);
1112 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 }
1114
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001116 ScopedObjectAccess soa(env);
1117 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001118 }
1119
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001120 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001123 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001124 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1125 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 va_end(ap);
1127 return local_result;
1128 }
1129
1130 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001131 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001132 ScopedObjectAccess soa(env);
1133 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1134 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 }
1136
1137 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001138 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001139 ScopedObjectAccess soa(env);
1140 JValue result(InvokeWithJValues(soa, obj, mid, args));
1141 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 }
1143
1144 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001145 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001146 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001148 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001149 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001151 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 }
1153
1154 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001155 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001156 ScopedObjectAccess soa(env);
1157 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001158 }
1159
1160 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001161 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001162 ScopedObjectAccess soa(env);
1163 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 }
1165
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001166 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001167 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001168 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001169 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001170 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001172 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 }
1174
1175 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001176 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001177 ScopedObjectAccess soa(env);
1178 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001179 }
1180
1181 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001182 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001183 ScopedObjectAccess soa(env);
1184 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 }
1186
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001187 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001188 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001189 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001190 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001191 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001193 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 }
1195
1196 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001197 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001198 ScopedObjectAccess soa(env);
1199 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 }
1201
1202 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001203 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001204 ScopedObjectAccess soa(env);
1205 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 }
1207
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001208 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001209 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001210 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001211 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001212 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001214 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001215 }
1216
1217 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001218 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001219 ScopedObjectAccess soa(env);
1220 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001221 }
1222
1223 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001224 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001225 ScopedObjectAccess soa(env);
1226 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001227 }
1228
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001229 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001232 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001233 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001235 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 }
1237
1238 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001239 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001240 ScopedObjectAccess soa(env);
1241 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001242 }
1243
1244 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001245 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001246 ScopedObjectAccess soa(env);
1247 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 }
1249
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001250 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001251 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001252 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001253 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001254 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001256 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 }
1258
1259 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001260 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001261 ScopedObjectAccess soa(env);
1262 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001263 }
1264
1265 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001266 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001267 ScopedObjectAccess soa(env);
1268 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001269 }
1270
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001271 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001273 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001274 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001275 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001277 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 }
1279
1280 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001281 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001282 ScopedObjectAccess soa(env);
1283 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001284 }
1285
1286 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001287 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001288 ScopedObjectAccess soa(env);
1289 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 }
1291
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001292 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001294 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001295 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001296 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001297 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001298 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 }
1300
1301 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001302 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001303 ScopedObjectAccess soa(env);
1304 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 }
1306
1307 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001308 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001309 ScopedObjectAccess soa(env);
1310 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001311 }
1312
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001313 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001315 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001316 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001317 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 va_end(ap);
1319 }
1320
1321 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001322 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001323 ScopedObjectAccess soa(env);
1324 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 }
1326
1327 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001328 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001329 ScopedObjectAccess soa(env);
1330 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 }
1332
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001333 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001334 ScopedObjectAccess soa(env);
1335 return FindFieldID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001336 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001337
1338
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001339 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001340 ScopedObjectAccess soa(env);
1341 return FindFieldID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001343
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001344 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001345 ScopedObjectAccess soa(env);
1346 Object* o = soa.Decode<Object*>(obj);
1347 Field* f = soa.DecodeField(fid);
1348 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001350
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001351 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001352 ScopedObjectAccess soa(env);
1353 Field* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001354 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 }
1356
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001357 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001358 ScopedObjectAccess soa(env);
1359 Object* o = soa.Decode<Object*>(java_object);
1360 Object* v = soa.Decode<Object*>(java_value);
1361 Field* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001362 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 }
1364
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001365 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001366 ScopedObjectAccess soa(env);
1367 Object* v = soa.Decode<Object*>(java_value);
1368 Field* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001369 f->SetObject(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 }
1371
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001372#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001373 ScopedObjectAccess soa(env); \
1374 Object* o = soa.Decode<Object*>(instance); \
1375 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001376 return f->fn(o)
1377
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001378#define GET_STATIC_PRIMITIVE_FIELD(fn) \
1379 ScopedObjectAccess soa(env); \
1380 Field* f = soa.DecodeField(fid); \
1381 return f->fn(f->GetDeclaringClass())
1382
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001383#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001384 ScopedObjectAccess soa(env); \
1385 Object* o = soa.Decode<Object*>(instance); \
1386 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001387 f->fn(o, value)
1388
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001389#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
1390 ScopedObjectAccess soa(env); \
1391 Field* f = soa.DecodeField(fid); \
1392 f->fn(f->GetDeclaringClass(), value)
1393
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001394 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1395 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 }
1397
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001398 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1399 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 }
1401
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001402 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1403 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001404 }
1405
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001406 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1407 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001408 }
1409
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001410 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1411 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 }
1413
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001414 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1415 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001416 }
1417
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001418 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1419 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 }
1421
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001422 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1423 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 }
1425
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001426 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001427 GET_STATIC_PRIMITIVE_FIELD(GetBoolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 }
1429
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001430 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001431 GET_STATIC_PRIMITIVE_FIELD(GetByte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 }
1433
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001434 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001435 GET_STATIC_PRIMITIVE_FIELD(GetChar);
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 }
1437
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001438 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001439 GET_STATIC_PRIMITIVE_FIELD(GetShort);
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 }
1441
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001442 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001443 GET_STATIC_PRIMITIVE_FIELD(GetInt);
Elliott Hughescdf53122011-08-19 15:46:09 -07001444 }
1445
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001446 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001447 GET_STATIC_PRIMITIVE_FIELD(GetLong);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001448 }
1449
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001450 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001451 GET_STATIC_PRIMITIVE_FIELD(GetFloat);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001452 }
1453
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001454 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001455 GET_STATIC_PRIMITIVE_FIELD(GetDouble);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001456 }
1457
1458 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1459 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1460 }
1461
1462 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1463 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1464 }
1465
1466 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1467 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1468 }
1469
1470 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1471 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1472 }
1473
1474 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1475 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1476 }
1477
1478 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1479 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1480 }
1481
1482 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1483 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1484 }
1485
1486 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1487 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1488 }
1489
1490 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001491 SET_STATIC_PRIMITIVE_FIELD(SetBoolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001492 }
1493
1494 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001495 SET_STATIC_PRIMITIVE_FIELD(SetByte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001496 }
1497
1498 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001499 SET_STATIC_PRIMITIVE_FIELD(SetChar, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001500 }
1501
1502 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001503 SET_STATIC_PRIMITIVE_FIELD(SetFloat, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001504 }
1505
1506 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001507 SET_STATIC_PRIMITIVE_FIELD(SetDouble, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001508 }
1509
1510 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001511 SET_STATIC_PRIMITIVE_FIELD(SetInt, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001512 }
1513
1514 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001515 SET_STATIC_PRIMITIVE_FIELD(SetLong, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001516 }
1517
1518 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001519 SET_STATIC_PRIMITIVE_FIELD(SetShort, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001520 }
1521
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001522 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001523 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001524 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001525 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001526 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1527 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001528 va_end(ap);
1529 return local_result;
1530 }
1531
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001532 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001533 ScopedObjectAccess soa(env);
1534 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1535 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001536 }
1537
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001538 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001539 ScopedObjectAccess soa(env);
1540 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1541 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 }
1543
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001544 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001545 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001546 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001547 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001548 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001549 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001550 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001551 }
1552
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001553 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001554 ScopedObjectAccess soa(env);
1555 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001556 }
1557
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001558 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001559 ScopedObjectAccess soa(env);
1560 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001561 }
1562
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001563 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001564 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001565 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001566 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001567 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001568 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001569 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001570 }
1571
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001572 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001573 ScopedObjectAccess soa(env);
1574 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001575 }
1576
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001577 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001578 ScopedObjectAccess soa(env);
1579 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001580 }
1581
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001582 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001583 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001584 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001585 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001586 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001587 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001588 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 }
1590
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001591 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001592 ScopedObjectAccess soa(env);
1593 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001594 }
1595
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001596 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001597 ScopedObjectAccess soa(env);
1598 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 }
1600
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001601 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001602 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001603 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001604 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001605 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001607 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001608 }
1609
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001610 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001611 ScopedObjectAccess soa(env);
1612 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001613 }
1614
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001615 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001616 ScopedObjectAccess soa(env);
1617 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 }
1619
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001620 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001621 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001622 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001623 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001624 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001626 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 }
1628
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001629 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001630 ScopedObjectAccess soa(env);
1631 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 }
1633
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001634 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001635 ScopedObjectAccess soa(env);
1636 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 }
1638
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001639 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001640 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001641 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001642 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001643 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001644 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001645 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 }
1647
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001648 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001649 ScopedObjectAccess soa(env);
1650 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 }
1652
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001653 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001654 ScopedObjectAccess soa(env);
1655 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001656 }
1657
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001658 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001659 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001660 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001661 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001662 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001663 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001664 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 }
1666
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001667 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001668 ScopedObjectAccess soa(env);
1669 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 }
1671
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001672 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001673 ScopedObjectAccess soa(env);
1674 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 }
1676
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001677 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001678 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001679 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001680 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001681 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001682 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001683 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 }
1685
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001686 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001687 ScopedObjectAccess soa(env);
1688 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 }
1690
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001691 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001692 ScopedObjectAccess soa(env);
1693 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 }
1695
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001696 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001697 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001698 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001699 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001700 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001701 va_end(ap);
1702 }
1703
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001704 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001705 ScopedObjectAccess soa(env);
1706 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001707 }
1708
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001709 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001710 ScopedObjectAccess soa(env);
1711 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 }
1713
Elliott Hughes814e4032011-08-23 12:07:56 -07001714 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001715 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001716 String* result = String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001717 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 }
1719
1720 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 if (utf == NULL) {
1722 return NULL;
1723 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001724 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001725 String* result = String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001726 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 }
1728
Elliott Hughes814e4032011-08-23 12:07:56 -07001729 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001730 ScopedObjectAccess soa(env);
1731 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001732 }
1733
1734 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001735 ScopedObjectAccess soa(env);
1736 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001737 }
1738
Elliott Hughesb465ab02011-08-24 11:21:21 -07001739 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001740 ScopedObjectAccess soa(env);
1741 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001742 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001743 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001744 } else {
1745 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1746 memcpy(buf, chars + start, length * sizeof(jchar));
1747 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001748 }
1749
Elliott Hughesb465ab02011-08-24 11:21:21 -07001750 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001751 ScopedObjectAccess soa(env);
1752 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001753 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001754 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001755 } else {
1756 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1757 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1758 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001759 }
1760
Elliott Hughes75770752011-08-24 17:52:38 -07001761 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001762 ScopedObjectAccess soa(env);
1763 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001764 const CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001765 PinPrimitiveArray(soa, chars);
Elliott Hughes75770752011-08-24 17:52:38 -07001766 if (is_copy != NULL) {
1767 *is_copy = JNI_FALSE;
1768 }
1769 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001770 }
1771
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001772 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001773 ScopedObjectAccess soa(env);
1774 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 }
1776
Elliott Hughes75770752011-08-24 17:52:38 -07001777 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001778 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 }
1780
Elliott Hughes75770752011-08-24 17:52:38 -07001781 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001782 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001783 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001784 }
1785
Elliott Hughes75770752011-08-24 17:52:38 -07001786 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001787 if (java_string == NULL) {
1788 return NULL;
1789 }
1790 if (is_copy != NULL) {
1791 *is_copy = JNI_TRUE;
1792 }
Ian Rogersef28b142012-11-30 14:22:18 -08001793 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001794 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001795 size_t byte_count = s->GetUtfLength();
1796 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001797 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001798 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1799 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1800 bytes[byte_count] = '\0';
1801 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001802 }
1803
Elliott Hughes75770752011-08-24 17:52:38 -07001804 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001805 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001806 }
1807
Elliott Hughesbd935992011-08-22 11:59:34 -07001808 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001809 ScopedObjectAccess soa(env);
1810 Object* obj = soa.Decode<Object*>(java_array);
Elliott Hughes96a98872012-12-19 14:21:15 -08001811 if (!obj->IsArrayInstance()) {
1812 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1813 }
Elliott Hughesbd935992011-08-22 11:59:34 -07001814 Array* array = obj->AsArray();
1815 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 }
1817
Elliott Hughes814e4032011-08-23 12:07:56 -07001818 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001819 ScopedObjectAccess soa(env);
1820 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1821 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001822 }
1823
1824 static void SetObjectArrayElement(JNIEnv* env,
1825 jobjectArray java_array, jsize index, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001826 ScopedObjectAccess soa(env);
1827 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1828 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 array->Set(index, value);
1830 }
1831
1832 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001833 ScopedObjectAccess soa(env);
1834 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001835 }
1836
1837 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001838 ScopedObjectAccess soa(env);
1839 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001840 }
1841
1842 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001843 ScopedObjectAccess soa(env);
1844 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001845 }
1846
1847 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001848 ScopedObjectAccess soa(env);
1849 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001850 }
1851
1852 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001853 ScopedObjectAccess soa(env);
1854 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001855 }
1856
1857 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001858 ScopedObjectAccess soa(env);
1859 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001860 }
1861
1862 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001863 ScopedObjectAccess soa(env);
1864 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001865 }
1866
1867 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001868 ScopedObjectAccess soa(env);
Elliott Hughes96a98872012-12-19 14:21:15 -08001869 if (length < 0) {
1870 JniAbortF("NewObjectArray", "negative array length: %d", length);
1871 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001872
1873 // Compute the array class corresponding to the given element class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001874 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001875 std::string descriptor;
1876 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001877 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001878
1879 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001880 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1881 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001882 return NULL;
1883 }
1884
Elliott Hughes75770752011-08-24 17:52:38 -07001885 // Allocate and initialize if necessary.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001886 Class* array_class = soa.Decode<Class*>(java_array_class.get());
Ian Rogers50b35e22012-10-04 10:09:15 -07001887 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001888 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001889 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07001890 for (jsize i = 0; i < length; ++i) {
1891 result->Set(i, initial_object);
1892 }
1893 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001894 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001895 }
1896
1897 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001898 ScopedObjectAccess soa(env);
1899 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001900 }
1901
Ian Rogersa15e67d2012-02-28 13:51:55 -08001902 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001903 ScopedObjectAccess soa(env);
1904 Array* array = soa.Decode<Array*>(java_array);
1905 PinPrimitiveArray(soa, array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001906 if (is_copy != NULL) {
1907 *is_copy = JNI_FALSE;
1908 }
1909 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001910 }
1911
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001912 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001913 ReleasePrimitiveArray(env, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001914 }
1915
Elliott Hughes75770752011-08-24 17:52:38 -07001916 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001917 ScopedObjectAccess soa(env);
1918 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001919 }
1920
Elliott Hughes75770752011-08-24 17:52:38 -07001921 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001922 ScopedObjectAccess soa(env);
1923 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001924 }
1925
Elliott Hughes75770752011-08-24 17:52:38 -07001926 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001927 ScopedObjectAccess soa(env);
1928 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001929 }
1930
Elliott Hughes75770752011-08-24 17:52:38 -07001931 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001932 ScopedObjectAccess soa(env);
1933 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001934 }
1935
Elliott Hughes75770752011-08-24 17:52:38 -07001936 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001937 ScopedObjectAccess soa(env);
1938 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001939 }
1940
Elliott Hughes75770752011-08-24 17:52:38 -07001941 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001942 ScopedObjectAccess soa(env);
1943 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001944 }
1945
Elliott Hughes75770752011-08-24 17:52:38 -07001946 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001947 ScopedObjectAccess soa(env);
1948 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001949 }
1950
Elliott Hughes75770752011-08-24 17:52:38 -07001951 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001952 ScopedObjectAccess soa(env);
1953 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001954 }
1955
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001956 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001957 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001958 }
1959
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001960 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001961 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001962 }
1963
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001964 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001965 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001966 }
1967
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001968 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001969 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001970 }
1971
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001972 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001973 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001974 }
1975
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001976 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001977 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001978 }
1979
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001980 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001981 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 }
1983
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001984 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001985 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001986 }
1987
Elliott Hughes814e4032011-08-23 12:07:56 -07001988 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001989 ScopedObjectAccess soa(env);
1990 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001991 }
1992
Elliott Hughes814e4032011-08-23 12:07:56 -07001993 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001994 ScopedObjectAccess soa(env);
1995 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001996 }
1997
Elliott Hughes814e4032011-08-23 12:07:56 -07001998 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001999 ScopedObjectAccess soa(env);
2000 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002001 }
2002
Elliott Hughes814e4032011-08-23 12:07:56 -07002003 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002004 ScopedObjectAccess soa(env);
2005 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 }
2007
Elliott Hughes814e4032011-08-23 12:07:56 -07002008 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002009 ScopedObjectAccess soa(env);
2010 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 }
2012
Elliott Hughes814e4032011-08-23 12:07:56 -07002013 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002014 ScopedObjectAccess soa(env);
2015 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002016 }
2017
Elliott Hughes814e4032011-08-23 12:07:56 -07002018 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002019 ScopedObjectAccess soa(env);
2020 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002021 }
2022
Elliott Hughes814e4032011-08-23 12:07:56 -07002023 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002024 ScopedObjectAccess soa(env);
2025 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002026 }
2027
Elliott Hughes814e4032011-08-23 12:07:56 -07002028 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002029 ScopedObjectAccess soa(env);
2030 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002031 }
2032
Elliott Hughes814e4032011-08-23 12:07:56 -07002033 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002034 ScopedObjectAccess soa(env);
2035 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002036 }
2037
Elliott Hughes814e4032011-08-23 12:07:56 -07002038 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002039 ScopedObjectAccess soa(env);
2040 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 }
2042
Elliott Hughes814e4032011-08-23 12:07:56 -07002043 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002044 ScopedObjectAccess soa(env);
2045 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002046 }
2047
Elliott Hughes814e4032011-08-23 12:07:56 -07002048 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002049 ScopedObjectAccess soa(env);
2050 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002051 }
2052
Elliott Hughes814e4032011-08-23 12:07:56 -07002053 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002054 ScopedObjectAccess soa(env);
2055 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 }
2057
Elliott Hughes814e4032011-08-23 12:07:56 -07002058 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002059 ScopedObjectAccess soa(env);
2060 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002061 }
2062
Elliott Hughes814e4032011-08-23 12:07:56 -07002063 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002064 ScopedObjectAccess soa(env);
2065 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002066 }
2067
Elliott Hughes5174fe62011-08-23 15:12:35 -07002068 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002069 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2070 }
2071
2072 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 -07002073 ScopedObjectAccess soa(env);
2074 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002075
Elliott Hughesc8fece32013-01-02 11:27:23 -08002076 for (size_t i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 const char* name = methods[i].name;
2078 const char* sig = methods[i].signature;
2079
2080 if (*sig == '!') {
2081 // TODO: fast jni. it's too noisy to log all these.
2082 ++sig;
2083 }
2084
Mathieu Chartier66f19252012-09-18 08:57:04 -07002085 AbstractMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002086 if (m == NULL) {
2087 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002088 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002089 if (m == NULL) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002090 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
2091 << PrettyDescriptor(c) << "." << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002092 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002093 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002094 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002095 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
2096 << PrettyDescriptor(c) << "." << name << sig
2097 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002098 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002099 return JNI_ERR;
2100 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002101
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002102 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002103
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002104 m->RegisterNative(soa.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002105 }
2106 return JNI_OK;
2107 }
2108
Elliott Hughes5174fe62011-08-23 15:12:35 -07002109 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002110 ScopedObjectAccess soa(env);
2111 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002112
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002113 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002114
2115 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002116 AbstractMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002117 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002118 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002119 }
2120 }
2121 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002122 AbstractMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002123 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002124 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002125 }
2126 }
2127
2128 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 }
2130
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002131 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2132 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
2133 ScopedObjectAccess soa(env);
2134 Object* o = soa.Decode<Object*>(java_object);
2135 o->MonitorEnter(soa.Self());
2136 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002137 return JNI_ERR;
2138 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002139 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002140 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002141 }
2142
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002143 static jint MonitorExit(JNIEnv* env, jobject java_object)
2144 UNLOCK_FUNCTION(monitor_lock_) {
2145 ScopedObjectAccess soa(env);
2146 Object* o = soa.Decode<Object*>(java_object);
2147 o->MonitorExit(soa.Self());
2148 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002149 return JNI_ERR;
2150 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002151 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002152 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 }
2154
2155 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002156 Runtime* runtime = Runtime::Current();
2157 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002158 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002159 } else {
2160 *vm = NULL;
2161 }
2162 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2163 }
2164
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002166 if (capacity < 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002167 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002168 }
Elliott Hughes11a796e2012-12-19 14:42:57 -08002169 if (address == NULL && capacity != 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002170 JniAbortF("NewDirectByteBuffer", "non-zero capacity for NULL pointer: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002171 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002172
Elliott Hughes96a98872012-12-19 14:21:15 -08002173 // At the moment, the Java side is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002174 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2175 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002176 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002177 jint capacity_arg = static_cast<jint>(capacity);
2178
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002179 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2180 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002181 address_arg, capacity_arg);
Ian Rogersef28b142012-11-30 14:22:18 -08002182 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002183 }
2184
Elliott Hughesb465ab02011-08-24 11:21:21 -07002185 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002186 return reinterpret_cast<void*>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002187 }
2188
Elliott Hughesb465ab02011-08-24 11:21:21 -07002189 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002190 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002191 }
2192
Elliott Hughesb465ab02011-08-24 11:21:21 -07002193 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002194 if (java_object == NULL) {
2195 JniAbortF("GetObjectRefType", "null object");
2196 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002197
2198 // Do we definitely know what kind of reference this is?
2199 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2200 IndirectRefKind kind = GetIndirectRefKind(ref);
2201 switch (kind) {
2202 case kLocal:
Ian Rogersef28b142012-11-30 14:22:18 -08002203 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002204 return JNILocalRefType;
2205 }
2206 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002207 case kGlobal:
2208 return JNIGlobalRefType;
2209 case kWeakGlobal:
2210 return JNIWeakGlobalRefType;
2211 case kSirtOrInvalid:
2212 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002213 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002214 return JNILocalRefType;
2215 }
2216
Ian Rogersef28b142012-11-30 14:22:18 -08002217 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002218 return JNIInvalidRefType;
2219 }
2220
Elliott Hughesb465ab02011-08-24 11:21:21 -07002221 // If we're handing out direct pointers, check whether it's a direct pointer
2222 // to a local reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002223 {
2224 ScopedObjectAccess soa(env);
2225 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2226 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
2227 return JNILocalRefType;
2228 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002229 }
2230 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002231 return JNIInvalidRefType;
2232 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002233 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2234 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002235 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002236
2237 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002238 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2239 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002240 // TODO: we should try to expand the table if necessary.
2241 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
2242 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2243 return JNI_ERR;
2244 }
2245 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002246 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002247 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2248 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002249 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002250 soa.Self()->ThrowOutOfMemoryError(caller);
2251 }
2252 return okay ? JNI_OK : JNI_ERR;
2253 }
2254
2255 template<typename JniT, typename ArtT>
2256 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002257 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002258 if (length < 0) {
2259 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
2260 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002261 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002262 return soa.AddLocalReference<JniT>(result);
2263 }
2264
2265 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2266 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2267 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002268 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002269 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2270 PinPrimitiveArray(soa, array);
2271 if (is_copy != NULL) {
2272 *is_copy = JNI_FALSE;
2273 }
2274 return array->GetData();
2275 }
2276
2277 template <typename ArrayT>
Ian Rogersef28b142012-11-30 14:22:18 -08002278 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002279 if (mode != JNI_COMMIT) {
Ian Rogersef28b142012-11-30 14:22:18 -08002280 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002281 Array* array = soa.Decode<Array*>(java_array);
2282 UnpinPrimitiveArray(soa, array);
2283 }
2284 }
2285
2286 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2287 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2288 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002289 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002290 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2291 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2292 ThrowAIOOBE(soa, array, start, length, "src");
2293 } else {
2294 JavaT* data = array->GetData();
2295 memcpy(buf, data + start, length * sizeof(JavaT));
2296 }
2297 }
2298
2299 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2300 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2301 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002302 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002303 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2304 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2305 ThrowAIOOBE(soa, array, start, length, "dst");
2306 } else {
2307 JavaT* data = array->GetData();
2308 memcpy(data + start, buf, length * sizeof(JavaT));
2309 }
2310 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002311};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002312
Elliott Hughes88c5c352012-03-15 18:49:48 -07002313const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002314 NULL, // reserved0.
2315 NULL, // reserved1.
2316 NULL, // reserved2.
2317 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002318 JNI::GetVersion,
2319 JNI::DefineClass,
2320 JNI::FindClass,
2321 JNI::FromReflectedMethod,
2322 JNI::FromReflectedField,
2323 JNI::ToReflectedMethod,
2324 JNI::GetSuperclass,
2325 JNI::IsAssignableFrom,
2326 JNI::ToReflectedField,
2327 JNI::Throw,
2328 JNI::ThrowNew,
2329 JNI::ExceptionOccurred,
2330 JNI::ExceptionDescribe,
2331 JNI::ExceptionClear,
2332 JNI::FatalError,
2333 JNI::PushLocalFrame,
2334 JNI::PopLocalFrame,
2335 JNI::NewGlobalRef,
2336 JNI::DeleteGlobalRef,
2337 JNI::DeleteLocalRef,
2338 JNI::IsSameObject,
2339 JNI::NewLocalRef,
2340 JNI::EnsureLocalCapacity,
2341 JNI::AllocObject,
2342 JNI::NewObject,
2343 JNI::NewObjectV,
2344 JNI::NewObjectA,
2345 JNI::GetObjectClass,
2346 JNI::IsInstanceOf,
2347 JNI::GetMethodID,
2348 JNI::CallObjectMethod,
2349 JNI::CallObjectMethodV,
2350 JNI::CallObjectMethodA,
2351 JNI::CallBooleanMethod,
2352 JNI::CallBooleanMethodV,
2353 JNI::CallBooleanMethodA,
2354 JNI::CallByteMethod,
2355 JNI::CallByteMethodV,
2356 JNI::CallByteMethodA,
2357 JNI::CallCharMethod,
2358 JNI::CallCharMethodV,
2359 JNI::CallCharMethodA,
2360 JNI::CallShortMethod,
2361 JNI::CallShortMethodV,
2362 JNI::CallShortMethodA,
2363 JNI::CallIntMethod,
2364 JNI::CallIntMethodV,
2365 JNI::CallIntMethodA,
2366 JNI::CallLongMethod,
2367 JNI::CallLongMethodV,
2368 JNI::CallLongMethodA,
2369 JNI::CallFloatMethod,
2370 JNI::CallFloatMethodV,
2371 JNI::CallFloatMethodA,
2372 JNI::CallDoubleMethod,
2373 JNI::CallDoubleMethodV,
2374 JNI::CallDoubleMethodA,
2375 JNI::CallVoidMethod,
2376 JNI::CallVoidMethodV,
2377 JNI::CallVoidMethodA,
2378 JNI::CallNonvirtualObjectMethod,
2379 JNI::CallNonvirtualObjectMethodV,
2380 JNI::CallNonvirtualObjectMethodA,
2381 JNI::CallNonvirtualBooleanMethod,
2382 JNI::CallNonvirtualBooleanMethodV,
2383 JNI::CallNonvirtualBooleanMethodA,
2384 JNI::CallNonvirtualByteMethod,
2385 JNI::CallNonvirtualByteMethodV,
2386 JNI::CallNonvirtualByteMethodA,
2387 JNI::CallNonvirtualCharMethod,
2388 JNI::CallNonvirtualCharMethodV,
2389 JNI::CallNonvirtualCharMethodA,
2390 JNI::CallNonvirtualShortMethod,
2391 JNI::CallNonvirtualShortMethodV,
2392 JNI::CallNonvirtualShortMethodA,
2393 JNI::CallNonvirtualIntMethod,
2394 JNI::CallNonvirtualIntMethodV,
2395 JNI::CallNonvirtualIntMethodA,
2396 JNI::CallNonvirtualLongMethod,
2397 JNI::CallNonvirtualLongMethodV,
2398 JNI::CallNonvirtualLongMethodA,
2399 JNI::CallNonvirtualFloatMethod,
2400 JNI::CallNonvirtualFloatMethodV,
2401 JNI::CallNonvirtualFloatMethodA,
2402 JNI::CallNonvirtualDoubleMethod,
2403 JNI::CallNonvirtualDoubleMethodV,
2404 JNI::CallNonvirtualDoubleMethodA,
2405 JNI::CallNonvirtualVoidMethod,
2406 JNI::CallNonvirtualVoidMethodV,
2407 JNI::CallNonvirtualVoidMethodA,
2408 JNI::GetFieldID,
2409 JNI::GetObjectField,
2410 JNI::GetBooleanField,
2411 JNI::GetByteField,
2412 JNI::GetCharField,
2413 JNI::GetShortField,
2414 JNI::GetIntField,
2415 JNI::GetLongField,
2416 JNI::GetFloatField,
2417 JNI::GetDoubleField,
2418 JNI::SetObjectField,
2419 JNI::SetBooleanField,
2420 JNI::SetByteField,
2421 JNI::SetCharField,
2422 JNI::SetShortField,
2423 JNI::SetIntField,
2424 JNI::SetLongField,
2425 JNI::SetFloatField,
2426 JNI::SetDoubleField,
2427 JNI::GetStaticMethodID,
2428 JNI::CallStaticObjectMethod,
2429 JNI::CallStaticObjectMethodV,
2430 JNI::CallStaticObjectMethodA,
2431 JNI::CallStaticBooleanMethod,
2432 JNI::CallStaticBooleanMethodV,
2433 JNI::CallStaticBooleanMethodA,
2434 JNI::CallStaticByteMethod,
2435 JNI::CallStaticByteMethodV,
2436 JNI::CallStaticByteMethodA,
2437 JNI::CallStaticCharMethod,
2438 JNI::CallStaticCharMethodV,
2439 JNI::CallStaticCharMethodA,
2440 JNI::CallStaticShortMethod,
2441 JNI::CallStaticShortMethodV,
2442 JNI::CallStaticShortMethodA,
2443 JNI::CallStaticIntMethod,
2444 JNI::CallStaticIntMethodV,
2445 JNI::CallStaticIntMethodA,
2446 JNI::CallStaticLongMethod,
2447 JNI::CallStaticLongMethodV,
2448 JNI::CallStaticLongMethodA,
2449 JNI::CallStaticFloatMethod,
2450 JNI::CallStaticFloatMethodV,
2451 JNI::CallStaticFloatMethodA,
2452 JNI::CallStaticDoubleMethod,
2453 JNI::CallStaticDoubleMethodV,
2454 JNI::CallStaticDoubleMethodA,
2455 JNI::CallStaticVoidMethod,
2456 JNI::CallStaticVoidMethodV,
2457 JNI::CallStaticVoidMethodA,
2458 JNI::GetStaticFieldID,
2459 JNI::GetStaticObjectField,
2460 JNI::GetStaticBooleanField,
2461 JNI::GetStaticByteField,
2462 JNI::GetStaticCharField,
2463 JNI::GetStaticShortField,
2464 JNI::GetStaticIntField,
2465 JNI::GetStaticLongField,
2466 JNI::GetStaticFloatField,
2467 JNI::GetStaticDoubleField,
2468 JNI::SetStaticObjectField,
2469 JNI::SetStaticBooleanField,
2470 JNI::SetStaticByteField,
2471 JNI::SetStaticCharField,
2472 JNI::SetStaticShortField,
2473 JNI::SetStaticIntField,
2474 JNI::SetStaticLongField,
2475 JNI::SetStaticFloatField,
2476 JNI::SetStaticDoubleField,
2477 JNI::NewString,
2478 JNI::GetStringLength,
2479 JNI::GetStringChars,
2480 JNI::ReleaseStringChars,
2481 JNI::NewStringUTF,
2482 JNI::GetStringUTFLength,
2483 JNI::GetStringUTFChars,
2484 JNI::ReleaseStringUTFChars,
2485 JNI::GetArrayLength,
2486 JNI::NewObjectArray,
2487 JNI::GetObjectArrayElement,
2488 JNI::SetObjectArrayElement,
2489 JNI::NewBooleanArray,
2490 JNI::NewByteArray,
2491 JNI::NewCharArray,
2492 JNI::NewShortArray,
2493 JNI::NewIntArray,
2494 JNI::NewLongArray,
2495 JNI::NewFloatArray,
2496 JNI::NewDoubleArray,
2497 JNI::GetBooleanArrayElements,
2498 JNI::GetByteArrayElements,
2499 JNI::GetCharArrayElements,
2500 JNI::GetShortArrayElements,
2501 JNI::GetIntArrayElements,
2502 JNI::GetLongArrayElements,
2503 JNI::GetFloatArrayElements,
2504 JNI::GetDoubleArrayElements,
2505 JNI::ReleaseBooleanArrayElements,
2506 JNI::ReleaseByteArrayElements,
2507 JNI::ReleaseCharArrayElements,
2508 JNI::ReleaseShortArrayElements,
2509 JNI::ReleaseIntArrayElements,
2510 JNI::ReleaseLongArrayElements,
2511 JNI::ReleaseFloatArrayElements,
2512 JNI::ReleaseDoubleArrayElements,
2513 JNI::GetBooleanArrayRegion,
2514 JNI::GetByteArrayRegion,
2515 JNI::GetCharArrayRegion,
2516 JNI::GetShortArrayRegion,
2517 JNI::GetIntArrayRegion,
2518 JNI::GetLongArrayRegion,
2519 JNI::GetFloatArrayRegion,
2520 JNI::GetDoubleArrayRegion,
2521 JNI::SetBooleanArrayRegion,
2522 JNI::SetByteArrayRegion,
2523 JNI::SetCharArrayRegion,
2524 JNI::SetShortArrayRegion,
2525 JNI::SetIntArrayRegion,
2526 JNI::SetLongArrayRegion,
2527 JNI::SetFloatArrayRegion,
2528 JNI::SetDoubleArrayRegion,
2529 JNI::RegisterNatives,
2530 JNI::UnregisterNatives,
2531 JNI::MonitorEnter,
2532 JNI::MonitorExit,
2533 JNI::GetJavaVM,
2534 JNI::GetStringRegion,
2535 JNI::GetStringUTFRegion,
2536 JNI::GetPrimitiveArrayCritical,
2537 JNI::ReleasePrimitiveArrayCritical,
2538 JNI::GetStringCritical,
2539 JNI::ReleaseStringCritical,
2540 JNI::NewWeakGlobalRef,
2541 JNI::DeleteWeakGlobalRef,
2542 JNI::ExceptionCheck,
2543 JNI::NewDirectByteBuffer,
2544 JNI::GetDirectBufferAddress,
2545 JNI::GetDirectBufferCapacity,
2546 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002547};
2548
Elliott Hughes75770752011-08-24 17:52:38 -07002549JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002550 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002551 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002552 local_ref_cookie(IRT_FIRST_SEGMENT),
2553 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002554 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002555 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002556 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002557 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002558 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002559 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002560 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002561 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2562 // errors will ensue.
2563 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2564 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002565}
2566
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002567JNIEnvExt::~JNIEnvExt() {
2568}
2569
Elliott Hughes88c5c352012-03-15 18:49:48 -07002570void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2571 check_jni = enabled;
2572 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002573}
2574
Elliott Hughes73e66f72012-05-09 09:34:45 -07002575void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2576 locals.Dump(os);
2577 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002578}
2579
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002580void JNIEnvExt::PushFrame(int /*capacity*/) {
2581 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002582 stacked_local_ref_cookies.push_back(local_ref_cookie);
2583 local_ref_cookie = locals.GetSegmentState();
2584}
2585
2586void JNIEnvExt::PopFrame() {
2587 locals.SetSegmentState(local_ref_cookie);
2588 local_ref_cookie = stacked_local_ref_cookies.back();
2589 stacked_local_ref_cookies.pop_back();
2590}
2591
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002592Offset JNIEnvExt::SegmentStateOffset() {
2593 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2594 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2595}
2596
Carl Shapiroea4dca82011-08-01 13:45:38 -07002597// JNI Invocation interface.
2598
Brian Carlstrombddf9762013-05-14 11:35:37 -07002599extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002600 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002601 if (IsBadJniVersion(args->version)) {
2602 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002603 return JNI_EVERSION;
2604 }
2605 Runtime::Options options;
2606 for (int i = 0; i < args->nOptions; ++i) {
2607 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002608 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002609 }
2610 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002611 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002612 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002613 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002614 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002615 bool started = runtime->Start();
2616 if (!started) {
2617 delete Thread::Current()->GetJniEnv();
2618 delete runtime->GetJavaVM();
2619 LOG(WARNING) << "CreateJavaVM failed";
2620 return JNI_ERR;
2621 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002622 *p_env = Thread::Current()->GetJniEnv();
2623 *p_vm = runtime->GetJavaVM();
2624 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002625}
2626
Elliott Hughesf2682d52011-08-15 16:37:04 -07002627extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002628 Runtime* runtime = Runtime::Current();
2629 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002630 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002631 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002632 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002633 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002634 }
2635 return JNI_OK;
2636}
2637
2638// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002639extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002640 return JNI_ERR;
2641}
2642
Elliott Hughescdf53122011-08-19 15:46:09 -07002643class JII {
2644 public:
2645 static jint DestroyJavaVM(JavaVM* vm) {
2646 if (vm == NULL) {
2647 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002648 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002649 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2650 delete raw_vm->runtime;
2651 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002652 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002653
Elliott Hughescdf53122011-08-19 15:46:09 -07002654 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002655 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002656 }
2657
2658 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002659 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002660 }
2661
2662 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002663 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002664 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002665 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002666 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2667 Runtime* runtime = raw_vm->runtime;
2668 runtime->DetachCurrentThread();
2669 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002670 }
2671
2672 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes83a25322013-03-14 11:18:53 -07002673 if (IsBadJniVersion(version)) {
2674 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07002675 return JNI_EVERSION;
2676 }
2677 if (vm == NULL || env == NULL) {
2678 return JNI_ERR;
2679 }
2680 Thread* thread = Thread::Current();
2681 if (thread == NULL) {
2682 *env = NULL;
2683 return JNI_EDETACHED;
2684 }
2685 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002686 return JNI_OK;
2687 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002688};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002689
Elliott Hughes88c5c352012-03-15 18:49:48 -07002690const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002691 NULL, // reserved0
2692 NULL, // reserved1
2693 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002694 JII::DestroyJavaVM,
2695 JII::AttachCurrentThread,
2696 JII::DetachCurrentThread,
2697 JII::GetEnv,
2698 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002699};
2700
Elliott Hughesa0957642011-09-02 14:27:33 -07002701JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002702 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002703 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002704 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002705 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002706 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002707 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002708 work_around_app_jni_bugs(false),
Ian Rogers62d6c772013-02-27 08:32:07 -08002709 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002710 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002711 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002712 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002713 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002714 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002715 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Elliott Hughes79082e32011-08-25 12:07:32 -07002716 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002717 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002718 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002719 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002720 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002721}
2722
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002723JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002724 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002725}
2726
Elliott Hughes88c5c352012-03-15 18:49:48 -07002727void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2728 check_jni = enabled;
2729 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002730}
2731
Elliott Hughesae80b492012-04-24 10:43:17 -07002732void JavaVMExt::DumpForSigQuit(std::ostream& os) {
2733 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
2734 if (force_copy) {
2735 os << " (with forcecopy)";
2736 }
2737 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07002738 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07002739 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002740 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002741 os << "; pins=" << pin_table.Size();
2742 }
2743 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002744 MutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002745 os << "; globals=" << globals.Capacity();
2746 }
2747 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002748 MutexLock mu(self, weak_globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002749 if (weak_globals.Capacity() > 0) {
2750 os << " (plus " << weak_globals.Capacity() << " weak)";
2751 }
2752 }
2753 os << '\n';
2754
2755 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002756 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002757 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
2758 }
2759}
2760
Elliott Hughes73e66f72012-05-09 09:34:45 -07002761void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002762 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002763 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002764 MutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002765 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002766 }
2767 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002768 MutexLock mu(self, weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002769 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002770 }
2771 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002772 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002773 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002774 }
2775}
2776
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002777bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
2778 std::string& detail) {
Elliott Hughes75770752011-08-24 17:52:38 -07002779 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002780
2781 // See if we've already loaded this library. If we have, and the class loader
2782 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002783 // TODO: for better results we should canonicalize the pathname (or even compare
2784 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002785 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07002786 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07002787 {
2788 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07002789 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002790 library = libraries->Get(path);
2791 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002792 if (library != NULL) {
2793 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002794 // The library will be associated with class_loader. The JNI
2795 // spec says we can't load the same library into more than one
2796 // class loader.
2797 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2798 "ClassLoader %p; can't open in ClassLoader %p",
2799 path.c_str(), library->GetClassLoader(), class_loader);
2800 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002801 return false;
2802 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002803 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2804 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002805 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002806 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2807 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002808 return false;
2809 }
2810 return true;
2811 }
2812
2813 // Open the shared library. Because we're using a full path, the system
2814 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2815 // resolve this library's dependencies though.)
2816
2817 // Failures here are expected when java.library.path has several entries
2818 // and we have to hunt for the lib.
2819
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002820 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
2821 // class unloading. Libraries will only be unloaded when the reference count (incremented by
2822 // dlopen) becomes zero from dlclose.
2823
Elliott Hughescdf53122011-08-19 15:46:09 -07002824 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002825 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002826 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
2827 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
2828 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07002829
Elliott Hughes84b2f142012-09-27 09:16:28 -07002830 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002831
2832 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002833 detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07002834 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002835 return false;
2836 }
2837
2838 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002839 // TODO: move the locking (and more of this logic) into Libraries.
2840 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002841 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002842 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002843 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002844 if (library == NULL) { // We won race to get libraries_lock
2845 library = new SharedLibrary(path, handle, class_loader);
2846 libraries->Put(path, library);
2847 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002848 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002849 }
2850 if (!created_library) {
2851 LOG(INFO) << "WOW: we lost a race to add shared library: "
2852 << "\"" << path << "\" ClassLoader=" << class_loader;
2853 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002854 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002855
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002856 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002857
2858 bool result = true;
2859 void* sym = dlsym(handle, "JNI_OnLoad");
2860 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002861 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002862 } else {
2863 // Call JNI_OnLoad. We have to override the current class
2864 // loader, which will always be "null" since the stuff at the
2865 // top of the stack is around Runtime.loadLibrary(). (See
2866 // the comments in the JNI FindClass function.)
2867 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2868 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07002869 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002870 self->SetClassLoaderOverride(class_loader);
2871
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002872 int version = 0;
2873 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002874 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002875 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002876 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002877 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002878
Brian Carlstromaded5f72011-10-07 17:15:04 -07002879 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002880
Elliott Hughes83a25322013-03-14 11:18:53 -07002881 if (IsBadJniVersion(version)) {
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07002882 StringAppendF(&detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
2883 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07002884 // It's unwise to call dlclose() here, but we can mark it
2885 // as bad and ensure that future load attempts will fail.
2886 // We don't know how far JNI_OnLoad got, so there could
2887 // be some partially-initialized stuff accessible through
2888 // newly-registered native method calls. We could try to
2889 // unregister them, but that doesn't seem worthwhile.
2890 result = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07002891 }
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07002892 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2893 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002894 }
2895
2896 library->SetResult(result);
2897 return result;
2898}
2899
Mathieu Chartier66f19252012-09-18 08:57:04 -07002900void* JavaVMExt::FindCodeForNativeMethod(AbstractMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002901 CHECK(m->IsNative());
2902
2903 Class* c = m->GetDeclaringClass();
2904
2905 // If this is a static method, it could be called before the class
2906 // has been initialized.
2907 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002908 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002909 return NULL;
2910 }
2911 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002912 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002913 }
2914
Brian Carlstrom16192862011-09-12 17:50:06 -07002915 std::string detail;
2916 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07002917 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07002918 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002919 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07002920 native_method = libraries->FindNativeMethod(m, detail);
2921 }
Ian Rogers62d6c772013-02-27 08:32:07 -08002922 // Throwing can cause libraries_lock to be reacquired.
Brian Carlstrom16192862011-09-12 17:50:06 -07002923 if (native_method == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002924 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
2925 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002926 }
2927 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002928}
2929
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002930void JavaVMExt::VisitRoots(RootVisitor* visitor, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002931 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07002932 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002933 MutexLock mu(self, globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002934 globals.VisitRoots(visitor, arg);
2935 }
2936 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002937 MutexLock mu(self, pins_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002938 pin_table.VisitRoots(visitor, arg);
2939 }
2940 // The weak_globals table is visited by the GC itself (because it mutates the table).
2941}
2942
Elliott Hughesc8fece32013-01-02 11:27:23 -08002943void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
2944 size_t method_count) {
2945 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
2946 if (c.get() == NULL) {
2947 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
2948 }
2949 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
2950}
2951
Ian Rogersdf20fe02011-07-20 20:34:16 -07002952} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002953
2954std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2955 switch (rhs) {
2956 case JNIInvalidRefType:
2957 os << "JNIInvalidRefType";
2958 return os;
2959 case JNILocalRefType:
2960 os << "JNILocalRefType";
2961 return os;
2962 case JNIGlobalRefType:
2963 os << "JNIGlobalRefType";
2964 return os;
2965 case JNIWeakGlobalRefType:
2966 os << "JNIWeakGlobalRefType";
2967 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002968 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002969 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002970 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002971 }
2972}