blob: 6a0990eec6e05e49cf4b3183f434fc01b3670979 [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_) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070089 return soa.Vm()->AddWeakGlobalReference(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -070090}
91
Jeff Hao19c5d372013-03-15 14:33:43 -070092static bool IsBadJniVersion(int version) {
93 // We don't support JNI_VERSION_1_1. These are the only other valid versions.
94 return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
95}
96
Brian Carlstromea46f952013-07-30 01:26:50 -070097static void CheckMethodArguments(ArtMethod* m, uint32_t* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -070098 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesb264f082012-04-06 17:10:10 -070099 MethodHelper mh(m);
Ian Rogers50b35e22012-10-04 10:09:15 -0700100 const DexFile::TypeList* params = mh.GetParameterTypeList();
101 if (params == NULL) {
102 return; // No arguments so nothing to check.
103 }
Jeff Hao5d917302013-02-27 17:57:33 -0800104 uint32_t offset = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -0700105 uint32_t num_params = params->Size();
Elliott Hughesb264f082012-04-06 17:10:10 -0700106 size_t error_count = 0;
Jeff Hao5d917302013-02-27 17:57:33 -0800107 if (!m->IsStatic()) {
108 offset = 1;
109 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700110 for (uint32_t i = 0; i < num_params; i++) {
111 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
112 Class* param_type = mh.GetClassFromTypeIdx(type_idx);
113 if (param_type == NULL) {
114 Thread* self = Thread::Current();
115 CHECK(self->IsExceptionPending());
116 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
117 << mh.GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
Ian Rogers62d6c772013-02-27 08:32:07 -0800118 << self->GetException(NULL)->Dump();
Ian Rogers50b35e22012-10-04 10:09:15 -0700119 self->ClearException();
120 ++error_count;
121 } else if (!param_type->IsPrimitive()) {
122 // TODO: check primitives are in range.
Jeff Hao5d917302013-02-27 17:57:33 -0800123 Object* argument = reinterpret_cast<Object*>(args[i + offset]);
Ian Rogers50b35e22012-10-04 10:09:15 -0700124 if (argument != NULL && !argument->InstanceOf(param_type)) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700125 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
126 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
127 ++error_count;
128 }
Jeff Hao5d917302013-02-27 17:57:33 -0800129 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
130 offset++;
Elliott Hughesb264f082012-04-06 17:10:10 -0700131 }
132 }
133 if (error_count > 0) {
134 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
135 // with an argument.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700136 JniAbortF(NULL, "bad arguments passed to %s (see above for details)", PrettyMethod(m).c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -0700137 }
138}
Elliott Hughesb264f082012-04-06 17:10:10 -0700139
Brian Carlstromea46f952013-07-30 01:26:50 -0700140void InvokeWithArgArray(const ScopedObjectAccess& soa, ArtMethod* method,
Jeff Hao6474d192013-03-26 14:08:09 -0700141 ArgArray* arg_array, JValue* result, char result_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700142 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700143 uint32_t* args = arg_array->GetArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700144 if (UNLIKELY(soa.Env()->check_jni)) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700145 CheckMethodArguments(method, args);
Elliott Hughes4cacde82012-04-11 18:32:27 -0700146 }
Sebastien Hertz7d658cf2013-07-09 10:56:11 +0200147 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, result_type);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700148}
149
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700150static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
151 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700152 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700153 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700154 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700155 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800156 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700157 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800158 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700159 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
160 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700161}
162
Brian Carlstromea46f952013-07-30 01:26:50 -0700163static ArtMethod* FindVirtualMethod(Object* receiver, ArtMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700164 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700165 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700166}
167
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700168static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
169 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700170 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700171 Object* receiver = soa.Decode<Object*>(obj);
Brian Carlstromea46f952013-07-30 01:26:50 -0700172 ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700173 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800174 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700175 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800176 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700177 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
178 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700179}
180
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700181static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
182 jobject obj, jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700183 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700184 Object* receiver = soa.Decode<Object*>(obj);
Brian Carlstromea46f952013-07-30 01:26:50 -0700185 ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700186 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800187 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700188 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800189 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700190 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
191 return result;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700192}
193
Elliott Hughes6b436852011-08-12 10:16:44 -0700194// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
195// separated with slashes but aren't wrapped with "L;" like regular descriptors
196// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
197// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
198// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700199static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700200 std::string result;
201 // Add the missing "L;" if necessary.
202 if (name[0] == '[') {
203 result = name;
204 } else {
205 result += 'L';
206 result += name;
207 result += ';';
208 }
209 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700210 if (result.find('.') != std::string::npos) {
211 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
212 << "\"" << name << "\"";
213 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700214 }
215 return result;
216}
217
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700218static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, Class* c,
219 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700220 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800221 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
222 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
223 "no %s method \"%s.%s%s\"",
224 kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700225}
226
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700227static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
228 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700229 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700230 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700231 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700232 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700233 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700234
Brian Carlstromea46f952013-07-30 01:26:50 -0700235 ArtMethod* method = NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700236 if (is_static) {
237 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700238 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700239 method = c->FindVirtualMethod(name, sig);
240 if (method == NULL) {
241 // No virtual method matching the signature. Search declared
242 // private methods and constructors.
243 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700244 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700245 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700246
Elliott Hughescdf53122011-08-19 15:46:09 -0700247 if (method == NULL || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700248 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700249 return NULL;
250 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700251
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700252 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700253}
254
Ian Rogersef28b142012-11-30 14:22:18 -0800255static ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700256 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700257 ArtMethod* method = soa.Self()->GetCurrentMethod(NULL);
Brian Carlstromce888532013-10-10 00:32:58 -0700258 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
259 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogersef28b142012-11-30 14:22:18 -0800260 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700261 }
Brian Carlstromce888532013-10-10 00:32:58 -0700262 // If we have a method, use its ClassLoader for context.
263 if (method != NULL) {
264 return method->GetDeclaringClass()->GetClassLoader();
265 }
266 // We don't have a method, so try to use the system ClassLoader.
267 ClassLoader* class_loader = soa.Decode<ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
268 if (class_loader != NULL) {
269 return class_loader;
270 }
271 // See if the override ClassLoader is set for gtests.
272 class_loader = soa.Self()->GetClassLoaderOverride();
273 if (class_loader != NULL) {
274 // If so, CommonTest should have set UseCompileTimeClassPath.
275 CHECK(Runtime::Current()->UseCompileTimeClassPath());
276 return class_loader;
277 }
278 // Use the BOOTCLASSPATH.
279 return NULL;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700280}
281
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700282static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
283 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700284 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700285 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700286 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700287 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700288 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700289
Brian Carlstromea46f952013-07-30 01:26:50 -0700290 ArtField* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700291 Class* field_type;
292 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
293 if (sig[1] != '\0') {
Ian Rogersef28b142012-11-30 14:22:18 -0800294 ClassLoader* cl = GetClassLoader(soa);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700295 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700296 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700297 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700298 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700299 if (field_type == NULL) {
300 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700301 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800302 ThrowLocation throw_location;
Brian Carlstromea46f952013-07-30 01:26:50 -0700303 SirtRef<Throwable> cause(soa.Self(), soa.Self()->GetException(&throw_location));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700304 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800305 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
306 "no type \"%s\" found and so no field \"%s\" could be found in class "
307 "\"%s\" or its superclasses", sig, name,
308 ClassHelper(c).GetDescriptor());
309 soa.Self()->GetException(NULL)->SetCause(cause.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700310 return NULL;
311 }
312 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800313 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700314 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800315 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700316 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700317 if (field == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800318 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
319 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
320 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
321 sig, name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700322 return NULL;
323 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700324 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700325}
326
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700327static void PinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700328 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700329 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700330 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700331 vm->pin_table.Add(array);
332}
333
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700334static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700335 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700336 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700337 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700338 vm->pin_table.Remove(array);
339}
340
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700341static void ThrowAIOOBE(ScopedObjectAccess& soa, Array* array, jsize start,
342 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700343 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700344 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800345 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
346 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
347 "%s offset=%d length=%d %s.length=%d",
348 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700349}
Ian Rogers0571d352011-11-03 19:51:38 -0700350
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700351static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
352 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700353 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800354 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
355 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
356 "offset=%d length=%d string.length()=%d", start, length,
357 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700358}
Elliott Hughes814e4032011-08-23 12:07:56 -0700359
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700360int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700361 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700362 // Turn the const char* into a java.lang.String.
363 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
364 if (msg != NULL && s.get() == NULL) {
365 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700366 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700367
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700368 // Choose an appropriate constructor and set up the arguments.
369 jvalue args[2];
370 const char* signature;
371 if (msg == NULL && cause == NULL) {
372 signature = "()V";
373 } else if (msg != NULL && cause == NULL) {
374 signature = "(Ljava/lang/String;)V";
375 args[0].l = s.get();
376 } else if (msg == NULL && cause != NULL) {
377 signature = "(Ljava/lang/Throwable;)V";
378 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700379 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700380 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
381 args[0].l = s.get();
382 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700383 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700384 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
385 if (mid == NULL) {
Ian Rogersef28b142012-11-30 14:22:18 -0800386 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700387 LOG(ERROR) << "No <init>" << signature << " in "
388 << PrettyClass(soa.Decode<Class*>(exception_class));
389 return JNI_ERR;
390 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700391
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700392 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
393 if (exception.get() == NULL) {
394 return JNI_ERR;
395 }
Ian Rogersef28b142012-11-30 14:22:18 -0800396 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800397 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
398 soa.Self()->SetException(throw_location, soa.Decode<Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700399 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700400}
401
Elliott Hughes462c9442012-03-23 18:47:50 -0700402static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700403 if (vm == NULL || p_env == NULL) {
404 return JNI_ERR;
405 }
406
Elliott Hughes462c9442012-03-23 18:47:50 -0700407 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700408 Thread* self = Thread::Current();
409 if (self != NULL) {
410 *p_env = self->GetJniEnv();
411 return JNI_OK;
412 }
413
414 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
415
416 // No threads allowed in zygote mode.
417 if (runtime->IsZygote()) {
418 LOG(ERROR) << "Attempt to attach a thread in the zygote";
419 return JNI_ERR;
420 }
421
Elliott Hughes462c9442012-03-23 18:47:50 -0700422 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
423 const char* thread_name = NULL;
Ian Rogers365c1022012-06-22 15:05:28 -0700424 jobject thread_group = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -0700425 if (args != NULL) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700426 if (IsBadJniVersion(args->version)) {
427 LOG(ERROR) << "Bad JNI version passed to "
428 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
429 << args->version;
430 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700431 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700432 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700433 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700434 }
Elliott Hughes75770752011-08-24 17:52:38 -0700435
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800436 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700437 *p_env = NULL;
438 return JNI_ERR;
439 } else {
440 *p_env = Thread::Current()->GetJniEnv();
441 return JNI_OK;
442 }
Elliott Hughes75770752011-08-24 17:52:38 -0700443}
444
Elliott Hughes79082e32011-08-25 12:07:32 -0700445class SharedLibrary {
446 public:
447 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
448 : path_(path),
449 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700450 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700451 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700452 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700453 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700454 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700455 }
456
Elliott Hughes79082e32011-08-25 12:07:32 -0700457 Object* GetClassLoader() {
458 return class_loader_;
459 }
460
461 std::string GetPath() {
462 return path_;
463 }
464
465 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700466 * Check the result of an earlier call to JNI_OnLoad on this library.
467 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700468 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700469 bool CheckOnLoadResult()
470 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700471 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700472 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700473 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
474 bool okay;
475 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700476 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700477
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700478 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
479 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
480 // caller can continue.
481 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
482 okay = true;
483 } else {
484 while (jni_on_load_result_ == kPending) {
485 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700486 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700487 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700488
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700489 okay = (jni_on_load_result_ == kOkay);
490 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
491 << (okay ? "succeeded" : "failed") << "]";
492 }
493 }
494 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700495 return okay;
496 }
497
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700498 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700499 Thread* self = Thread::Current();
500 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700501
Elliott Hughes79082e32011-08-25 12:07:32 -0700502 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700503 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700504
505 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700506 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700507 }
508
509 void* FindSymbol(const std::string& symbol_name) {
510 return dlsym(handle_, symbol_name.c_str());
511 }
512
513 private:
514 enum JNI_OnLoadState {
515 kPending,
516 kFailed,
517 kOkay,
518 };
519
520 // Path to library "/system/lib/libjni.so".
521 std::string path_;
522
523 // The void* returned by dlopen(3).
524 void* handle_;
525
526 // The ClassLoader this library is associated with.
527 Object* class_loader_;
528
529 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700530 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700531 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700532 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700533 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700534 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700535 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700536 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700537};
538
Elliott Hughes79082e32011-08-25 12:07:32 -0700539// This exists mainly to keep implementation details out of the header file.
540class Libraries {
541 public:
542 Libraries() {
543 }
544
545 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700546 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700547 }
548
Elliott Hughesae80b492012-04-24 10:43:17 -0700549 void Dump(std::ostream& os) const {
550 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700551 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700552 if (!first) {
553 os << ' ';
554 }
555 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700556 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700557 }
558 }
559
560 size_t size() const {
561 return libraries_.size();
562 }
563
Elliott Hughes79082e32011-08-25 12:07:32 -0700564 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700565 auto it = libraries_.find(path);
Ian Rogers712462a2012-04-12 16:32:29 -0700566 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700567 }
568
569 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700570 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700571 }
572
573 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstromea46f952013-07-30 01:26:50 -0700574 void* FindNativeMethod(const ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700575 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700576 std::string jni_short_name(JniShortName(m));
577 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700578 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700579 for (const auto& lib : libraries_) {
580 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700581 if (library->GetClassLoader() != declaring_class_loader) {
582 // We only search libraries loaded by the appropriate ClassLoader.
583 continue;
584 }
585 // Try the short name then the long name...
586 void* fn = library->FindSymbol(jni_short_name);
587 if (fn == NULL) {
588 fn = library->FindSymbol(jni_long_name);
589 }
590 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800591 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
592 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700593 return fn;
594 }
595 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700596 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700597 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700598 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700599 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700600 return NULL;
601 }
602
603 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700604 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700605};
606
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700607JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
608 jvalue* args) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700609 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700610 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700611 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800612 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700613 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800614 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700615 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
616 return result;
Elliott Hughesd07986f2011-12-06 18:27:45 -0800617}
618
Ian Rogersbc939662013-08-15 10:26:54 -0700619#define CHECK_NON_NULL_ARGUMENT(fn, value) \
620 if (UNLIKELY(value == NULL)) { \
621 JniAbortF(#fn, #value " == null"); \
622 }
623
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700624#define CHECK_NON_NULL_MEMCPY_ARGUMENT(fn, length, value) \
625 if (UNLIKELY(length != 0 && value == NULL)) { \
626 JniAbortF(#fn, #value " == null"); \
627 }
628
Elliott Hughescdf53122011-08-19 15:46:09 -0700629class JNI {
630 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700631 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700632 return JNI_VERSION_1_6;
633 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700634
Ian Rogers25e8b912012-09-07 11:31:36 -0700635 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700636 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700637 return NULL;
638 }
639
Elliott Hughescdf53122011-08-19 15:46:09 -0700640 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogersbc939662013-08-15 10:26:54 -0700641 CHECK_NON_NULL_ARGUMENT(FindClass, name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700642 Runtime* runtime = Runtime::Current();
643 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700644 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700645 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700646 Class* c = NULL;
647 if (runtime->IsStarted()) {
Ian Rogersef28b142012-11-30 14:22:18 -0800648 ClassLoader* cl = GetClassLoader(soa);
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800649 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700650 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800651 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700652 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700653 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700654 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700655
Elliott Hughescdf53122011-08-19 15:46:09 -0700656 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogersbc939662013-08-15 10:26:54 -0700657 CHECK_NON_NULL_ARGUMENT(FromReflectedMethod, java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700658 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700659 jobject art_method = env->GetObjectField(java_method,
660 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod);
661 ArtMethod* method = soa.Decode<ArtMethod*>(art_method);
662 DCHECK(method != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700663 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700664 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700665
Elliott Hughescdf53122011-08-19 15:46:09 -0700666 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogersbc939662013-08-15 10:26:54 -0700667 CHECK_NON_NULL_ARGUMENT(FromReflectedField, java_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700668 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700669 jobject art_field = env->GetObjectField(java_field,
670 WellKnownClasses::java_lang_reflect_Field_artField);
671 ArtField* field = soa.Decode<ArtField*>(art_field);
672 DCHECK(field != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700673 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700674 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700675
Elliott Hughescdf53122011-08-19 15:46:09 -0700676 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700677 CHECK_NON_NULL_ARGUMENT(ToReflectedMethod, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700678 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700679 ArtMethod* m = soa.DecodeMethod(mid);
680 jobject art_method = soa.AddLocalReference<jobject>(m);
681 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
682 if (env->ExceptionCheck()) {
683 return NULL;
684 }
685 SetObjectField(env,
686 reflect_method,
687 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod,
688 art_method);
689 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700690 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700691
Elliott Hughescdf53122011-08-19 15:46:09 -0700692 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700693 CHECK_NON_NULL_ARGUMENT(ToReflectedField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700694 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700695 ArtField* f = soa.DecodeField(fid);
696 jobject art_field = soa.AddLocalReference<jobject>(f);
697 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
698 if (env->ExceptionCheck()) {
699 return NULL;
700 }
701 SetObjectField(env,
702 reflect_field,
703 WellKnownClasses::java_lang_reflect_Field_artField,
704 art_field);
705 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700706 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700707
Elliott Hughes37f7a402011-08-22 18:56:01 -0700708 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -0700709 CHECK_NON_NULL_ARGUMENT(GetObjectClass, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700710 ScopedObjectAccess soa(env);
711 Object* o = soa.Decode<Object*>(java_object);
712 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700713 }
714
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700715 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700716 CHECK_NON_NULL_ARGUMENT(GetSuperclass, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700717 ScopedObjectAccess soa(env);
718 Class* c = soa.Decode<Class*>(java_class);
719 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700720 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700721
Elliott Hughes37f7a402011-08-22 18:56:01 -0700722 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogersbc939662013-08-15 10:26:54 -0700723 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class1);
724 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class2);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700725 ScopedObjectAccess soa(env);
726 Class* c1 = soa.Decode<Class*>(java_class1);
727 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700728 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700729 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700730
Elliott Hughese84278b2012-03-22 10:06:53 -0700731 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700732 CHECK_NON_NULL_ARGUMENT(IsInstanceOf, java_class);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700733 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700734 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700735 return JNI_TRUE;
736 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700737 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700738 Object* obj = soa.Decode<Object*>(jobj);
739 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700740 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700741 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700742 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700743
Elliott Hughes37f7a402011-08-22 18:56:01 -0700744 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700745 ScopedObjectAccess soa(env);
746 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700747 if (exception == NULL) {
748 return JNI_ERR;
749 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800750 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
751 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700752 return JNI_OK;
753 }
754
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700755 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogersbc939662013-08-15 10:26:54 -0700756 CHECK_NON_NULL_ARGUMENT(ThrowNew, c);
Elliott Hughesa4f94742012-05-29 16:28:38 -0700757 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700758 }
759
760 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700761 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700762 }
763
764 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700765 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700766 }
767
768 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700769 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700770
Brian Carlstromea46f952013-07-30 01:26:50 -0700771 SirtRef<Object> old_throw_this_object(soa.Self(), NULL);
772 SirtRef<ArtMethod> old_throw_method(soa.Self(), NULL);
773 SirtRef<Throwable> old_exception(soa.Self(), NULL);
Ian Rogers62d6c772013-02-27 08:32:07 -0800774 uint32_t old_throw_dex_pc;
775 {
776 ThrowLocation old_throw_location;
Brian Carlstromea46f952013-07-30 01:26:50 -0700777 Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Ian Rogers62d6c772013-02-27 08:32:07 -0800778 old_throw_this_object.reset(old_throw_location.GetThis());
779 old_throw_method.reset(old_throw_location.GetMethod());
780 old_exception.reset(old_exception_obj);
781 old_throw_dex_pc = old_throw_location.GetDexPc();
782 soa.Self()->ClearException();
783 }
784 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(old_exception.get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700785 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
786 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
787 if (mid == NULL) {
788 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Ian Rogers62d6c772013-02-27 08:32:07 -0800789 << PrettyTypeOf(old_exception.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700790 } else {
791 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800792 if (soa.Self()->IsExceptionPending()) {
793 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(NULL))
Elliott Hughes72025e52011-08-23 17:50:30 -0700794 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800795 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700796 }
797 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800798 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
799 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700800
Ian Rogers62d6c772013-02-27 08:32:07 -0800801 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700802 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700803
Elliott Hughescdf53122011-08-19 15:46:09 -0700804 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700805 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800806 Object* exception = soa.Self()->GetException(NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700807 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700808 }
809
Ian Rogers25e8b912012-09-07 11:31:36 -0700810 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700811 LOG(FATAL) << "JNI FatalError called: " << msg;
812 }
813
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700814 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800815 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700816 return JNI_ERR;
817 }
Ian Rogersef28b142012-11-30 14:22:18 -0800818 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700819 return JNI_OK;
820 }
821
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700822 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700823 ScopedObjectAccess soa(env);
824 Object* survivor = soa.Decode<Object*>(java_survivor);
825 soa.Env()->PopFrame();
826 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700827 }
828
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700829 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800830 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700831 }
832
Elliott Hughescdf53122011-08-19 15:46:09 -0700833 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700834 if (obj == NULL) {
835 return NULL;
836 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700837 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700838 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700839 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700840 Object* decoded_obj = soa.Decode<Object*>(obj);
Ian Rogersb8a0b942013-08-20 18:09:52 -0700841 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700842 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700843 return reinterpret_cast<jobject>(ref);
844 }
845
846 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700847 if (obj == NULL) {
848 return;
849 }
Ian Rogersef28b142012-11-30 14:22:18 -0800850 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700851 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800852 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700853 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700854
855 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
856 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
857 << "failed to find entry";
858 }
859 }
860
861 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700862 ScopedObjectAccess soa(env);
863 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700864 }
865
866 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700867 if (obj != nullptr) {
868 ScopedObjectAccess soa(env);
869 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700870 }
871 }
872
873 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700874 if (obj == NULL) {
875 return NULL;
876 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700877 ScopedObjectAccess soa(env);
Elliott Hughes9dcd45c2013-07-29 14:40:52 -0700878 return soa.AddLocalReference<jobject>(soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700879 }
880
881 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700882 if (obj == NULL) {
883 return;
884 }
Ian Rogersef28b142012-11-30 14:22:18 -0800885 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700886
Ian Rogersef28b142012-11-30 14:22:18 -0800887 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700888 if (!locals.Remove(cookie, obj)) {
889 // Attempting to delete a local reference that is not in the
890 // topmost local reference frame is a no-op. DeleteLocalRef returns
891 // void and doesn't throw any exceptions, but we should probably
892 // complain about it so the user will notice that things aren't
893 // going quite the way they expect.
894 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
895 << "failed to find entry";
896 }
897 }
898
899 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700900 if (obj1 == obj2) {
901 return JNI_TRUE;
902 } else {
903 ScopedObjectAccess soa(env);
904 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
905 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700906 }
907
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700908 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700909 CHECK_NON_NULL_ARGUMENT(AllocObject, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700910 ScopedObjectAccess soa(env);
911 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700912 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700913 return NULL;
914 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700915 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700916 }
917
Ian Rogersbc939662013-08-15 10:26:54 -0700918 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700919 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700920 va_start(args, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700921 CHECK_NON_NULL_ARGUMENT(NewObject, java_class);
922 CHECK_NON_NULL_ARGUMENT(NewObject, mid);
923 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700924 va_end(args);
925 return result;
926 }
927
Elliott Hughes72025e52011-08-23 17:50:30 -0700928 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700929 CHECK_NON_NULL_ARGUMENT(NewObjectV, java_class);
930 CHECK_NON_NULL_ARGUMENT(NewObjectV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700931 ScopedObjectAccess soa(env);
932 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700933 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700934 return NULL;
935 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700936 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700937 if (result == NULL) {
938 return NULL;
939 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700940 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700941 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700942 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700943 return local_result;
944 } else {
945 return NULL;
946 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700947 }
948
Elliott Hughes72025e52011-08-23 17:50:30 -0700949 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700950 CHECK_NON_NULL_ARGUMENT(NewObjectA, java_class);
951 CHECK_NON_NULL_ARGUMENT(NewObjectA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700952 ScopedObjectAccess soa(env);
953 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700954 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700955 return NULL;
956 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700957 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700958 if (result == NULL) {
959 return NULL;
960 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700961 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700962 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700963 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700964 return local_result;
965 } else {
966 return NULL;
967 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700968 }
969
Ian Rogersbc939662013-08-15 10:26:54 -0700970 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
971 CHECK_NON_NULL_ARGUMENT(GetMethodID, java_class);
972 CHECK_NON_NULL_ARGUMENT(GetMethodID, name);
973 CHECK_NON_NULL_ARGUMENT(GetMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700974 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700975 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700976 }
977
Ian Rogersbc939662013-08-15 10:26:54 -0700978 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
979 const char* sig) {
980 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, java_class);
981 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, name);
982 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700983 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700984 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700985 }
986
Elliott Hughes72025e52011-08-23 17:50:30 -0700987 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700988 va_list ap;
989 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700990 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, obj);
991 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700992 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700993 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700994 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700995 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700996 }
997
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700999 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, obj);
1000 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001001 ScopedObjectAccess soa(env);
1002 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
1003 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001004 }
1005
Elliott Hughes72025e52011-08-23 17:50:30 -07001006 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001007 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, obj);
1008 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001009 ScopedObjectAccess soa(env);
1010 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
1011 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001012 }
1013
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 va_list ap;
1016 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001017 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, obj);
1018 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001019 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001020 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001021 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001022 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001023 }
1024
Elliott Hughes72025e52011-08-23 17:50:30 -07001025 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001026 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, obj);
1027 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001028 ScopedObjectAccess soa(env);
1029 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001030 }
1031
Elliott Hughes72025e52011-08-23 17:50:30 -07001032 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001033 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, obj);
1034 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001035 ScopedObjectAccess soa(env);
1036 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001037 }
1038
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001040 va_list ap;
1041 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001042 CHECK_NON_NULL_ARGUMENT(CallByteMethod, obj);
1043 CHECK_NON_NULL_ARGUMENT(CallByteMethod, mid);
1044 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001045 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001046 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001047 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001048 }
1049
Elliott Hughes72025e52011-08-23 17:50:30 -07001050 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001051 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, obj);
1052 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001053 ScopedObjectAccess soa(env);
1054 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001055 }
1056
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001058 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, obj);
1059 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001060 ScopedObjectAccess soa(env);
1061 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 }
1063
Elliott Hughes72025e52011-08-23 17:50:30 -07001064 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001065 va_list ap;
1066 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001067 CHECK_NON_NULL_ARGUMENT(CallCharMethod, obj);
1068 CHECK_NON_NULL_ARGUMENT(CallCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001069 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001070 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001072 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 }
1074
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001076 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, obj);
1077 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001078 ScopedObjectAccess soa(env);
1079 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001080 }
1081
Elliott Hughes72025e52011-08-23 17:50:30 -07001082 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001083 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, obj);
1084 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001085 ScopedObjectAccess soa(env);
1086 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 }
1088
Elliott Hughes72025e52011-08-23 17:50:30 -07001089 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 va_list ap;
1091 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001092 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, obj);
1093 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001094 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001095 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001096 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001097 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001098 }
1099
Elliott Hughes72025e52011-08-23 17:50:30 -07001100 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001101 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, obj);
1102 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001103 ScopedObjectAccess soa(env);
1104 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 }
1106
Elliott Hughes72025e52011-08-23 17:50:30 -07001107 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001108 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, obj);
1109 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001110 ScopedObjectAccess soa(env);
1111 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 }
1113
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 va_list ap;
1116 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001117 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, obj);
1118 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, mid);
1119 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001120 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001121 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001122 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 }
1124
Elliott Hughes72025e52011-08-23 17:50:30 -07001125 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001126 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, obj);
1127 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001128 ScopedObjectAccess soa(env);
1129 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001130 }
1131
Elliott Hughes72025e52011-08-23 17:50:30 -07001132 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001133 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, obj);
1134 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001135 ScopedObjectAccess soa(env);
1136 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 }
1138
Elliott Hughes72025e52011-08-23 17:50:30 -07001139 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001140 va_list ap;
1141 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001142 CHECK_NON_NULL_ARGUMENT(CallIntMethod, obj);
1143 CHECK_NON_NULL_ARGUMENT(CallIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001144 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001145 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001146 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001147 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 }
1149
Elliott Hughes72025e52011-08-23 17:50:30 -07001150 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001151 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, obj);
1152 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001153 ScopedObjectAccess soa(env);
1154 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 }
1156
Elliott Hughes72025e52011-08-23 17:50:30 -07001157 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001158 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, obj);
1159 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001160 ScopedObjectAccess soa(env);
1161 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001162 }
1163
Elliott Hughes72025e52011-08-23 17:50:30 -07001164 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 va_list ap;
1166 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001167 CHECK_NON_NULL_ARGUMENT(CallLongMethod, obj);
1168 CHECK_NON_NULL_ARGUMENT(CallLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001169 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001170 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001171 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001172 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 }
1174
Elliott Hughes72025e52011-08-23 17:50:30 -07001175 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001176 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, obj);
1177 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001178 ScopedObjectAccess soa(env);
1179 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001180 }
1181
Elliott Hughes72025e52011-08-23 17:50:30 -07001182 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001183 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, obj);
1184 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001185 ScopedObjectAccess soa(env);
1186 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 }
1188
Elliott Hughes72025e52011-08-23 17:50:30 -07001189 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001190 va_list ap;
1191 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001192 CHECK_NON_NULL_ARGUMENT(CallShortMethod, obj);
1193 CHECK_NON_NULL_ARGUMENT(CallShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001194 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001195 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001196 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001197 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001198 }
1199
Elliott Hughes72025e52011-08-23 17:50:30 -07001200 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001201 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, obj);
1202 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001203 ScopedObjectAccess soa(env);
1204 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 }
1206
Elliott Hughes72025e52011-08-23 17:50:30 -07001207 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001208 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, obj);
1209 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001210 ScopedObjectAccess soa(env);
1211 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001212 }
1213
Elliott Hughes72025e52011-08-23 17:50:30 -07001214 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001215 va_list ap;
1216 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001217 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, obj);
1218 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001219 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001220 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001221 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 }
1223
Elliott Hughes72025e52011-08-23 17:50:30 -07001224 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001225 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, obj);
1226 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001227 ScopedObjectAccess soa(env);
1228 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 }
1230
Elliott Hughes72025e52011-08-23 17:50:30 -07001231 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001232 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, obj);
1233 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001234 ScopedObjectAccess soa(env);
1235 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 }
1237
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001238 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001239 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001240 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001241 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, obj);
1242 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001243 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001244 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1245 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 va_end(ap);
1247 return local_result;
1248 }
1249
Ian Rogersbc939662013-08-15 10:26:54 -07001250 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1251 va_list args) {
1252 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, obj);
1253 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001254 ScopedObjectAccess soa(env);
1255 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1256 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 }
1258
Ian Rogersbc939662013-08-15 10:26:54 -07001259 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1260 jvalue* args) {
1261 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, obj);
1262 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001263 ScopedObjectAccess soa(env);
1264 JValue result(InvokeWithJValues(soa, obj, mid, args));
1265 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001266 }
1267
Ian Rogersbc939662013-08-15 10:26:54 -07001268 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1269 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001271 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001272 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, obj);
1273 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001274 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001275 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001276 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001277 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 }
1279
Ian Rogersbc939662013-08-15 10:26:54 -07001280 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1281 va_list args) {
1282 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, obj);
1283 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001284 ScopedObjectAccess soa(env);
1285 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 }
1287
Ian Rogersbc939662013-08-15 10:26:54 -07001288 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1289 jvalue* args) {
1290 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, obj);
1291 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001292 ScopedObjectAccess soa(env);
1293 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 }
1295
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001296 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001297 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001298 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001299 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, obj);
1300 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001301 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001302 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001303 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001304 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001305 }
1306
Ian Rogersbc939662013-08-15 10:26:54 -07001307 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1308 va_list args) {
1309 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, obj);
1310 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001311 ScopedObjectAccess soa(env);
1312 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 }
1314
Ian Rogersbc939662013-08-15 10:26:54 -07001315 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1316 jvalue* args) {
1317 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, obj);
1318 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001319 ScopedObjectAccess soa(env);
1320 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 }
1322
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001323 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001324 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001325 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001326 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, obj);
1327 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, mid);
1328 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001329 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001330 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001331 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 }
1333
Ian Rogersbc939662013-08-15 10:26:54 -07001334 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1335 va_list args) {
1336 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, obj);
1337 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001338 ScopedObjectAccess soa(env);
1339 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 }
1341
Ian Rogersbc939662013-08-15 10:26:54 -07001342 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1343 jvalue* args) {
1344 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, obj);
1345 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001346 ScopedObjectAccess soa(env);
1347 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 }
1349
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001350 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001351 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001352 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001353 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, obj);
1354 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001355 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001356 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001358 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001359 }
1360
Ian Rogersbc939662013-08-15 10:26:54 -07001361 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1362 va_list args) {
1363 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, obj);
1364 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001365 ScopedObjectAccess soa(env);
1366 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001367 }
1368
Ian Rogersbc939662013-08-15 10:26:54 -07001369 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1370 jvalue* args) {
1371 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, obj);
1372 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001373 ScopedObjectAccess soa(env);
1374 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 }
1376
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001377 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001379 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001380 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, obj);
1381 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001382 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001383 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001384 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001385 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001386 }
1387
Ian Rogersbc939662013-08-15 10:26:54 -07001388 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1389 va_list args) {
1390 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, obj);
1391 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001392 ScopedObjectAccess soa(env);
1393 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001394 }
1395
Ian Rogersbc939662013-08-15 10:26:54 -07001396 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1397 jvalue* args) {
1398 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, obj);
1399 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001400 ScopedObjectAccess soa(env);
1401 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001402 }
1403
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001404 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001405 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001406 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001407 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, obj);
1408 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001409 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001410 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001411 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001412 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 }
1414
Ian Rogersbc939662013-08-15 10:26:54 -07001415 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1416 va_list args) {
1417 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, obj);
1418 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001419 ScopedObjectAccess soa(env);
1420 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001421 }
1422
Ian Rogersbc939662013-08-15 10:26:54 -07001423 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1424 jvalue* args) {
1425 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, obj);
1426 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001427 ScopedObjectAccess soa(env);
1428 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001429 }
1430
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001431 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001433 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001434 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, obj);
1435 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001436 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001437 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001439 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 }
1441
Ian Rogersbc939662013-08-15 10:26:54 -07001442 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1443 va_list args) {
1444 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, obj);
1445 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001446 ScopedObjectAccess soa(env);
1447 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001448 }
1449
Ian Rogersbc939662013-08-15 10:26:54 -07001450 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1451 jvalue* args) {
1452 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, obj);
1453 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001454 ScopedObjectAccess soa(env);
1455 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001456 }
1457
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001458 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001459 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001460 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001461 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, obj);
1462 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001463 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001464 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001465 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001466 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001467 }
1468
Ian Rogersbc939662013-08-15 10:26:54 -07001469 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1470 va_list args) {
1471 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, obj);
1472 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001473 ScopedObjectAccess soa(env);
1474 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001475 }
1476
Ian Rogersbc939662013-08-15 10:26:54 -07001477 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1478 jvalue* args) {
1479 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, obj);
1480 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001481 ScopedObjectAccess soa(env);
1482 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001483 }
1484
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001485 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001486 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001487 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001488 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, obj);
1489 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001490 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001491 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001492 va_end(ap);
1493 }
1494
Brian Carlstromea46f952013-07-30 01:26:50 -07001495 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1496 va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001497 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, obj);
1498 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001499 ScopedObjectAccess soa(env);
1500 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001501 }
1502
Ian Rogersbc939662013-08-15 10:26:54 -07001503 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1504 jvalue* args) {
1505 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, obj);
1506 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001507 ScopedObjectAccess soa(env);
1508 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001509 }
1510
Ian Rogersbc939662013-08-15 10:26:54 -07001511 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
1512 CHECK_NON_NULL_ARGUMENT(GetFieldID, java_class);
1513 CHECK_NON_NULL_ARGUMENT(GetFieldID, name);
1514 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001515 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001516 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001517 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001518
Ian Rogersbc939662013-08-15 10:26:54 -07001519 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1520 const char* sig) {
1521 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, java_class);
1522 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, name);
1523 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001524 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001525 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001526 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001527
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001528 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001529 CHECK_NON_NULL_ARGUMENT(GetObjectField, obj);
1530 CHECK_NON_NULL_ARGUMENT(GetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001531 ScopedObjectAccess soa(env);
1532 Object* o = soa.Decode<Object*>(obj);
Brian Carlstromea46f952013-07-30 01:26:50 -07001533 ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001534 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001535 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001536
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001537 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001538 CHECK_NON_NULL_ARGUMENT(GetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001539 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -07001540 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001541 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 }
1543
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001544 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001545 CHECK_NON_NULL_ARGUMENT(SetObjectField, java_object);
1546 CHECK_NON_NULL_ARGUMENT(SetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001547 ScopedObjectAccess soa(env);
1548 Object* o = soa.Decode<Object*>(java_object);
1549 Object* v = soa.Decode<Object*>(java_value);
Brian Carlstromea46f952013-07-30 01:26:50 -07001550 ArtField* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001551 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001552 }
1553
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001554 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001555 CHECK_NON_NULL_ARGUMENT(SetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001556 ScopedObjectAccess soa(env);
1557 Object* v = soa.Decode<Object*>(java_value);
Brian Carlstromea46f952013-07-30 01:26:50 -07001558 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001559 f->SetObject(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001560 }
1561
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001562#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogersbc939662013-08-15 10:26:54 -07001563 CHECK_NON_NULL_ARGUMENT(Get #fn Field, instance); \
1564 CHECK_NON_NULL_ARGUMENT(Get #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001565 ScopedObjectAccess soa(env); \
1566 Object* o = soa.Decode<Object*>(instance); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001567 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001568 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001569
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001570#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogersbc939662013-08-15 10:26:54 -07001571 CHECK_NON_NULL_ARGUMENT(GetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001572 ScopedObjectAccess soa(env); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001573 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001574 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001575
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001576#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001577 CHECK_NON_NULL_ARGUMENT(Set #fn Field, instance); \
1578 CHECK_NON_NULL_ARGUMENT(Set #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001579 ScopedObjectAccess soa(env); \
1580 Object* o = soa.Decode<Object*>(instance); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001581 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001582 f->Set ##fn(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001583
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001584#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001585 CHECK_NON_NULL_ARGUMENT(SetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001586 ScopedObjectAccess soa(env); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001587 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001588 f->Set ##fn(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001589
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001590 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001591 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001592 }
1593
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001594 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001595 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001596 }
1597
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001598 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001599 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001600 }
1601
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001602 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001603 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001604 }
1605
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001606 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001607 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001608 }
1609
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001610 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001611 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001612 }
1613
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001614 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001615 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001616 }
1617
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001618 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001619 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 }
1621
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001622 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001623 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 }
1625
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001626 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001627 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001628 }
1629
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001630 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001631 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 }
1633
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001634 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001635 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001636 }
1637
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001638 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001639 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001640 }
1641
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001642 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001643 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001644 }
1645
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001646 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001647 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001648 }
1649
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001650 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001651 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001652 }
1653
1654 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001655 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001656 }
1657
1658 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001659 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001660 }
1661
1662 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001663 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001664 }
1665
1666 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001667 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001668 }
1669
1670 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001671 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001672 }
1673
1674 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001675 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001676 }
1677
1678 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001679 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001680 }
1681
1682 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001683 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001684 }
1685
1686 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001687 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001688 }
1689
1690 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001691 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001692 }
1693
1694 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001695 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001696 }
1697
1698 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001699 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001700 }
1701
1702 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001703 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001704 }
1705
1706 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001707 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001708 }
1709
1710 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001711 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001712 }
1713
1714 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001715 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001716 }
1717
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001718 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001720 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001721 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001722 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001723 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1724 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001725 va_end(ap);
1726 return local_result;
1727 }
1728
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001729 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001730 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001731 ScopedObjectAccess soa(env);
1732 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1733 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 }
1735
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001736 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001737 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001738 ScopedObjectAccess soa(env);
1739 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1740 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001741 }
1742
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001743 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001744 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001745 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001746 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001747 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001748 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001749 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001750 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001751 }
1752
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001753 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001754 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001755 ScopedObjectAccess soa(env);
1756 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 }
1758
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001759 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001760 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001761 ScopedObjectAccess soa(env);
1762 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001763 }
1764
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001765 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001766 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001767 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001768 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001769 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001770 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001771 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001772 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001773 }
1774
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001775 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001776 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001777 ScopedObjectAccess soa(env);
1778 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001779 }
1780
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001781 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001782 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001783 ScopedObjectAccess soa(env);
1784 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001785 }
1786
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001787 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001788 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001789 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001790 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001791 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001792 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001793 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001794 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001795 }
1796
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001797 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001798 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001799 ScopedObjectAccess soa(env);
1800 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001801 }
1802
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001803 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001804 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001805 ScopedObjectAccess soa(env);
1806 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001807 }
1808
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001809 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001810 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001811 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001812 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001813 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001814 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001815 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001816 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001817 }
1818
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001819 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001820 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001821 ScopedObjectAccess soa(env);
1822 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001823 }
1824
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001825 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001826 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001827 ScopedObjectAccess soa(env);
1828 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 }
1830
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001831 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001832 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001833 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001834 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001835 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001836 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001837 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001838 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001839 }
1840
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001841 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001842 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001843 ScopedObjectAccess soa(env);
1844 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001845 }
1846
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001847 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001848 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001849 ScopedObjectAccess soa(env);
1850 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001851 }
1852
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001853 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001854 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001855 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001856 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001857 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001858 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001859 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001860 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001861 }
1862
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001863 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001864 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001865 ScopedObjectAccess soa(env);
1866 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001867 }
1868
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001869 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001870 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001871 ScopedObjectAccess soa(env);
1872 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001873 }
1874
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001875 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001876 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001877 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001878 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001879 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001880 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001881 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001882 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001883 }
1884
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001885 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001886 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001887 ScopedObjectAccess soa(env);
1888 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001889 }
1890
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001891 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001892 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001893 ScopedObjectAccess soa(env);
1894 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001895 }
1896
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001897 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001898 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001899 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001900 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001901 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001902 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001903 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001904 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001905 }
1906
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001907 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001908 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001909 ScopedObjectAccess soa(env);
1910 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001911 }
1912
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001913 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001914 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001915 ScopedObjectAccess soa(env);
1916 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001917 }
1918
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001919 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001920 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001921 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001922 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001923 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001924 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001925 va_end(ap);
1926 }
1927
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001928 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001929 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001930 ScopedObjectAccess soa(env);
1931 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001932 }
1933
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001934 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001935 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001936 ScopedObjectAccess soa(env);
1937 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001938 }
1939
Elliott Hughes814e4032011-08-23 12:07:56 -07001940 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogersbc939662013-08-15 10:26:54 -07001941 if (UNLIKELY(chars == NULL && char_count > 0)) { \
1942 JniAbortF("NewString", "char == null && char_count > 0"); \
1943 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001944 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001945 String* result = String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001946 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001947 }
1948
1949 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001950 if (utf == NULL) {
1951 return NULL;
1952 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001953 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001954 String* result = String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001955 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001956 }
1957
Elliott Hughes814e4032011-08-23 12:07:56 -07001958 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001959 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001960 ScopedObjectAccess soa(env);
1961 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001962 }
1963
1964 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001965 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001966 ScopedObjectAccess soa(env);
1967 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001968 }
1969
Ian Rogersbc939662013-08-15 10:26:54 -07001970 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1971 jchar* buf) {
1972 CHECK_NON_NULL_ARGUMENT(GetStringRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001973 ScopedObjectAccess soa(env);
1974 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001975 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001976 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001977 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07001978 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001979 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1980 memcpy(buf, chars + start, length * sizeof(jchar));
1981 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001982 }
1983
Ian Rogersbc939662013-08-15 10:26:54 -07001984 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1985 char* buf) {
1986 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001987 ScopedObjectAccess soa(env);
1988 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001989 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001990 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001991 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07001992 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringUTFRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001993 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1994 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1995 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001996 }
1997
Elliott Hughes75770752011-08-24 17:52:38 -07001998 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07001999 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002000 ScopedObjectAccess soa(env);
2001 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002002 const CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002003 PinPrimitiveArray(soa, chars);
Elliott Hughes75770752011-08-24 17:52:38 -07002004 if (is_copy != NULL) {
2005 *is_copy = JNI_FALSE;
2006 }
2007 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07002008 }
2009
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002010 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Ian Rogersbc939662013-08-15 10:26:54 -07002011 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002012 ScopedObjectAccess soa(env);
2013 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 }
2015
Elliott Hughes75770752011-08-24 17:52:38 -07002016 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07002017 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002018 }
2019
Elliott Hughes75770752011-08-24 17:52:38 -07002020 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002021 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 }
2023
Elliott Hughes75770752011-08-24 17:52:38 -07002024 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07002025 if (java_string == NULL) {
2026 return NULL;
2027 }
2028 if (is_copy != NULL) {
2029 *is_copy = JNI_TRUE;
2030 }
Ian Rogersef28b142012-11-30 14:22:18 -08002031 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002032 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002033 size_t byte_count = s->GetUtfLength();
2034 char* bytes = new char[byte_count + 1];
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002035 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07002036 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
2037 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
2038 bytes[byte_count] = '\0';
2039 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002040 }
2041
Elliott Hughes75770752011-08-24 17:52:38 -07002042 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002043 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002044 }
2045
Elliott Hughesbd935992011-08-22 11:59:34 -07002046 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogersbc939662013-08-15 10:26:54 -07002047 CHECK_NON_NULL_ARGUMENT(GetArrayLength, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002048 ScopedObjectAccess soa(env);
2049 Object* obj = soa.Decode<Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07002050 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002051 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
2052 }
Elliott Hughesbd935992011-08-22 11:59:34 -07002053 Array* array = obj->AsArray();
2054 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07002055 }
2056
Elliott Hughes814e4032011-08-23 12:07:56 -07002057 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogersbc939662013-08-15 10:26:54 -07002058 CHECK_NON_NULL_ARGUMENT(GetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002059 ScopedObjectAccess soa(env);
2060 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
2061 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 }
2063
Ian Rogersbc939662013-08-15 10:26:54 -07002064 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2065 jobject java_value) {
2066 CHECK_NON_NULL_ARGUMENT(SetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002067 ScopedObjectAccess soa(env);
2068 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
2069 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002070 array->Set(index, value);
2071 }
2072
2073 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002074 ScopedObjectAccess soa(env);
2075 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002076 }
2077
2078 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002079 ScopedObjectAccess soa(env);
2080 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002081 }
2082
2083 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002084 ScopedObjectAccess soa(env);
2085 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 }
2087
2088 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002089 ScopedObjectAccess soa(env);
2090 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002091 }
2092
2093 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002094 ScopedObjectAccess soa(env);
2095 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002096 }
2097
2098 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002099 ScopedObjectAccess soa(env);
2100 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 }
2102
2103 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002104 ScopedObjectAccess soa(env);
2105 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
2107
2108 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002109 if (length < 0) {
2110 JniAbortF("NewObjectArray", "negative array length: %d", length);
2111 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002112
2113 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002114 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002115 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07002116 std::string descriptor;
2117 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002118 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07002119
2120 // Find the class.
Brian Carlstromce888532013-10-10 00:32:58 -07002121 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2122 Class* array_class = class_linker->FindClass(descriptor.c_str(),
2123 element_class->GetClassLoader());
2124 if (array_class == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 return NULL;
2126 }
2127
Elliott Hughes75770752011-08-24 17:52:38 -07002128 // Allocate and initialize if necessary.
Ian Rogers50b35e22012-10-04 10:09:15 -07002129 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07002130 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002131 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07002132 for (jsize i = 0; i < length; ++i) {
2133 result->Set(i, initial_object);
2134 }
2135 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002136 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002137 }
2138
2139 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002140 ScopedObjectAccess soa(env);
2141 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 }
2143
Ian Rogersa15e67d2012-02-28 13:51:55 -08002144 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002145 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayCritical, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002146 ScopedObjectAccess soa(env);
2147 Array* array = soa.Decode<Array*>(java_array);
2148 PinPrimitiveArray(soa, array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08002149 if (is_copy != NULL) {
2150 *is_copy = JNI_FALSE;
2151 }
2152 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002153 }
2154
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002155 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Ian Rogersbc939662013-08-15 10:26:54 -07002156 CHECK_NON_NULL_ARGUMENT(ReleasePrimitiveArrayCritical, array);
Ian Rogersef28b142012-11-30 14:22:18 -08002157 ReleasePrimitiveArray(env, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002158 }
2159
Elliott Hughes75770752011-08-24 17:52:38 -07002160 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002161 CHECK_NON_NULL_ARGUMENT(GetBooleanArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002162 ScopedObjectAccess soa(env);
2163 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002164 }
2165
Elliott Hughes75770752011-08-24 17:52:38 -07002166 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002167 CHECK_NON_NULL_ARGUMENT(GetByteArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002168 ScopedObjectAccess soa(env);
2169 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002170 }
2171
Elliott Hughes75770752011-08-24 17:52:38 -07002172 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002173 CHECK_NON_NULL_ARGUMENT(GetCharArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002174 ScopedObjectAccess soa(env);
2175 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002176 }
2177
Elliott Hughes75770752011-08-24 17:52:38 -07002178 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002179 CHECK_NON_NULL_ARGUMENT(GetDoubleArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002180 ScopedObjectAccess soa(env);
2181 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002182 }
2183
Elliott Hughes75770752011-08-24 17:52:38 -07002184 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002185 CHECK_NON_NULL_ARGUMENT(GetFloatArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002186 ScopedObjectAccess soa(env);
2187 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002188 }
2189
Elliott Hughes75770752011-08-24 17:52:38 -07002190 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002191 CHECK_NON_NULL_ARGUMENT(GetIntArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002192 ScopedObjectAccess soa(env);
2193 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002194 }
2195
Elliott Hughes75770752011-08-24 17:52:38 -07002196 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002197 CHECK_NON_NULL_ARGUMENT(GetLongArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002198 ScopedObjectAccess soa(env);
2199 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002200 }
2201
Elliott Hughes75770752011-08-24 17:52:38 -07002202 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002203 CHECK_NON_NULL_ARGUMENT(GetShortArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002204 ScopedObjectAccess soa(env);
2205 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002206 }
2207
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002208 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002209 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002210 }
2211
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002212 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002213 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002214 }
2215
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002216 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002217 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002218 }
2219
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002220 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002221 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002222 }
2223
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002224 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002225 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002226 }
2227
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002228 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002229 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002230 }
2231
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002232 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002233 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002234 }
2235
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002236 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002237 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002238 }
2239
Ian Rogersbc939662013-08-15 10:26:54 -07002240 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2241 jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002242 ScopedObjectAccess soa(env);
2243 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002244 }
2245
Ian Rogersbc939662013-08-15 10:26:54 -07002246 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2247 jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002248 ScopedObjectAccess soa(env);
2249 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002250 }
2251
Ian Rogersbc939662013-08-15 10:26:54 -07002252 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2253 jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002254 ScopedObjectAccess soa(env);
2255 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002256 }
2257
Ian Rogersbc939662013-08-15 10:26:54 -07002258 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2259 jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002260 ScopedObjectAccess soa(env);
2261 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002262 }
2263
Ian Rogersbc939662013-08-15 10:26:54 -07002264 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2265 jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002266 ScopedObjectAccess soa(env);
2267 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002268 }
2269
Ian Rogersbc939662013-08-15 10:26:54 -07002270 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2271 jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002272 ScopedObjectAccess soa(env);
2273 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002274 }
2275
Ian Rogersbc939662013-08-15 10:26:54 -07002276 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2277 jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002278 ScopedObjectAccess soa(env);
2279 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002280 }
2281
Ian Rogersbc939662013-08-15 10:26:54 -07002282 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2283 jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002284 ScopedObjectAccess soa(env);
2285 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002286 }
2287
Ian Rogersbc939662013-08-15 10:26:54 -07002288 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2289 const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002290 ScopedObjectAccess soa(env);
2291 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002292 }
2293
Ian Rogersbc939662013-08-15 10:26:54 -07002294 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2295 const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002296 ScopedObjectAccess soa(env);
2297 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002298 }
2299
Ian Rogersbc939662013-08-15 10:26:54 -07002300 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2301 const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002302 ScopedObjectAccess soa(env);
2303 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002304 }
2305
Ian Rogersbc939662013-08-15 10:26:54 -07002306 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2307 const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002308 ScopedObjectAccess soa(env);
2309 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002310 }
2311
Ian Rogersbc939662013-08-15 10:26:54 -07002312 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2313 const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002314 ScopedObjectAccess soa(env);
2315 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002316 }
2317
Ian Rogersbc939662013-08-15 10:26:54 -07002318 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2319 const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002320 ScopedObjectAccess soa(env);
2321 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002322 }
2323
Ian Rogersbc939662013-08-15 10:26:54 -07002324 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2325 const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002326 ScopedObjectAccess soa(env);
2327 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002328 }
2329
Ian Rogersbc939662013-08-15 10:26:54 -07002330 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2331 const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002332 ScopedObjectAccess soa(env);
2333 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002334 }
2335
Ian Rogersbc939662013-08-15 10:26:54 -07002336 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2337 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002338 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2339 }
2340
Ian Rogersbc939662013-08-15 10:26:54 -07002341 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2342 jint method_count, bool return_errors) {
2343 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002344 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002345 return JNI_ERR; // Not reached.
2346 }
2347 CHECK_NON_NULL_ARGUMENT(RegisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002348 ScopedObjectAccess soa(env);
2349 Class* c = soa.Decode<Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002350 if (UNLIKELY(method_count == 0)) {
2351 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2352 << PrettyDescriptor(c);
2353 return JNI_OK;
2354 }
2355 CHECK_NON_NULL_ARGUMENT(RegisterNatives, methods);
2356 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002357 const char* name = methods[i].name;
2358 const char* sig = methods[i].signature;
2359
2360 if (*sig == '!') {
2361 // TODO: fast jni. it's too noisy to log all these.
2362 ++sig;
2363 }
2364
Brian Carlstromea46f952013-07-30 01:26:50 -07002365 ArtMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002366 if (m == NULL) {
2367 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002368 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002369 if (m == NULL) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002370 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002371 << PrettyDescriptor(c) << "." << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002372 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002373 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002374 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002375 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002376 << PrettyDescriptor(c) << "." << name << sig
2377 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002378 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002379 return JNI_ERR;
2380 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002381
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002382 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002383
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002384 m->RegisterNative(soa.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002385 }
2386 return JNI_OK;
2387 }
2388
Elliott Hughes5174fe62011-08-23 15:12:35 -07002389 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -07002390 CHECK_NON_NULL_ARGUMENT(UnregisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002391 ScopedObjectAccess soa(env);
2392 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002393
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002394 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002395
2396 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07002397 ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002398 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002399 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002400 }
2401 }
2402 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07002403 ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002404 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002405 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002406 }
2407 }
2408
2409 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002410 }
2411
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002412 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2413 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002414 CHECK_NON_NULL_ARGUMENT(MonitorEnter, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002415 ScopedObjectAccess soa(env);
2416 Object* o = soa.Decode<Object*>(java_object);
2417 o->MonitorEnter(soa.Self());
2418 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002419 return JNI_ERR;
2420 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002421 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002422 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002423 }
2424
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002425 static jint MonitorExit(JNIEnv* env, jobject java_object)
2426 UNLOCK_FUNCTION(monitor_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002427 CHECK_NON_NULL_ARGUMENT(MonitorExit, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002428 ScopedObjectAccess soa(env);
2429 Object* o = soa.Decode<Object*>(java_object);
2430 o->MonitorExit(soa.Self());
2431 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002432 return JNI_ERR;
2433 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002434 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002435 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002436 }
2437
2438 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogersbc939662013-08-15 10:26:54 -07002439 CHECK_NON_NULL_ARGUMENT(GetJavaVM, vm);
Elliott Hughescdf53122011-08-19 15:46:09 -07002440 Runtime* runtime = Runtime::Current();
2441 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002442 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002443 } else {
2444 *vm = NULL;
2445 }
2446 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2447 }
2448
Elliott Hughescdf53122011-08-19 15:46:09 -07002449 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002450 if (capacity < 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002451 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002452 }
Elliott Hughes11a796e2012-12-19 14:42:57 -08002453 if (address == NULL && capacity != 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002454 JniAbortF("NewDirectByteBuffer", "non-zero capacity for NULL pointer: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002455 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002456
Elliott Hughes96a98872012-12-19 14:21:15 -08002457 // At the moment, the Java side is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002458 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2459 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002460 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002461 jint capacity_arg = static_cast<jint>(capacity);
2462
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002463 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2464 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002465 address_arg, capacity_arg);
Ian Rogersef28b142012-11-30 14:22:18 -08002466 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002467 }
2468
Elliott Hughesb465ab02011-08-24 11:21:21 -07002469 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Jeff Hao534f2b62013-07-10 15:29:36 -07002470 return reinterpret_cast<void*>(env->GetLongField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002471 }
2472
Elliott Hughesb465ab02011-08-24 11:21:21 -07002473 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002474 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002475 }
2476
Elliott Hughesb465ab02011-08-24 11:21:21 -07002477 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -07002478 CHECK_NON_NULL_ARGUMENT(GetObjectRefType, java_object);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002479
2480 // Do we definitely know what kind of reference this is?
2481 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2482 IndirectRefKind kind = GetIndirectRefKind(ref);
2483 switch (kind) {
2484 case kLocal:
Ian Rogersef28b142012-11-30 14:22:18 -08002485 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002486 return JNILocalRefType;
2487 }
2488 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002489 case kGlobal:
2490 return JNIGlobalRefType;
2491 case kWeakGlobal:
2492 return JNIWeakGlobalRefType;
2493 case kSirtOrInvalid:
2494 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002495 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002496 return JNILocalRefType;
2497 }
2498
Ian Rogersef28b142012-11-30 14:22:18 -08002499 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002500 return JNIInvalidRefType;
2501 }
2502
Elliott Hughesb465ab02011-08-24 11:21:21 -07002503 // If we're handing out direct pointers, check whether it's a direct pointer
2504 // to a local reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002505 {
2506 ScopedObjectAccess soa(env);
2507 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2508 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
2509 return JNILocalRefType;
2510 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002511 }
2512 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002513 return JNIInvalidRefType;
2514 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002515 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2516 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002517 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002518
2519 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002520 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2521 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002522 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002523 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002524 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2525 return JNI_ERR;
2526 }
2527 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002528 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002529 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2530 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002531 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002532 soa.Self()->ThrowOutOfMemoryError(caller);
2533 }
2534 return okay ? JNI_OK : JNI_ERR;
2535 }
2536
2537 template<typename JniT, typename ArtT>
2538 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002539 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002540 if (length < 0) {
2541 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
2542 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002543 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002544 return soa.AddLocalReference<JniT>(result);
2545 }
2546
2547 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2548 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2549 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002550 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002551 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2552 PinPrimitiveArray(soa, array);
2553 if (is_copy != NULL) {
2554 *is_copy = JNI_FALSE;
2555 }
2556 return array->GetData();
2557 }
2558
2559 template <typename ArrayT>
Ian Rogersef28b142012-11-30 14:22:18 -08002560 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002561 if (mode != JNI_COMMIT) {
Ian Rogersef28b142012-11-30 14:22:18 -08002562 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002563 Array* array = soa.Decode<Array*>(java_array);
2564 UnpinPrimitiveArray(soa, array);
2565 }
2566 }
2567
2568 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2569 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2570 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002571 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002572 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002573 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2574 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2575 ThrowAIOOBE(soa, array, start, length, "src");
2576 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002577 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002578 JavaT* data = array->GetData();
2579 memcpy(buf, data + start, length * sizeof(JavaT));
2580 }
2581 }
2582
2583 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2584 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2585 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002586 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002587 CHECK_NON_NULL_ARGUMENT(SetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002588 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2589 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2590 ThrowAIOOBE(soa, array, start, length, "dst");
2591 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002592 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002593 JavaT* data = array->GetData();
2594 memcpy(data + start, buf, length * sizeof(JavaT));
2595 }
2596 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002597};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002598
Elliott Hughes88c5c352012-03-15 18:49:48 -07002599const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002600 NULL, // reserved0.
2601 NULL, // reserved1.
2602 NULL, // reserved2.
2603 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002604 JNI::GetVersion,
2605 JNI::DefineClass,
2606 JNI::FindClass,
2607 JNI::FromReflectedMethod,
2608 JNI::FromReflectedField,
2609 JNI::ToReflectedMethod,
2610 JNI::GetSuperclass,
2611 JNI::IsAssignableFrom,
2612 JNI::ToReflectedField,
2613 JNI::Throw,
2614 JNI::ThrowNew,
2615 JNI::ExceptionOccurred,
2616 JNI::ExceptionDescribe,
2617 JNI::ExceptionClear,
2618 JNI::FatalError,
2619 JNI::PushLocalFrame,
2620 JNI::PopLocalFrame,
2621 JNI::NewGlobalRef,
2622 JNI::DeleteGlobalRef,
2623 JNI::DeleteLocalRef,
2624 JNI::IsSameObject,
2625 JNI::NewLocalRef,
2626 JNI::EnsureLocalCapacity,
2627 JNI::AllocObject,
2628 JNI::NewObject,
2629 JNI::NewObjectV,
2630 JNI::NewObjectA,
2631 JNI::GetObjectClass,
2632 JNI::IsInstanceOf,
2633 JNI::GetMethodID,
2634 JNI::CallObjectMethod,
2635 JNI::CallObjectMethodV,
2636 JNI::CallObjectMethodA,
2637 JNI::CallBooleanMethod,
2638 JNI::CallBooleanMethodV,
2639 JNI::CallBooleanMethodA,
2640 JNI::CallByteMethod,
2641 JNI::CallByteMethodV,
2642 JNI::CallByteMethodA,
2643 JNI::CallCharMethod,
2644 JNI::CallCharMethodV,
2645 JNI::CallCharMethodA,
2646 JNI::CallShortMethod,
2647 JNI::CallShortMethodV,
2648 JNI::CallShortMethodA,
2649 JNI::CallIntMethod,
2650 JNI::CallIntMethodV,
2651 JNI::CallIntMethodA,
2652 JNI::CallLongMethod,
2653 JNI::CallLongMethodV,
2654 JNI::CallLongMethodA,
2655 JNI::CallFloatMethod,
2656 JNI::CallFloatMethodV,
2657 JNI::CallFloatMethodA,
2658 JNI::CallDoubleMethod,
2659 JNI::CallDoubleMethodV,
2660 JNI::CallDoubleMethodA,
2661 JNI::CallVoidMethod,
2662 JNI::CallVoidMethodV,
2663 JNI::CallVoidMethodA,
2664 JNI::CallNonvirtualObjectMethod,
2665 JNI::CallNonvirtualObjectMethodV,
2666 JNI::CallNonvirtualObjectMethodA,
2667 JNI::CallNonvirtualBooleanMethod,
2668 JNI::CallNonvirtualBooleanMethodV,
2669 JNI::CallNonvirtualBooleanMethodA,
2670 JNI::CallNonvirtualByteMethod,
2671 JNI::CallNonvirtualByteMethodV,
2672 JNI::CallNonvirtualByteMethodA,
2673 JNI::CallNonvirtualCharMethod,
2674 JNI::CallNonvirtualCharMethodV,
2675 JNI::CallNonvirtualCharMethodA,
2676 JNI::CallNonvirtualShortMethod,
2677 JNI::CallNonvirtualShortMethodV,
2678 JNI::CallNonvirtualShortMethodA,
2679 JNI::CallNonvirtualIntMethod,
2680 JNI::CallNonvirtualIntMethodV,
2681 JNI::CallNonvirtualIntMethodA,
2682 JNI::CallNonvirtualLongMethod,
2683 JNI::CallNonvirtualLongMethodV,
2684 JNI::CallNonvirtualLongMethodA,
2685 JNI::CallNonvirtualFloatMethod,
2686 JNI::CallNonvirtualFloatMethodV,
2687 JNI::CallNonvirtualFloatMethodA,
2688 JNI::CallNonvirtualDoubleMethod,
2689 JNI::CallNonvirtualDoubleMethodV,
2690 JNI::CallNonvirtualDoubleMethodA,
2691 JNI::CallNonvirtualVoidMethod,
2692 JNI::CallNonvirtualVoidMethodV,
2693 JNI::CallNonvirtualVoidMethodA,
2694 JNI::GetFieldID,
2695 JNI::GetObjectField,
2696 JNI::GetBooleanField,
2697 JNI::GetByteField,
2698 JNI::GetCharField,
2699 JNI::GetShortField,
2700 JNI::GetIntField,
2701 JNI::GetLongField,
2702 JNI::GetFloatField,
2703 JNI::GetDoubleField,
2704 JNI::SetObjectField,
2705 JNI::SetBooleanField,
2706 JNI::SetByteField,
2707 JNI::SetCharField,
2708 JNI::SetShortField,
2709 JNI::SetIntField,
2710 JNI::SetLongField,
2711 JNI::SetFloatField,
2712 JNI::SetDoubleField,
2713 JNI::GetStaticMethodID,
2714 JNI::CallStaticObjectMethod,
2715 JNI::CallStaticObjectMethodV,
2716 JNI::CallStaticObjectMethodA,
2717 JNI::CallStaticBooleanMethod,
2718 JNI::CallStaticBooleanMethodV,
2719 JNI::CallStaticBooleanMethodA,
2720 JNI::CallStaticByteMethod,
2721 JNI::CallStaticByteMethodV,
2722 JNI::CallStaticByteMethodA,
2723 JNI::CallStaticCharMethod,
2724 JNI::CallStaticCharMethodV,
2725 JNI::CallStaticCharMethodA,
2726 JNI::CallStaticShortMethod,
2727 JNI::CallStaticShortMethodV,
2728 JNI::CallStaticShortMethodA,
2729 JNI::CallStaticIntMethod,
2730 JNI::CallStaticIntMethodV,
2731 JNI::CallStaticIntMethodA,
2732 JNI::CallStaticLongMethod,
2733 JNI::CallStaticLongMethodV,
2734 JNI::CallStaticLongMethodA,
2735 JNI::CallStaticFloatMethod,
2736 JNI::CallStaticFloatMethodV,
2737 JNI::CallStaticFloatMethodA,
2738 JNI::CallStaticDoubleMethod,
2739 JNI::CallStaticDoubleMethodV,
2740 JNI::CallStaticDoubleMethodA,
2741 JNI::CallStaticVoidMethod,
2742 JNI::CallStaticVoidMethodV,
2743 JNI::CallStaticVoidMethodA,
2744 JNI::GetStaticFieldID,
2745 JNI::GetStaticObjectField,
2746 JNI::GetStaticBooleanField,
2747 JNI::GetStaticByteField,
2748 JNI::GetStaticCharField,
2749 JNI::GetStaticShortField,
2750 JNI::GetStaticIntField,
2751 JNI::GetStaticLongField,
2752 JNI::GetStaticFloatField,
2753 JNI::GetStaticDoubleField,
2754 JNI::SetStaticObjectField,
2755 JNI::SetStaticBooleanField,
2756 JNI::SetStaticByteField,
2757 JNI::SetStaticCharField,
2758 JNI::SetStaticShortField,
2759 JNI::SetStaticIntField,
2760 JNI::SetStaticLongField,
2761 JNI::SetStaticFloatField,
2762 JNI::SetStaticDoubleField,
2763 JNI::NewString,
2764 JNI::GetStringLength,
2765 JNI::GetStringChars,
2766 JNI::ReleaseStringChars,
2767 JNI::NewStringUTF,
2768 JNI::GetStringUTFLength,
2769 JNI::GetStringUTFChars,
2770 JNI::ReleaseStringUTFChars,
2771 JNI::GetArrayLength,
2772 JNI::NewObjectArray,
2773 JNI::GetObjectArrayElement,
2774 JNI::SetObjectArrayElement,
2775 JNI::NewBooleanArray,
2776 JNI::NewByteArray,
2777 JNI::NewCharArray,
2778 JNI::NewShortArray,
2779 JNI::NewIntArray,
2780 JNI::NewLongArray,
2781 JNI::NewFloatArray,
2782 JNI::NewDoubleArray,
2783 JNI::GetBooleanArrayElements,
2784 JNI::GetByteArrayElements,
2785 JNI::GetCharArrayElements,
2786 JNI::GetShortArrayElements,
2787 JNI::GetIntArrayElements,
2788 JNI::GetLongArrayElements,
2789 JNI::GetFloatArrayElements,
2790 JNI::GetDoubleArrayElements,
2791 JNI::ReleaseBooleanArrayElements,
2792 JNI::ReleaseByteArrayElements,
2793 JNI::ReleaseCharArrayElements,
2794 JNI::ReleaseShortArrayElements,
2795 JNI::ReleaseIntArrayElements,
2796 JNI::ReleaseLongArrayElements,
2797 JNI::ReleaseFloatArrayElements,
2798 JNI::ReleaseDoubleArrayElements,
2799 JNI::GetBooleanArrayRegion,
2800 JNI::GetByteArrayRegion,
2801 JNI::GetCharArrayRegion,
2802 JNI::GetShortArrayRegion,
2803 JNI::GetIntArrayRegion,
2804 JNI::GetLongArrayRegion,
2805 JNI::GetFloatArrayRegion,
2806 JNI::GetDoubleArrayRegion,
2807 JNI::SetBooleanArrayRegion,
2808 JNI::SetByteArrayRegion,
2809 JNI::SetCharArrayRegion,
2810 JNI::SetShortArrayRegion,
2811 JNI::SetIntArrayRegion,
2812 JNI::SetLongArrayRegion,
2813 JNI::SetFloatArrayRegion,
2814 JNI::SetDoubleArrayRegion,
2815 JNI::RegisterNatives,
2816 JNI::UnregisterNatives,
2817 JNI::MonitorEnter,
2818 JNI::MonitorExit,
2819 JNI::GetJavaVM,
2820 JNI::GetStringRegion,
2821 JNI::GetStringUTFRegion,
2822 JNI::GetPrimitiveArrayCritical,
2823 JNI::ReleasePrimitiveArrayCritical,
2824 JNI::GetStringCritical,
2825 JNI::ReleaseStringCritical,
2826 JNI::NewWeakGlobalRef,
2827 JNI::DeleteWeakGlobalRef,
2828 JNI::ExceptionCheck,
2829 JNI::NewDirectByteBuffer,
2830 JNI::GetDirectBufferAddress,
2831 JNI::GetDirectBufferCapacity,
2832 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002833};
2834
Elliott Hughes75770752011-08-24 17:52:38 -07002835JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002836 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002837 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002838 local_ref_cookie(IRT_FIRST_SEGMENT),
2839 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002840 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002841 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002842 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002843 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002844 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002845 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002846 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002847 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2848 // errors will ensue.
2849 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2850 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002851}
2852
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002853JNIEnvExt::~JNIEnvExt() {
2854}
2855
Elliott Hughes88c5c352012-03-15 18:49:48 -07002856void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2857 check_jni = enabled;
2858 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002859}
2860
Elliott Hughes73e66f72012-05-09 09:34:45 -07002861void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2862 locals.Dump(os);
2863 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002864}
2865
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002866void JNIEnvExt::PushFrame(int /*capacity*/) {
2867 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002868 stacked_local_ref_cookies.push_back(local_ref_cookie);
2869 local_ref_cookie = locals.GetSegmentState();
2870}
2871
2872void JNIEnvExt::PopFrame() {
2873 locals.SetSegmentState(local_ref_cookie);
2874 local_ref_cookie = stacked_local_ref_cookies.back();
2875 stacked_local_ref_cookies.pop_back();
2876}
2877
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002878Offset JNIEnvExt::SegmentStateOffset() {
2879 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2880 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2881}
2882
Carl Shapiroea4dca82011-08-01 13:45:38 -07002883// JNI Invocation interface.
2884
Brian Carlstrombddf9762013-05-14 11:35:37 -07002885extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002886 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002887 if (IsBadJniVersion(args->version)) {
2888 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002889 return JNI_EVERSION;
2890 }
2891 Runtime::Options options;
2892 for (int i = 0; i < args->nOptions; ++i) {
2893 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002894 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002895 }
2896 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002897 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002898 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002899 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002900 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002901 bool started = runtime->Start();
2902 if (!started) {
2903 delete Thread::Current()->GetJniEnv();
2904 delete runtime->GetJavaVM();
2905 LOG(WARNING) << "CreateJavaVM failed";
2906 return JNI_ERR;
2907 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002908 *p_env = Thread::Current()->GetJniEnv();
2909 *p_vm = runtime->GetJavaVM();
2910 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002911}
2912
Elliott Hughesf2682d52011-08-15 16:37:04 -07002913extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002914 Runtime* runtime = Runtime::Current();
2915 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002916 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002917 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002918 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002919 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002920 }
2921 return JNI_OK;
2922}
2923
2924// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002925extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002926 return JNI_ERR;
2927}
2928
Elliott Hughescdf53122011-08-19 15:46:09 -07002929class JII {
2930 public:
2931 static jint DestroyJavaVM(JavaVM* vm) {
2932 if (vm == NULL) {
2933 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002934 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002935 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2936 delete raw_vm->runtime;
2937 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002938 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002939
Elliott Hughescdf53122011-08-19 15:46:09 -07002940 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002941 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002942 }
2943
2944 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002945 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002946 }
2947
2948 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002949 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002950 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002951 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002952 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2953 Runtime* runtime = raw_vm->runtime;
2954 runtime->DetachCurrentThread();
2955 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002956 }
2957
2958 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07002959 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
2960 // and unlike other calls that take a JNI version doesn't care if you supply
2961 // JNI_VERSION_1_1, which we don't otherwise support.
2962 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07002963 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07002964 return JNI_EVERSION;
2965 }
2966 if (vm == NULL || env == NULL) {
2967 return JNI_ERR;
2968 }
2969 Thread* thread = Thread::Current();
2970 if (thread == NULL) {
2971 *env = NULL;
2972 return JNI_EDETACHED;
2973 }
2974 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002975 return JNI_OK;
2976 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002977};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002978
Elliott Hughes88c5c352012-03-15 18:49:48 -07002979const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002980 NULL, // reserved0
2981 NULL, // reserved1
2982 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002983 JII::DestroyJavaVM,
2984 JII::AttachCurrentThread,
2985 JII::DetachCurrentThread,
2986 JII::GetEnv,
2987 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002988};
2989
Elliott Hughesa0957642011-09-02 14:27:33 -07002990JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002991 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002992 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002993 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002994 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002995 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002996 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002997 work_around_app_jni_bugs(false),
Ian Rogers62d6c772013-02-27 08:32:07 -08002998 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002999 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07003000 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07003001 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003002 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003003 libraries(new Libraries),
3004 weak_globals_lock_("JNI weak global reference table lock"),
3005 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
3006 allow_new_weak_globals_(true),
3007 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003008 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003009 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003010 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003011 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003012}
3013
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003014JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003015 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003016}
3017
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003018jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3019 if (obj == nullptr) {
3020 return nullptr;
3021 }
3022 MutexLock mu(self, weak_globals_lock_);
3023 while (UNLIKELY(!allow_new_weak_globals_)) {
3024 weak_globals_add_condition_.WaitHoldingLocks(self);
3025 }
3026 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3027 return reinterpret_cast<jweak>(ref);
3028}
3029
3030void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3031 MutexLock mu(self, weak_globals_lock_);
3032 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3033 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3034 << "failed to find entry";
3035 }
3036}
3037
Elliott Hughes88c5c352012-03-15 18:49:48 -07003038void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3039 check_jni = enabled;
3040 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003041}
3042
Elliott Hughesae80b492012-04-24 10:43:17 -07003043void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3044 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3045 if (force_copy) {
3046 os << " (with forcecopy)";
3047 }
3048 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07003049 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003050 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003051 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003052 os << "; pins=" << pin_table.Size();
3053 }
3054 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003055 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003056 os << "; globals=" << globals.Capacity();
3057 }
3058 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003059 MutexLock mu(self, weak_globals_lock_);
3060 if (weak_globals_.Capacity() > 0) {
3061 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003062 }
3063 }
3064 os << '\n';
3065
3066 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003067 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003068 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3069 }
3070}
3071
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003072void JavaVMExt::DisallowNewWeakGlobals() {
3073 MutexLock mu(Thread::Current(), weak_globals_lock_);
3074 allow_new_weak_globals_ = false;
3075}
3076
3077void JavaVMExt::AllowNewWeakGlobals() {
3078 Thread* self = Thread::Current();
3079 MutexLock mu(self, weak_globals_lock_);
3080 allow_new_weak_globals_ = true;
3081 weak_globals_add_condition_.Broadcast(self);
3082}
3083
3084void JavaVMExt::SweepWeakGlobals(IsMarkedTester is_marked, void* arg) {
3085 MutexLock mu(Thread::Current(), weak_globals_lock_);
3086 for (const Object** entry : weak_globals_) {
3087 if (!is_marked(*entry, arg)) {
3088 *entry = kClearedJniWeakGlobal;
3089 }
3090 }
3091}
3092
3093mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3094 MutexLock mu(self, weak_globals_lock_);
3095 while (UNLIKELY(!allow_new_weak_globals_)) {
3096 weak_globals_add_condition_.WaitHoldingLocks(self);
3097 }
3098 return const_cast<mirror::Object*>(weak_globals_.Get(ref));
3099}
3100
Elliott Hughes73e66f72012-05-09 09:34:45 -07003101void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003102 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003103 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003104 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003105 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003106 }
3107 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003108 MutexLock mu(self, weak_globals_lock_);
3109 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003110 }
3111 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003112 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003113 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003114 }
3115}
3116
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003117bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
3118 std::string& detail) {
Elliott Hughes75770752011-08-24 17:52:38 -07003119 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003120
3121 // See if we've already loaded this library. If we have, and the class loader
3122 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003123 // TODO: for better results we should canonicalize the pathname (or even compare
3124 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003125 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003126 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003127 {
3128 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003129 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003130 library = libraries->Get(path);
3131 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003132 if (library != NULL) {
3133 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07003134 // The library will be associated with class_loader. The JNI
3135 // spec says we can't load the same library into more than one
3136 // class loader.
3137 StringAppendF(&detail, "Shared library \"%s\" already opened by "
3138 "ClassLoader %p; can't open in ClassLoader %p",
3139 path.c_str(), library->GetClassLoader(), class_loader);
3140 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003141 return false;
3142 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003143 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
3144 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003145 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07003146 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
3147 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003148 return false;
3149 }
3150 return true;
3151 }
3152
3153 // Open the shared library. Because we're using a full path, the system
3154 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3155 // resolve this library's dependencies though.)
3156
3157 // Failures here are expected when java.library.path has several entries
3158 // and we have to hunt for the lib.
3159
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003160 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3161 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3162 // dlopen) becomes zero from dlclose.
3163
Elliott Hughescdf53122011-08-19 15:46:09 -07003164 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003165 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003166 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
3167 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
3168 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003169
Elliott Hughes84b2f142012-09-27 09:16:28 -07003170 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003171
3172 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07003173 detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07003174 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003175 return false;
3176 }
3177
3178 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003179 // TODO: move the locking (and more of this logic) into Libraries.
3180 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003181 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003182 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003183 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003184 if (library == NULL) { // We won race to get libraries_lock
3185 library = new SharedLibrary(path, handle, class_loader);
3186 libraries->Put(path, library);
3187 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003188 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003189 }
3190 if (!created_library) {
3191 LOG(INFO) << "WOW: we lost a race to add shared library: "
3192 << "\"" << path << "\" ClassLoader=" << class_loader;
3193 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003194 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003195
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003196 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003197
Elliott Hughes79353722013-08-02 16:52:18 -07003198 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003199 void* sym = dlsym(handle, "JNI_OnLoad");
3200 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003201 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003202 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003203 } else {
3204 // Call JNI_OnLoad. We have to override the current class
3205 // loader, which will always be "null" since the stuff at the
3206 // top of the stack is around Runtime.loadLibrary(). (See
3207 // the comments in the JNI FindClass function.)
3208 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3209 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07003210 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07003211 self->SetClassLoaderOverride(class_loader);
3212
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003213 int version = 0;
3214 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003215 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003216 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003217 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07003218 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003219
Brian Carlstromaded5f72011-10-07 17:15:04 -07003220 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07003221
Elliott Hughes79353722013-08-02 16:52:18 -07003222 if (version == JNI_ERR) {
3223 StringAppendF(&detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
3224 } else if (IsBadJniVersion(version)) {
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003225 StringAppendF(&detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
3226 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003227 // It's unwise to call dlclose() here, but we can mark it
3228 // as bad and ensure that future load attempts will fail.
3229 // We don't know how far JNI_OnLoad got, so there could
3230 // be some partially-initialized stuff accessible through
3231 // newly-registered native method calls. We could try to
3232 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003233 } else {
3234 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003235 }
Elliott Hughes79353722013-08-02 16:52:18 -07003236 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003237 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003238 }
3239
Elliott Hughes79353722013-08-02 16:52:18 -07003240 library->SetResult(was_successful);
3241 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003242}
3243
Brian Carlstromea46f952013-07-30 01:26:50 -07003244void* JavaVMExt::FindCodeForNativeMethod(ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003245 CHECK(m->IsNative());
3246
3247 Class* c = m->GetDeclaringClass();
3248
3249 // If this is a static method, it could be called before the class
3250 // has been initialized.
3251 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07003252 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003253 return NULL;
3254 }
3255 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003256 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003257 }
3258
Brian Carlstrom16192862011-09-12 17:50:06 -07003259 std::string detail;
3260 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003261 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003262 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003263 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003264 native_method = libraries->FindNativeMethod(m, detail);
3265 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003266 // Throwing can cause libraries_lock to be reacquired.
Brian Carlstrom16192862011-09-12 17:50:06 -07003267 if (native_method == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003268 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3269 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003270 }
3271 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003272}
3273
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003274void JavaVMExt::VisitRoots(RootVisitor* visitor, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003275 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003276 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003277 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003278 globals.VisitRoots(visitor, arg);
3279 }
3280 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003281 MutexLock mu(self, pins_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003282 pin_table.VisitRoots(visitor, arg);
3283 }
3284 // The weak_globals table is visited by the GC itself (because it mutates the table).
3285}
3286
Elliott Hughesc8fece32013-01-02 11:27:23 -08003287void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003288 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003289 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
3290 if (c.get() == NULL) {
3291 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3292 }
3293 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3294}
3295
Ian Rogersdf20fe02011-07-20 20:34:16 -07003296} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003297
3298std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3299 switch (rhs) {
3300 case JNIInvalidRefType:
3301 os << "JNIInvalidRefType";
3302 return os;
3303 case JNILocalRefType:
3304 os << "JNILocalRefType";
3305 return os;
3306 case JNIGlobalRefType:
3307 os << "JNIGlobalRefType";
3308 return os;
3309 case JNIWeakGlobalRefType:
3310 os << "JNIWeakGlobalRefType";
3311 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003312 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003313 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003314 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003315 }
3316}