blob: 852dd00a77391b6fcece04f373a1361f06080f3e [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;
541 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
542 if (!first) {
543 os << ' ';
544 }
545 first = false;
546 os << it->first;
547 }
548 }
549
550 size_t size() const {
551 return libraries_.size();
552 }
553
Elliott Hughes79082e32011-08-25 12:07:32 -0700554 SharedLibrary* Get(const std::string& path) {
Ian Rogers712462a2012-04-12 16:32:29 -0700555 It it = libraries_.find(path);
556 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700557 }
558
559 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700560 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700561 }
562
563 // See section 11.3 "Linking Native Methods" of the JNI spec.
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();
Elliott Hughes79082e32011-08-25 12:07:32 -0700569 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
570 SharedLibrary* library = it->second;
571 if (library->GetClassLoader() != declaring_class_loader) {
572 // We only search libraries loaded by the appropriate ClassLoader.
573 continue;
574 }
575 // Try the short name then the long name...
576 void* fn = library->FindSymbol(jni_short_name);
577 if (fn == NULL) {
578 fn = library->FindSymbol(jni_long_name);
579 }
580 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800581 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
582 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700583 return fn;
584 }
585 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700586 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700587 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700588 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700589 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700590 return NULL;
591 }
592
593 private:
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700594 typedef SafeMap<std::string, SharedLibrary*>::const_iterator It; // TODO: C++0x auto
Elliott Hughes79082e32011-08-25 12:07:32 -0700595
Elliott Hughesa0e18062012-04-13 15:59:59 -0700596 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700597};
598
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700599JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
600 jvalue* args) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700601 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700602 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700603 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800604 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700605 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800606 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700607 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
608 return result;
Elliott Hughesd07986f2011-12-06 18:27:45 -0800609}
610
Elliott Hughescdf53122011-08-19 15:46:09 -0700611class JNI {
612 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700613 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700614 return JNI_VERSION_1_6;
615 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700616
Ian Rogers25e8b912012-09-07 11:31:36 -0700617 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700618 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700619 return NULL;
620 }
621
Elliott Hughescdf53122011-08-19 15:46:09 -0700622 static jclass FindClass(JNIEnv* env, const char* name) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700623 Runtime* runtime = Runtime::Current();
624 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700625 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700626 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700627 Class* c = NULL;
628 if (runtime->IsStarted()) {
Ian Rogersef28b142012-11-30 14:22:18 -0800629 ClassLoader* cl = GetClassLoader(soa);
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800630 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700631 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800632 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700633 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700634 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700635 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700636
Elliott Hughescdf53122011-08-19 15:46:09 -0700637 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700638 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700639 jobject art_method = env->GetObjectField(java_method,
640 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod);
641 ArtMethod* method = soa.Decode<ArtMethod*>(art_method);
642 DCHECK(method != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700643 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700644 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700645
Elliott Hughescdf53122011-08-19 15:46:09 -0700646 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700647 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700648 jobject art_field = env->GetObjectField(java_field,
649 WellKnownClasses::java_lang_reflect_Field_artField);
650 ArtField* field = soa.Decode<ArtField*>(art_field);
651 DCHECK(field != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700652 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700653 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700654
Elliott Hughescdf53122011-08-19 15:46:09 -0700655 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700656 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700657 ArtMethod* m = soa.DecodeMethod(mid);
658 jobject art_method = soa.AddLocalReference<jobject>(m);
659 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
660 if (env->ExceptionCheck()) {
661 return NULL;
662 }
663 SetObjectField(env,
664 reflect_method,
665 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod,
666 art_method);
667 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700668 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700669
Elliott Hughescdf53122011-08-19 15:46:09 -0700670 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700671 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700672 ArtField* f = soa.DecodeField(fid);
673 jobject art_field = soa.AddLocalReference<jobject>(f);
674 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
675 if (env->ExceptionCheck()) {
676 return NULL;
677 }
678 SetObjectField(env,
679 reflect_field,
680 WellKnownClasses::java_lang_reflect_Field_artField,
681 art_field);
682 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700683 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700684
Elliott Hughes37f7a402011-08-22 18:56:01 -0700685 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700686 ScopedObjectAccess soa(env);
687 Object* o = soa.Decode<Object*>(java_object);
688 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700689 }
690
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700691 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700692 ScopedObjectAccess soa(env);
693 Class* c = soa.Decode<Class*>(java_class);
694 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700695 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700696
Elliott Hughes37f7a402011-08-22 18:56:01 -0700697 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700698 ScopedObjectAccess soa(env);
699 Class* c1 = soa.Decode<Class*>(java_class1);
700 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700701 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700702 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700703
Elliott Hughese84278b2012-03-22 10:06:53 -0700704 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Elliott Hughes96a98872012-12-19 14:21:15 -0800705 if (java_class == NULL) {
706 JniAbortF("IsInstanceOf", "null class (second argument)");
707 }
Elliott Hughes37f7a402011-08-22 18:56:01 -0700708 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700709 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700710 return JNI_TRUE;
711 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700712 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700713 Object* obj = soa.Decode<Object*>(jobj);
714 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700715 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700716 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700717 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700718
Elliott Hughes37f7a402011-08-22 18:56:01 -0700719 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700720 ScopedObjectAccess soa(env);
721 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700722 if (exception == NULL) {
723 return JNI_ERR;
724 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800725 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
726 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700727 return JNI_OK;
728 }
729
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700730 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700731 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700732 }
733
734 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700735 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700736 }
737
738 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700739 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700740 }
741
742 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700743 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700744
Brian Carlstromea46f952013-07-30 01:26:50 -0700745 SirtRef<Object> old_throw_this_object(soa.Self(), NULL);
746 SirtRef<ArtMethod> old_throw_method(soa.Self(), NULL);
747 SirtRef<Throwable> old_exception(soa.Self(), NULL);
Ian Rogers62d6c772013-02-27 08:32:07 -0800748 uint32_t old_throw_dex_pc;
749 {
750 ThrowLocation old_throw_location;
Brian Carlstromea46f952013-07-30 01:26:50 -0700751 Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Ian Rogers62d6c772013-02-27 08:32:07 -0800752 old_throw_this_object.reset(old_throw_location.GetThis());
753 old_throw_method.reset(old_throw_location.GetMethod());
754 old_exception.reset(old_exception_obj);
755 old_throw_dex_pc = old_throw_location.GetDexPc();
756 soa.Self()->ClearException();
757 }
758 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(old_exception.get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700759 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
760 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
761 if (mid == NULL) {
762 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Ian Rogers62d6c772013-02-27 08:32:07 -0800763 << PrettyTypeOf(old_exception.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700764 } else {
765 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800766 if (soa.Self()->IsExceptionPending()) {
767 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(NULL))
Elliott Hughes72025e52011-08-23 17:50:30 -0700768 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800769 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700770 }
771 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800772 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
773 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700774
Ian Rogers62d6c772013-02-27 08:32:07 -0800775 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700776 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700777
Elliott Hughescdf53122011-08-19 15:46:09 -0700778 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700779 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800780 Object* exception = soa.Self()->GetException(NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700781 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700782 }
783
Ian Rogers25e8b912012-09-07 11:31:36 -0700784 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700785 LOG(FATAL) << "JNI FatalError called: " << msg;
786 }
787
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700788 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800789 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700790 return JNI_ERR;
791 }
Ian Rogersef28b142012-11-30 14:22:18 -0800792 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700793 return JNI_OK;
794 }
795
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700796 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700797 ScopedObjectAccess soa(env);
798 Object* survivor = soa.Decode<Object*>(java_survivor);
799 soa.Env()->PopFrame();
800 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700801 }
802
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700803 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800804 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700805 }
806
Elliott Hughescdf53122011-08-19 15:46:09 -0700807 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700808 if (obj == NULL) {
809 return NULL;
810 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700811 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700812 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700813 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700814 Object* decoded_obj = soa.Decode<Object*>(obj);
Ian Rogers50b35e22012-10-04 10:09:15 -0700815 MutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700816 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700817 return reinterpret_cast<jobject>(ref);
818 }
819
820 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700821 if (obj == NULL) {
822 return;
823 }
Ian Rogersef28b142012-11-30 14:22:18 -0800824 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700825 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800826 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
827 MutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700828
829 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
830 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
831 << "failed to find entry";
832 }
833 }
834
835 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700836 ScopedObjectAccess soa(env);
837 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700838 }
839
840 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700841 if (obj == NULL) {
842 return;
843 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700844 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700845 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700846 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -0700847 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700848
849 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
850 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
851 << "failed to find entry";
852 }
853 }
854
855 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700856 if (obj == NULL) {
857 return NULL;
858 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700859 ScopedObjectAccess soa(env);
Elliott Hughes9dcd45c2013-07-29 14:40:52 -0700860 return soa.AddLocalReference<jobject>(soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700861 }
862
863 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700864 if (obj == NULL) {
865 return;
866 }
Ian Rogersef28b142012-11-30 14:22:18 -0800867 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700868
Ian Rogersef28b142012-11-30 14:22:18 -0800869 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700870 if (!locals.Remove(cookie, obj)) {
871 // Attempting to delete a local reference that is not in the
872 // topmost local reference frame is a no-op. DeleteLocalRef returns
873 // void and doesn't throw any exceptions, but we should probably
874 // complain about it so the user will notice that things aren't
875 // going quite the way they expect.
876 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
877 << "failed to find entry";
878 }
879 }
880
881 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700882 if (obj1 == obj2) {
883 return JNI_TRUE;
884 } else {
885 ScopedObjectAccess soa(env);
886 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
887 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700888 }
889
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700890 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700891 ScopedObjectAccess soa(env);
892 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700893 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700894 return NULL;
895 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700896 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700897 }
898
Elliott Hughese84278b2012-03-22 10:06:53 -0700899 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700900 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700901 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -0700902 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700903 va_end(args);
904 return result;
905 }
906
Elliott Hughes72025e52011-08-23 17:50:30 -0700907 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700908 ScopedObjectAccess soa(env);
909 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700910 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700911 return NULL;
912 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700913 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700914 if (result == NULL) {
915 return NULL;
916 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700917 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700918 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700919 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700920 return local_result;
921 } else {
922 return NULL;
923 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700924 }
925
Elliott Hughes72025e52011-08-23 17:50:30 -0700926 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700927 ScopedObjectAccess soa(env);
928 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700929 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700930 return NULL;
931 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700932 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700933 if (result == NULL) {
934 return NULL;
935 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700936 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700937 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700938 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700939 return local_result;
940 } else {
941 return NULL;
942 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700943 }
944
Elliott Hughescdf53122011-08-19 15:46:09 -0700945 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700946 ScopedObjectAccess soa(env);
947 return FindMethodID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700948 }
949
950 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700951 ScopedObjectAccess soa(env);
952 return FindMethodID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700953 }
954
Elliott Hughes72025e52011-08-23 17:50:30 -0700955 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700956 va_list ap;
957 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700958 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700959 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700960 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700961 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700962 }
963
Elliott Hughes72025e52011-08-23 17:50:30 -0700964 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700965 ScopedObjectAccess soa(env);
966 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
967 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700968 }
969
Elliott Hughes72025e52011-08-23 17:50:30 -0700970 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700971 ScopedObjectAccess soa(env);
972 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
973 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700974 }
975
Elliott Hughes72025e52011-08-23 17:50:30 -0700976 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700977 va_list ap;
978 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700979 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700980 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700981 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700982 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700983 }
984
Elliott Hughes72025e52011-08-23 17:50:30 -0700985 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700986 ScopedObjectAccess soa(env);
987 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700988 }
989
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700991 ScopedObjectAccess soa(env);
992 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700993 }
994
Elliott Hughes72025e52011-08-23 17:50:30 -0700995 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700996 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700997 va_list ap;
998 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700999 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001000 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001001 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 }
1003
Elliott Hughes72025e52011-08-23 17:50:30 -07001004 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001005 ScopedObjectAccess soa(env);
1006 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 }
1008
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001010 ScopedObjectAccess soa(env);
1011 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001012 }
1013
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 va_list ap;
1016 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001017 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001018 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001020 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 }
1022
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001024 ScopedObjectAccess soa(env);
1025 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 }
1027
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001029 ScopedObjectAccess soa(env);
1030 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001031 }
1032
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001034 va_list ap;
1035 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001036 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001037 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001039 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 }
1041
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001043 ScopedObjectAccess soa(env);
1044 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 }
1046
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001048 ScopedObjectAccess soa(env);
1049 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001050 }
1051
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001053 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001054 va_list ap;
1055 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001056 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001058 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 }
1060
Elliott Hughes72025e52011-08-23 17:50:30 -07001061 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001062 ScopedObjectAccess soa(env);
1063 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 }
1065
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001067 ScopedObjectAccess soa(env);
1068 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001069 }
1070
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001072 va_list ap;
1073 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001074 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001075 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001077 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 }
1079
Elliott Hughes72025e52011-08-23 17:50:30 -07001080 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001081 ScopedObjectAccess soa(env);
1082 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 }
1084
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001086 ScopedObjectAccess soa(env);
1087 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 }
1089
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 va_list ap;
1092 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001093 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001094 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001096 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 }
1098
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001100 ScopedObjectAccess soa(env);
1101 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 }
1103
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001105 ScopedObjectAccess soa(env);
1106 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 }
1108
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 va_list ap;
1111 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001112 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001113 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001115 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 }
1117
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001119 ScopedObjectAccess soa(env);
1120 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 }
1122
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001124 ScopedObjectAccess soa(env);
1125 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 }
1127
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 va_list ap;
1130 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001131 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001132 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001134 }
1135
Elliott Hughes72025e52011-08-23 17:50:30 -07001136 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001137 ScopedObjectAccess soa(env);
1138 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 }
1140
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001142 ScopedObjectAccess soa(env);
1143 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 }
1145
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001146 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001149 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001150 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1151 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 va_end(ap);
1153 return local_result;
1154 }
1155
1156 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001157 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001158 ScopedObjectAccess soa(env);
1159 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1160 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001161 }
1162
1163 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001164 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001165 ScopedObjectAccess soa(env);
1166 JValue result(InvokeWithJValues(soa, obj, mid, args));
1167 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001168 }
1169
1170 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001171 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001174 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001175 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001176 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001177 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 }
1179
1180 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001181 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001182 ScopedObjectAccess soa(env);
1183 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001184 }
1185
1186 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001187 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001188 ScopedObjectAccess soa(env);
1189 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 }
1191
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001192 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001193 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001194 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001195 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001196 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001198 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 }
1200
1201 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001202 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001203 ScopedObjectAccess soa(env);
1204 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 }
1206
1207 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001208 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001209 ScopedObjectAccess soa(env);
1210 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 }
1212
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001213 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001214 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001215 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001216 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001217 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001218 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001219 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 }
1221
1222 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001223 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001224 ScopedObjectAccess soa(env);
1225 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001226 }
1227
1228 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001229 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001230 ScopedObjectAccess soa(env);
1231 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 }
1233
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001234 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001235 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001236 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001237 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001238 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001240 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001241 }
1242
1243 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001244 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001245 ScopedObjectAccess soa(env);
1246 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 }
1248
1249 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001250 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001251 ScopedObjectAccess soa(env);
1252 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 }
1254
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001255 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001257 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001258 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001259 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001260 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001261 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001262 }
1263
1264 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001265 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001266 ScopedObjectAccess soa(env);
1267 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001268 }
1269
1270 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001271 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001272 ScopedObjectAccess soa(env);
1273 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 }
1275
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001276 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001277 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001278 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001279 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001280 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001281 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001282 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001283 }
1284
1285 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001286 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001287 ScopedObjectAccess soa(env);
1288 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001289 }
1290
1291 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001292 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001293 ScopedObjectAccess soa(env);
1294 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 }
1296
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001297 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001299 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001300 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001301 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001302 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001303 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 }
1305
1306 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001307 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001308 ScopedObjectAccess soa(env);
1309 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001310 }
1311
1312 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001313 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001314 ScopedObjectAccess soa(env);
1315 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 }
1317
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001318 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001320 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001321 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001322 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001323 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001324 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 }
1326
1327 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001328 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001329 ScopedObjectAccess soa(env);
1330 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 }
1332
1333 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001334 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001335 ScopedObjectAccess soa(env);
1336 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001337 }
1338
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001339 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001341 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001342 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001343 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001344 va_end(ap);
1345 }
1346
Brian Carlstromea46f952013-07-30 01:26:50 -07001347 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1348 va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001349 ScopedObjectAccess soa(env);
1350 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001351 }
1352
1353 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001354 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001355 ScopedObjectAccess soa(env);
1356 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 }
1358
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001359 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001360 ScopedObjectAccess soa(env);
1361 return FindFieldID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001362 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001363
1364
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001365 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001366 ScopedObjectAccess soa(env);
1367 return FindFieldID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001369
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001370 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001371 ScopedObjectAccess soa(env);
1372 Object* o = soa.Decode<Object*>(obj);
Brian Carlstromea46f952013-07-30 01:26:50 -07001373 ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001374 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001376
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001377 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001378 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -07001379 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001380 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001381 }
1382
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001383 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001384 ScopedObjectAccess soa(env);
1385 Object* o = soa.Decode<Object*>(java_object);
1386 Object* v = soa.Decode<Object*>(java_value);
Brian Carlstromea46f952013-07-30 01:26:50 -07001387 ArtField* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001388 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 }
1390
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001391 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001392 ScopedObjectAccess soa(env);
1393 Object* v = soa.Decode<Object*>(java_value);
Brian Carlstromea46f952013-07-30 01:26:50 -07001394 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001395 f->SetObject(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 }
1397
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001398#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001399 ScopedObjectAccess soa(env); \
1400 Object* o = soa.Decode<Object*>(instance); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001401 ArtField* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001402 return f->fn(o)
1403
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001404#define GET_STATIC_PRIMITIVE_FIELD(fn) \
1405 ScopedObjectAccess soa(env); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001406 ArtField* f = soa.DecodeField(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001407 return f->fn(f->GetDeclaringClass())
1408
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001409#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001410 ScopedObjectAccess soa(env); \
1411 Object* o = soa.Decode<Object*>(instance); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001412 ArtField* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001413 f->fn(o, value)
1414
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001415#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
1416 ScopedObjectAccess soa(env); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001417 ArtField* f = soa.DecodeField(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001418 f->fn(f->GetDeclaringClass(), value)
1419
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001420 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1421 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 }
1423
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001424 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1425 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 }
1427
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001428 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1429 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 }
1431
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001432 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1433 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 }
1435
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001436 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1437 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 }
1439
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001440 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1441 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001442 }
1443
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001444 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1445 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001446 }
1447
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001448 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1449 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001450 }
1451
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001452 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001453 GET_STATIC_PRIMITIVE_FIELD(GetBoolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 }
1455
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001456 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001457 GET_STATIC_PRIMITIVE_FIELD(GetByte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 }
1459
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001460 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001461 GET_STATIC_PRIMITIVE_FIELD(GetChar);
Elliott Hughescdf53122011-08-19 15:46:09 -07001462 }
1463
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001464 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001465 GET_STATIC_PRIMITIVE_FIELD(GetShort);
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 }
1467
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001468 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001469 GET_STATIC_PRIMITIVE_FIELD(GetInt);
Elliott Hughescdf53122011-08-19 15:46:09 -07001470 }
1471
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001472 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001473 GET_STATIC_PRIMITIVE_FIELD(GetLong);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001474 }
1475
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001476 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001477 GET_STATIC_PRIMITIVE_FIELD(GetFloat);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001478 }
1479
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001480 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001481 GET_STATIC_PRIMITIVE_FIELD(GetDouble);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001482 }
1483
1484 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1485 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1486 }
1487
1488 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1489 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1490 }
1491
1492 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1493 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1494 }
1495
1496 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1497 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1498 }
1499
1500 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1501 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1502 }
1503
1504 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1505 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1506 }
1507
1508 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1509 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1510 }
1511
1512 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1513 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1514 }
1515
1516 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001517 SET_STATIC_PRIMITIVE_FIELD(SetBoolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001518 }
1519
1520 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001521 SET_STATIC_PRIMITIVE_FIELD(SetByte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001522 }
1523
1524 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001525 SET_STATIC_PRIMITIVE_FIELD(SetChar, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001526 }
1527
1528 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001529 SET_STATIC_PRIMITIVE_FIELD(SetFloat, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001530 }
1531
1532 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001533 SET_STATIC_PRIMITIVE_FIELD(SetDouble, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001534 }
1535
1536 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001537 SET_STATIC_PRIMITIVE_FIELD(SetInt, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001538 }
1539
1540 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001541 SET_STATIC_PRIMITIVE_FIELD(SetLong, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001542 }
1543
1544 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001545 SET_STATIC_PRIMITIVE_FIELD(SetShort, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001546 }
1547
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001548 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001549 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001550 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001551 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001552 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1553 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001554 va_end(ap);
1555 return local_result;
1556 }
1557
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001558 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001559 ScopedObjectAccess soa(env);
1560 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1561 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001562 }
1563
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001564 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001565 ScopedObjectAccess soa(env);
1566 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1567 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001568 }
1569
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001570 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001571 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001572 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001573 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001574 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001575 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001576 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001577 }
1578
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001579 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001580 ScopedObjectAccess soa(env);
1581 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001582 }
1583
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001584 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001585 ScopedObjectAccess soa(env);
1586 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001587 }
1588
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001589 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001591 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001592 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001593 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001594 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001595 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001596 }
1597
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001598 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001599 ScopedObjectAccess soa(env);
1600 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001601 }
1602
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001603 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001604 ScopedObjectAccess soa(env);
1605 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001606 }
1607
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001608 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001609 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001610 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001611 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001612 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001613 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001614 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001615 }
1616
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001617 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001618 ScopedObjectAccess soa(env);
1619 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 }
1621
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001622 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001623 ScopedObjectAccess soa(env);
1624 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 }
1626
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001627 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001628 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001629 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001630 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001631 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001633 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001634 }
1635
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001636 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001637 ScopedObjectAccess soa(env);
1638 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 }
1640
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001641 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001642 ScopedObjectAccess soa(env);
1643 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001644 }
1645
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001646 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001647 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001648 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001649 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001650 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001652 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001653 }
1654
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001655 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001656 ScopedObjectAccess soa(env);
1657 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 }
1659
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001660 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001661 ScopedObjectAccess soa(env);
1662 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001663 }
1664
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001665 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001666 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001667 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001668 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001669 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001670 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001671 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001672 }
1673
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001674 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001675 ScopedObjectAccess soa(env);
1676 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 }
1678
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001679 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001680 ScopedObjectAccess soa(env);
1681 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001682 }
1683
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001684 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001685 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001686 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001687 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001688 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001690 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 }
1692
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001693 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001694 ScopedObjectAccess soa(env);
1695 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 }
1697
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001698 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001699 ScopedObjectAccess soa(env);
1700 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001701 }
1702
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001703 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001704 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001705 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001706 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001707 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001708 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001709 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001710 }
1711
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001712 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001713 ScopedObjectAccess soa(env);
1714 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001715 }
1716
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001717 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001718 ScopedObjectAccess soa(env);
1719 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001720 }
1721
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001722 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001724 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001725 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001726 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001727 va_end(ap);
1728 }
1729
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001730 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001731 ScopedObjectAccess soa(env);
1732 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001733 }
1734
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001735 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001736 ScopedObjectAccess soa(env);
1737 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 }
1739
Elliott Hughes814e4032011-08-23 12:07:56 -07001740 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001741 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001742 String* result = String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001743 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 }
1745
1746 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001747 if (utf == NULL) {
1748 return NULL;
1749 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001750 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001751 String* result = String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001752 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001753 }
1754
Elliott Hughes814e4032011-08-23 12:07:56 -07001755 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001756 ScopedObjectAccess soa(env);
1757 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001758 }
1759
1760 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001761 ScopedObjectAccess soa(env);
1762 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001763 }
1764
Elliott Hughesb465ab02011-08-24 11:21:21 -07001765 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001766 ScopedObjectAccess soa(env);
1767 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001768 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001769 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001770 } else {
1771 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1772 memcpy(buf, chars + start, length * sizeof(jchar));
1773 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001774 }
1775
Elliott Hughesb465ab02011-08-24 11:21:21 -07001776 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001777 ScopedObjectAccess soa(env);
1778 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001779 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001780 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001781 } else {
1782 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1783 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1784 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001785 }
1786
Elliott Hughes75770752011-08-24 17:52:38 -07001787 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001788 ScopedObjectAccess soa(env);
1789 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001790 const CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001791 PinPrimitiveArray(soa, chars);
Elliott Hughes75770752011-08-24 17:52:38 -07001792 if (is_copy != NULL) {
1793 *is_copy = JNI_FALSE;
1794 }
1795 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001796 }
1797
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001798 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001799 ScopedObjectAccess soa(env);
1800 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001801 }
1802
Elliott Hughes75770752011-08-24 17:52:38 -07001803 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001804 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001805 }
1806
Elliott Hughes75770752011-08-24 17:52:38 -07001807 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001808 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001809 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001810 }
1811
Elliott Hughes75770752011-08-24 17:52:38 -07001812 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001813 if (java_string == NULL) {
1814 return NULL;
1815 }
1816 if (is_copy != NULL) {
1817 *is_copy = JNI_TRUE;
1818 }
Ian Rogersef28b142012-11-30 14:22:18 -08001819 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001820 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001821 size_t byte_count = s->GetUtfLength();
1822 char* bytes = new char[byte_count + 1];
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001823 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001824 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1825 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1826 bytes[byte_count] = '\0';
1827 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001828 }
1829
Elliott Hughes75770752011-08-24 17:52:38 -07001830 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001831 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001832 }
1833
Elliott Hughesbd935992011-08-22 11:59:34 -07001834 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001835 ScopedObjectAccess soa(env);
1836 Object* obj = soa.Decode<Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07001837 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08001838 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1839 }
Elliott Hughesbd935992011-08-22 11:59:34 -07001840 Array* array = obj->AsArray();
1841 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001842 }
1843
Elliott Hughes814e4032011-08-23 12:07:56 -07001844 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001845 ScopedObjectAccess soa(env);
1846 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1847 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001848 }
1849
1850 static void SetObjectArrayElement(JNIEnv* env,
1851 jobjectArray java_array, jsize index, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001852 ScopedObjectAccess soa(env);
1853 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1854 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001855 array->Set(index, value);
1856 }
1857
1858 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001859 ScopedObjectAccess soa(env);
1860 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001861 }
1862
1863 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001864 ScopedObjectAccess soa(env);
1865 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001866 }
1867
1868 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001869 ScopedObjectAccess soa(env);
1870 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001871 }
1872
1873 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001874 ScopedObjectAccess soa(env);
1875 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001876 }
1877
1878 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001879 ScopedObjectAccess soa(env);
1880 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001881 }
1882
1883 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001884 ScopedObjectAccess soa(env);
1885 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001886 }
1887
1888 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001889 ScopedObjectAccess soa(env);
1890 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001891 }
1892
1893 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Elliott Hughes96a98872012-12-19 14:21:15 -08001894 if (length < 0) {
1895 JniAbortF("NewObjectArray", "negative array length: %d", length);
1896 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001897
1898 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07001899 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001900 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001901 std::string descriptor;
1902 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001903 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001904
1905 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001906 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1907 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001908 return NULL;
1909 }
1910
Elliott Hughes75770752011-08-24 17:52:38 -07001911 // Allocate and initialize if necessary.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001912 Class* array_class = soa.Decode<Class*>(java_array_class.get());
Ian Rogers50b35e22012-10-04 10:09:15 -07001913 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001914 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001915 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07001916 for (jsize i = 0; i < length; ++i) {
1917 result->Set(i, initial_object);
1918 }
1919 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001920 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001921 }
1922
1923 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001924 ScopedObjectAccess soa(env);
1925 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001926 }
1927
Ian Rogersa15e67d2012-02-28 13:51:55 -08001928 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001929 ScopedObjectAccess soa(env);
1930 Array* array = soa.Decode<Array*>(java_array);
1931 PinPrimitiveArray(soa, array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001932 if (is_copy != NULL) {
1933 *is_copy = JNI_FALSE;
1934 }
1935 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001936 }
1937
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001938 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001939 ReleasePrimitiveArray(env, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001940 }
1941
Elliott Hughes75770752011-08-24 17:52:38 -07001942 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001943 ScopedObjectAccess soa(env);
1944 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001945 }
1946
Elliott Hughes75770752011-08-24 17:52:38 -07001947 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001948 ScopedObjectAccess soa(env);
1949 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001950 }
1951
Elliott Hughes75770752011-08-24 17:52:38 -07001952 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001953 ScopedObjectAccess soa(env);
1954 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001955 }
1956
Elliott Hughes75770752011-08-24 17:52:38 -07001957 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001958 ScopedObjectAccess soa(env);
1959 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001960 }
1961
Elliott Hughes75770752011-08-24 17:52:38 -07001962 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001963 ScopedObjectAccess soa(env);
1964 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001965 }
1966
Elliott Hughes75770752011-08-24 17:52:38 -07001967 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001968 ScopedObjectAccess soa(env);
1969 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001970 }
1971
Elliott Hughes75770752011-08-24 17:52:38 -07001972 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001973 ScopedObjectAccess soa(env);
1974 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001975 }
1976
Elliott Hughes75770752011-08-24 17:52:38 -07001977 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001978 ScopedObjectAccess soa(env);
1979 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001980 }
1981
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001982 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001983 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 }
1985
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001986 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001987 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001988 }
1989
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001990 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001991 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 }
1993
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001994 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001995 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001996 }
1997
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001998 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001999 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002000 }
2001
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002002 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002003 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002004 }
2005
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002006 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002007 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002008 }
2009
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002010 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002011 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 }
2013
Elliott Hughes814e4032011-08-23 12:07:56 -07002014 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002015 ScopedObjectAccess soa(env);
2016 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 }
2018
Elliott Hughes814e4032011-08-23 12:07:56 -07002019 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002020 ScopedObjectAccess soa(env);
2021 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 }
2023
Elliott Hughes814e4032011-08-23 12:07:56 -07002024 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002025 ScopedObjectAccess soa(env);
2026 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 }
2028
Elliott Hughes814e4032011-08-23 12:07:56 -07002029 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002030 ScopedObjectAccess soa(env);
2031 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 }
2033
Elliott Hughes814e4032011-08-23 12:07:56 -07002034 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002035 ScopedObjectAccess soa(env);
2036 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 }
2038
Elliott Hughes814e4032011-08-23 12:07:56 -07002039 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002040 ScopedObjectAccess soa(env);
2041 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 }
2043
Elliott Hughes814e4032011-08-23 12:07:56 -07002044 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002045 ScopedObjectAccess soa(env);
2046 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 }
2048
Elliott Hughes814e4032011-08-23 12:07:56 -07002049 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002050 ScopedObjectAccess soa(env);
2051 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 }
2053
Elliott Hughes814e4032011-08-23 12:07:56 -07002054 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002055 ScopedObjectAccess soa(env);
2056 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 }
2058
Elliott Hughes814e4032011-08-23 12:07:56 -07002059 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002060 ScopedObjectAccess soa(env);
2061 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 }
2063
Elliott Hughes814e4032011-08-23 12:07:56 -07002064 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002065 ScopedObjectAccess soa(env);
2066 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 }
2068
Elliott Hughes814e4032011-08-23 12:07:56 -07002069 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002070 ScopedObjectAccess soa(env);
2071 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 }
2073
Elliott Hughes814e4032011-08-23 12:07:56 -07002074 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002075 ScopedObjectAccess soa(env);
2076 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 }
2078
Elliott Hughes814e4032011-08-23 12:07:56 -07002079 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002080 ScopedObjectAccess soa(env);
2081 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 }
2083
Elliott Hughes814e4032011-08-23 12:07:56 -07002084 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002085 ScopedObjectAccess soa(env);
2086 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 }
2088
Elliott Hughes814e4032011-08-23 12:07:56 -07002089 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002090 ScopedObjectAccess soa(env);
2091 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 }
2093
Elliott Hughes5174fe62011-08-23 15:12:35 -07002094 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002095 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2096 }
2097
2098 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 -07002099 ScopedObjectAccess soa(env);
2100 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002101
Elliott Hughesc8fece32013-01-02 11:27:23 -08002102 for (size_t i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002103 const char* name = methods[i].name;
2104 const char* sig = methods[i].signature;
2105
2106 if (*sig == '!') {
2107 // TODO: fast jni. it's too noisy to log all these.
2108 ++sig;
2109 }
2110
Brian Carlstromea46f952013-07-30 01:26:50 -07002111 ArtMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002112 if (m == NULL) {
2113 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002115 if (m == NULL) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002116 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
2117 << PrettyDescriptor(c) << "." << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002118 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002119 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002120 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002121 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
2122 << PrettyDescriptor(c) << "." << name << sig
2123 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002124 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 return JNI_ERR;
2126 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002127
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002128 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002129
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002130 m->RegisterNative(soa.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 }
2132 return JNI_OK;
2133 }
2134
Elliott Hughes5174fe62011-08-23 15:12:35 -07002135 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002136 ScopedObjectAccess soa(env);
2137 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002138
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002139 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002140
2141 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07002142 ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002143 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002144 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002145 }
2146 }
2147 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07002148 ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002149 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002150 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002151 }
2152 }
2153
2154 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002155 }
2156
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002157 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2158 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
2159 ScopedObjectAccess soa(env);
2160 Object* o = soa.Decode<Object*>(java_object);
2161 o->MonitorEnter(soa.Self());
2162 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002163 return JNI_ERR;
2164 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002165 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002166 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002167 }
2168
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002169 static jint MonitorExit(JNIEnv* env, jobject java_object)
2170 UNLOCK_FUNCTION(monitor_lock_) {
2171 ScopedObjectAccess soa(env);
2172 Object* o = soa.Decode<Object*>(java_object);
2173 o->MonitorExit(soa.Self());
2174 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002175 return JNI_ERR;
2176 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002177 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002178 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 }
2180
2181 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002182 Runtime* runtime = Runtime::Current();
2183 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002184 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 } else {
2186 *vm = NULL;
2187 }
2188 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2189 }
2190
Elliott Hughescdf53122011-08-19 15:46:09 -07002191 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002192 if (capacity < 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002193 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002194 }
Elliott Hughes11a796e2012-12-19 14:42:57 -08002195 if (address == NULL && capacity != 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002196 JniAbortF("NewDirectByteBuffer", "non-zero capacity for NULL pointer: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002197 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002198
Elliott Hughes96a98872012-12-19 14:21:15 -08002199 // At the moment, the Java side is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002200 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2201 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002202 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002203 jint capacity_arg = static_cast<jint>(capacity);
2204
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002205 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2206 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002207 address_arg, capacity_arg);
Ian Rogersef28b142012-11-30 14:22:18 -08002208 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002209 }
2210
Elliott Hughesb465ab02011-08-24 11:21:21 -07002211 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Jeff Hao534f2b62013-07-10 15:29:36 -07002212 return reinterpret_cast<void*>(env->GetLongField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002213 }
2214
Elliott Hughesb465ab02011-08-24 11:21:21 -07002215 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002216 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002217 }
2218
Elliott Hughesb465ab02011-08-24 11:21:21 -07002219 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002220 if (java_object == NULL) {
2221 JniAbortF("GetObjectRefType", "null object");
2222 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002223
2224 // Do we definitely know what kind of reference this is?
2225 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2226 IndirectRefKind kind = GetIndirectRefKind(ref);
2227 switch (kind) {
2228 case kLocal:
Ian Rogersef28b142012-11-30 14:22:18 -08002229 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002230 return JNILocalRefType;
2231 }
2232 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002233 case kGlobal:
2234 return JNIGlobalRefType;
2235 case kWeakGlobal:
2236 return JNIWeakGlobalRefType;
2237 case kSirtOrInvalid:
2238 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002239 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002240 return JNILocalRefType;
2241 }
2242
Ian Rogersef28b142012-11-30 14:22:18 -08002243 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002244 return JNIInvalidRefType;
2245 }
2246
Elliott Hughesb465ab02011-08-24 11:21:21 -07002247 // If we're handing out direct pointers, check whether it's a direct pointer
2248 // to a local reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002249 {
2250 ScopedObjectAccess soa(env);
2251 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2252 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
2253 return JNILocalRefType;
2254 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002255 }
2256 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002257 return JNIInvalidRefType;
2258 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002259 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2260 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002261 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002262
2263 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002264 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2265 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002266 // TODO: we should try to expand the table if necessary.
2267 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
2268 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2269 return JNI_ERR;
2270 }
2271 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002272 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002273 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2274 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002275 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002276 soa.Self()->ThrowOutOfMemoryError(caller);
2277 }
2278 return okay ? JNI_OK : JNI_ERR;
2279 }
2280
2281 template<typename JniT, typename ArtT>
2282 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002283 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002284 if (length < 0) {
2285 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
2286 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002287 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002288 return soa.AddLocalReference<JniT>(result);
2289 }
2290
2291 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2292 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2293 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002294 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002295 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2296 PinPrimitiveArray(soa, array);
2297 if (is_copy != NULL) {
2298 *is_copy = JNI_FALSE;
2299 }
2300 return array->GetData();
2301 }
2302
2303 template <typename ArrayT>
Ian Rogersef28b142012-11-30 14:22:18 -08002304 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002305 if (mode != JNI_COMMIT) {
Ian Rogersef28b142012-11-30 14:22:18 -08002306 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002307 Array* array = soa.Decode<Array*>(java_array);
2308 UnpinPrimitiveArray(soa, array);
2309 }
2310 }
2311
2312 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2313 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2314 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002315 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002316 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2317 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2318 ThrowAIOOBE(soa, array, start, length, "src");
2319 } else {
2320 JavaT* data = array->GetData();
2321 memcpy(buf, data + start, length * sizeof(JavaT));
2322 }
2323 }
2324
2325 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2326 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2327 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002328 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002329 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2330 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2331 ThrowAIOOBE(soa, array, start, length, "dst");
2332 } else {
2333 JavaT* data = array->GetData();
2334 memcpy(data + start, buf, length * sizeof(JavaT));
2335 }
2336 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002337};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002338
Elliott Hughes88c5c352012-03-15 18:49:48 -07002339const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002340 NULL, // reserved0.
2341 NULL, // reserved1.
2342 NULL, // reserved2.
2343 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002344 JNI::GetVersion,
2345 JNI::DefineClass,
2346 JNI::FindClass,
2347 JNI::FromReflectedMethod,
2348 JNI::FromReflectedField,
2349 JNI::ToReflectedMethod,
2350 JNI::GetSuperclass,
2351 JNI::IsAssignableFrom,
2352 JNI::ToReflectedField,
2353 JNI::Throw,
2354 JNI::ThrowNew,
2355 JNI::ExceptionOccurred,
2356 JNI::ExceptionDescribe,
2357 JNI::ExceptionClear,
2358 JNI::FatalError,
2359 JNI::PushLocalFrame,
2360 JNI::PopLocalFrame,
2361 JNI::NewGlobalRef,
2362 JNI::DeleteGlobalRef,
2363 JNI::DeleteLocalRef,
2364 JNI::IsSameObject,
2365 JNI::NewLocalRef,
2366 JNI::EnsureLocalCapacity,
2367 JNI::AllocObject,
2368 JNI::NewObject,
2369 JNI::NewObjectV,
2370 JNI::NewObjectA,
2371 JNI::GetObjectClass,
2372 JNI::IsInstanceOf,
2373 JNI::GetMethodID,
2374 JNI::CallObjectMethod,
2375 JNI::CallObjectMethodV,
2376 JNI::CallObjectMethodA,
2377 JNI::CallBooleanMethod,
2378 JNI::CallBooleanMethodV,
2379 JNI::CallBooleanMethodA,
2380 JNI::CallByteMethod,
2381 JNI::CallByteMethodV,
2382 JNI::CallByteMethodA,
2383 JNI::CallCharMethod,
2384 JNI::CallCharMethodV,
2385 JNI::CallCharMethodA,
2386 JNI::CallShortMethod,
2387 JNI::CallShortMethodV,
2388 JNI::CallShortMethodA,
2389 JNI::CallIntMethod,
2390 JNI::CallIntMethodV,
2391 JNI::CallIntMethodA,
2392 JNI::CallLongMethod,
2393 JNI::CallLongMethodV,
2394 JNI::CallLongMethodA,
2395 JNI::CallFloatMethod,
2396 JNI::CallFloatMethodV,
2397 JNI::CallFloatMethodA,
2398 JNI::CallDoubleMethod,
2399 JNI::CallDoubleMethodV,
2400 JNI::CallDoubleMethodA,
2401 JNI::CallVoidMethod,
2402 JNI::CallVoidMethodV,
2403 JNI::CallVoidMethodA,
2404 JNI::CallNonvirtualObjectMethod,
2405 JNI::CallNonvirtualObjectMethodV,
2406 JNI::CallNonvirtualObjectMethodA,
2407 JNI::CallNonvirtualBooleanMethod,
2408 JNI::CallNonvirtualBooleanMethodV,
2409 JNI::CallNonvirtualBooleanMethodA,
2410 JNI::CallNonvirtualByteMethod,
2411 JNI::CallNonvirtualByteMethodV,
2412 JNI::CallNonvirtualByteMethodA,
2413 JNI::CallNonvirtualCharMethod,
2414 JNI::CallNonvirtualCharMethodV,
2415 JNI::CallNonvirtualCharMethodA,
2416 JNI::CallNonvirtualShortMethod,
2417 JNI::CallNonvirtualShortMethodV,
2418 JNI::CallNonvirtualShortMethodA,
2419 JNI::CallNonvirtualIntMethod,
2420 JNI::CallNonvirtualIntMethodV,
2421 JNI::CallNonvirtualIntMethodA,
2422 JNI::CallNonvirtualLongMethod,
2423 JNI::CallNonvirtualLongMethodV,
2424 JNI::CallNonvirtualLongMethodA,
2425 JNI::CallNonvirtualFloatMethod,
2426 JNI::CallNonvirtualFloatMethodV,
2427 JNI::CallNonvirtualFloatMethodA,
2428 JNI::CallNonvirtualDoubleMethod,
2429 JNI::CallNonvirtualDoubleMethodV,
2430 JNI::CallNonvirtualDoubleMethodA,
2431 JNI::CallNonvirtualVoidMethod,
2432 JNI::CallNonvirtualVoidMethodV,
2433 JNI::CallNonvirtualVoidMethodA,
2434 JNI::GetFieldID,
2435 JNI::GetObjectField,
2436 JNI::GetBooleanField,
2437 JNI::GetByteField,
2438 JNI::GetCharField,
2439 JNI::GetShortField,
2440 JNI::GetIntField,
2441 JNI::GetLongField,
2442 JNI::GetFloatField,
2443 JNI::GetDoubleField,
2444 JNI::SetObjectField,
2445 JNI::SetBooleanField,
2446 JNI::SetByteField,
2447 JNI::SetCharField,
2448 JNI::SetShortField,
2449 JNI::SetIntField,
2450 JNI::SetLongField,
2451 JNI::SetFloatField,
2452 JNI::SetDoubleField,
2453 JNI::GetStaticMethodID,
2454 JNI::CallStaticObjectMethod,
2455 JNI::CallStaticObjectMethodV,
2456 JNI::CallStaticObjectMethodA,
2457 JNI::CallStaticBooleanMethod,
2458 JNI::CallStaticBooleanMethodV,
2459 JNI::CallStaticBooleanMethodA,
2460 JNI::CallStaticByteMethod,
2461 JNI::CallStaticByteMethodV,
2462 JNI::CallStaticByteMethodA,
2463 JNI::CallStaticCharMethod,
2464 JNI::CallStaticCharMethodV,
2465 JNI::CallStaticCharMethodA,
2466 JNI::CallStaticShortMethod,
2467 JNI::CallStaticShortMethodV,
2468 JNI::CallStaticShortMethodA,
2469 JNI::CallStaticIntMethod,
2470 JNI::CallStaticIntMethodV,
2471 JNI::CallStaticIntMethodA,
2472 JNI::CallStaticLongMethod,
2473 JNI::CallStaticLongMethodV,
2474 JNI::CallStaticLongMethodA,
2475 JNI::CallStaticFloatMethod,
2476 JNI::CallStaticFloatMethodV,
2477 JNI::CallStaticFloatMethodA,
2478 JNI::CallStaticDoubleMethod,
2479 JNI::CallStaticDoubleMethodV,
2480 JNI::CallStaticDoubleMethodA,
2481 JNI::CallStaticVoidMethod,
2482 JNI::CallStaticVoidMethodV,
2483 JNI::CallStaticVoidMethodA,
2484 JNI::GetStaticFieldID,
2485 JNI::GetStaticObjectField,
2486 JNI::GetStaticBooleanField,
2487 JNI::GetStaticByteField,
2488 JNI::GetStaticCharField,
2489 JNI::GetStaticShortField,
2490 JNI::GetStaticIntField,
2491 JNI::GetStaticLongField,
2492 JNI::GetStaticFloatField,
2493 JNI::GetStaticDoubleField,
2494 JNI::SetStaticObjectField,
2495 JNI::SetStaticBooleanField,
2496 JNI::SetStaticByteField,
2497 JNI::SetStaticCharField,
2498 JNI::SetStaticShortField,
2499 JNI::SetStaticIntField,
2500 JNI::SetStaticLongField,
2501 JNI::SetStaticFloatField,
2502 JNI::SetStaticDoubleField,
2503 JNI::NewString,
2504 JNI::GetStringLength,
2505 JNI::GetStringChars,
2506 JNI::ReleaseStringChars,
2507 JNI::NewStringUTF,
2508 JNI::GetStringUTFLength,
2509 JNI::GetStringUTFChars,
2510 JNI::ReleaseStringUTFChars,
2511 JNI::GetArrayLength,
2512 JNI::NewObjectArray,
2513 JNI::GetObjectArrayElement,
2514 JNI::SetObjectArrayElement,
2515 JNI::NewBooleanArray,
2516 JNI::NewByteArray,
2517 JNI::NewCharArray,
2518 JNI::NewShortArray,
2519 JNI::NewIntArray,
2520 JNI::NewLongArray,
2521 JNI::NewFloatArray,
2522 JNI::NewDoubleArray,
2523 JNI::GetBooleanArrayElements,
2524 JNI::GetByteArrayElements,
2525 JNI::GetCharArrayElements,
2526 JNI::GetShortArrayElements,
2527 JNI::GetIntArrayElements,
2528 JNI::GetLongArrayElements,
2529 JNI::GetFloatArrayElements,
2530 JNI::GetDoubleArrayElements,
2531 JNI::ReleaseBooleanArrayElements,
2532 JNI::ReleaseByteArrayElements,
2533 JNI::ReleaseCharArrayElements,
2534 JNI::ReleaseShortArrayElements,
2535 JNI::ReleaseIntArrayElements,
2536 JNI::ReleaseLongArrayElements,
2537 JNI::ReleaseFloatArrayElements,
2538 JNI::ReleaseDoubleArrayElements,
2539 JNI::GetBooleanArrayRegion,
2540 JNI::GetByteArrayRegion,
2541 JNI::GetCharArrayRegion,
2542 JNI::GetShortArrayRegion,
2543 JNI::GetIntArrayRegion,
2544 JNI::GetLongArrayRegion,
2545 JNI::GetFloatArrayRegion,
2546 JNI::GetDoubleArrayRegion,
2547 JNI::SetBooleanArrayRegion,
2548 JNI::SetByteArrayRegion,
2549 JNI::SetCharArrayRegion,
2550 JNI::SetShortArrayRegion,
2551 JNI::SetIntArrayRegion,
2552 JNI::SetLongArrayRegion,
2553 JNI::SetFloatArrayRegion,
2554 JNI::SetDoubleArrayRegion,
2555 JNI::RegisterNatives,
2556 JNI::UnregisterNatives,
2557 JNI::MonitorEnter,
2558 JNI::MonitorExit,
2559 JNI::GetJavaVM,
2560 JNI::GetStringRegion,
2561 JNI::GetStringUTFRegion,
2562 JNI::GetPrimitiveArrayCritical,
2563 JNI::ReleasePrimitiveArrayCritical,
2564 JNI::GetStringCritical,
2565 JNI::ReleaseStringCritical,
2566 JNI::NewWeakGlobalRef,
2567 JNI::DeleteWeakGlobalRef,
2568 JNI::ExceptionCheck,
2569 JNI::NewDirectByteBuffer,
2570 JNI::GetDirectBufferAddress,
2571 JNI::GetDirectBufferCapacity,
2572 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002573};
2574
Elliott Hughes75770752011-08-24 17:52:38 -07002575JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002576 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002577 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002578 local_ref_cookie(IRT_FIRST_SEGMENT),
2579 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002580 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002581 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002582 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002583 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002584 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002585 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002586 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002587 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2588 // errors will ensue.
2589 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2590 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002591}
2592
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002593JNIEnvExt::~JNIEnvExt() {
2594}
2595
Elliott Hughes88c5c352012-03-15 18:49:48 -07002596void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2597 check_jni = enabled;
2598 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002599}
2600
Elliott Hughes73e66f72012-05-09 09:34:45 -07002601void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2602 locals.Dump(os);
2603 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002604}
2605
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002606void JNIEnvExt::PushFrame(int /*capacity*/) {
2607 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002608 stacked_local_ref_cookies.push_back(local_ref_cookie);
2609 local_ref_cookie = locals.GetSegmentState();
2610}
2611
2612void JNIEnvExt::PopFrame() {
2613 locals.SetSegmentState(local_ref_cookie);
2614 local_ref_cookie = stacked_local_ref_cookies.back();
2615 stacked_local_ref_cookies.pop_back();
2616}
2617
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002618Offset JNIEnvExt::SegmentStateOffset() {
2619 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2620 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2621}
2622
Carl Shapiroea4dca82011-08-01 13:45:38 -07002623// JNI Invocation interface.
2624
Brian Carlstrombddf9762013-05-14 11:35:37 -07002625extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002626 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002627 if (IsBadJniVersion(args->version)) {
2628 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002629 return JNI_EVERSION;
2630 }
2631 Runtime::Options options;
2632 for (int i = 0; i < args->nOptions; ++i) {
2633 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002634 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002635 }
2636 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002637 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002638 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002639 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002640 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002641 bool started = runtime->Start();
2642 if (!started) {
2643 delete Thread::Current()->GetJniEnv();
2644 delete runtime->GetJavaVM();
2645 LOG(WARNING) << "CreateJavaVM failed";
2646 return JNI_ERR;
2647 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002648 *p_env = Thread::Current()->GetJniEnv();
2649 *p_vm = runtime->GetJavaVM();
2650 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002651}
2652
Elliott Hughesf2682d52011-08-15 16:37:04 -07002653extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002654 Runtime* runtime = Runtime::Current();
2655 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002656 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002657 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002658 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002659 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002660 }
2661 return JNI_OK;
2662}
2663
2664// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002665extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002666 return JNI_ERR;
2667}
2668
Elliott Hughescdf53122011-08-19 15:46:09 -07002669class JII {
2670 public:
2671 static jint DestroyJavaVM(JavaVM* vm) {
2672 if (vm == NULL) {
2673 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002674 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002675 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2676 delete raw_vm->runtime;
2677 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002678 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002679
Elliott Hughescdf53122011-08-19 15:46:09 -07002680 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002681 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002682 }
2683
2684 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002685 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002686 }
2687
2688 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002689 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002690 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002691 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002692 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2693 Runtime* runtime = raw_vm->runtime;
2694 runtime->DetachCurrentThread();
2695 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002696 }
2697
2698 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes83a25322013-03-14 11:18:53 -07002699 if (IsBadJniVersion(version)) {
2700 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07002701 return JNI_EVERSION;
2702 }
2703 if (vm == NULL || env == NULL) {
2704 return JNI_ERR;
2705 }
2706 Thread* thread = Thread::Current();
2707 if (thread == NULL) {
2708 *env = NULL;
2709 return JNI_EDETACHED;
2710 }
2711 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002712 return JNI_OK;
2713 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002714};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002715
Elliott Hughes88c5c352012-03-15 18:49:48 -07002716const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002717 NULL, // reserved0
2718 NULL, // reserved1
2719 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002720 JII::DestroyJavaVM,
2721 JII::AttachCurrentThread,
2722 JII::DetachCurrentThread,
2723 JII::GetEnv,
2724 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002725};
2726
Elliott Hughesa0957642011-09-02 14:27:33 -07002727JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002728 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002729 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002730 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002731 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002732 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002733 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002734 work_around_app_jni_bugs(false),
Ian Rogers62d6c772013-02-27 08:32:07 -08002735 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002736 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002737 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002738 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002739 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002740 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002741 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Elliott Hughes79082e32011-08-25 12:07:32 -07002742 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002743 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002744 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002745 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002746 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002747}
2748
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002749JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002750 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002751}
2752
Elliott Hughes88c5c352012-03-15 18:49:48 -07002753void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2754 check_jni = enabled;
2755 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002756}
2757
Elliott Hughesae80b492012-04-24 10:43:17 -07002758void JavaVMExt::DumpForSigQuit(std::ostream& os) {
2759 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
2760 if (force_copy) {
2761 os << " (with forcecopy)";
2762 }
2763 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07002764 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07002765 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002766 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002767 os << "; pins=" << pin_table.Size();
2768 }
2769 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002770 MutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002771 os << "; globals=" << globals.Capacity();
2772 }
2773 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002774 MutexLock mu(self, weak_globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002775 if (weak_globals.Capacity() > 0) {
2776 os << " (plus " << weak_globals.Capacity() << " weak)";
2777 }
2778 }
2779 os << '\n';
2780
2781 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002782 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002783 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
2784 }
2785}
2786
Elliott Hughes73e66f72012-05-09 09:34:45 -07002787void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002788 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002789 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002790 MutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002791 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002792 }
2793 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002794 MutexLock mu(self, weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002795 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002796 }
2797 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002798 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002799 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002800 }
2801}
2802
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002803bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
2804 std::string& detail) {
Elliott Hughes75770752011-08-24 17:52:38 -07002805 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002806
2807 // See if we've already loaded this library. If we have, and the class loader
2808 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002809 // TODO: for better results we should canonicalize the pathname (or even compare
2810 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002811 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07002812 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07002813 {
2814 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07002815 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002816 library = libraries->Get(path);
2817 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002818 if (library != NULL) {
2819 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002820 // The library will be associated with class_loader. The JNI
2821 // spec says we can't load the same library into more than one
2822 // class loader.
2823 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2824 "ClassLoader %p; can't open in ClassLoader %p",
2825 path.c_str(), library->GetClassLoader(), class_loader);
2826 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002827 return false;
2828 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002829 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2830 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002831 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002832 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2833 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002834 return false;
2835 }
2836 return true;
2837 }
2838
2839 // Open the shared library. Because we're using a full path, the system
2840 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2841 // resolve this library's dependencies though.)
2842
2843 // Failures here are expected when java.library.path has several entries
2844 // and we have to hunt for the lib.
2845
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002846 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
2847 // class unloading. Libraries will only be unloaded when the reference count (incremented by
2848 // dlopen) becomes zero from dlclose.
2849
Elliott Hughescdf53122011-08-19 15:46:09 -07002850 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002851 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002852 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
2853 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
2854 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07002855
Elliott Hughes84b2f142012-09-27 09:16:28 -07002856 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002857
2858 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002859 detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07002860 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002861 return false;
2862 }
2863
2864 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002865 // TODO: move the locking (and more of this logic) into Libraries.
2866 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002867 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002868 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002869 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002870 if (library == NULL) { // We won race to get libraries_lock
2871 library = new SharedLibrary(path, handle, class_loader);
2872 libraries->Put(path, library);
2873 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002874 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002875 }
2876 if (!created_library) {
2877 LOG(INFO) << "WOW: we lost a race to add shared library: "
2878 << "\"" << path << "\" ClassLoader=" << class_loader;
2879 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002880 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002881
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002882 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002883
Elliott Hughes79353722013-08-02 16:52:18 -07002884 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07002885 void* sym = dlsym(handle, "JNI_OnLoad");
2886 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002887 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07002888 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07002889 } else {
2890 // Call JNI_OnLoad. We have to override the current class
2891 // loader, which will always be "null" since the stuff at the
2892 // top of the stack is around Runtime.loadLibrary(). (See
2893 // the comments in the JNI FindClass function.)
2894 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2895 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07002896 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002897 self->SetClassLoaderOverride(class_loader);
2898
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002899 int version = 0;
2900 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002901 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002902 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002903 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002904 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002905
Brian Carlstromaded5f72011-10-07 17:15:04 -07002906 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002907
Elliott Hughes79353722013-08-02 16:52:18 -07002908 if (version == JNI_ERR) {
2909 StringAppendF(&detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
2910 } else if (IsBadJniVersion(version)) {
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07002911 StringAppendF(&detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
2912 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07002913 // It's unwise to call dlclose() here, but we can mark it
2914 // as bad and ensure that future load attempts will fail.
2915 // We don't know how far JNI_OnLoad got, so there could
2916 // be some partially-initialized stuff accessible through
2917 // newly-registered native method calls. We could try to
2918 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07002919 } else {
2920 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07002921 }
Elliott Hughes79353722013-08-02 16:52:18 -07002922 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07002923 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002924 }
2925
Elliott Hughes79353722013-08-02 16:52:18 -07002926 library->SetResult(was_successful);
2927 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07002928}
2929
Brian Carlstromea46f952013-07-30 01:26:50 -07002930void* JavaVMExt::FindCodeForNativeMethod(ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002931 CHECK(m->IsNative());
2932
2933 Class* c = m->GetDeclaringClass();
2934
2935 // If this is a static method, it could be called before the class
2936 // has been initialized.
2937 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002938 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002939 return NULL;
2940 }
2941 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002942 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002943 }
2944
Brian Carlstrom16192862011-09-12 17:50:06 -07002945 std::string detail;
2946 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07002947 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07002948 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002949 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07002950 native_method = libraries->FindNativeMethod(m, detail);
2951 }
Ian Rogers62d6c772013-02-27 08:32:07 -08002952 // Throwing can cause libraries_lock to be reacquired.
Brian Carlstrom16192862011-09-12 17:50:06 -07002953 if (native_method == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002954 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
2955 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002956 }
2957 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002958}
2959
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002960void JavaVMExt::VisitRoots(RootVisitor* visitor, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002961 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07002962 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002963 MutexLock mu(self, globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002964 globals.VisitRoots(visitor, arg);
2965 }
2966 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002967 MutexLock mu(self, pins_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002968 pin_table.VisitRoots(visitor, arg);
2969 }
2970 // The weak_globals table is visited by the GC itself (because it mutates the table).
2971}
2972
Elliott Hughesc8fece32013-01-02 11:27:23 -08002973void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
2974 size_t method_count) {
2975 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
2976 if (c.get() == NULL) {
2977 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
2978 }
2979 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
2980}
2981
Ian Rogersdf20fe02011-07-20 20:34:16 -07002982} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002983
2984std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2985 switch (rhs) {
2986 case JNIInvalidRefType:
2987 os << "JNIInvalidRefType";
2988 return os;
2989 case JNILocalRefType:
2990 os << "JNILocalRefType";
2991 return os;
2992 case JNIGlobalRefType:
2993 os << "JNIGlobalRefType";
2994 return os;
2995 case JNIWeakGlobalRefType:
2996 os << "JNIWeakGlobalRefType";
2997 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002998 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002999 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003000 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003001 }
3002}