blob: 460e3b0f301cd04b1df5c7ce7b523cdd4e7e401b [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ian Rogersdf20fe02011-07-20 20:34:16 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070018
Elliott Hughes0af55432011-08-17 18:37:28 -070019#include <dlfcn.h>
Elliott Hughes79082e32011-08-25 12:07:32 -070020
21#include <cstdarg>
Elliott Hughes0af55432011-08-17 18:37:28 -070022#include <utility>
23#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080026#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080027#include "base/stl_util.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080028#include "base/stringpiece.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070029#include "class_linker.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070031#include "gc/accounting/card_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070032#include "interpreter/interpreter.h"
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070033#include "invoke_arg_array_builder.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070034#include "jni.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070035#include "mirror/art_field-inl.h"
36#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/class-inl.h"
38#include "mirror/class_loader.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/object-inl.h"
40#include "mirror/object_array-inl.h"
41#include "mirror/throwable.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080042#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070043#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070044#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070045#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070046#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070047#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048#include "utf.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070049#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070050#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070051
Brian Carlstromea46f952013-07-30 01:26:50 -070052using ::art::mirror::ArtField;
53using ::art::mirror::ArtMethod;
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070054using ::art::mirror::Array;
55using ::art::mirror::BooleanArray;
56using ::art::mirror::ByteArray;
57using ::art::mirror::CharArray;
58using ::art::mirror::Class;
59using ::art::mirror::ClassLoader;
60using ::art::mirror::DoubleArray;
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070061using ::art::mirror::FloatArray;
62using ::art::mirror::IntArray;
63using ::art::mirror::LongArray;
64using ::art::mirror::Object;
65using ::art::mirror::ObjectArray;
66using ::art::mirror::ShortArray;
67using ::art::mirror::String;
68using ::art::mirror::Throwable;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070070namespace art {
71
Brian Carlstrom7934ac22013-07-26 10:54:15 -070072static const size_t kMonitorsInitial = 32; // Arbitrary.
73static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070074
Brian Carlstrom7934ac22013-07-26 10:54:15 -070075static const size_t kLocalsInitial = 64; // Arbitrary.
76static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070077
Brian Carlstrom7934ac22013-07-26 10:54:15 -070078static const size_t kPinTableInitial = 16; // Arbitrary.
79static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070080
Brian Carlstrom7934ac22013-07-26 10:54:15 -070081static size_t gGlobalsInitial = 512; // Arbitrary.
82static size_t gGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Elliott Hughes2ced6a52011-10-16 18:44:48 -070083
Brian Carlstrom7934ac22013-07-26 10:54:15 -070084static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
85static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Ian Rogersdf20fe02011-07-20 20:34:16 -070086
Ian Rogers00f7d0e2012-07-19 15:28:27 -070087static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070088 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescdf53122011-08-19 15:46:09 -070089 if (obj == NULL) {
90 return NULL;
91 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070092 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -070093 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian 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
Brian Carlstromea46f952013-07-30 01:26:50 -0700104static void CheckMethodArguments(ArtMethod* m, uint32_t* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700105 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700106 MethodHelper mh(m);
Ian Rogers50b35e22012-10-04 10:09:15 -0700107 const DexFile::TypeList* params = mh.GetParameterTypeList();
108 if (params == NULL) {
109 return; // No arguments so nothing to check.
110 }
Jeff Hao5d917302013-02-27 17:57:33 -0800111 uint32_t offset = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -0700112 uint32_t num_params = params->Size();
Elliott Hughesb264f082012-04-06 17:10:10 -0700113 size_t error_count = 0;
Jeff Hao5d917302013-02-27 17:57:33 -0800114 if (!m->IsStatic()) {
115 offset = 1;
116 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700117 for (uint32_t i = 0; i < num_params; i++) {
118 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
119 Class* param_type = mh.GetClassFromTypeIdx(type_idx);
120 if (param_type == NULL) {
121 Thread* self = Thread::Current();
122 CHECK(self->IsExceptionPending());
123 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
124 << mh.GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
Ian Rogers62d6c772013-02-27 08:32:07 -0800125 << self->GetException(NULL)->Dump();
Ian Rogers50b35e22012-10-04 10:09:15 -0700126 self->ClearException();
127 ++error_count;
128 } else if (!param_type->IsPrimitive()) {
129 // TODO: check primitives are in range.
Jeff Hao5d917302013-02-27 17:57:33 -0800130 Object* argument = reinterpret_cast<Object*>(args[i + offset]);
Ian Rogers50b35e22012-10-04 10:09:15 -0700131 if (argument != NULL && !argument->InstanceOf(param_type)) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700132 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
133 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
134 ++error_count;
135 }
Jeff Hao5d917302013-02-27 17:57:33 -0800136 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
137 offset++;
Elliott Hughesb264f082012-04-06 17:10:10 -0700138 }
139 }
140 if (error_count > 0) {
141 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
142 // with an argument.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700143 JniAbortF(NULL, "bad arguments passed to %s (see above for details)", PrettyMethod(m).c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -0700144 }
145}
Elliott Hughesb264f082012-04-06 17:10:10 -0700146
Brian Carlstromea46f952013-07-30 01:26:50 -0700147void InvokeWithArgArray(const ScopedObjectAccess& soa, ArtMethod* method,
Jeff Hao6474d192013-03-26 14:08:09 -0700148 ArgArray* arg_array, JValue* result, char result_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700150 uint32_t* args = arg_array->GetArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700151 if (UNLIKELY(soa.Env()->check_jni)) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700152 CheckMethodArguments(method, args);
Elliott Hughes4cacde82012-04-11 18:32:27 -0700153 }
Sebastien Hertz7d658cf2013-07-09 10:56:11 +0200154 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, result_type);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700155}
156
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700157static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
158 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700159 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700160 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700161 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700162 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800163 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700164 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800165 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700166 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
167 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700168}
169
Brian Carlstromea46f952013-07-30 01:26:50 -0700170static ArtMethod* FindVirtualMethod(Object* receiver, ArtMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700171 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700172 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700173}
174
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700175static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
176 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700177 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700178 Object* receiver = soa.Decode<Object*>(obj);
Brian Carlstromea46f952013-07-30 01:26:50 -0700179 ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700180 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800181 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700182 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800183 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700184 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
185 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700186}
187
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700188static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
189 jobject obj, jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700190 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700191 Object* receiver = soa.Decode<Object*>(obj);
Brian Carlstromea46f952013-07-30 01:26:50 -0700192 ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700193 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800194 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700195 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800196 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700197 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
198 return result;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700199}
200
Elliott Hughes6b436852011-08-12 10:16:44 -0700201// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
202// separated with slashes but aren't wrapped with "L;" like regular descriptors
203// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
204// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
205// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700206static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700207 std::string result;
208 // Add the missing "L;" if necessary.
209 if (name[0] == '[') {
210 result = name;
211 } else {
212 result += 'L';
213 result += name;
214 result += ';';
215 }
216 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700217 if (result.find('.') != std::string::npos) {
218 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
219 << "\"" << name << "\"";
220 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700221 }
222 return result;
223}
224
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700225static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, Class* c,
226 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700227 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800228 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
229 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
230 "no %s method \"%s.%s%s\"",
231 kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700232}
233
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700234static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
235 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700236 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700237 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700238 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700239 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700240 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700241
Brian Carlstromea46f952013-07-30 01:26:50 -0700242 ArtMethod* method = NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700243 if (is_static) {
244 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700245 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700246 method = c->FindVirtualMethod(name, sig);
247 if (method == NULL) {
248 // No virtual method matching the signature. Search declared
249 // private methods and constructors.
250 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700251 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700252 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700253
Elliott Hughescdf53122011-08-19 15:46:09 -0700254 if (method == NULL || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700255 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700256 return NULL;
257 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700258
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700259 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700260}
261
Ian Rogersef28b142012-11-30 14:22:18 -0800262static ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700263 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700264 ArtMethod* method = soa.Self()->GetCurrentMethod(NULL);
Ian Rogersef28b142012-11-30 14:22:18 -0800265 if (method == NULL ||
266 method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
267 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700268 }
269 return method->GetDeclaringClass()->GetClassLoader();
270}
271
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
273 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700274 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700275 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700276 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700277 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700278 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700279
Brian Carlstromea46f952013-07-30 01:26:50 -0700280 ArtField* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700281 Class* field_type;
282 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
283 if (sig[1] != '\0') {
Ian Rogersef28b142012-11-30 14:22:18 -0800284 ClassLoader* cl = GetClassLoader(soa);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700285 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700286 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700287 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700288 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700289 if (field_type == NULL) {
290 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700291 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800292 ThrowLocation throw_location;
Brian Carlstromea46f952013-07-30 01:26:50 -0700293 SirtRef<Throwable> cause(soa.Self(), soa.Self()->GetException(&throw_location));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700294 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800295 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
296 "no type \"%s\" found and so no field \"%s\" could be found in class "
297 "\"%s\" or its superclasses", sig, name,
298 ClassHelper(c).GetDescriptor());
299 soa.Self()->GetException(NULL)->SetCause(cause.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700300 return NULL;
301 }
302 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800303 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700304 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800305 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700306 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700307 if (field == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800308 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
309 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
310 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
311 sig, name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700312 return NULL;
313 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700314 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700315}
316
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700317static void PinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700318 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700319 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700320 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700321 vm->pin_table.Add(array);
322}
323
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700324static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700325 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700326 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700327 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700328 vm->pin_table.Remove(array);
329}
330
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700331static void ThrowAIOOBE(ScopedObjectAccess& soa, Array* array, jsize start,
332 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700333 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700334 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800335 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
336 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
337 "%s offset=%d length=%d %s.length=%d",
338 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700339}
Ian Rogers0571d352011-11-03 19:51:38 -0700340
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700341static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
342 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700343 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800344 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
345 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
346 "offset=%d length=%d string.length()=%d", start, length,
347 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700348}
Elliott Hughes814e4032011-08-23 12:07:56 -0700349
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700350int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700351 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700352 // Turn the const char* into a java.lang.String.
353 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
354 if (msg != NULL && s.get() == NULL) {
355 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700356 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700357
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700358 // Choose an appropriate constructor and set up the arguments.
359 jvalue args[2];
360 const char* signature;
361 if (msg == NULL && cause == NULL) {
362 signature = "()V";
363 } else if (msg != NULL && cause == NULL) {
364 signature = "(Ljava/lang/String;)V";
365 args[0].l = s.get();
366 } else if (msg == NULL && cause != NULL) {
367 signature = "(Ljava/lang/Throwable;)V";
368 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700369 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700370 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
371 args[0].l = s.get();
372 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700373 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700374 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
375 if (mid == NULL) {
Ian Rogersef28b142012-11-30 14:22:18 -0800376 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700377 LOG(ERROR) << "No <init>" << signature << " in "
378 << PrettyClass(soa.Decode<Class*>(exception_class));
379 return JNI_ERR;
380 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700381
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700382 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
383 if (exception.get() == NULL) {
384 return JNI_ERR;
385 }
Ian Rogersef28b142012-11-30 14:22:18 -0800386 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800387 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
388 soa.Self()->SetException(throw_location, soa.Decode<Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700389 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700390}
391
Elliott Hughes462c9442012-03-23 18:47:50 -0700392static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700393 if (vm == NULL || p_env == NULL) {
394 return JNI_ERR;
395 }
396
Elliott Hughes462c9442012-03-23 18:47:50 -0700397 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700398 Thread* self = Thread::Current();
399 if (self != NULL) {
400 *p_env = self->GetJniEnv();
401 return JNI_OK;
402 }
403
404 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
405
406 // No threads allowed in zygote mode.
407 if (runtime->IsZygote()) {
408 LOG(ERROR) << "Attempt to attach a thread in the zygote";
409 return JNI_ERR;
410 }
411
Elliott Hughes462c9442012-03-23 18:47:50 -0700412 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
413 const char* thread_name = NULL;
Ian Rogers365c1022012-06-22 15:05:28 -0700414 jobject thread_group = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -0700415 if (args != NULL) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700416 if (IsBadJniVersion(args->version)) {
417 LOG(ERROR) << "Bad JNI version passed to "
418 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
419 << args->version;
420 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700421 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700422 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700423 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700424 }
Elliott Hughes75770752011-08-24 17:52:38 -0700425
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800426 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700427 *p_env = NULL;
428 return JNI_ERR;
429 } else {
430 *p_env = Thread::Current()->GetJniEnv();
431 return JNI_OK;
432 }
Elliott Hughes75770752011-08-24 17:52:38 -0700433}
434
Elliott Hughes79082e32011-08-25 12:07:32 -0700435class SharedLibrary {
436 public:
437 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
438 : path_(path),
439 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700440 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700441 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700442 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700443 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700444 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700445 }
446
Elliott Hughes79082e32011-08-25 12:07:32 -0700447 Object* GetClassLoader() {
448 return class_loader_;
449 }
450
451 std::string GetPath() {
452 return path_;
453 }
454
455 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700456 * Check the result of an earlier call to JNI_OnLoad on this library.
457 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700458 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700459 bool CheckOnLoadResult()
460 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700461 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700462 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700463 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
464 bool okay;
465 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700466 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700467
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700468 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
469 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
470 // caller can continue.
471 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
472 okay = true;
473 } else {
474 while (jni_on_load_result_ == kPending) {
475 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700476 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700477 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700478
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700479 okay = (jni_on_load_result_ == kOkay);
480 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
481 << (okay ? "succeeded" : "failed") << "]";
482 }
483 }
484 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700485 return okay;
486 }
487
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700488 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700489 Thread* self = Thread::Current();
490 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700491
Elliott Hughes79082e32011-08-25 12:07:32 -0700492 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700493 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700494
495 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700496 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700497 }
498
499 void* FindSymbol(const std::string& symbol_name) {
500 return dlsym(handle_, symbol_name.c_str());
501 }
502
503 private:
504 enum JNI_OnLoadState {
505 kPending,
506 kFailed,
507 kOkay,
508 };
509
510 // Path to library "/system/lib/libjni.so".
511 std::string path_;
512
513 // The void* returned by dlopen(3).
514 void* handle_;
515
516 // The ClassLoader this library is associated with.
517 Object* class_loader_;
518
519 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700520 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700521 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700522 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700523 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700524 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700525 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700526 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700527};
528
Elliott Hughes79082e32011-08-25 12:07:32 -0700529// This exists mainly to keep implementation details out of the header file.
530class Libraries {
531 public:
532 Libraries() {
533 }
534
535 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700536 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700537 }
538
Elliott Hughesae80b492012-04-24 10:43:17 -0700539 void Dump(std::ostream& os) const {
540 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700541 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700542 if (!first) {
543 os << ' ';
544 }
545 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700546 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700547 }
548 }
549
550 size_t size() const {
551 return libraries_.size();
552 }
553
Elliott Hughes79082e32011-08-25 12:07:32 -0700554 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700555 auto it = libraries_.find(path);
Ian Rogers712462a2012-04-12 16:32:29 -0700556 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700557 }
558
559 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700560 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700561 }
562
563 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstromea46f952013-07-30 01:26:50 -0700564 void* FindNativeMethod(const ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700565 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700566 std::string jni_short_name(JniShortName(m));
567 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700568 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700569 for (const auto& lib : libraries_) {
570 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700571 if (library->GetClassLoader() != declaring_class_loader) {
572 // We only search libraries loaded by the appropriate ClassLoader.
573 continue;
574 }
575 // Try the short name then the long name...
576 void* fn = library->FindSymbol(jni_short_name);
577 if (fn == NULL) {
578 fn = library->FindSymbol(jni_long_name);
579 }
580 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800581 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
582 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700583 return fn;
584 }
585 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700586 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700587 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700588 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700589 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700590 return NULL;
591 }
592
593 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700594 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700595};
596
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700597JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
598 jvalue* args) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700599 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700600 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700601 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800602 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700603 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800604 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700605 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
606 return result;
Elliott Hughesd07986f2011-12-06 18:27:45 -0800607}
608
Elliott Hughescdf53122011-08-19 15:46:09 -0700609class JNI {
610 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700611 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700612 return JNI_VERSION_1_6;
613 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700614
Ian Rogers25e8b912012-09-07 11:31:36 -0700615 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700616 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700617 return NULL;
618 }
619
Elliott Hughescdf53122011-08-19 15:46:09 -0700620 static jclass FindClass(JNIEnv* env, const char* name) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700621 Runtime* runtime = Runtime::Current();
622 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700623 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700624 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700625 Class* c = NULL;
626 if (runtime->IsStarted()) {
Ian Rogersef28b142012-11-30 14:22:18 -0800627 ClassLoader* cl = GetClassLoader(soa);
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800628 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700629 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800630 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700631 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700632 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700633 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700634
Elliott Hughescdf53122011-08-19 15:46:09 -0700635 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700636 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700637 jobject art_method = env->GetObjectField(java_method,
638 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod);
639 ArtMethod* method = soa.Decode<ArtMethod*>(art_method);
640 DCHECK(method != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700641 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700642 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700643
Elliott Hughescdf53122011-08-19 15:46:09 -0700644 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700645 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700646 jobject art_field = env->GetObjectField(java_field,
647 WellKnownClasses::java_lang_reflect_Field_artField);
648 ArtField* field = soa.Decode<ArtField*>(art_field);
649 DCHECK(field != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700650 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700651 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700652
Elliott Hughescdf53122011-08-19 15:46:09 -0700653 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700654 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700655 ArtMethod* m = soa.DecodeMethod(mid);
656 jobject art_method = soa.AddLocalReference<jobject>(m);
657 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
658 if (env->ExceptionCheck()) {
659 return NULL;
660 }
661 SetObjectField(env,
662 reflect_method,
663 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod,
664 art_method);
665 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700666 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700667
Elliott Hughescdf53122011-08-19 15:46:09 -0700668 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700669 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700670 ArtField* f = soa.DecodeField(fid);
671 jobject art_field = soa.AddLocalReference<jobject>(f);
672 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
673 if (env->ExceptionCheck()) {
674 return NULL;
675 }
676 SetObjectField(env,
677 reflect_field,
678 WellKnownClasses::java_lang_reflect_Field_artField,
679 art_field);
680 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700681 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700682
Elliott Hughes37f7a402011-08-22 18:56:01 -0700683 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700684 ScopedObjectAccess soa(env);
685 Object* o = soa.Decode<Object*>(java_object);
686 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700687 }
688
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700689 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700690 ScopedObjectAccess soa(env);
691 Class* c = soa.Decode<Class*>(java_class);
692 return soa.AddLocalReference<jclass>(c->GetSuperClass());
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 jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700696 ScopedObjectAccess soa(env);
697 Class* c1 = soa.Decode<Class*>(java_class1);
698 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700699 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700700 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700701
Elliott Hughese84278b2012-03-22 10:06:53 -0700702 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Elliott Hughes96a98872012-12-19 14:21:15 -0800703 if (java_class == NULL) {
704 JniAbortF("IsInstanceOf", "null class (second argument)");
705 }
Elliott Hughes37f7a402011-08-22 18:56:01 -0700706 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700707 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700708 return JNI_TRUE;
709 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700710 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700711 Object* obj = soa.Decode<Object*>(jobj);
712 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700713 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700714 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700715 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700716
Elliott Hughes37f7a402011-08-22 18:56:01 -0700717 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700718 ScopedObjectAccess soa(env);
719 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700720 if (exception == NULL) {
721 return JNI_ERR;
722 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800723 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
724 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700725 return JNI_OK;
726 }
727
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700728 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700729 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700730 }
731
732 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700733 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700734 }
735
736 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700737 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700738 }
739
740 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700741 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700742
Brian Carlstromea46f952013-07-30 01:26:50 -0700743 SirtRef<Object> old_throw_this_object(soa.Self(), NULL);
744 SirtRef<ArtMethod> old_throw_method(soa.Self(), NULL);
745 SirtRef<Throwable> old_exception(soa.Self(), NULL);
Ian Rogers62d6c772013-02-27 08:32:07 -0800746 uint32_t old_throw_dex_pc;
747 {
748 ThrowLocation old_throw_location;
Brian Carlstromea46f952013-07-30 01:26:50 -0700749 Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Ian Rogers62d6c772013-02-27 08:32:07 -0800750 old_throw_this_object.reset(old_throw_location.GetThis());
751 old_throw_method.reset(old_throw_location.GetMethod());
752 old_exception.reset(old_exception_obj);
753 old_throw_dex_pc = old_throw_location.GetDexPc();
754 soa.Self()->ClearException();
755 }
756 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(old_exception.get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700757 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
758 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
759 if (mid == NULL) {
760 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Ian Rogers62d6c772013-02-27 08:32:07 -0800761 << PrettyTypeOf(old_exception.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700762 } else {
763 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800764 if (soa.Self()->IsExceptionPending()) {
765 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(NULL))
Elliott Hughes72025e52011-08-23 17:50:30 -0700766 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800767 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700768 }
769 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800770 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
771 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700772
Ian Rogers62d6c772013-02-27 08:32:07 -0800773 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700774 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700775
Elliott Hughescdf53122011-08-19 15:46:09 -0700776 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700777 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800778 Object* exception = soa.Self()->GetException(NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700779 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700780 }
781
Ian Rogers25e8b912012-09-07 11:31:36 -0700782 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700783 LOG(FATAL) << "JNI FatalError called: " << msg;
784 }
785
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700786 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800787 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700788 return JNI_ERR;
789 }
Ian Rogersef28b142012-11-30 14:22:18 -0800790 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700791 return JNI_OK;
792 }
793
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700794 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700795 ScopedObjectAccess soa(env);
796 Object* survivor = soa.Decode<Object*>(java_survivor);
797 soa.Env()->PopFrame();
798 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700799 }
800
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700801 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800802 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700803 }
804
Elliott Hughescdf53122011-08-19 15:46:09 -0700805 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700806 if (obj == NULL) {
807 return NULL;
808 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700809 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700810 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700811 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700812 Object* decoded_obj = soa.Decode<Object*>(obj);
Ian Rogers50b35e22012-10-04 10:09:15 -0700813 MutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700814 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700815 return reinterpret_cast<jobject>(ref);
816 }
817
818 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700819 if (obj == NULL) {
820 return;
821 }
Ian Rogersef28b142012-11-30 14:22:18 -0800822 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700823 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800824 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
825 MutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700826
827 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
828 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
829 << "failed to find entry";
830 }
831 }
832
833 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700834 ScopedObjectAccess soa(env);
835 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700836 }
837
838 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700839 if (obj == NULL) {
840 return;
841 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700842 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700843 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700844 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -0700845 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700846
847 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
848 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
849 << "failed to find entry";
850 }
851 }
852
853 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700854 if (obj == NULL) {
855 return NULL;
856 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700857 ScopedObjectAccess soa(env);
Elliott Hughes9dcd45c2013-07-29 14:40:52 -0700858 return soa.AddLocalReference<jobject>(soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700859 }
860
861 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700862 if (obj == NULL) {
863 return;
864 }
Ian Rogersef28b142012-11-30 14:22:18 -0800865 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700866
Ian Rogersef28b142012-11-30 14:22:18 -0800867 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700868 if (!locals.Remove(cookie, obj)) {
869 // Attempting to delete a local reference that is not in the
870 // topmost local reference frame is a no-op. DeleteLocalRef returns
871 // void and doesn't throw any exceptions, but we should probably
872 // complain about it so the user will notice that things aren't
873 // going quite the way they expect.
874 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
875 << "failed to find entry";
876 }
877 }
878
879 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700880 if (obj1 == obj2) {
881 return JNI_TRUE;
882 } else {
883 ScopedObjectAccess soa(env);
884 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
885 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700886 }
887
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700888 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700889 ScopedObjectAccess soa(env);
890 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700891 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700892 return NULL;
893 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700894 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700895 }
896
Elliott Hughese84278b2012-03-22 10:06:53 -0700897 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700898 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700899 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -0700900 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700901 va_end(args);
902 return result;
903 }
904
Elliott Hughes72025e52011-08-23 17:50:30 -0700905 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700906 ScopedObjectAccess soa(env);
907 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700908 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700909 return NULL;
910 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700911 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700912 if (result == NULL) {
913 return NULL;
914 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700915 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700916 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700917 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700918 return local_result;
919 } else {
920 return NULL;
921 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700922 }
923
Elliott Hughes72025e52011-08-23 17:50:30 -0700924 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700925 ScopedObjectAccess soa(env);
926 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700927 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700928 return NULL;
929 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700930 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700931 if (result == NULL) {
932 return NULL;
933 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700934 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700935 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700936 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700937 return local_result;
938 } else {
939 return NULL;
940 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700941 }
942
Elliott Hughescdf53122011-08-19 15:46:09 -0700943 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700944 ScopedObjectAccess soa(env);
945 return FindMethodID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700946 }
947
948 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700949 ScopedObjectAccess soa(env);
950 return FindMethodID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700951 }
952
Elliott Hughes72025e52011-08-23 17:50:30 -0700953 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700954 va_list ap;
955 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700956 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700957 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700958 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700959 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700960 }
961
Elliott Hughes72025e52011-08-23 17:50:30 -0700962 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700963 ScopedObjectAccess soa(env);
964 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
965 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700966 }
967
Elliott Hughes72025e52011-08-23 17:50:30 -0700968 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700969 ScopedObjectAccess soa(env);
970 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
971 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700972 }
973
Elliott Hughes72025e52011-08-23 17:50:30 -0700974 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700975 va_list ap;
976 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700977 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700978 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700979 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700980 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700981 }
982
Elliott Hughes72025e52011-08-23 17:50:30 -0700983 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700984 ScopedObjectAccess soa(env);
985 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 }
987
Elliott Hughes72025e52011-08-23 17:50:30 -0700988 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700989 ScopedObjectAccess soa(env);
990 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 }
992
Elliott Hughes72025e52011-08-23 17:50:30 -0700993 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700994 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700995 va_list ap;
996 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700997 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700999 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001000 }
1001
Elliott Hughes72025e52011-08-23 17:50:30 -07001002 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001003 ScopedObjectAccess soa(env);
1004 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 }
1006
Elliott Hughes72025e52011-08-23 17:50:30 -07001007 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001008 ScopedObjectAccess soa(env);
1009 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001010 }
1011
Elliott Hughes72025e52011-08-23 17:50:30 -07001012 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001013 va_list ap;
1014 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001015 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001016 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001017 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001018 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001019 }
1020
Elliott Hughes72025e52011-08-23 17:50:30 -07001021 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001022 ScopedObjectAccess soa(env);
1023 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 }
1025
Elliott Hughes72025e52011-08-23 17:50:30 -07001026 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001027 ScopedObjectAccess soa(env);
1028 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001029 }
1030
Elliott Hughes72025e52011-08-23 17:50:30 -07001031 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001032 va_list ap;
1033 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001034 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001035 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001036 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001037 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001038 }
1039
Elliott Hughes72025e52011-08-23 17:50:30 -07001040 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001041 ScopedObjectAccess soa(env);
1042 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001043 }
1044
Elliott Hughes72025e52011-08-23 17:50:30 -07001045 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001046 ScopedObjectAccess soa(env);
1047 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 }
1049
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001051 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 va_list ap;
1053 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001054 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001056 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001057 }
1058
Elliott Hughes72025e52011-08-23 17:50:30 -07001059 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001060 ScopedObjectAccess soa(env);
1061 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 }
1063
Elliott Hughes72025e52011-08-23 17:50:30 -07001064 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001065 ScopedObjectAccess soa(env);
1066 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001067 }
1068
Elliott Hughes72025e52011-08-23 17:50:30 -07001069 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001070 va_list ap;
1071 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001072 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001073 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001074 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001075 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001076 }
1077
Elliott Hughes72025e52011-08-23 17:50:30 -07001078 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001079 ScopedObjectAccess soa(env);
1080 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 }
1082
Elliott Hughes72025e52011-08-23 17:50:30 -07001083 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001084 ScopedObjectAccess soa(env);
1085 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 }
1087
Elliott Hughes72025e52011-08-23 17:50:30 -07001088 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001089 va_list ap;
1090 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001091 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001092 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001093 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001094 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001095 }
1096
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001098 ScopedObjectAccess soa(env);
1099 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001100 }
1101
Elliott Hughes72025e52011-08-23 17:50:30 -07001102 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001103 ScopedObjectAccess soa(env);
1104 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 }
1106
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001108 va_list ap;
1109 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001110 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001111 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001112 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001113 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001114 }
1115
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001117 ScopedObjectAccess soa(env);
1118 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 }
1120
Elliott Hughes72025e52011-08-23 17:50:30 -07001121 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001122 ScopedObjectAccess soa(env);
1123 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 }
1125
Elliott Hughes72025e52011-08-23 17:50:30 -07001126 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001127 va_list ap;
1128 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001129 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001130 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001131 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 }
1133
Elliott Hughes72025e52011-08-23 17:50:30 -07001134 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001135 ScopedObjectAccess soa(env);
1136 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 }
1138
Elliott Hughes72025e52011-08-23 17:50:30 -07001139 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001140 ScopedObjectAccess soa(env);
1141 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001142 }
1143
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001144 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001146 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001147 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001148 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1149 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001150 va_end(ap);
1151 return local_result;
1152 }
1153
1154 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001155 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001156 ScopedObjectAccess soa(env);
1157 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1158 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 }
1160
1161 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001162 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001163 ScopedObjectAccess soa(env);
1164 JValue result(InvokeWithJValues(soa, obj, mid, args));
1165 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 }
1167
1168 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001169 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001171 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001172 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001173 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001174 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001175 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 }
1177
1178 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001179 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001180 ScopedObjectAccess soa(env);
1181 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 }
1183
1184 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001185 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001186 ScopedObjectAccess soa(env);
1187 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 }
1189
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001190 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001191 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001193 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001194 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001196 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 }
1198
1199 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001200 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001201 ScopedObjectAccess soa(env);
1202 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001203 }
1204
1205 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001206 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001207 ScopedObjectAccess soa(env);
1208 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001209 }
1210
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001211 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001212 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001214 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001215 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001217 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 }
1219
1220 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001221 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001222 ScopedObjectAccess soa(env);
1223 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001224 }
1225
1226 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001227 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001228 ScopedObjectAccess soa(env);
1229 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 }
1231
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001232 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001233 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001234 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001235 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001236 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001238 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 }
1240
1241 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001242 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001243 ScopedObjectAccess soa(env);
1244 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001245 }
1246
1247 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001248 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001249 ScopedObjectAccess soa(env);
1250 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001251 }
1252
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001253 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001255 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001256 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001257 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001259 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 }
1261
1262 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001263 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001264 ScopedObjectAccess soa(env);
1265 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 }
1267
1268 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001269 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001270 ScopedObjectAccess soa(env);
1271 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001272 }
1273
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001274 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001275 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001276 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001277 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001278 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001280 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 }
1282
1283 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001284 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001285 ScopedObjectAccess soa(env);
1286 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 }
1288
1289 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001290 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001291 ScopedObjectAccess soa(env);
1292 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 }
1294
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001295 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001296 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001297 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001298 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001299 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001301 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 }
1303
1304 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001305 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001306 ScopedObjectAccess soa(env);
1307 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 }
1309
1310 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001311 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001312 ScopedObjectAccess soa(env);
1313 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 }
1315
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001316 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001317 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001318 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001319 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001320 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001322 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 }
1324
1325 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001326 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001327 ScopedObjectAccess soa(env);
1328 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001329 }
1330
1331 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001332 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001333 ScopedObjectAccess soa(env);
1334 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001335 }
1336
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001337 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001338 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001339 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001340 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001341 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 va_end(ap);
1343 }
1344
Brian Carlstromea46f952013-07-30 01:26:50 -07001345 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1346 va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001347 ScopedObjectAccess soa(env);
1348 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 }
1350
1351 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001352 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001353 ScopedObjectAccess soa(env);
1354 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001355 }
1356
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001357 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001358 ScopedObjectAccess soa(env);
1359 return FindFieldID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001361
1362
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001363 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001364 ScopedObjectAccess soa(env);
1365 return FindFieldID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001366 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001367
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001368 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001369 ScopedObjectAccess soa(env);
1370 Object* o = soa.Decode<Object*>(obj);
Brian Carlstromea46f952013-07-30 01:26:50 -07001371 ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001372 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001373 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001374
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001375 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001376 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -07001377 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001378 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 }
1380
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001381 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001382 ScopedObjectAccess soa(env);
1383 Object* o = soa.Decode<Object*>(java_object);
1384 Object* v = soa.Decode<Object*>(java_value);
Brian Carlstromea46f952013-07-30 01:26:50 -07001385 ArtField* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001386 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 }
1388
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001389 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001390 ScopedObjectAccess soa(env);
1391 Object* v = soa.Decode<Object*>(java_value);
Brian Carlstromea46f952013-07-30 01:26:50 -07001392 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001393 f->SetObject(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 }
1395
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001396#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001397 ScopedObjectAccess soa(env); \
1398 Object* o = soa.Decode<Object*>(instance); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001399 ArtField* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001400 return f->fn(o)
1401
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001402#define GET_STATIC_PRIMITIVE_FIELD(fn) \
1403 ScopedObjectAccess soa(env); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001404 ArtField* f = soa.DecodeField(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001405 return f->fn(f->GetDeclaringClass())
1406
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001407#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001408 ScopedObjectAccess soa(env); \
1409 Object* o = soa.Decode<Object*>(instance); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001410 ArtField* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001411 f->fn(o, value)
1412
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001413#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
1414 ScopedObjectAccess soa(env); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001415 ArtField* f = soa.DecodeField(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001416 f->fn(f->GetDeclaringClass(), value)
1417
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001418 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1419 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 }
1421
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001422 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1423 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 }
1425
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001426 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1427 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 }
1429
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001430 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1431 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 }
1433
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001434 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1435 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 }
1437
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001438 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1439 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 }
1441
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001442 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1443 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001444 }
1445
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001446 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1447 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001448 }
1449
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001450 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001451 GET_STATIC_PRIMITIVE_FIELD(GetBoolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001452 }
1453
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001454 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001455 GET_STATIC_PRIMITIVE_FIELD(GetByte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001456 }
1457
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001458 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001459 GET_STATIC_PRIMITIVE_FIELD(GetChar);
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 }
1461
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001462 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001463 GET_STATIC_PRIMITIVE_FIELD(GetShort);
Elliott Hughescdf53122011-08-19 15:46:09 -07001464 }
1465
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001466 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001467 GET_STATIC_PRIMITIVE_FIELD(GetInt);
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 }
1469
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001470 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001471 GET_STATIC_PRIMITIVE_FIELD(GetLong);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001472 }
1473
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001474 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001475 GET_STATIC_PRIMITIVE_FIELD(GetFloat);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001476 }
1477
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001478 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001479 GET_STATIC_PRIMITIVE_FIELD(GetDouble);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001480 }
1481
1482 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1483 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1484 }
1485
1486 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1487 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1488 }
1489
1490 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1491 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1492 }
1493
1494 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1495 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1496 }
1497
1498 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1499 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1500 }
1501
1502 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1503 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1504 }
1505
1506 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1507 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1508 }
1509
1510 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1511 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1512 }
1513
1514 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001515 SET_STATIC_PRIMITIVE_FIELD(SetBoolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001516 }
1517
1518 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001519 SET_STATIC_PRIMITIVE_FIELD(SetByte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001520 }
1521
1522 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001523 SET_STATIC_PRIMITIVE_FIELD(SetChar, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001524 }
1525
1526 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001527 SET_STATIC_PRIMITIVE_FIELD(SetFloat, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001528 }
1529
1530 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001531 SET_STATIC_PRIMITIVE_FIELD(SetDouble, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001532 }
1533
1534 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001535 SET_STATIC_PRIMITIVE_FIELD(SetInt, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001536 }
1537
1538 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001539 SET_STATIC_PRIMITIVE_FIELD(SetLong, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001540 }
1541
1542 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001543 SET_STATIC_PRIMITIVE_FIELD(SetShort, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001544 }
1545
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001546 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001547 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001548 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001549 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001550 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1551 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001552 va_end(ap);
1553 return local_result;
1554 }
1555
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001556 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001557 ScopedObjectAccess soa(env);
1558 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1559 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001560 }
1561
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001562 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001563 ScopedObjectAccess soa(env);
1564 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1565 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 }
1567
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001568 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001569 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001570 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001571 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001572 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001574 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001575 }
1576
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001577 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001578 ScopedObjectAccess soa(env);
1579 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001580 }
1581
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001582 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001583 ScopedObjectAccess soa(env);
1584 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 }
1586
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001587 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001588 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001589 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001590 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001591 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001592 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001593 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001594 }
1595
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001596 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001597 ScopedObjectAccess soa(env);
1598 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 }
1600
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001601 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001602 ScopedObjectAccess soa(env);
1603 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001604 }
1605
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001606 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001608 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001609 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001610 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001612 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001613 }
1614
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001615 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001616 ScopedObjectAccess soa(env);
1617 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 }
1619
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001620 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001621 ScopedObjectAccess soa(env);
1622 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001623 }
1624
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001625 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001626 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001627 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001628 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001629 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001630 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001631 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 }
1633
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001634 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001635 ScopedObjectAccess soa(env);
1636 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 }
1638
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001639 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001640 ScopedObjectAccess soa(env);
1641 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 }
1643
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001644 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001645 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001646 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001647 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001648 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001649 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001650 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 }
1652
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001653 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001654 ScopedObjectAccess soa(env);
1655 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001656 }
1657
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001658 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001659 ScopedObjectAccess soa(env);
1660 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001661 }
1662
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001663 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001664 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001665 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001666 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001667 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001668 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001669 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 }
1671
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001672 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001673 ScopedObjectAccess soa(env);
1674 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 }
1676
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001677 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001678 ScopedObjectAccess soa(env);
1679 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001680 }
1681
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001682 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001683 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001684 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001685 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001686 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001687 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001688 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 }
1690
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001691 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001692 ScopedObjectAccess soa(env);
1693 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 }
1695
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001696 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001697 ScopedObjectAccess soa(env);
1698 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 }
1700
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001701 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001702 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001703 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001704 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001705 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001707 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 }
1709
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001710 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001711 ScopedObjectAccess soa(env);
1712 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001713 }
1714
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001715 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001716 ScopedObjectAccess soa(env);
1717 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 }
1719
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001720 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001722 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001723 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001724 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 va_end(ap);
1726 }
1727
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001728 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001729 ScopedObjectAccess soa(env);
1730 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001731 }
1732
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001733 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001734 ScopedObjectAccess soa(env);
1735 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 }
1737
Elliott Hughes814e4032011-08-23 12:07:56 -07001738 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001739 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001740 String* result = String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001741 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 }
1743
1744 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001745 if (utf == NULL) {
1746 return NULL;
1747 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001748 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001749 String* result = String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001750 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001751 }
1752
Elliott Hughes814e4032011-08-23 12:07:56 -07001753 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001754 ScopedObjectAccess soa(env);
1755 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001756 }
1757
1758 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001759 ScopedObjectAccess soa(env);
1760 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001761 }
1762
Elliott Hughesb465ab02011-08-24 11:21:21 -07001763 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001764 ScopedObjectAccess soa(env);
1765 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001766 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001767 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001768 } else {
1769 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1770 memcpy(buf, chars + start, length * sizeof(jchar));
1771 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001772 }
1773
Elliott Hughesb465ab02011-08-24 11:21:21 -07001774 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001775 ScopedObjectAccess soa(env);
1776 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001777 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001778 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001779 } else {
1780 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1781 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1782 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001783 }
1784
Elliott Hughes75770752011-08-24 17:52:38 -07001785 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001786 ScopedObjectAccess soa(env);
1787 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001788 const CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001789 PinPrimitiveArray(soa, chars);
Elliott Hughes75770752011-08-24 17:52:38 -07001790 if (is_copy != NULL) {
1791 *is_copy = JNI_FALSE;
1792 }
1793 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001794 }
1795
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001796 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001797 ScopedObjectAccess soa(env);
1798 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001799 }
1800
Elliott Hughes75770752011-08-24 17:52:38 -07001801 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001802 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001803 }
1804
Elliott Hughes75770752011-08-24 17:52:38 -07001805 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001806 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001807 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001808 }
1809
Elliott Hughes75770752011-08-24 17:52:38 -07001810 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001811 if (java_string == NULL) {
1812 return NULL;
1813 }
1814 if (is_copy != NULL) {
1815 *is_copy = JNI_TRUE;
1816 }
Ian Rogersef28b142012-11-30 14:22:18 -08001817 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001818 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001819 size_t byte_count = s->GetUtfLength();
1820 char* bytes = new char[byte_count + 1];
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001821 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001822 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1823 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1824 bytes[byte_count] = '\0';
1825 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001826 }
1827
Elliott Hughes75770752011-08-24 17:52:38 -07001828 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001829 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001830 }
1831
Elliott Hughesbd935992011-08-22 11:59:34 -07001832 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001833 ScopedObjectAccess soa(env);
1834 Object* obj = soa.Decode<Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07001835 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08001836 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1837 }
Elliott Hughesbd935992011-08-22 11:59:34 -07001838 Array* array = obj->AsArray();
1839 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001840 }
1841
Elliott Hughes814e4032011-08-23 12:07:56 -07001842 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001843 ScopedObjectAccess soa(env);
1844 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1845 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001846 }
1847
1848 static void SetObjectArrayElement(JNIEnv* env,
1849 jobjectArray java_array, jsize index, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001850 ScopedObjectAccess soa(env);
1851 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1852 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001853 array->Set(index, value);
1854 }
1855
1856 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001857 ScopedObjectAccess soa(env);
1858 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001859 }
1860
1861 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001862 ScopedObjectAccess soa(env);
1863 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001864 }
1865
1866 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001867 ScopedObjectAccess soa(env);
1868 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001869 }
1870
1871 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001872 ScopedObjectAccess soa(env);
1873 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001874 }
1875
1876 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001877 ScopedObjectAccess soa(env);
1878 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001879 }
1880
1881 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001882 ScopedObjectAccess soa(env);
1883 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001884 }
1885
1886 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001887 ScopedObjectAccess soa(env);
1888 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001889 }
1890
1891 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Elliott Hughes96a98872012-12-19 14:21:15 -08001892 if (length < 0) {
1893 JniAbortF("NewObjectArray", "negative array length: %d", length);
1894 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001895
1896 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07001897 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001898 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001899 std::string descriptor;
1900 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001901 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001902
1903 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001904 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1905 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001906 return NULL;
1907 }
1908
Elliott Hughes75770752011-08-24 17:52:38 -07001909 // Allocate and initialize if necessary.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001910 Class* array_class = soa.Decode<Class*>(java_array_class.get());
Ian Rogers50b35e22012-10-04 10:09:15 -07001911 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001912 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001913 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07001914 for (jsize i = 0; i < length; ++i) {
1915 result->Set(i, initial_object);
1916 }
1917 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001918 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001919 }
1920
1921 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001922 ScopedObjectAccess soa(env);
1923 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001924 }
1925
Ian Rogersa15e67d2012-02-28 13:51:55 -08001926 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001927 ScopedObjectAccess soa(env);
1928 Array* array = soa.Decode<Array*>(java_array);
1929 PinPrimitiveArray(soa, array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001930 if (is_copy != NULL) {
1931 *is_copy = JNI_FALSE;
1932 }
1933 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001934 }
1935
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001936 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001937 ReleasePrimitiveArray(env, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001938 }
1939
Elliott Hughes75770752011-08-24 17:52:38 -07001940 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001941 ScopedObjectAccess soa(env);
1942 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001943 }
1944
Elliott Hughes75770752011-08-24 17:52:38 -07001945 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001946 ScopedObjectAccess soa(env);
1947 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001948 }
1949
Elliott Hughes75770752011-08-24 17:52:38 -07001950 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001951 ScopedObjectAccess soa(env);
1952 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001953 }
1954
Elliott Hughes75770752011-08-24 17:52:38 -07001955 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001956 ScopedObjectAccess soa(env);
1957 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001958 }
1959
Elliott Hughes75770752011-08-24 17:52:38 -07001960 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001961 ScopedObjectAccess soa(env);
1962 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001963 }
1964
Elliott Hughes75770752011-08-24 17:52:38 -07001965 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001966 ScopedObjectAccess soa(env);
1967 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001968 }
1969
Elliott Hughes75770752011-08-24 17:52:38 -07001970 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001971 ScopedObjectAccess soa(env);
1972 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001973 }
1974
Elliott Hughes75770752011-08-24 17:52:38 -07001975 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001976 ScopedObjectAccess soa(env);
1977 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001978 }
1979
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001980 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001981 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 }
1983
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001984 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001985 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001986 }
1987
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001988 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001989 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001990 }
1991
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001992 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001993 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001994 }
1995
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001996 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001997 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001998 }
1999
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002000 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002001 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 }
2003
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002004 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002005 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002006 }
2007
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002008 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002009 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002010 }
2011
Elliott Hughes814e4032011-08-23 12:07:56 -07002012 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002013 ScopedObjectAccess soa(env);
2014 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002015 }
2016
Elliott Hughes814e4032011-08-23 12:07:56 -07002017 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002018 ScopedObjectAccess soa(env);
2019 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002020 }
2021
Elliott Hughes814e4032011-08-23 12:07:56 -07002022 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002023 ScopedObjectAccess soa(env);
2024 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002025 }
2026
Elliott Hughes814e4032011-08-23 12:07:56 -07002027 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002028 ScopedObjectAccess soa(env);
2029 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002030 }
2031
Elliott Hughes814e4032011-08-23 12:07:56 -07002032 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002033 ScopedObjectAccess soa(env);
2034 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002035 }
2036
Elliott Hughes814e4032011-08-23 12:07:56 -07002037 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002038 ScopedObjectAccess soa(env);
2039 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002040 }
2041
Elliott Hughes814e4032011-08-23 12:07:56 -07002042 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002043 ScopedObjectAccess soa(env);
2044 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002045 }
2046
Elliott Hughes814e4032011-08-23 12:07:56 -07002047 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002048 ScopedObjectAccess soa(env);
2049 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002050 }
2051
Elliott Hughes814e4032011-08-23 12:07:56 -07002052 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002053 ScopedObjectAccess soa(env);
2054 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 }
2056
Elliott Hughes814e4032011-08-23 12:07:56 -07002057 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002058 ScopedObjectAccess soa(env);
2059 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 }
2061
Elliott Hughes814e4032011-08-23 12:07:56 -07002062 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002063 ScopedObjectAccess soa(env);
2064 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002065 }
2066
Elliott Hughes814e4032011-08-23 12:07:56 -07002067 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002068 ScopedObjectAccess soa(env);
2069 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 }
2071
Elliott Hughes814e4032011-08-23 12:07:56 -07002072 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002073 ScopedObjectAccess soa(env);
2074 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002075 }
2076
Elliott Hughes814e4032011-08-23 12:07:56 -07002077 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002078 ScopedObjectAccess soa(env);
2079 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002080 }
2081
Elliott Hughes814e4032011-08-23 12:07:56 -07002082 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002083 ScopedObjectAccess soa(env);
2084 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002085 }
2086
Elliott Hughes814e4032011-08-23 12:07:56 -07002087 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002088 ScopedObjectAccess soa(env);
2089 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 }
2091
Elliott Hughes5174fe62011-08-23 15:12:35 -07002092 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002093 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2094 }
2095
2096 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 -07002097 ScopedObjectAccess soa(env);
2098 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002099
Elliott Hughesc8fece32013-01-02 11:27:23 -08002100 for (size_t i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 const char* name = methods[i].name;
2102 const char* sig = methods[i].signature;
2103
2104 if (*sig == '!') {
2105 // TODO: fast jni. it's too noisy to log all these.
2106 ++sig;
2107 }
2108
Brian Carlstromea46f952013-07-30 01:26:50 -07002109 ArtMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002110 if (m == NULL) {
2111 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002113 if (m == NULL) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002114 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
2115 << PrettyDescriptor(c) << "." << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002116 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002118 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002119 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
2120 << PrettyDescriptor(c) << "." << name << sig
2121 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002122 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002123 return JNI_ERR;
2124 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002125
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002126 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002127
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002128 m->RegisterNative(soa.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 }
2130 return JNI_OK;
2131 }
2132
Elliott Hughes5174fe62011-08-23 15:12:35 -07002133 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002134 ScopedObjectAccess soa(env);
2135 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002136
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002137 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002138
2139 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07002140 ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002141 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002142 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002143 }
2144 }
2145 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07002146 ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002147 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002148 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002149 }
2150 }
2151
2152 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 }
2154
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002155 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2156 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
2157 ScopedObjectAccess soa(env);
2158 Object* o = soa.Decode<Object*>(java_object);
2159 o->MonitorEnter(soa.Self());
2160 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002161 return JNI_ERR;
2162 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002163 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002164 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 }
2166
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002167 static jint MonitorExit(JNIEnv* env, jobject java_object)
2168 UNLOCK_FUNCTION(monitor_lock_) {
2169 ScopedObjectAccess soa(env);
2170 Object* o = soa.Decode<Object*>(java_object);
2171 o->MonitorExit(soa.Self());
2172 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002173 return JNI_ERR;
2174 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002175 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002176 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002177 }
2178
2179 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002180 Runtime* runtime = Runtime::Current();
2181 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002182 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002183 } else {
2184 *vm = NULL;
2185 }
2186 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2187 }
2188
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002190 if (capacity < 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002191 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002192 }
Elliott Hughes11a796e2012-12-19 14:42:57 -08002193 if (address == NULL && capacity != 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002194 JniAbortF("NewDirectByteBuffer", "non-zero capacity for NULL pointer: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002195 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002196
Elliott Hughes96a98872012-12-19 14:21:15 -08002197 // At the moment, the Java side is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002198 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2199 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002200 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002201 jint capacity_arg = static_cast<jint>(capacity);
2202
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002203 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2204 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002205 address_arg, capacity_arg);
Ian Rogersef28b142012-11-30 14:22:18 -08002206 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002207 }
2208
Elliott Hughesb465ab02011-08-24 11:21:21 -07002209 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Jeff Hao534f2b62013-07-10 15:29:36 -07002210 return reinterpret_cast<void*>(env->GetLongField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002211 }
2212
Elliott Hughesb465ab02011-08-24 11:21:21 -07002213 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002214 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002215 }
2216
Elliott Hughesb465ab02011-08-24 11:21:21 -07002217 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002218 if (java_object == NULL) {
2219 JniAbortF("GetObjectRefType", "null object");
2220 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002221
2222 // Do we definitely know what kind of reference this is?
2223 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2224 IndirectRefKind kind = GetIndirectRefKind(ref);
2225 switch (kind) {
2226 case kLocal:
Ian Rogersef28b142012-11-30 14:22:18 -08002227 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002228 return JNILocalRefType;
2229 }
2230 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002231 case kGlobal:
2232 return JNIGlobalRefType;
2233 case kWeakGlobal:
2234 return JNIWeakGlobalRefType;
2235 case kSirtOrInvalid:
2236 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002237 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002238 return JNILocalRefType;
2239 }
2240
Ian Rogersef28b142012-11-30 14:22:18 -08002241 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002242 return JNIInvalidRefType;
2243 }
2244
Elliott Hughesb465ab02011-08-24 11:21:21 -07002245 // If we're handing out direct pointers, check whether it's a direct pointer
2246 // to a local reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002247 {
2248 ScopedObjectAccess soa(env);
2249 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2250 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
2251 return JNILocalRefType;
2252 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002253 }
2254 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002255 return JNIInvalidRefType;
2256 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002257 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2258 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002259 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002260
2261 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002262 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2263 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002264 // TODO: we should try to expand the table if necessary.
2265 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
2266 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2267 return JNI_ERR;
2268 }
2269 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002270 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002271 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2272 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002273 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002274 soa.Self()->ThrowOutOfMemoryError(caller);
2275 }
2276 return okay ? JNI_OK : JNI_ERR;
2277 }
2278
2279 template<typename JniT, typename ArtT>
2280 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002281 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002282 if (length < 0) {
2283 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
2284 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002285 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002286 return soa.AddLocalReference<JniT>(result);
2287 }
2288
2289 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2290 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2291 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002292 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002293 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2294 PinPrimitiveArray(soa, array);
2295 if (is_copy != NULL) {
2296 *is_copy = JNI_FALSE;
2297 }
2298 return array->GetData();
2299 }
2300
2301 template <typename ArrayT>
Ian Rogersef28b142012-11-30 14:22:18 -08002302 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002303 if (mode != JNI_COMMIT) {
Ian Rogersef28b142012-11-30 14:22:18 -08002304 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002305 Array* array = soa.Decode<Array*>(java_array);
2306 UnpinPrimitiveArray(soa, array);
2307 }
2308 }
2309
2310 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2311 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2312 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002313 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002314 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2315 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2316 ThrowAIOOBE(soa, array, start, length, "src");
2317 } else {
2318 JavaT* data = array->GetData();
2319 memcpy(buf, data + start, length * sizeof(JavaT));
2320 }
2321 }
2322
2323 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2324 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2325 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002326 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002327 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2328 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2329 ThrowAIOOBE(soa, array, start, length, "dst");
2330 } else {
2331 JavaT* data = array->GetData();
2332 memcpy(data + start, buf, length * sizeof(JavaT));
2333 }
2334 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002335};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002336
Elliott Hughes88c5c352012-03-15 18:49:48 -07002337const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002338 NULL, // reserved0.
2339 NULL, // reserved1.
2340 NULL, // reserved2.
2341 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002342 JNI::GetVersion,
2343 JNI::DefineClass,
2344 JNI::FindClass,
2345 JNI::FromReflectedMethod,
2346 JNI::FromReflectedField,
2347 JNI::ToReflectedMethod,
2348 JNI::GetSuperclass,
2349 JNI::IsAssignableFrom,
2350 JNI::ToReflectedField,
2351 JNI::Throw,
2352 JNI::ThrowNew,
2353 JNI::ExceptionOccurred,
2354 JNI::ExceptionDescribe,
2355 JNI::ExceptionClear,
2356 JNI::FatalError,
2357 JNI::PushLocalFrame,
2358 JNI::PopLocalFrame,
2359 JNI::NewGlobalRef,
2360 JNI::DeleteGlobalRef,
2361 JNI::DeleteLocalRef,
2362 JNI::IsSameObject,
2363 JNI::NewLocalRef,
2364 JNI::EnsureLocalCapacity,
2365 JNI::AllocObject,
2366 JNI::NewObject,
2367 JNI::NewObjectV,
2368 JNI::NewObjectA,
2369 JNI::GetObjectClass,
2370 JNI::IsInstanceOf,
2371 JNI::GetMethodID,
2372 JNI::CallObjectMethod,
2373 JNI::CallObjectMethodV,
2374 JNI::CallObjectMethodA,
2375 JNI::CallBooleanMethod,
2376 JNI::CallBooleanMethodV,
2377 JNI::CallBooleanMethodA,
2378 JNI::CallByteMethod,
2379 JNI::CallByteMethodV,
2380 JNI::CallByteMethodA,
2381 JNI::CallCharMethod,
2382 JNI::CallCharMethodV,
2383 JNI::CallCharMethodA,
2384 JNI::CallShortMethod,
2385 JNI::CallShortMethodV,
2386 JNI::CallShortMethodA,
2387 JNI::CallIntMethod,
2388 JNI::CallIntMethodV,
2389 JNI::CallIntMethodA,
2390 JNI::CallLongMethod,
2391 JNI::CallLongMethodV,
2392 JNI::CallLongMethodA,
2393 JNI::CallFloatMethod,
2394 JNI::CallFloatMethodV,
2395 JNI::CallFloatMethodA,
2396 JNI::CallDoubleMethod,
2397 JNI::CallDoubleMethodV,
2398 JNI::CallDoubleMethodA,
2399 JNI::CallVoidMethod,
2400 JNI::CallVoidMethodV,
2401 JNI::CallVoidMethodA,
2402 JNI::CallNonvirtualObjectMethod,
2403 JNI::CallNonvirtualObjectMethodV,
2404 JNI::CallNonvirtualObjectMethodA,
2405 JNI::CallNonvirtualBooleanMethod,
2406 JNI::CallNonvirtualBooleanMethodV,
2407 JNI::CallNonvirtualBooleanMethodA,
2408 JNI::CallNonvirtualByteMethod,
2409 JNI::CallNonvirtualByteMethodV,
2410 JNI::CallNonvirtualByteMethodA,
2411 JNI::CallNonvirtualCharMethod,
2412 JNI::CallNonvirtualCharMethodV,
2413 JNI::CallNonvirtualCharMethodA,
2414 JNI::CallNonvirtualShortMethod,
2415 JNI::CallNonvirtualShortMethodV,
2416 JNI::CallNonvirtualShortMethodA,
2417 JNI::CallNonvirtualIntMethod,
2418 JNI::CallNonvirtualIntMethodV,
2419 JNI::CallNonvirtualIntMethodA,
2420 JNI::CallNonvirtualLongMethod,
2421 JNI::CallNonvirtualLongMethodV,
2422 JNI::CallNonvirtualLongMethodA,
2423 JNI::CallNonvirtualFloatMethod,
2424 JNI::CallNonvirtualFloatMethodV,
2425 JNI::CallNonvirtualFloatMethodA,
2426 JNI::CallNonvirtualDoubleMethod,
2427 JNI::CallNonvirtualDoubleMethodV,
2428 JNI::CallNonvirtualDoubleMethodA,
2429 JNI::CallNonvirtualVoidMethod,
2430 JNI::CallNonvirtualVoidMethodV,
2431 JNI::CallNonvirtualVoidMethodA,
2432 JNI::GetFieldID,
2433 JNI::GetObjectField,
2434 JNI::GetBooleanField,
2435 JNI::GetByteField,
2436 JNI::GetCharField,
2437 JNI::GetShortField,
2438 JNI::GetIntField,
2439 JNI::GetLongField,
2440 JNI::GetFloatField,
2441 JNI::GetDoubleField,
2442 JNI::SetObjectField,
2443 JNI::SetBooleanField,
2444 JNI::SetByteField,
2445 JNI::SetCharField,
2446 JNI::SetShortField,
2447 JNI::SetIntField,
2448 JNI::SetLongField,
2449 JNI::SetFloatField,
2450 JNI::SetDoubleField,
2451 JNI::GetStaticMethodID,
2452 JNI::CallStaticObjectMethod,
2453 JNI::CallStaticObjectMethodV,
2454 JNI::CallStaticObjectMethodA,
2455 JNI::CallStaticBooleanMethod,
2456 JNI::CallStaticBooleanMethodV,
2457 JNI::CallStaticBooleanMethodA,
2458 JNI::CallStaticByteMethod,
2459 JNI::CallStaticByteMethodV,
2460 JNI::CallStaticByteMethodA,
2461 JNI::CallStaticCharMethod,
2462 JNI::CallStaticCharMethodV,
2463 JNI::CallStaticCharMethodA,
2464 JNI::CallStaticShortMethod,
2465 JNI::CallStaticShortMethodV,
2466 JNI::CallStaticShortMethodA,
2467 JNI::CallStaticIntMethod,
2468 JNI::CallStaticIntMethodV,
2469 JNI::CallStaticIntMethodA,
2470 JNI::CallStaticLongMethod,
2471 JNI::CallStaticLongMethodV,
2472 JNI::CallStaticLongMethodA,
2473 JNI::CallStaticFloatMethod,
2474 JNI::CallStaticFloatMethodV,
2475 JNI::CallStaticFloatMethodA,
2476 JNI::CallStaticDoubleMethod,
2477 JNI::CallStaticDoubleMethodV,
2478 JNI::CallStaticDoubleMethodA,
2479 JNI::CallStaticVoidMethod,
2480 JNI::CallStaticVoidMethodV,
2481 JNI::CallStaticVoidMethodA,
2482 JNI::GetStaticFieldID,
2483 JNI::GetStaticObjectField,
2484 JNI::GetStaticBooleanField,
2485 JNI::GetStaticByteField,
2486 JNI::GetStaticCharField,
2487 JNI::GetStaticShortField,
2488 JNI::GetStaticIntField,
2489 JNI::GetStaticLongField,
2490 JNI::GetStaticFloatField,
2491 JNI::GetStaticDoubleField,
2492 JNI::SetStaticObjectField,
2493 JNI::SetStaticBooleanField,
2494 JNI::SetStaticByteField,
2495 JNI::SetStaticCharField,
2496 JNI::SetStaticShortField,
2497 JNI::SetStaticIntField,
2498 JNI::SetStaticLongField,
2499 JNI::SetStaticFloatField,
2500 JNI::SetStaticDoubleField,
2501 JNI::NewString,
2502 JNI::GetStringLength,
2503 JNI::GetStringChars,
2504 JNI::ReleaseStringChars,
2505 JNI::NewStringUTF,
2506 JNI::GetStringUTFLength,
2507 JNI::GetStringUTFChars,
2508 JNI::ReleaseStringUTFChars,
2509 JNI::GetArrayLength,
2510 JNI::NewObjectArray,
2511 JNI::GetObjectArrayElement,
2512 JNI::SetObjectArrayElement,
2513 JNI::NewBooleanArray,
2514 JNI::NewByteArray,
2515 JNI::NewCharArray,
2516 JNI::NewShortArray,
2517 JNI::NewIntArray,
2518 JNI::NewLongArray,
2519 JNI::NewFloatArray,
2520 JNI::NewDoubleArray,
2521 JNI::GetBooleanArrayElements,
2522 JNI::GetByteArrayElements,
2523 JNI::GetCharArrayElements,
2524 JNI::GetShortArrayElements,
2525 JNI::GetIntArrayElements,
2526 JNI::GetLongArrayElements,
2527 JNI::GetFloatArrayElements,
2528 JNI::GetDoubleArrayElements,
2529 JNI::ReleaseBooleanArrayElements,
2530 JNI::ReleaseByteArrayElements,
2531 JNI::ReleaseCharArrayElements,
2532 JNI::ReleaseShortArrayElements,
2533 JNI::ReleaseIntArrayElements,
2534 JNI::ReleaseLongArrayElements,
2535 JNI::ReleaseFloatArrayElements,
2536 JNI::ReleaseDoubleArrayElements,
2537 JNI::GetBooleanArrayRegion,
2538 JNI::GetByteArrayRegion,
2539 JNI::GetCharArrayRegion,
2540 JNI::GetShortArrayRegion,
2541 JNI::GetIntArrayRegion,
2542 JNI::GetLongArrayRegion,
2543 JNI::GetFloatArrayRegion,
2544 JNI::GetDoubleArrayRegion,
2545 JNI::SetBooleanArrayRegion,
2546 JNI::SetByteArrayRegion,
2547 JNI::SetCharArrayRegion,
2548 JNI::SetShortArrayRegion,
2549 JNI::SetIntArrayRegion,
2550 JNI::SetLongArrayRegion,
2551 JNI::SetFloatArrayRegion,
2552 JNI::SetDoubleArrayRegion,
2553 JNI::RegisterNatives,
2554 JNI::UnregisterNatives,
2555 JNI::MonitorEnter,
2556 JNI::MonitorExit,
2557 JNI::GetJavaVM,
2558 JNI::GetStringRegion,
2559 JNI::GetStringUTFRegion,
2560 JNI::GetPrimitiveArrayCritical,
2561 JNI::ReleasePrimitiveArrayCritical,
2562 JNI::GetStringCritical,
2563 JNI::ReleaseStringCritical,
2564 JNI::NewWeakGlobalRef,
2565 JNI::DeleteWeakGlobalRef,
2566 JNI::ExceptionCheck,
2567 JNI::NewDirectByteBuffer,
2568 JNI::GetDirectBufferAddress,
2569 JNI::GetDirectBufferCapacity,
2570 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002571};
2572
Elliott Hughes75770752011-08-24 17:52:38 -07002573JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002574 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002575 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002576 local_ref_cookie(IRT_FIRST_SEGMENT),
2577 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002578 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002579 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002580 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002581 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002582 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002583 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002584 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002585 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2586 // errors will ensue.
2587 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2588 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002589}
2590
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002591JNIEnvExt::~JNIEnvExt() {
2592}
2593
Elliott Hughes88c5c352012-03-15 18:49:48 -07002594void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2595 check_jni = enabled;
2596 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002597}
2598
Elliott Hughes73e66f72012-05-09 09:34:45 -07002599void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2600 locals.Dump(os);
2601 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002602}
2603
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002604void JNIEnvExt::PushFrame(int /*capacity*/) {
2605 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002606 stacked_local_ref_cookies.push_back(local_ref_cookie);
2607 local_ref_cookie = locals.GetSegmentState();
2608}
2609
2610void JNIEnvExt::PopFrame() {
2611 locals.SetSegmentState(local_ref_cookie);
2612 local_ref_cookie = stacked_local_ref_cookies.back();
2613 stacked_local_ref_cookies.pop_back();
2614}
2615
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002616Offset JNIEnvExt::SegmentStateOffset() {
2617 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2618 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2619}
2620
Carl Shapiroea4dca82011-08-01 13:45:38 -07002621// JNI Invocation interface.
2622
Brian Carlstrombddf9762013-05-14 11:35:37 -07002623extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002624 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002625 if (IsBadJniVersion(args->version)) {
2626 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002627 return JNI_EVERSION;
2628 }
2629 Runtime::Options options;
2630 for (int i = 0; i < args->nOptions; ++i) {
2631 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002632 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002633 }
2634 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002635 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002636 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002637 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002638 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002639 bool started = runtime->Start();
2640 if (!started) {
2641 delete Thread::Current()->GetJniEnv();
2642 delete runtime->GetJavaVM();
2643 LOG(WARNING) << "CreateJavaVM failed";
2644 return JNI_ERR;
2645 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002646 *p_env = Thread::Current()->GetJniEnv();
2647 *p_vm = runtime->GetJavaVM();
2648 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002649}
2650
Elliott Hughesf2682d52011-08-15 16:37:04 -07002651extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002652 Runtime* runtime = Runtime::Current();
2653 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002654 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002655 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002656 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002657 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002658 }
2659 return JNI_OK;
2660}
2661
2662// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002663extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002664 return JNI_ERR;
2665}
2666
Elliott Hughescdf53122011-08-19 15:46:09 -07002667class JII {
2668 public:
2669 static jint DestroyJavaVM(JavaVM* vm) {
2670 if (vm == NULL) {
2671 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002672 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002673 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2674 delete raw_vm->runtime;
2675 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002676 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002677
Elliott Hughescdf53122011-08-19 15:46:09 -07002678 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002679 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002680 }
2681
2682 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002683 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002684 }
2685
2686 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002687 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002688 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002689 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002690 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2691 Runtime* runtime = raw_vm->runtime;
2692 runtime->DetachCurrentThread();
2693 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002694 }
2695
2696 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes83a25322013-03-14 11:18:53 -07002697 if (IsBadJniVersion(version)) {
2698 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07002699 return JNI_EVERSION;
2700 }
2701 if (vm == NULL || env == NULL) {
2702 return JNI_ERR;
2703 }
2704 Thread* thread = Thread::Current();
2705 if (thread == NULL) {
2706 *env = NULL;
2707 return JNI_EDETACHED;
2708 }
2709 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002710 return JNI_OK;
2711 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002712};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002713
Elliott Hughes88c5c352012-03-15 18:49:48 -07002714const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002715 NULL, // reserved0
2716 NULL, // reserved1
2717 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002718 JII::DestroyJavaVM,
2719 JII::AttachCurrentThread,
2720 JII::DetachCurrentThread,
2721 JII::GetEnv,
2722 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002723};
2724
Elliott Hughesa0957642011-09-02 14:27:33 -07002725JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002726 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002727 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002728 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002729 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002730 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002731 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002732 work_around_app_jni_bugs(false),
Ian Rogers62d6c772013-02-27 08:32:07 -08002733 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002734 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002735 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002736 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002737 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002738 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002739 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Elliott Hughes79082e32011-08-25 12:07:32 -07002740 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002741 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002742 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002743 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002744 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002745}
2746
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002747JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002748 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002749}
2750
Elliott Hughes88c5c352012-03-15 18:49:48 -07002751void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2752 check_jni = enabled;
2753 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002754}
2755
Elliott Hughesae80b492012-04-24 10:43:17 -07002756void JavaVMExt::DumpForSigQuit(std::ostream& os) {
2757 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
2758 if (force_copy) {
2759 os << " (with forcecopy)";
2760 }
2761 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07002762 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07002763 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002764 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002765 os << "; pins=" << pin_table.Size();
2766 }
2767 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002768 MutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002769 os << "; globals=" << globals.Capacity();
2770 }
2771 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002772 MutexLock mu(self, weak_globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002773 if (weak_globals.Capacity() > 0) {
2774 os << " (plus " << weak_globals.Capacity() << " weak)";
2775 }
2776 }
2777 os << '\n';
2778
2779 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002780 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002781 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
2782 }
2783}
2784
Elliott Hughes73e66f72012-05-09 09:34:45 -07002785void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002786 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002787 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002788 MutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002789 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002790 }
2791 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002792 MutexLock mu(self, weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002793 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002794 }
2795 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002796 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002797 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002798 }
2799}
2800
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002801bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
2802 std::string& detail) {
Elliott Hughes75770752011-08-24 17:52:38 -07002803 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002804
2805 // See if we've already loaded this library. If we have, and the class loader
2806 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002807 // TODO: for better results we should canonicalize the pathname (or even compare
2808 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002809 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07002810 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07002811 {
2812 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07002813 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002814 library = libraries->Get(path);
2815 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002816 if (library != NULL) {
2817 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002818 // The library will be associated with class_loader. The JNI
2819 // spec says we can't load the same library into more than one
2820 // class loader.
2821 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2822 "ClassLoader %p; can't open in ClassLoader %p",
2823 path.c_str(), library->GetClassLoader(), class_loader);
2824 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002825 return false;
2826 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002827 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2828 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002829 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002830 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2831 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002832 return false;
2833 }
2834 return true;
2835 }
2836
2837 // Open the shared library. Because we're using a full path, the system
2838 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2839 // resolve this library's dependencies though.)
2840
2841 // Failures here are expected when java.library.path has several entries
2842 // and we have to hunt for the lib.
2843
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002844 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
2845 // class unloading. Libraries will only be unloaded when the reference count (incremented by
2846 // dlopen) becomes zero from dlclose.
2847
Elliott Hughescdf53122011-08-19 15:46:09 -07002848 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002849 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002850 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
2851 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
2852 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07002853
Elliott Hughes84b2f142012-09-27 09:16:28 -07002854 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002855
2856 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002857 detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07002858 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002859 return false;
2860 }
2861
2862 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002863 // TODO: move the locking (and more of this logic) into Libraries.
2864 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002865 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002866 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002867 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002868 if (library == NULL) { // We won race to get libraries_lock
2869 library = new SharedLibrary(path, handle, class_loader);
2870 libraries->Put(path, library);
2871 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002872 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002873 }
2874 if (!created_library) {
2875 LOG(INFO) << "WOW: we lost a race to add shared library: "
2876 << "\"" << path << "\" ClassLoader=" << class_loader;
2877 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002878 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002879
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002880 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002881
Elliott Hughes79353722013-08-02 16:52:18 -07002882 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07002883 void* sym = dlsym(handle, "JNI_OnLoad");
2884 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002885 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07002886 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07002887 } else {
2888 // Call JNI_OnLoad. We have to override the current class
2889 // loader, which will always be "null" since the stuff at the
2890 // top of the stack is around Runtime.loadLibrary(). (See
2891 // the comments in the JNI FindClass function.)
2892 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2893 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07002894 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002895 self->SetClassLoaderOverride(class_loader);
2896
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002897 int version = 0;
2898 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002899 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002900 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002901 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002902 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002903
Brian Carlstromaded5f72011-10-07 17:15:04 -07002904 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002905
Elliott Hughes79353722013-08-02 16:52:18 -07002906 if (version == JNI_ERR) {
2907 StringAppendF(&detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
2908 } else if (IsBadJniVersion(version)) {
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07002909 StringAppendF(&detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
2910 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07002911 // It's unwise to call dlclose() here, but we can mark it
2912 // as bad and ensure that future load attempts will fail.
2913 // We don't know how far JNI_OnLoad got, so there could
2914 // be some partially-initialized stuff accessible through
2915 // newly-registered native method calls. We could try to
2916 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07002917 } else {
2918 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07002919 }
Elliott Hughes79353722013-08-02 16:52:18 -07002920 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07002921 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002922 }
2923
Elliott Hughes79353722013-08-02 16:52:18 -07002924 library->SetResult(was_successful);
2925 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07002926}
2927
Brian Carlstromea46f952013-07-30 01:26:50 -07002928void* JavaVMExt::FindCodeForNativeMethod(ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002929 CHECK(m->IsNative());
2930
2931 Class* c = m->GetDeclaringClass();
2932
2933 // If this is a static method, it could be called before the class
2934 // has been initialized.
2935 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002936 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002937 return NULL;
2938 }
2939 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002940 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002941 }
2942
Brian Carlstrom16192862011-09-12 17:50:06 -07002943 std::string detail;
2944 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07002945 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07002946 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002947 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07002948 native_method = libraries->FindNativeMethod(m, detail);
2949 }
Ian Rogers62d6c772013-02-27 08:32:07 -08002950 // Throwing can cause libraries_lock to be reacquired.
Brian Carlstrom16192862011-09-12 17:50:06 -07002951 if (native_method == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002952 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
2953 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002954 }
2955 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002956}
2957
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002958void JavaVMExt::VisitRoots(RootVisitor* visitor, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002959 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07002960 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002961 MutexLock mu(self, globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002962 globals.VisitRoots(visitor, arg);
2963 }
2964 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002965 MutexLock mu(self, pins_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002966 pin_table.VisitRoots(visitor, arg);
2967 }
2968 // The weak_globals table is visited by the GC itself (because it mutates the table).
2969}
2970
Elliott Hughesc8fece32013-01-02 11:27:23 -08002971void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
2972 size_t method_count) {
2973 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
2974 if (c.get() == NULL) {
2975 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
2976 }
2977 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
2978}
2979
Ian Rogersdf20fe02011-07-20 20:34:16 -07002980} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002981
2982std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2983 switch (rhs) {
2984 case JNIInvalidRefType:
2985 os << "JNIInvalidRefType";
2986 return os;
2987 case JNILocalRefType:
2988 os << "JNILocalRefType";
2989 return os;
2990 case JNIGlobalRefType:
2991 os << "JNIGlobalRefType";
2992 return os;
2993 case JNIWeakGlobalRefType:
2994 os << "JNIWeakGlobalRefType";
2995 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002996 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002997 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002998 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002999 }
3000}