blob: d1de6e6f5abcc3a0411fcc4b6b13a4f067bf81e2 [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
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070052using ::art::mirror::AbstractMethod;
53using ::art::mirror::Array;
54using ::art::mirror::BooleanArray;
55using ::art::mirror::ByteArray;
56using ::art::mirror::CharArray;
57using ::art::mirror::Class;
58using ::art::mirror::ClassLoader;
59using ::art::mirror::DoubleArray;
60using ::art::mirror::Field;
61using ::art::mirror::FloatArray;
62using ::art::mirror::IntArray;
63using ::art::mirror::LongArray;
64using ::art::mirror::Object;
65using ::art::mirror::ObjectArray;
66using ::art::mirror::ShortArray;
67using ::art::mirror::String;
68using ::art::mirror::Throwable;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070070namespace art {
71
Brian Carlstrom7934ac22013-07-26 10:54:15 -070072static const size_t kMonitorsInitial = 32; // Arbitrary.
73static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070074
Brian Carlstrom7934ac22013-07-26 10:54:15 -070075static const size_t kLocalsInitial = 64; // Arbitrary.
76static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070077
Brian Carlstrom7934ac22013-07-26 10:54:15 -070078static const size_t kPinTableInitial = 16; // Arbitrary.
79static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070080
Brian Carlstrom7934ac22013-07-26 10:54:15 -070081static size_t gGlobalsInitial = 512; // Arbitrary.
82static size_t gGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Elliott Hughes2ced6a52011-10-16 18:44:48 -070083
Brian Carlstrom7934ac22013-07-26 10:54:15 -070084static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
85static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Ian Rogersdf20fe02011-07-20 20:34:16 -070086
Ian Rogers00f7d0e2012-07-19 15:28:27 -070087static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070088 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescdf53122011-08-19 15:46:09 -070089 if (obj == NULL) {
90 return NULL;
91 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070092 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -070093 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -070094 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -070095 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
96 return reinterpret_cast<jweak>(ref);
97}
98
Jeff Hao19c5d372013-03-15 14:33:43 -070099static bool IsBadJniVersion(int version) {
100 // We don't support JNI_VERSION_1_1. These are the only other valid versions.
101 return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
102}
103
Jeff Hao5d917302013-02-27 17:57:33 -0800104static void CheckMethodArguments(AbstractMethod* m, uint32_t* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700105 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700106 MethodHelper mh(m);
Ian Rogers50b35e22012-10-04 10:09:15 -0700107 const DexFile::TypeList* params = mh.GetParameterTypeList();
108 if (params == NULL) {
109 return; // No arguments so nothing to check.
110 }
Jeff Hao5d917302013-02-27 17:57:33 -0800111 uint32_t offset = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -0700112 uint32_t num_params = params->Size();
Elliott Hughesb264f082012-04-06 17:10:10 -0700113 size_t error_count = 0;
Jeff Hao5d917302013-02-27 17:57:33 -0800114 if (!m->IsStatic()) {
115 offset = 1;
116 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700117 for (uint32_t i = 0; i < num_params; i++) {
118 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
119 Class* param_type = mh.GetClassFromTypeIdx(type_idx);
120 if (param_type == NULL) {
121 Thread* self = Thread::Current();
122 CHECK(self->IsExceptionPending());
123 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
124 << mh.GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
Ian Rogers62d6c772013-02-27 08:32:07 -0800125 << self->GetException(NULL)->Dump();
Ian Rogers50b35e22012-10-04 10:09:15 -0700126 self->ClearException();
127 ++error_count;
128 } else if (!param_type->IsPrimitive()) {
129 // TODO: check primitives are in range.
Jeff Hao5d917302013-02-27 17:57:33 -0800130 Object* argument = reinterpret_cast<Object*>(args[i + offset]);
Ian Rogers50b35e22012-10-04 10:09:15 -0700131 if (argument != NULL && !argument->InstanceOf(param_type)) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700132 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
133 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
134 ++error_count;
135 }
Jeff Hao5d917302013-02-27 17:57:33 -0800136 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
137 offset++;
Elliott Hughesb264f082012-04-06 17:10:10 -0700138 }
139 }
140 if (error_count > 0) {
141 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
142 // with an argument.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700143 JniAbortF(NULL, "bad arguments passed to %s (see above for details)", PrettyMethod(m).c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -0700144 }
145}
Elliott Hughesb264f082012-04-06 17:10:10 -0700146
Jeff Hao5d917302013-02-27 17:57:33 -0800147void InvokeWithArgArray(const ScopedObjectAccess& soa, AbstractMethod* method,
Jeff Hao6474d192013-03-26 14:08:09 -0700148 ArgArray* arg_array, JValue* result, char result_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700150 uint32_t* args = arg_array->GetArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700151 if (UNLIKELY(soa.Env()->check_jni)) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700152 CheckMethodArguments(method, args);
Elliott Hughes4cacde82012-04-11 18:32:27 -0700153 }
Sebastien Hertz7d658cf2013-07-09 10:56:11 +0200154 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, result_type);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700155}
156
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700157static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
158 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700159 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700160 AbstractMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700161 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700162 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800163 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700164 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800165 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700166 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
167 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700168}
169
Mathieu Chartier66f19252012-09-18 08:57:04 -0700170static AbstractMethod* FindVirtualMethod(Object* receiver, AbstractMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700171 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700172 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700173}
174
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700175static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
176 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700177 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700178 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700179 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700180 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800181 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700182 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800183 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700184 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
185 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700186}
187
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700188static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
189 jobject obj, jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700190 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700191 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700192 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700193 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800194 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700195 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800196 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700197 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
198 return result;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700199}
200
Elliott Hughes6b436852011-08-12 10:16:44 -0700201// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
202// separated with slashes but aren't wrapped with "L;" like regular descriptors
203// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
204// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
205// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700206static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700207 std::string result;
208 // Add the missing "L;" if necessary.
209 if (name[0] == '[') {
210 result = name;
211 } else {
212 result += 'L';
213 result += name;
214 result += ';';
215 }
216 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700217 if (result.find('.') != std::string::npos) {
218 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
219 << "\"" << name << "\"";
220 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700221 }
222 return result;
223}
224
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700225static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, Class* c,
226 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700227 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800228 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
229 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
230 "no %s method \"%s.%s%s\"",
231 kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700232}
233
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700234static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
235 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700236 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700237 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700238 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700239 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700240 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700241
Mathieu Chartier66f19252012-09-18 08:57:04 -0700242 AbstractMethod* method = NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700243 if (is_static) {
244 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700245 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700246 method = c->FindVirtualMethod(name, sig);
247 if (method == NULL) {
248 // No virtual method matching the signature. Search declared
249 // private methods and constructors.
250 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700251 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700252 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700253
Elliott Hughescdf53122011-08-19 15:46:09 -0700254 if (method == NULL || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700255 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700256 return NULL;
257 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700258
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700259 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700260}
261
Ian Rogersef28b142012-11-30 14:22:18 -0800262static ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700263 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800264 AbstractMethod* method = soa.Self()->GetCurrentMethod(NULL);
Ian Rogersef28b142012-11-30 14:22:18 -0800265 if (method == NULL ||
266 method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
267 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700268 }
269 return method->GetDeclaringClass()->GetClassLoader();
270}
271
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
273 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700274 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700275 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700276 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700277 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700278 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700279
280 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700281 Class* field_type;
282 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
283 if (sig[1] != '\0') {
Ian Rogersef28b142012-11-30 14:22:18 -0800284 ClassLoader* cl = GetClassLoader(soa);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700285 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700286 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700287 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700288 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700289 if (field_type == NULL) {
290 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700291 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800292 ThrowLocation throw_location;
293 SirtRef<mirror::Throwable> cause(soa.Self(), soa.Self()->GetException(&throw_location));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700294 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800295 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
296 "no type \"%s\" found and so no field \"%s\" could be found in class "
297 "\"%s\" or its superclasses", sig, name,
298 ClassHelper(c).GetDescriptor());
299 soa.Self()->GetException(NULL)->SetCause(cause.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700300 return NULL;
301 }
302 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800303 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700304 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800305 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700306 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700307 if (field == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800308 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
309 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
310 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
311 sig, name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700312 return NULL;
313 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700314 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700315}
316
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700317static void PinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700318 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700319 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700320 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700321 vm->pin_table.Add(array);
322}
323
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700324static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700325 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700326 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700327 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700328 vm->pin_table.Remove(array);
329}
330
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700331static void ThrowAIOOBE(ScopedObjectAccess& soa, Array* array, jsize start,
332 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700333 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700334 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800335 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
336 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
337 "%s offset=%d length=%d %s.length=%d",
338 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700339}
Ian Rogers0571d352011-11-03 19:51:38 -0700340
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700341static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
342 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700343 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800344 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
345 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
346 "offset=%d length=%d string.length()=%d", start, length,
347 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700348}
Elliott Hughes814e4032011-08-23 12:07:56 -0700349
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700350int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700351 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700352 // Turn the const char* into a java.lang.String.
353 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
354 if (msg != NULL && s.get() == NULL) {
355 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700356 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700357
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700358 // Choose an appropriate constructor and set up the arguments.
359 jvalue args[2];
360 const char* signature;
361 if (msg == NULL && cause == NULL) {
362 signature = "()V";
363 } else if (msg != NULL && cause == NULL) {
364 signature = "(Ljava/lang/String;)V";
365 args[0].l = s.get();
366 } else if (msg == NULL && cause != NULL) {
367 signature = "(Ljava/lang/Throwable;)V";
368 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700369 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700370 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
371 args[0].l = s.get();
372 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700373 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700374 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
375 if (mid == NULL) {
Ian Rogersef28b142012-11-30 14:22:18 -0800376 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700377 LOG(ERROR) << "No <init>" << signature << " in "
378 << PrettyClass(soa.Decode<Class*>(exception_class));
379 return JNI_ERR;
380 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700381
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700382 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
383 if (exception.get() == NULL) {
384 return JNI_ERR;
385 }
Ian Rogersef28b142012-11-30 14:22:18 -0800386 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800387 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
388 soa.Self()->SetException(throw_location, soa.Decode<Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700389 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700390}
391
Elliott Hughes462c9442012-03-23 18:47:50 -0700392static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700393 if (vm == NULL || p_env == NULL) {
394 return JNI_ERR;
395 }
396
Elliott Hughes462c9442012-03-23 18:47:50 -0700397 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700398 Thread* self = Thread::Current();
399 if (self != NULL) {
400 *p_env = self->GetJniEnv();
401 return JNI_OK;
402 }
403
404 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
405
406 // No threads allowed in zygote mode.
407 if (runtime->IsZygote()) {
408 LOG(ERROR) << "Attempt to attach a thread in the zygote";
409 return JNI_ERR;
410 }
411
Elliott Hughes462c9442012-03-23 18:47:50 -0700412 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
413 const char* thread_name = NULL;
Ian Rogers365c1022012-06-22 15:05:28 -0700414 jobject thread_group = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -0700415 if (args != NULL) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700416 if (IsBadJniVersion(args->version)) {
417 LOG(ERROR) << "Bad JNI version passed to "
418 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
419 << args->version;
420 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700421 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700422 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700423 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700424 }
Elliott Hughes75770752011-08-24 17:52:38 -0700425
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800426 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700427 *p_env = NULL;
428 return JNI_ERR;
429 } else {
430 *p_env = Thread::Current()->GetJniEnv();
431 return JNI_OK;
432 }
Elliott Hughes75770752011-08-24 17:52:38 -0700433}
434
Elliott Hughes79082e32011-08-25 12:07:32 -0700435class SharedLibrary {
436 public:
437 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
438 : path_(path),
439 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700440 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700441 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700442 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700443 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700444 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700445 }
446
Elliott Hughes79082e32011-08-25 12:07:32 -0700447 Object* GetClassLoader() {
448 return class_loader_;
449 }
450
451 std::string GetPath() {
452 return path_;
453 }
454
455 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700456 * Check the result of an earlier call to JNI_OnLoad on this library.
457 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700458 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700459 bool CheckOnLoadResult()
460 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700461 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700462 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700463 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
464 bool okay;
465 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700466 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700467
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700468 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
469 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
470 // caller can continue.
471 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
472 okay = true;
473 } else {
474 while (jni_on_load_result_ == kPending) {
475 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700476 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700477 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700478
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700479 okay = (jni_on_load_result_ == kOkay);
480 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
481 << (okay ? "succeeded" : "failed") << "]";
482 }
483 }
484 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700485 return okay;
486 }
487
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700488 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700489 Thread* self = Thread::Current();
490 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700491
Elliott Hughes79082e32011-08-25 12:07:32 -0700492 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700493 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700494
495 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700496 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700497 }
498
499 void* FindSymbol(const std::string& symbol_name) {
500 return dlsym(handle_, symbol_name.c_str());
501 }
502
503 private:
504 enum JNI_OnLoadState {
505 kPending,
506 kFailed,
507 kOkay,
508 };
509
510 // Path to library "/system/lib/libjni.so".
511 std::string path_;
512
513 // The void* returned by dlopen(3).
514 void* handle_;
515
516 // The ClassLoader this library is associated with.
517 Object* class_loader_;
518
519 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700520 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700521 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700522 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700523 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700524 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700525 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700526 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700527};
528
Elliott Hughes79082e32011-08-25 12:07:32 -0700529// This exists mainly to keep implementation details out of the header file.
530class Libraries {
531 public:
532 Libraries() {
533 }
534
535 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700536 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700537 }
538
Elliott Hughesae80b492012-04-24 10:43:17 -0700539 void Dump(std::ostream& os) const {
540 bool first = true;
541 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
542 if (!first) {
543 os << ' ';
544 }
545 first = false;
546 os << it->first;
547 }
548 }
549
550 size_t size() const {
551 return libraries_.size();
552 }
553
Elliott Hughes79082e32011-08-25 12:07:32 -0700554 SharedLibrary* Get(const std::string& path) {
Ian Rogers712462a2012-04-12 16:32:29 -0700555 It it = libraries_.find(path);
556 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700557 }
558
559 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700560 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700561 }
562
563 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700564 void* FindNativeMethod(const AbstractMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700565 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700566 std::string jni_short_name(JniShortName(m));
567 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700568 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700569 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
570 SharedLibrary* library = it->second;
571 if (library->GetClassLoader() != declaring_class_loader) {
572 // We only search libraries loaded by the appropriate ClassLoader.
573 continue;
574 }
575 // Try the short name then the long name...
576 void* fn = library->FindSymbol(jni_short_name);
577 if (fn == NULL) {
578 fn = library->FindSymbol(jni_long_name);
579 }
580 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800581 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
582 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700583 return fn;
584 }
585 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700586 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700587 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700588 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700589 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700590 return NULL;
591 }
592
593 private:
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700594 typedef SafeMap<std::string, SharedLibrary*>::const_iterator It; // TODO: C++0x auto
Elliott Hughes79082e32011-08-25 12:07:32 -0700595
Elliott Hughesa0e18062012-04-13 15:59:59 -0700596 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700597};
598
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700599JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
600 jvalue* args) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700601 AbstractMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700602 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700603 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800604 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700605 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800606 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700607 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
608 return result;
Elliott Hughesd07986f2011-12-06 18:27:45 -0800609}
610
Elliott Hughescdf53122011-08-19 15:46:09 -0700611class JNI {
612 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700613 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700614 return JNI_VERSION_1_6;
615 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700616
Ian Rogers25e8b912012-09-07 11:31:36 -0700617 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700618 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700619 return NULL;
620 }
621
Elliott Hughescdf53122011-08-19 15:46:09 -0700622 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700623 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700624 Runtime* runtime = Runtime::Current();
625 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700626 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700627 Class* c = NULL;
628 if (runtime->IsStarted()) {
Ian Rogersef28b142012-11-30 14:22:18 -0800629 ClassLoader* cl = GetClassLoader(soa);
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800630 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700631 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800632 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700633 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700634 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700635 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700636
Elliott Hughescdf53122011-08-19 15:46:09 -0700637 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700638 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700639 AbstractMethod* method = soa.Decode<AbstractMethod*>(java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700640 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700641 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700642
Elliott Hughescdf53122011-08-19 15:46:09 -0700643 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700644 ScopedObjectAccess soa(env);
645 Field* field = soa.Decode<Field*>(java_field);
646 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700647 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700648
Elliott Hughescdf53122011-08-19 15:46:09 -0700649 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700650 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700651 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700652 return soa.AddLocalReference<jobject>(method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700653 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700654
Elliott Hughescdf53122011-08-19 15:46:09 -0700655 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700656 ScopedObjectAccess soa(env);
657 Field* field = soa.DecodeField(fid);
658 return soa.AddLocalReference<jobject>(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700659 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700660
Elliott Hughes37f7a402011-08-22 18:56:01 -0700661 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700662 ScopedObjectAccess soa(env);
663 Object* o = soa.Decode<Object*>(java_object);
664 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700665 }
666
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700667 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700668 ScopedObjectAccess soa(env);
669 Class* c = soa.Decode<Class*>(java_class);
670 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700671 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700672
Elliott Hughes37f7a402011-08-22 18:56:01 -0700673 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700674 ScopedObjectAccess soa(env);
675 Class* c1 = soa.Decode<Class*>(java_class1);
676 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700677 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700678 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700679
Elliott Hughese84278b2012-03-22 10:06:53 -0700680 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700681 ScopedObjectAccess soa(env);
Elliott Hughes96a98872012-12-19 14:21:15 -0800682 if (java_class == NULL) {
683 JniAbortF("IsInstanceOf", "null class (second argument)");
684 }
Elliott Hughes37f7a402011-08-22 18:56:01 -0700685 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700686 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700687 return JNI_TRUE;
688 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700689 Object* obj = soa.Decode<Object*>(jobj);
690 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700691 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700692 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700693 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700694
Elliott Hughes37f7a402011-08-22 18:56:01 -0700695 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700696 ScopedObjectAccess soa(env);
697 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700698 if (exception == NULL) {
699 return JNI_ERR;
700 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800701 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
702 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700703 return JNI_OK;
704 }
705
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700706 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700707 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700708 }
709
710 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700711 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700712 }
713
714 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700715 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700716 }
717
718 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700719 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700720
Ian Rogers62d6c772013-02-27 08:32:07 -0800721 SirtRef<mirror::Object> old_throw_this_object(soa.Self(), NULL);
722 SirtRef<mirror::AbstractMethod> old_throw_method(soa.Self(), NULL);
723 SirtRef<mirror::Throwable> old_exception(soa.Self(), NULL);
724 uint32_t old_throw_dex_pc;
725 {
726 ThrowLocation old_throw_location;
727 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
728 old_throw_this_object.reset(old_throw_location.GetThis());
729 old_throw_method.reset(old_throw_location.GetMethod());
730 old_exception.reset(old_exception_obj);
731 old_throw_dex_pc = old_throw_location.GetDexPc();
732 soa.Self()->ClearException();
733 }
734 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(old_exception.get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700735 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
736 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
737 if (mid == NULL) {
738 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Ian Rogers62d6c772013-02-27 08:32:07 -0800739 << PrettyTypeOf(old_exception.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700740 } else {
741 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800742 if (soa.Self()->IsExceptionPending()) {
743 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(NULL))
Elliott Hughes72025e52011-08-23 17:50:30 -0700744 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800745 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700746 }
747 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800748 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
749 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700750
Ian Rogers62d6c772013-02-27 08:32:07 -0800751 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700752 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700753
Elliott Hughescdf53122011-08-19 15:46:09 -0700754 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700755 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800756 Object* exception = soa.Self()->GetException(NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700757 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700758 }
759
Ian Rogers25e8b912012-09-07 11:31:36 -0700760 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700761 LOG(FATAL) << "JNI FatalError called: " << msg;
762 }
763
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700764 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800765 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700766 return JNI_ERR;
767 }
Ian Rogersef28b142012-11-30 14:22:18 -0800768 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700769 return JNI_OK;
770 }
771
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700772 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700773 ScopedObjectAccess soa(env);
774 Object* survivor = soa.Decode<Object*>(java_survivor);
775 soa.Env()->PopFrame();
776 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700777 }
778
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700779 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800780 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700781 }
782
Elliott Hughescdf53122011-08-19 15:46:09 -0700783 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700784 if (obj == NULL) {
785 return NULL;
786 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700787 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700788 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700789 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700790 Object* decoded_obj = soa.Decode<Object*>(obj);
Ian Rogers50b35e22012-10-04 10:09:15 -0700791 MutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700792 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700793 return reinterpret_cast<jobject>(ref);
794 }
795
796 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700797 if (obj == NULL) {
798 return;
799 }
Ian Rogersef28b142012-11-30 14:22:18 -0800800 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700801 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800802 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
803 MutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700804
805 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
806 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
807 << "failed to find entry";
808 }
809 }
810
811 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700812 ScopedObjectAccess soa(env);
813 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700814 }
815
816 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700817 if (obj == NULL) {
818 return;
819 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700820 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700821 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700822 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -0700823 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700824
825 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
826 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
827 << "failed to find entry";
828 }
829 }
830
831 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700832 if (obj == NULL) {
833 return NULL;
834 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700835 ScopedObjectAccess soa(env);
Elliott Hughes9dcd45c2013-07-29 14:40:52 -0700836 return soa.AddLocalReference<jobject>(soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700837 }
838
839 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700840 if (obj == NULL) {
841 return;
842 }
Ian Rogersef28b142012-11-30 14:22:18 -0800843 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700844
Ian Rogersef28b142012-11-30 14:22:18 -0800845 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700846 if (!locals.Remove(cookie, obj)) {
847 // Attempting to delete a local reference that is not in the
848 // topmost local reference frame is a no-op. DeleteLocalRef returns
849 // void and doesn't throw any exceptions, but we should probably
850 // complain about it so the user will notice that things aren't
851 // going quite the way they expect.
852 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
853 << "failed to find entry";
854 }
855 }
856
857 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700858 ScopedObjectAccess soa(env);
Ian Rogers120f1c72012-09-28 17:17:10 -0700859 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700860 }
861
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700862 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700863 ScopedObjectAccess soa(env);
864 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700865 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700866 return NULL;
867 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700868 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700869 }
870
Elliott Hughese84278b2012-03-22 10:06:53 -0700871 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700872 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700873 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -0700874 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700875 va_end(args);
876 return result;
877 }
878
Elliott Hughes72025e52011-08-23 17:50:30 -0700879 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700880 ScopedObjectAccess soa(env);
881 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700882 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700883 return NULL;
884 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700885 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700886 if (result == NULL) {
887 return NULL;
888 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700889 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700890 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700891 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700892 return local_result;
893 } else {
894 return NULL;
895 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700896 }
897
Elliott Hughes72025e52011-08-23 17:50:30 -0700898 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700899 ScopedObjectAccess soa(env);
900 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700901 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700902 return NULL;
903 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700904 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700905 if (result == NULL) {
906 return NULL;
907 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700908 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700909 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700910 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700911 return local_result;
912 } else {
913 return NULL;
914 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700915 }
916
Elliott Hughescdf53122011-08-19 15:46:09 -0700917 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700918 ScopedObjectAccess soa(env);
919 return FindMethodID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700920 }
921
922 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700923 ScopedObjectAccess soa(env);
924 return FindMethodID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700925 }
926
Elliott Hughes72025e52011-08-23 17:50:30 -0700927 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700928 va_list ap;
929 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700930 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700931 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700932 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700933 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700934 }
935
Elliott Hughes72025e52011-08-23 17:50:30 -0700936 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700937 ScopedObjectAccess soa(env);
938 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
939 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700940 }
941
Elliott Hughes72025e52011-08-23 17:50:30 -0700942 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700943 ScopedObjectAccess soa(env);
944 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
945 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700946 }
947
Elliott Hughes72025e52011-08-23 17:50:30 -0700948 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700949 va_list ap;
950 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700951 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700952 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700953 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700954 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700955 }
956
Elliott Hughes72025e52011-08-23 17:50:30 -0700957 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700958 ScopedObjectAccess soa(env);
959 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700960 }
961
Elliott Hughes72025e52011-08-23 17:50:30 -0700962 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700963 ScopedObjectAccess soa(env);
964 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700965 }
966
Elliott Hughes72025e52011-08-23 17:50:30 -0700967 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700968 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700969 va_list ap;
970 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700971 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700972 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700973 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700974 }
975
Elliott Hughes72025e52011-08-23 17:50:30 -0700976 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700977 ScopedObjectAccess soa(env);
978 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700979 }
980
Elliott Hughes72025e52011-08-23 17:50:30 -0700981 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700982 ScopedObjectAccess soa(env);
983 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700984 }
985
Elliott Hughes72025e52011-08-23 17:50:30 -0700986 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700987 va_list ap;
988 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700989 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700990 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700991 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700992 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700993 }
994
Elliott Hughes72025e52011-08-23 17:50:30 -0700995 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700996 ScopedObjectAccess soa(env);
997 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700998 }
999
Elliott Hughes72025e52011-08-23 17:50:30 -07001000 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001001 ScopedObjectAccess soa(env);
1002 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001003 }
1004
Elliott Hughes72025e52011-08-23 17:50:30 -07001005 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001006 va_list ap;
1007 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001008 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001009 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001010 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001011 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001012 }
1013
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001015 ScopedObjectAccess soa(env);
1016 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001017 }
1018
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001020 ScopedObjectAccess soa(env);
1021 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001022 }
1023
Elliott Hughes72025e52011-08-23 17:50:30 -07001024 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001025 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001026 va_list ap;
1027 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001028 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001029 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001030 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001031 }
1032
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001034 ScopedObjectAccess soa(env);
1035 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001036 }
1037
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001039 ScopedObjectAccess soa(env);
1040 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001041 }
1042
Elliott Hughes72025e52011-08-23 17:50:30 -07001043 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001044 va_list ap;
1045 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001046 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001047 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001049 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001050 }
1051
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001053 ScopedObjectAccess soa(env);
1054 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001055 }
1056
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001058 ScopedObjectAccess soa(env);
1059 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001060 }
1061
Elliott Hughes72025e52011-08-23 17:50:30 -07001062 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 va_list ap;
1064 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001065 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001066 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001068 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001069 }
1070
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001072 ScopedObjectAccess soa(env);
1073 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 }
1075
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001077 ScopedObjectAccess soa(env);
1078 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001079 }
1080
Elliott Hughes72025e52011-08-23 17:50:30 -07001081 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 va_list ap;
1083 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001084 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001085 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001087 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 }
1089
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001091 ScopedObjectAccess soa(env);
1092 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 }
1094
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001096 ScopedObjectAccess soa(env);
1097 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001098 }
1099
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 va_list ap;
1102 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001103 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001104 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001105 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 }
1107
Elliott Hughes72025e52011-08-23 17:50:30 -07001108 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001109 ScopedObjectAccess soa(env);
1110 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001111 }
1112
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001114 ScopedObjectAccess soa(env);
1115 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 }
1117
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001118 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001120 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001121 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001122 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1123 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 va_end(ap);
1125 return local_result;
1126 }
1127
1128 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001129 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001130 ScopedObjectAccess soa(env);
1131 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1132 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 }
1134
1135 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001136 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001137 ScopedObjectAccess soa(env);
1138 JValue result(InvokeWithJValues(soa, obj, mid, args));
1139 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 }
1141
1142 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001143 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001145 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001146 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001147 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001149 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 }
1151
1152 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001153 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001154 ScopedObjectAccess soa(env);
1155 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 }
1157
1158 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001159 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001160 ScopedObjectAccess soa(env);
1161 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 }
1163
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001164 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001166 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001167 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001168 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001170 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 }
1172
1173 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001174 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001175 ScopedObjectAccess soa(env);
1176 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 }
1178
1179 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001180 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001181 ScopedObjectAccess soa(env);
1182 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001183 }
1184
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001185 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001186 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001188 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001189 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001191 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001192 }
1193
1194 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001195 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001196 ScopedObjectAccess soa(env);
1197 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001198 }
1199
1200 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001201 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001202 ScopedObjectAccess soa(env);
1203 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001204 }
1205
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001206 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001207 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001208 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001209 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001210 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001212 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 }
1214
1215 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001216 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001217 ScopedObjectAccess soa(env);
1218 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 }
1220
1221 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001222 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001223 ScopedObjectAccess soa(env);
1224 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001225 }
1226
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001227 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001229 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001230 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001231 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001233 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001234 }
1235
1236 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001237 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001238 ScopedObjectAccess soa(env);
1239 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 }
1241
1242 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001243 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001244 ScopedObjectAccess soa(env);
1245 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 }
1247
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001248 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001249 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001250 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001251 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001252 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001254 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 }
1256
1257 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001258 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001259 ScopedObjectAccess soa(env);
1260 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001261 }
1262
1263 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001264 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001265 ScopedObjectAccess soa(env);
1266 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001267 }
1268
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001269 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001271 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001272 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001273 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001275 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 }
1277
1278 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001279 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001280 ScopedObjectAccess soa(env);
1281 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 }
1283
1284 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001285 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001286 ScopedObjectAccess soa(env);
1287 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001288 }
1289
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001290 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001292 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001293 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001294 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001296 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001297 }
1298
1299 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001300 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001301 ScopedObjectAccess soa(env);
1302 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001303 }
1304
1305 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001306 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001307 ScopedObjectAccess soa(env);
1308 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001309 }
1310
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001311 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001313 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001314 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001315 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 va_end(ap);
1317 }
1318
1319 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001320 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001321 ScopedObjectAccess soa(env);
1322 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 }
1324
1325 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001326 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001327 ScopedObjectAccess soa(env);
1328 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 }
1330
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001331 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001332 ScopedObjectAccess soa(env);
1333 return FindFieldID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001334 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001335
1336
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001337 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001338 ScopedObjectAccess soa(env);
1339 return FindFieldID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001341
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001342 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001343 ScopedObjectAccess soa(env);
1344 Object* o = soa.Decode<Object*>(obj);
1345 Field* f = soa.DecodeField(fid);
1346 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001347 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001348
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001349 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001350 ScopedObjectAccess soa(env);
1351 Field* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001352 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001353 }
1354
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001355 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001356 ScopedObjectAccess soa(env);
1357 Object* o = soa.Decode<Object*>(java_object);
1358 Object* v = soa.Decode<Object*>(java_value);
1359 Field* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001360 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 }
1362
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001363 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001364 ScopedObjectAccess soa(env);
1365 Object* v = soa.Decode<Object*>(java_value);
1366 Field* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001367 f->SetObject(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 }
1369
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001370#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001371 ScopedObjectAccess soa(env); \
1372 Object* o = soa.Decode<Object*>(instance); \
1373 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001374 return f->fn(o)
1375
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001376#define GET_STATIC_PRIMITIVE_FIELD(fn) \
1377 ScopedObjectAccess soa(env); \
1378 Field* f = soa.DecodeField(fid); \
1379 return f->fn(f->GetDeclaringClass())
1380
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001381#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001382 ScopedObjectAccess soa(env); \
1383 Object* o = soa.Decode<Object*>(instance); \
1384 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001385 f->fn(o, value)
1386
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001387#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
1388 ScopedObjectAccess soa(env); \
1389 Field* f = soa.DecodeField(fid); \
1390 f->fn(f->GetDeclaringClass(), value)
1391
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001392 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1393 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 }
1395
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001396 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1397 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 }
1399
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001400 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1401 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001402 }
1403
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001404 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1405 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 }
1407
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001408 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1409 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001410 }
1411
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001412 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1413 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001414 }
1415
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001416 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1417 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 }
1419
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001420 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1421 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 }
1423
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001424 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001425 GET_STATIC_PRIMITIVE_FIELD(GetBoolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 }
1427
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001428 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001429 GET_STATIC_PRIMITIVE_FIELD(GetByte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 }
1431
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001432 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001433 GET_STATIC_PRIMITIVE_FIELD(GetChar);
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 }
1435
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001436 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001437 GET_STATIC_PRIMITIVE_FIELD(GetShort);
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 }
1439
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001440 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001441 GET_STATIC_PRIMITIVE_FIELD(GetInt);
Elliott Hughescdf53122011-08-19 15:46:09 -07001442 }
1443
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001444 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001445 GET_STATIC_PRIMITIVE_FIELD(GetLong);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001446 }
1447
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001448 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001449 GET_STATIC_PRIMITIVE_FIELD(GetFloat);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001450 }
1451
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001452 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001453 GET_STATIC_PRIMITIVE_FIELD(GetDouble);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001454 }
1455
1456 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1457 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1458 }
1459
1460 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1461 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1462 }
1463
1464 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1465 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1466 }
1467
1468 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1469 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1470 }
1471
1472 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1473 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1474 }
1475
1476 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1477 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1478 }
1479
1480 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1481 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1482 }
1483
1484 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1485 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1486 }
1487
1488 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001489 SET_STATIC_PRIMITIVE_FIELD(SetBoolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001490 }
1491
1492 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001493 SET_STATIC_PRIMITIVE_FIELD(SetByte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001494 }
1495
1496 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001497 SET_STATIC_PRIMITIVE_FIELD(SetChar, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001498 }
1499
1500 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001501 SET_STATIC_PRIMITIVE_FIELD(SetFloat, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001502 }
1503
1504 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001505 SET_STATIC_PRIMITIVE_FIELD(SetDouble, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001506 }
1507
1508 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001509 SET_STATIC_PRIMITIVE_FIELD(SetInt, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001510 }
1511
1512 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001513 SET_STATIC_PRIMITIVE_FIELD(SetLong, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001514 }
1515
1516 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001517 SET_STATIC_PRIMITIVE_FIELD(SetShort, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001518 }
1519
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001520 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001521 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001522 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001523 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001524 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1525 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001526 va_end(ap);
1527 return local_result;
1528 }
1529
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001530 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001531 ScopedObjectAccess soa(env);
1532 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1533 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001534 }
1535
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001536 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001537 ScopedObjectAccess soa(env);
1538 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1539 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001540 }
1541
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001542 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001543 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001544 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001545 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001546 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001547 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001548 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001549 }
1550
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001551 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001552 ScopedObjectAccess soa(env);
1553 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001554 }
1555
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001556 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001557 ScopedObjectAccess soa(env);
1558 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001559 }
1560
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001561 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001562 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001563 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001564 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001565 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001567 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001568 }
1569
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001570 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001571 ScopedObjectAccess soa(env);
1572 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 }
1574
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001575 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001576 ScopedObjectAccess soa(env);
1577 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001578 }
1579
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001580 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001581 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001582 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001583 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001584 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001586 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001587 }
1588
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001589 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001590 ScopedObjectAccess soa(env);
1591 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001592 }
1593
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001594 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001595 ScopedObjectAccess soa(env);
1596 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001597 }
1598
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001599 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001600 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001601 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001602 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001603 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001604 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001605 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 }
1607
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001608 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001609 ScopedObjectAccess soa(env);
1610 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 }
1612
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001613 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001614 ScopedObjectAccess soa(env);
1615 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 }
1617
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001618 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001619 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001620 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001621 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001622 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001623 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001624 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 }
1626
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001627 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001628 ScopedObjectAccess soa(env);
1629 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001630 }
1631
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001632 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001633 ScopedObjectAccess soa(env);
1634 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001635 }
1636
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001637 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001638 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001639 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001640 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001641 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001643 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001644 }
1645
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001646 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001647 ScopedObjectAccess soa(env);
1648 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 }
1650
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001651 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001652 ScopedObjectAccess soa(env);
1653 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001654 }
1655
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001656 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001657 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001658 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001659 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001660 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001661 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001662 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001663 }
1664
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001665 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001666 ScopedObjectAccess soa(env);
1667 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001668 }
1669
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001670 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001671 ScopedObjectAccess soa(env);
1672 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 }
1674
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001675 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001676 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001677 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001678 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001679 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001680 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001681 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001682 }
1683
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001684 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001685 ScopedObjectAccess soa(env);
1686 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 }
1688
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001689 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001690 ScopedObjectAccess soa(env);
1691 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001692 }
1693
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001694 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001695 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001696 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001697 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001698 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 va_end(ap);
1700 }
1701
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001702 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001703 ScopedObjectAccess soa(env);
1704 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 }
1706
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001707 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001708 ScopedObjectAccess soa(env);
1709 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001710 }
1711
Elliott Hughes814e4032011-08-23 12:07:56 -07001712 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001713 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001714 String* result = String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001715 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001716 }
1717
1718 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 if (utf == NULL) {
1720 return NULL;
1721 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001722 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001723 String* result = String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001724 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 }
1726
Elliott Hughes814e4032011-08-23 12:07:56 -07001727 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001728 ScopedObjectAccess soa(env);
1729 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001730 }
1731
1732 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001733 ScopedObjectAccess soa(env);
1734 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001735 }
1736
Elliott Hughesb465ab02011-08-24 11:21:21 -07001737 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001738 ScopedObjectAccess soa(env);
1739 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001740 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001741 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001742 } else {
1743 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1744 memcpy(buf, chars + start, length * sizeof(jchar));
1745 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001746 }
1747
Elliott Hughesb465ab02011-08-24 11:21:21 -07001748 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001749 ScopedObjectAccess soa(env);
1750 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001751 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001752 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001753 } else {
1754 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1755 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1756 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001757 }
1758
Elliott Hughes75770752011-08-24 17:52:38 -07001759 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001760 ScopedObjectAccess soa(env);
1761 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001762 const CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001763 PinPrimitiveArray(soa, chars);
Elliott Hughes75770752011-08-24 17:52:38 -07001764 if (is_copy != NULL) {
1765 *is_copy = JNI_FALSE;
1766 }
1767 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001768 }
1769
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001770 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001771 ScopedObjectAccess soa(env);
1772 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001773 }
1774
Elliott Hughes75770752011-08-24 17:52:38 -07001775 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001776 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001777 }
1778
Elliott Hughes75770752011-08-24 17:52:38 -07001779 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001780 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001781 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001782 }
1783
Elliott Hughes75770752011-08-24 17:52:38 -07001784 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001785 if (java_string == NULL) {
1786 return NULL;
1787 }
1788 if (is_copy != NULL) {
1789 *is_copy = JNI_TRUE;
1790 }
Ian Rogersef28b142012-11-30 14:22:18 -08001791 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001792 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001793 size_t byte_count = s->GetUtfLength();
1794 char* bytes = new char[byte_count + 1];
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001795 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001796 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1797 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1798 bytes[byte_count] = '\0';
1799 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001800 }
1801
Elliott Hughes75770752011-08-24 17:52:38 -07001802 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001803 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001804 }
1805
Elliott Hughesbd935992011-08-22 11:59:34 -07001806 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001807 ScopedObjectAccess soa(env);
1808 Object* obj = soa.Decode<Object*>(java_array);
Elliott Hughes96a98872012-12-19 14:21:15 -08001809 if (!obj->IsArrayInstance()) {
1810 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1811 }
Elliott Hughesbd935992011-08-22 11:59:34 -07001812 Array* array = obj->AsArray();
1813 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001814 }
1815
Elliott Hughes814e4032011-08-23 12:07:56 -07001816 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001817 ScopedObjectAccess soa(env);
1818 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1819 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001820 }
1821
1822 static void SetObjectArrayElement(JNIEnv* env,
1823 jobjectArray java_array, jsize index, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001824 ScopedObjectAccess soa(env);
1825 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1826 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001827 array->Set(index, value);
1828 }
1829
1830 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001831 ScopedObjectAccess soa(env);
1832 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001833 }
1834
1835 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001836 ScopedObjectAccess soa(env);
1837 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001838 }
1839
1840 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001841 ScopedObjectAccess soa(env);
1842 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001843 }
1844
1845 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001846 ScopedObjectAccess soa(env);
1847 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001848 }
1849
1850 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001851 ScopedObjectAccess soa(env);
1852 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001853 }
1854
1855 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001856 ScopedObjectAccess soa(env);
1857 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001858 }
1859
1860 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001861 ScopedObjectAccess soa(env);
1862 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001863 }
1864
1865 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001866 ScopedObjectAccess soa(env);
Elliott Hughes96a98872012-12-19 14:21:15 -08001867 if (length < 0) {
1868 JniAbortF("NewObjectArray", "negative array length: %d", length);
1869 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001870
1871 // Compute the array class corresponding to the given element class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001872 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001873 std::string descriptor;
1874 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001875 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001876
1877 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001878 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1879 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001880 return NULL;
1881 }
1882
Elliott Hughes75770752011-08-24 17:52:38 -07001883 // Allocate and initialize if necessary.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001884 Class* array_class = soa.Decode<Class*>(java_array_class.get());
Ian Rogers50b35e22012-10-04 10:09:15 -07001885 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001886 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001887 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07001888 for (jsize i = 0; i < length; ++i) {
1889 result->Set(i, initial_object);
1890 }
1891 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001892 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001893 }
1894
1895 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001896 ScopedObjectAccess soa(env);
1897 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001898 }
1899
Ian Rogersa15e67d2012-02-28 13:51:55 -08001900 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001901 ScopedObjectAccess soa(env);
1902 Array* array = soa.Decode<Array*>(java_array);
1903 PinPrimitiveArray(soa, array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001904 if (is_copy != NULL) {
1905 *is_copy = JNI_FALSE;
1906 }
1907 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001908 }
1909
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001910 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001911 ReleasePrimitiveArray(env, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001912 }
1913
Elliott Hughes75770752011-08-24 17:52:38 -07001914 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001915 ScopedObjectAccess soa(env);
1916 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001917 }
1918
Elliott Hughes75770752011-08-24 17:52:38 -07001919 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001920 ScopedObjectAccess soa(env);
1921 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001922 }
1923
Elliott Hughes75770752011-08-24 17:52:38 -07001924 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001925 ScopedObjectAccess soa(env);
1926 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001927 }
1928
Elliott Hughes75770752011-08-24 17:52:38 -07001929 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001930 ScopedObjectAccess soa(env);
1931 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001932 }
1933
Elliott Hughes75770752011-08-24 17:52:38 -07001934 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001935 ScopedObjectAccess soa(env);
1936 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001937 }
1938
Elliott Hughes75770752011-08-24 17:52:38 -07001939 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001940 ScopedObjectAccess soa(env);
1941 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001942 }
1943
Elliott Hughes75770752011-08-24 17:52:38 -07001944 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001945 ScopedObjectAccess soa(env);
1946 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001947 }
1948
Elliott Hughes75770752011-08-24 17:52:38 -07001949 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001950 ScopedObjectAccess soa(env);
1951 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001952 }
1953
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001954 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001955 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001956 }
1957
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001958 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001959 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001960 }
1961
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001962 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001963 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001964 }
1965
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001966 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001967 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001968 }
1969
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001970 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001971 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001972 }
1973
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001974 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001975 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001976 }
1977
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001978 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001979 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001980 }
1981
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001982 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001983 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 }
1985
Elliott Hughes814e4032011-08-23 12:07:56 -07001986 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001987 ScopedObjectAccess soa(env);
1988 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001989 }
1990
Elliott Hughes814e4032011-08-23 12:07:56 -07001991 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001992 ScopedObjectAccess soa(env);
1993 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 }
1995
Elliott Hughes814e4032011-08-23 12:07:56 -07001996 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001997 ScopedObjectAccess soa(env);
1998 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 }
2000
Elliott Hughes814e4032011-08-23 12:07:56 -07002001 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002002 ScopedObjectAccess soa(env);
2003 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 }
2005
Elliott Hughes814e4032011-08-23 12:07:56 -07002006 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002007 ScopedObjectAccess soa(env);
2008 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002009 }
2010
Elliott Hughes814e4032011-08-23 12:07:56 -07002011 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002012 ScopedObjectAccess soa(env);
2013 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 }
2015
Elliott Hughes814e4032011-08-23 12:07:56 -07002016 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002017 ScopedObjectAccess soa(env);
2018 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 }
2020
Elliott Hughes814e4032011-08-23 12:07:56 -07002021 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002022 ScopedObjectAccess soa(env);
2023 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002024 }
2025
Elliott Hughes814e4032011-08-23 12:07:56 -07002026 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002027 ScopedObjectAccess soa(env);
2028 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002029 }
2030
Elliott Hughes814e4032011-08-23 12:07:56 -07002031 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002032 ScopedObjectAccess soa(env);
2033 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002034 }
2035
Elliott Hughes814e4032011-08-23 12:07:56 -07002036 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002037 ScopedObjectAccess soa(env);
2038 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002039 }
2040
Elliott Hughes814e4032011-08-23 12:07:56 -07002041 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002042 ScopedObjectAccess soa(env);
2043 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002044 }
2045
Elliott Hughes814e4032011-08-23 12:07:56 -07002046 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002047 ScopedObjectAccess soa(env);
2048 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002049 }
2050
Elliott Hughes814e4032011-08-23 12:07:56 -07002051 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002052 ScopedObjectAccess soa(env);
2053 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002054 }
2055
Elliott Hughes814e4032011-08-23 12:07:56 -07002056 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002057 ScopedObjectAccess soa(env);
2058 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002059 }
2060
Elliott Hughes814e4032011-08-23 12:07:56 -07002061 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002062 ScopedObjectAccess soa(env);
2063 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 }
2065
Elliott Hughes5174fe62011-08-23 15:12:35 -07002066 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002067 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2068 }
2069
2070 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 -07002071 ScopedObjectAccess soa(env);
2072 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002073
Elliott Hughesc8fece32013-01-02 11:27:23 -08002074 for (size_t i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 const char* name = methods[i].name;
2076 const char* sig = methods[i].signature;
2077
2078 if (*sig == '!') {
2079 // TODO: fast jni. it's too noisy to log all these.
2080 ++sig;
2081 }
2082
Mathieu Chartier66f19252012-09-18 08:57:04 -07002083 AbstractMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002084 if (m == NULL) {
2085 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002087 if (m == NULL) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002088 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
2089 << PrettyDescriptor(c) << "." << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002090 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002092 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002093 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
2094 << PrettyDescriptor(c) << "." << name << sig
2095 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002096 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 return JNI_ERR;
2098 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002099
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002100 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002101
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002102 m->RegisterNative(soa.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 }
2104 return JNI_OK;
2105 }
2106
Elliott Hughes5174fe62011-08-23 15:12:35 -07002107 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002108 ScopedObjectAccess soa(env);
2109 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002110
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002111 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002112
2113 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002114 AbstractMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002115 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002116 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002117 }
2118 }
2119 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002120 AbstractMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002121 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002122 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002123 }
2124 }
2125
2126 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 }
2128
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002129 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2130 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
2131 ScopedObjectAccess soa(env);
2132 Object* o = soa.Decode<Object*>(java_object);
2133 o->MonitorEnter(soa.Self());
2134 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002135 return JNI_ERR;
2136 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002137 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002138 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002139 }
2140
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002141 static jint MonitorExit(JNIEnv* env, jobject java_object)
2142 UNLOCK_FUNCTION(monitor_lock_) {
2143 ScopedObjectAccess soa(env);
2144 Object* o = soa.Decode<Object*>(java_object);
2145 o->MonitorExit(soa.Self());
2146 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002147 return JNI_ERR;
2148 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002149 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002150 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 }
2152
2153 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002154 Runtime* runtime = Runtime::Current();
2155 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002156 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002157 } else {
2158 *vm = NULL;
2159 }
2160 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2161 }
2162
Elliott Hughescdf53122011-08-19 15:46:09 -07002163 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002164 if (capacity < 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002165 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002166 }
Elliott Hughes11a796e2012-12-19 14:42:57 -08002167 if (address == NULL && capacity != 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002168 JniAbortF("NewDirectByteBuffer", "non-zero capacity for NULL pointer: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002169 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002170
Elliott Hughes96a98872012-12-19 14:21:15 -08002171 // At the moment, the Java side is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002172 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2173 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002174 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002175 jint capacity_arg = static_cast<jint>(capacity);
2176
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002177 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2178 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002179 address_arg, capacity_arg);
Ian Rogersef28b142012-11-30 14:22:18 -08002180 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002181 }
2182
Elliott Hughesb465ab02011-08-24 11:21:21 -07002183 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Jeff Hao534f2b62013-07-10 15:29:36 -07002184 return reinterpret_cast<void*>(env->GetLongField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 }
2186
Elliott Hughesb465ab02011-08-24 11:21:21 -07002187 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002188 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 }
2190
Elliott Hughesb465ab02011-08-24 11:21:21 -07002191 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002192 if (java_object == NULL) {
2193 JniAbortF("GetObjectRefType", "null object");
2194 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002195
2196 // Do we definitely know what kind of reference this is?
2197 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2198 IndirectRefKind kind = GetIndirectRefKind(ref);
2199 switch (kind) {
2200 case kLocal:
Ian Rogersef28b142012-11-30 14:22:18 -08002201 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002202 return JNILocalRefType;
2203 }
2204 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002205 case kGlobal:
2206 return JNIGlobalRefType;
2207 case kWeakGlobal:
2208 return JNIWeakGlobalRefType;
2209 case kSirtOrInvalid:
2210 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002211 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002212 return JNILocalRefType;
2213 }
2214
Ian Rogersef28b142012-11-30 14:22:18 -08002215 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002216 return JNIInvalidRefType;
2217 }
2218
Elliott Hughesb465ab02011-08-24 11:21:21 -07002219 // If we're handing out direct pointers, check whether it's a direct pointer
2220 // to a local reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002221 {
2222 ScopedObjectAccess soa(env);
2223 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2224 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
2225 return JNILocalRefType;
2226 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002227 }
2228 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002229 return JNIInvalidRefType;
2230 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002231 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2232 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002233 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002234
2235 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002236 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2237 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002238 // TODO: we should try to expand the table if necessary.
2239 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
2240 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2241 return JNI_ERR;
2242 }
2243 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002244 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002245 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2246 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002247 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002248 soa.Self()->ThrowOutOfMemoryError(caller);
2249 }
2250 return okay ? JNI_OK : JNI_ERR;
2251 }
2252
2253 template<typename JniT, typename ArtT>
2254 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002255 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002256 if (length < 0) {
2257 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
2258 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002259 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002260 return soa.AddLocalReference<JniT>(result);
2261 }
2262
2263 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2264 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2265 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002266 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002267 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2268 PinPrimitiveArray(soa, array);
2269 if (is_copy != NULL) {
2270 *is_copy = JNI_FALSE;
2271 }
2272 return array->GetData();
2273 }
2274
2275 template <typename ArrayT>
Ian Rogersef28b142012-11-30 14:22:18 -08002276 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002277 if (mode != JNI_COMMIT) {
Ian Rogersef28b142012-11-30 14:22:18 -08002278 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002279 Array* array = soa.Decode<Array*>(java_array);
2280 UnpinPrimitiveArray(soa, array);
2281 }
2282 }
2283
2284 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2285 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2286 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002287 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002288 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2289 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2290 ThrowAIOOBE(soa, array, start, length, "src");
2291 } else {
2292 JavaT* data = array->GetData();
2293 memcpy(buf, data + start, length * sizeof(JavaT));
2294 }
2295 }
2296
2297 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2298 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2299 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002300 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002301 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2302 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2303 ThrowAIOOBE(soa, array, start, length, "dst");
2304 } else {
2305 JavaT* data = array->GetData();
2306 memcpy(data + start, buf, length * sizeof(JavaT));
2307 }
2308 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002309};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002310
Elliott Hughes88c5c352012-03-15 18:49:48 -07002311const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002312 NULL, // reserved0.
2313 NULL, // reserved1.
2314 NULL, // reserved2.
2315 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002316 JNI::GetVersion,
2317 JNI::DefineClass,
2318 JNI::FindClass,
2319 JNI::FromReflectedMethod,
2320 JNI::FromReflectedField,
2321 JNI::ToReflectedMethod,
2322 JNI::GetSuperclass,
2323 JNI::IsAssignableFrom,
2324 JNI::ToReflectedField,
2325 JNI::Throw,
2326 JNI::ThrowNew,
2327 JNI::ExceptionOccurred,
2328 JNI::ExceptionDescribe,
2329 JNI::ExceptionClear,
2330 JNI::FatalError,
2331 JNI::PushLocalFrame,
2332 JNI::PopLocalFrame,
2333 JNI::NewGlobalRef,
2334 JNI::DeleteGlobalRef,
2335 JNI::DeleteLocalRef,
2336 JNI::IsSameObject,
2337 JNI::NewLocalRef,
2338 JNI::EnsureLocalCapacity,
2339 JNI::AllocObject,
2340 JNI::NewObject,
2341 JNI::NewObjectV,
2342 JNI::NewObjectA,
2343 JNI::GetObjectClass,
2344 JNI::IsInstanceOf,
2345 JNI::GetMethodID,
2346 JNI::CallObjectMethod,
2347 JNI::CallObjectMethodV,
2348 JNI::CallObjectMethodA,
2349 JNI::CallBooleanMethod,
2350 JNI::CallBooleanMethodV,
2351 JNI::CallBooleanMethodA,
2352 JNI::CallByteMethod,
2353 JNI::CallByteMethodV,
2354 JNI::CallByteMethodA,
2355 JNI::CallCharMethod,
2356 JNI::CallCharMethodV,
2357 JNI::CallCharMethodA,
2358 JNI::CallShortMethod,
2359 JNI::CallShortMethodV,
2360 JNI::CallShortMethodA,
2361 JNI::CallIntMethod,
2362 JNI::CallIntMethodV,
2363 JNI::CallIntMethodA,
2364 JNI::CallLongMethod,
2365 JNI::CallLongMethodV,
2366 JNI::CallLongMethodA,
2367 JNI::CallFloatMethod,
2368 JNI::CallFloatMethodV,
2369 JNI::CallFloatMethodA,
2370 JNI::CallDoubleMethod,
2371 JNI::CallDoubleMethodV,
2372 JNI::CallDoubleMethodA,
2373 JNI::CallVoidMethod,
2374 JNI::CallVoidMethodV,
2375 JNI::CallVoidMethodA,
2376 JNI::CallNonvirtualObjectMethod,
2377 JNI::CallNonvirtualObjectMethodV,
2378 JNI::CallNonvirtualObjectMethodA,
2379 JNI::CallNonvirtualBooleanMethod,
2380 JNI::CallNonvirtualBooleanMethodV,
2381 JNI::CallNonvirtualBooleanMethodA,
2382 JNI::CallNonvirtualByteMethod,
2383 JNI::CallNonvirtualByteMethodV,
2384 JNI::CallNonvirtualByteMethodA,
2385 JNI::CallNonvirtualCharMethod,
2386 JNI::CallNonvirtualCharMethodV,
2387 JNI::CallNonvirtualCharMethodA,
2388 JNI::CallNonvirtualShortMethod,
2389 JNI::CallNonvirtualShortMethodV,
2390 JNI::CallNonvirtualShortMethodA,
2391 JNI::CallNonvirtualIntMethod,
2392 JNI::CallNonvirtualIntMethodV,
2393 JNI::CallNonvirtualIntMethodA,
2394 JNI::CallNonvirtualLongMethod,
2395 JNI::CallNonvirtualLongMethodV,
2396 JNI::CallNonvirtualLongMethodA,
2397 JNI::CallNonvirtualFloatMethod,
2398 JNI::CallNonvirtualFloatMethodV,
2399 JNI::CallNonvirtualFloatMethodA,
2400 JNI::CallNonvirtualDoubleMethod,
2401 JNI::CallNonvirtualDoubleMethodV,
2402 JNI::CallNonvirtualDoubleMethodA,
2403 JNI::CallNonvirtualVoidMethod,
2404 JNI::CallNonvirtualVoidMethodV,
2405 JNI::CallNonvirtualVoidMethodA,
2406 JNI::GetFieldID,
2407 JNI::GetObjectField,
2408 JNI::GetBooleanField,
2409 JNI::GetByteField,
2410 JNI::GetCharField,
2411 JNI::GetShortField,
2412 JNI::GetIntField,
2413 JNI::GetLongField,
2414 JNI::GetFloatField,
2415 JNI::GetDoubleField,
2416 JNI::SetObjectField,
2417 JNI::SetBooleanField,
2418 JNI::SetByteField,
2419 JNI::SetCharField,
2420 JNI::SetShortField,
2421 JNI::SetIntField,
2422 JNI::SetLongField,
2423 JNI::SetFloatField,
2424 JNI::SetDoubleField,
2425 JNI::GetStaticMethodID,
2426 JNI::CallStaticObjectMethod,
2427 JNI::CallStaticObjectMethodV,
2428 JNI::CallStaticObjectMethodA,
2429 JNI::CallStaticBooleanMethod,
2430 JNI::CallStaticBooleanMethodV,
2431 JNI::CallStaticBooleanMethodA,
2432 JNI::CallStaticByteMethod,
2433 JNI::CallStaticByteMethodV,
2434 JNI::CallStaticByteMethodA,
2435 JNI::CallStaticCharMethod,
2436 JNI::CallStaticCharMethodV,
2437 JNI::CallStaticCharMethodA,
2438 JNI::CallStaticShortMethod,
2439 JNI::CallStaticShortMethodV,
2440 JNI::CallStaticShortMethodA,
2441 JNI::CallStaticIntMethod,
2442 JNI::CallStaticIntMethodV,
2443 JNI::CallStaticIntMethodA,
2444 JNI::CallStaticLongMethod,
2445 JNI::CallStaticLongMethodV,
2446 JNI::CallStaticLongMethodA,
2447 JNI::CallStaticFloatMethod,
2448 JNI::CallStaticFloatMethodV,
2449 JNI::CallStaticFloatMethodA,
2450 JNI::CallStaticDoubleMethod,
2451 JNI::CallStaticDoubleMethodV,
2452 JNI::CallStaticDoubleMethodA,
2453 JNI::CallStaticVoidMethod,
2454 JNI::CallStaticVoidMethodV,
2455 JNI::CallStaticVoidMethodA,
2456 JNI::GetStaticFieldID,
2457 JNI::GetStaticObjectField,
2458 JNI::GetStaticBooleanField,
2459 JNI::GetStaticByteField,
2460 JNI::GetStaticCharField,
2461 JNI::GetStaticShortField,
2462 JNI::GetStaticIntField,
2463 JNI::GetStaticLongField,
2464 JNI::GetStaticFloatField,
2465 JNI::GetStaticDoubleField,
2466 JNI::SetStaticObjectField,
2467 JNI::SetStaticBooleanField,
2468 JNI::SetStaticByteField,
2469 JNI::SetStaticCharField,
2470 JNI::SetStaticShortField,
2471 JNI::SetStaticIntField,
2472 JNI::SetStaticLongField,
2473 JNI::SetStaticFloatField,
2474 JNI::SetStaticDoubleField,
2475 JNI::NewString,
2476 JNI::GetStringLength,
2477 JNI::GetStringChars,
2478 JNI::ReleaseStringChars,
2479 JNI::NewStringUTF,
2480 JNI::GetStringUTFLength,
2481 JNI::GetStringUTFChars,
2482 JNI::ReleaseStringUTFChars,
2483 JNI::GetArrayLength,
2484 JNI::NewObjectArray,
2485 JNI::GetObjectArrayElement,
2486 JNI::SetObjectArrayElement,
2487 JNI::NewBooleanArray,
2488 JNI::NewByteArray,
2489 JNI::NewCharArray,
2490 JNI::NewShortArray,
2491 JNI::NewIntArray,
2492 JNI::NewLongArray,
2493 JNI::NewFloatArray,
2494 JNI::NewDoubleArray,
2495 JNI::GetBooleanArrayElements,
2496 JNI::GetByteArrayElements,
2497 JNI::GetCharArrayElements,
2498 JNI::GetShortArrayElements,
2499 JNI::GetIntArrayElements,
2500 JNI::GetLongArrayElements,
2501 JNI::GetFloatArrayElements,
2502 JNI::GetDoubleArrayElements,
2503 JNI::ReleaseBooleanArrayElements,
2504 JNI::ReleaseByteArrayElements,
2505 JNI::ReleaseCharArrayElements,
2506 JNI::ReleaseShortArrayElements,
2507 JNI::ReleaseIntArrayElements,
2508 JNI::ReleaseLongArrayElements,
2509 JNI::ReleaseFloatArrayElements,
2510 JNI::ReleaseDoubleArrayElements,
2511 JNI::GetBooleanArrayRegion,
2512 JNI::GetByteArrayRegion,
2513 JNI::GetCharArrayRegion,
2514 JNI::GetShortArrayRegion,
2515 JNI::GetIntArrayRegion,
2516 JNI::GetLongArrayRegion,
2517 JNI::GetFloatArrayRegion,
2518 JNI::GetDoubleArrayRegion,
2519 JNI::SetBooleanArrayRegion,
2520 JNI::SetByteArrayRegion,
2521 JNI::SetCharArrayRegion,
2522 JNI::SetShortArrayRegion,
2523 JNI::SetIntArrayRegion,
2524 JNI::SetLongArrayRegion,
2525 JNI::SetFloatArrayRegion,
2526 JNI::SetDoubleArrayRegion,
2527 JNI::RegisterNatives,
2528 JNI::UnregisterNatives,
2529 JNI::MonitorEnter,
2530 JNI::MonitorExit,
2531 JNI::GetJavaVM,
2532 JNI::GetStringRegion,
2533 JNI::GetStringUTFRegion,
2534 JNI::GetPrimitiveArrayCritical,
2535 JNI::ReleasePrimitiveArrayCritical,
2536 JNI::GetStringCritical,
2537 JNI::ReleaseStringCritical,
2538 JNI::NewWeakGlobalRef,
2539 JNI::DeleteWeakGlobalRef,
2540 JNI::ExceptionCheck,
2541 JNI::NewDirectByteBuffer,
2542 JNI::GetDirectBufferAddress,
2543 JNI::GetDirectBufferCapacity,
2544 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002545};
2546
Elliott Hughes75770752011-08-24 17:52:38 -07002547JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002548 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002549 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002550 local_ref_cookie(IRT_FIRST_SEGMENT),
2551 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002552 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002553 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002554 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002555 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002556 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002557 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002558 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002559 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2560 // errors will ensue.
2561 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2562 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002563}
2564
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002565JNIEnvExt::~JNIEnvExt() {
2566}
2567
Elliott Hughes88c5c352012-03-15 18:49:48 -07002568void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2569 check_jni = enabled;
2570 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002571}
2572
Elliott Hughes73e66f72012-05-09 09:34:45 -07002573void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2574 locals.Dump(os);
2575 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002576}
2577
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002578void JNIEnvExt::PushFrame(int /*capacity*/) {
2579 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002580 stacked_local_ref_cookies.push_back(local_ref_cookie);
2581 local_ref_cookie = locals.GetSegmentState();
2582}
2583
2584void JNIEnvExt::PopFrame() {
2585 locals.SetSegmentState(local_ref_cookie);
2586 local_ref_cookie = stacked_local_ref_cookies.back();
2587 stacked_local_ref_cookies.pop_back();
2588}
2589
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002590Offset JNIEnvExt::SegmentStateOffset() {
2591 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2592 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2593}
2594
Carl Shapiroea4dca82011-08-01 13:45:38 -07002595// JNI Invocation interface.
2596
Brian Carlstrombddf9762013-05-14 11:35:37 -07002597extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002598 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002599 if (IsBadJniVersion(args->version)) {
2600 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002601 return JNI_EVERSION;
2602 }
2603 Runtime::Options options;
2604 for (int i = 0; i < args->nOptions; ++i) {
2605 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002606 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002607 }
2608 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002609 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002610 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002611 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002612 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002613 bool started = runtime->Start();
2614 if (!started) {
2615 delete Thread::Current()->GetJniEnv();
2616 delete runtime->GetJavaVM();
2617 LOG(WARNING) << "CreateJavaVM failed";
2618 return JNI_ERR;
2619 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002620 *p_env = Thread::Current()->GetJniEnv();
2621 *p_vm = runtime->GetJavaVM();
2622 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002623}
2624
Elliott Hughesf2682d52011-08-15 16:37:04 -07002625extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002626 Runtime* runtime = Runtime::Current();
2627 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002628 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002629 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002630 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002631 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002632 }
2633 return JNI_OK;
2634}
2635
2636// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002637extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002638 return JNI_ERR;
2639}
2640
Elliott Hughescdf53122011-08-19 15:46:09 -07002641class JII {
2642 public:
2643 static jint DestroyJavaVM(JavaVM* vm) {
2644 if (vm == NULL) {
2645 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002646 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002647 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2648 delete raw_vm->runtime;
2649 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002650 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002651
Elliott Hughescdf53122011-08-19 15:46:09 -07002652 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002653 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002654 }
2655
2656 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002657 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002658 }
2659
2660 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002661 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002662 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002663 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002664 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2665 Runtime* runtime = raw_vm->runtime;
2666 runtime->DetachCurrentThread();
2667 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002668 }
2669
2670 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes83a25322013-03-14 11:18:53 -07002671 if (IsBadJniVersion(version)) {
2672 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07002673 return JNI_EVERSION;
2674 }
2675 if (vm == NULL || env == NULL) {
2676 return JNI_ERR;
2677 }
2678 Thread* thread = Thread::Current();
2679 if (thread == NULL) {
2680 *env = NULL;
2681 return JNI_EDETACHED;
2682 }
2683 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002684 return JNI_OK;
2685 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002686};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002687
Elliott Hughes88c5c352012-03-15 18:49:48 -07002688const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002689 NULL, // reserved0
2690 NULL, // reserved1
2691 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002692 JII::DestroyJavaVM,
2693 JII::AttachCurrentThread,
2694 JII::DetachCurrentThread,
2695 JII::GetEnv,
2696 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002697};
2698
Elliott Hughesa0957642011-09-02 14:27:33 -07002699JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002700 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002701 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002702 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002703 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002704 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002705 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002706 work_around_app_jni_bugs(false),
Ian Rogers62d6c772013-02-27 08:32:07 -08002707 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002708 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002709 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002710 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002711 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002712 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002713 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Elliott Hughes79082e32011-08-25 12:07:32 -07002714 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002715 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002716 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002717 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002718 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002719}
2720
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002721JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002722 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002723}
2724
Elliott Hughes88c5c352012-03-15 18:49:48 -07002725void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2726 check_jni = enabled;
2727 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002728}
2729
Elliott Hughesae80b492012-04-24 10:43:17 -07002730void JavaVMExt::DumpForSigQuit(std::ostream& os) {
2731 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
2732 if (force_copy) {
2733 os << " (with forcecopy)";
2734 }
2735 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07002736 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07002737 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002738 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002739 os << "; pins=" << pin_table.Size();
2740 }
2741 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002742 MutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002743 os << "; globals=" << globals.Capacity();
2744 }
2745 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002746 MutexLock mu(self, weak_globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002747 if (weak_globals.Capacity() > 0) {
2748 os << " (plus " << weak_globals.Capacity() << " weak)";
2749 }
2750 }
2751 os << '\n';
2752
2753 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002754 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002755 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
2756 }
2757}
2758
Elliott Hughes73e66f72012-05-09 09:34:45 -07002759void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002760 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002761 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002762 MutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002763 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002764 }
2765 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002766 MutexLock mu(self, weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002767 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002768 }
2769 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002770 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002771 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002772 }
2773}
2774
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002775bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
2776 std::string& detail) {
Elliott Hughes75770752011-08-24 17:52:38 -07002777 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002778
2779 // See if we've already loaded this library. If we have, and the class loader
2780 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002781 // TODO: for better results we should canonicalize the pathname (or even compare
2782 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002783 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07002784 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07002785 {
2786 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07002787 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002788 library = libraries->Get(path);
2789 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002790 if (library != NULL) {
2791 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002792 // The library will be associated with class_loader. The JNI
2793 // spec says we can't load the same library into more than one
2794 // class loader.
2795 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2796 "ClassLoader %p; can't open in ClassLoader %p",
2797 path.c_str(), library->GetClassLoader(), class_loader);
2798 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002799 return false;
2800 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002801 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2802 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002803 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002804 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2805 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002806 return false;
2807 }
2808 return true;
2809 }
2810
2811 // Open the shared library. Because we're using a full path, the system
2812 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2813 // resolve this library's dependencies though.)
2814
2815 // Failures here are expected when java.library.path has several entries
2816 // and we have to hunt for the lib.
2817
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002818 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
2819 // class unloading. Libraries will only be unloaded when the reference count (incremented by
2820 // dlopen) becomes zero from dlclose.
2821
Elliott Hughescdf53122011-08-19 15:46:09 -07002822 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002823 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002824 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
2825 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
2826 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07002827
Elliott Hughes84b2f142012-09-27 09:16:28 -07002828 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002829
2830 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002831 detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07002832 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002833 return false;
2834 }
2835
2836 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002837 // TODO: move the locking (and more of this logic) into Libraries.
2838 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002839 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002840 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002841 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002842 if (library == NULL) { // We won race to get libraries_lock
2843 library = new SharedLibrary(path, handle, class_loader);
2844 libraries->Put(path, library);
2845 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002846 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002847 }
2848 if (!created_library) {
2849 LOG(INFO) << "WOW: we lost a race to add shared library: "
2850 << "\"" << path << "\" ClassLoader=" << class_loader;
2851 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002852 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002853
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002854 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002855
Elliott Hughes79353722013-08-02 16:52:18 -07002856 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07002857 void* sym = dlsym(handle, "JNI_OnLoad");
2858 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002859 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07002860 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07002861 } else {
2862 // Call JNI_OnLoad. We have to override the current class
2863 // loader, which will always be "null" since the stuff at the
2864 // top of the stack is around Runtime.loadLibrary(). (See
2865 // the comments in the JNI FindClass function.)
2866 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2867 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07002868 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002869 self->SetClassLoaderOverride(class_loader);
2870
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002871 int version = 0;
2872 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002873 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002874 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002875 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002876 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002877
Brian Carlstromaded5f72011-10-07 17:15:04 -07002878 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002879
Elliott Hughes79353722013-08-02 16:52:18 -07002880 if (version == JNI_ERR) {
2881 StringAppendF(&detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
2882 } else if (IsBadJniVersion(version)) {
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07002883 StringAppendF(&detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
2884 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07002885 // It's unwise to call dlclose() here, but we can mark it
2886 // as bad and ensure that future load attempts will fail.
2887 // We don't know how far JNI_OnLoad got, so there could
2888 // be some partially-initialized stuff accessible through
2889 // newly-registered native method calls. We could try to
2890 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07002891 } else {
2892 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07002893 }
Elliott Hughes79353722013-08-02 16:52:18 -07002894 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07002895 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002896 }
2897
Elliott Hughes79353722013-08-02 16:52:18 -07002898 library->SetResult(was_successful);
2899 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07002900}
2901
Mathieu Chartier66f19252012-09-18 08:57:04 -07002902void* JavaVMExt::FindCodeForNativeMethod(AbstractMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002903 CHECK(m->IsNative());
2904
2905 Class* c = m->GetDeclaringClass();
2906
2907 // If this is a static method, it could be called before the class
2908 // has been initialized.
2909 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002910 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002911 return NULL;
2912 }
2913 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002914 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002915 }
2916
Brian Carlstrom16192862011-09-12 17:50:06 -07002917 std::string detail;
2918 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07002919 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07002920 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002921 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07002922 native_method = libraries->FindNativeMethod(m, detail);
2923 }
Ian Rogers62d6c772013-02-27 08:32:07 -08002924 // Throwing can cause libraries_lock to be reacquired.
Brian Carlstrom16192862011-09-12 17:50:06 -07002925 if (native_method == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002926 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
2927 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002928 }
2929 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002930}
2931
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002932void JavaVMExt::VisitRoots(RootVisitor* visitor, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002933 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07002934 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002935 MutexLock mu(self, globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002936 globals.VisitRoots(visitor, arg);
2937 }
2938 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002939 MutexLock mu(self, pins_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002940 pin_table.VisitRoots(visitor, arg);
2941 }
2942 // The weak_globals table is visited by the GC itself (because it mutates the table).
2943}
2944
Elliott Hughesc8fece32013-01-02 11:27:23 -08002945void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
2946 size_t method_count) {
2947 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
2948 if (c.get() == NULL) {
2949 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
2950 }
2951 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
2952}
2953
Ian Rogersdf20fe02011-07-20 20:34:16 -07002954} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002955
2956std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2957 switch (rhs) {
2958 case JNIInvalidRefType:
2959 os << "JNIInvalidRefType";
2960 return os;
2961 case JNILocalRefType:
2962 os << "JNILocalRefType";
2963 return os;
2964 case JNIGlobalRefType:
2965 os << "JNIGlobalRefType";
2966 return os;
2967 case JNIWeakGlobalRefType:
2968 os << "JNIWeakGlobalRefType";
2969 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002970 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002971 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002972 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002973 }
2974}