blob: ec717c1bee5ccde31003e74e9b8938352b538a95 [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 Rogersd91d6d62013-09-25 20:26:14 -0700231 DCHECK(c != nullptr);
Ian Rogers0045a292012-03-31 21:08:41 -0700232 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700233 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700234 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700235
Brian Carlstromea46f952013-07-30 01:26:50 -0700236 ArtMethod* method = NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700237 if (is_static) {
238 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700239 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700240 method = c->FindVirtualMethod(name, sig);
241 if (method == NULL) {
242 // No virtual method matching the signature. Search declared
243 // private methods and constructors.
244 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700245 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700246 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700247
Elliott Hughescdf53122011-08-19 15:46:09 -0700248 if (method == NULL || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700249 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700250 return NULL;
251 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700252
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700253 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700254}
255
Ian Rogersef28b142012-11-30 14:22:18 -0800256static ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700257 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700258 ArtMethod* method = soa.Self()->GetCurrentMethod(NULL);
Brian Carlstromce888532013-10-10 00:32:58 -0700259 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
260 if (method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Ian Rogersef28b142012-11-30 14:22:18 -0800261 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700262 }
Brian Carlstromce888532013-10-10 00:32:58 -0700263 // If we have a method, use its ClassLoader for context.
264 if (method != NULL) {
265 return method->GetDeclaringClass()->GetClassLoader();
266 }
267 // We don't have a method, so try to use the system ClassLoader.
268 ClassLoader* class_loader = soa.Decode<ClassLoader*>(Runtime::Current()->GetSystemClassLoader());
269 if (class_loader != NULL) {
270 return class_loader;
271 }
272 // See if the override ClassLoader is set for gtests.
273 class_loader = soa.Self()->GetClassLoaderOverride();
274 if (class_loader != NULL) {
275 // If so, CommonTest should have set UseCompileTimeClassPath.
276 CHECK(Runtime::Current()->UseCompileTimeClassPath());
277 return class_loader;
278 }
279 // Use the BOOTCLASSPATH.
280 return NULL;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700281}
282
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700283static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
284 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700285 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700286 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700287 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700288 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700289 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700290
Brian Carlstromea46f952013-07-30 01:26:50 -0700291 ArtField* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700292 Class* field_type;
293 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
294 if (sig[1] != '\0') {
Ian Rogersef28b142012-11-30 14:22:18 -0800295 ClassLoader* cl = GetClassLoader(soa);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700296 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700297 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700298 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700299 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700300 if (field_type == NULL) {
301 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700302 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800303 ThrowLocation throw_location;
Brian Carlstromea46f952013-07-30 01:26:50 -0700304 SirtRef<Throwable> cause(soa.Self(), soa.Self()->GetException(&throw_location));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700305 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800306 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
307 "no type \"%s\" found and so no field \"%s\" could be found in class "
308 "\"%s\" or its superclasses", sig, name,
309 ClassHelper(c).GetDescriptor());
310 soa.Self()->GetException(NULL)->SetCause(cause.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700311 return NULL;
312 }
313 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800314 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700315 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800316 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700317 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700318 if (field == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800319 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
320 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
321 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
322 sig, name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700323 return NULL;
324 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700325 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700326}
327
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700328static void PinPrimitiveArray(const ScopedObjectAccess& soa, Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700329 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700330 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700331 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700332 vm->pin_table.Add(array);
333}
334
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700335static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700336 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700337 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700338 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700339 vm->pin_table.Remove(array);
340}
341
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700342static void ThrowAIOOBE(ScopedObjectAccess& soa, Array* array, jsize start,
343 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700344 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700345 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800346 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
347 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
348 "%s offset=%d length=%d %s.length=%d",
349 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700350}
Ian Rogers0571d352011-11-03 19:51:38 -0700351
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700352static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
353 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700354 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800355 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
356 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
357 "offset=%d length=%d string.length()=%d", start, length,
358 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700359}
Elliott Hughes814e4032011-08-23 12:07:56 -0700360
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700361int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700362 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700363 // Turn the const char* into a java.lang.String.
364 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
365 if (msg != NULL && s.get() == NULL) {
366 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700367 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700368
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700369 // Choose an appropriate constructor and set up the arguments.
370 jvalue args[2];
371 const char* signature;
372 if (msg == NULL && cause == NULL) {
373 signature = "()V";
374 } else if (msg != NULL && cause == NULL) {
375 signature = "(Ljava/lang/String;)V";
376 args[0].l = s.get();
377 } else if (msg == NULL && cause != NULL) {
378 signature = "(Ljava/lang/Throwable;)V";
379 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700380 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700381 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
382 args[0].l = s.get();
383 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700384 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700385 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
386 if (mid == NULL) {
Ian Rogersef28b142012-11-30 14:22:18 -0800387 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700388 LOG(ERROR) << "No <init>" << signature << " in "
389 << PrettyClass(soa.Decode<Class*>(exception_class));
390 return JNI_ERR;
391 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700392
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700393 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
394 if (exception.get() == NULL) {
395 return JNI_ERR;
396 }
Ian Rogersef28b142012-11-30 14:22:18 -0800397 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800398 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
399 soa.Self()->SetException(throw_location, soa.Decode<Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700400 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700401}
402
Elliott Hughes462c9442012-03-23 18:47:50 -0700403static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700404 if (vm == NULL || p_env == NULL) {
405 return JNI_ERR;
406 }
407
Elliott Hughes462c9442012-03-23 18:47:50 -0700408 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700409 Thread* self = Thread::Current();
410 if (self != NULL) {
411 *p_env = self->GetJniEnv();
412 return JNI_OK;
413 }
414
415 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
416
417 // No threads allowed in zygote mode.
418 if (runtime->IsZygote()) {
419 LOG(ERROR) << "Attempt to attach a thread in the zygote";
420 return JNI_ERR;
421 }
422
Elliott Hughes462c9442012-03-23 18:47:50 -0700423 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
424 const char* thread_name = NULL;
Ian Rogers365c1022012-06-22 15:05:28 -0700425 jobject thread_group = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -0700426 if (args != NULL) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700427 if (IsBadJniVersion(args->version)) {
428 LOG(ERROR) << "Bad JNI version passed to "
429 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
430 << args->version;
431 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700432 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700433 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700434 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700435 }
Elliott Hughes75770752011-08-24 17:52:38 -0700436
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800437 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700438 *p_env = NULL;
439 return JNI_ERR;
440 } else {
441 *p_env = Thread::Current()->GetJniEnv();
442 return JNI_OK;
443 }
Elliott Hughes75770752011-08-24 17:52:38 -0700444}
445
Elliott Hughes79082e32011-08-25 12:07:32 -0700446class SharedLibrary {
447 public:
448 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
449 : path_(path),
450 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700451 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700452 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700453 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700454 jni_on_load_thread_id_(Thread::Current()->GetThreadId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700455 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700456 }
457
Elliott Hughes79082e32011-08-25 12:07:32 -0700458 Object* GetClassLoader() {
459 return class_loader_;
460 }
461
462 std::string GetPath() {
463 return path_;
464 }
465
466 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700467 * Check the result of an earlier call to JNI_OnLoad on this library.
468 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700469 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700470 bool CheckOnLoadResult()
471 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700472 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700473 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700474 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
475 bool okay;
476 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700477 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700478
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700479 if (jni_on_load_thread_id_ == self->GetThreadId()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700480 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
481 // caller can continue.
482 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
483 okay = true;
484 } else {
485 while (jni_on_load_result_ == kPending) {
486 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700487 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700488 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700489
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700490 okay = (jni_on_load_result_ == kOkay);
491 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
492 << (okay ? "succeeded" : "failed") << "]";
493 }
494 }
495 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700496 return okay;
497 }
498
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700499 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700500 Thread* self = Thread::Current();
501 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700502
Elliott Hughes79082e32011-08-25 12:07:32 -0700503 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700504 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700505
506 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700507 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700508 }
509
510 void* FindSymbol(const std::string& symbol_name) {
511 return dlsym(handle_, symbol_name.c_str());
512 }
513
514 private:
515 enum JNI_OnLoadState {
516 kPending,
517 kFailed,
518 kOkay,
519 };
520
521 // Path to library "/system/lib/libjni.so".
522 std::string path_;
523
524 // The void* returned by dlopen(3).
525 void* handle_;
526
527 // The ClassLoader this library is associated with.
528 Object* class_loader_;
529
530 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700531 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700532 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700533 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700534 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700535 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700536 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700537 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700538};
539
Elliott Hughes79082e32011-08-25 12:07:32 -0700540// This exists mainly to keep implementation details out of the header file.
541class Libraries {
542 public:
543 Libraries() {
544 }
545
546 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700547 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700548 }
549
Elliott Hughesae80b492012-04-24 10:43:17 -0700550 void Dump(std::ostream& os) const {
551 bool first = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700552 for (const auto& library : libraries_) {
Elliott Hughesae80b492012-04-24 10:43:17 -0700553 if (!first) {
554 os << ' ';
555 }
556 first = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700557 os << library.first;
Elliott Hughesae80b492012-04-24 10:43:17 -0700558 }
559 }
560
561 size_t size() const {
562 return libraries_.size();
563 }
564
Elliott Hughes79082e32011-08-25 12:07:32 -0700565 SharedLibrary* Get(const std::string& path) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700566 auto it = libraries_.find(path);
Ian Rogers712462a2012-04-12 16:32:29 -0700567 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700568 }
569
570 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700571 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700572 }
573
574 // See section 11.3 "Linking Native Methods" of the JNI spec.
Brian Carlstromea46f952013-07-30 01:26:50 -0700575 void* FindNativeMethod(const ArtMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700576 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700577 std::string jni_short_name(JniShortName(m));
578 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700579 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700580 for (const auto& lib : libraries_) {
581 SharedLibrary* library = lib.second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700582 if (library->GetClassLoader() != declaring_class_loader) {
583 // We only search libraries loaded by the appropriate ClassLoader.
584 continue;
585 }
586 // Try the short name then the long name...
587 void* fn = library->FindSymbol(jni_short_name);
588 if (fn == NULL) {
589 fn = library->FindSymbol(jni_long_name);
590 }
591 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800592 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
593 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700594 return fn;
595 }
596 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700597 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700598 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700599 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700600 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700601 return NULL;
602 }
603
604 private:
Elliott Hughesa0e18062012-04-13 15:59:59 -0700605 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700606};
607
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700608JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
609 jvalue* args) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700610 ArtMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700611 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700612 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800613 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700614 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800615 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700616 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
617 return result;
Elliott Hughesd07986f2011-12-06 18:27:45 -0800618}
619
Ian Rogersbc939662013-08-15 10:26:54 -0700620#define CHECK_NON_NULL_ARGUMENT(fn, value) \
621 if (UNLIKELY(value == NULL)) { \
622 JniAbortF(#fn, #value " == null"); \
623 }
624
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700625#define CHECK_NON_NULL_MEMCPY_ARGUMENT(fn, length, value) \
626 if (UNLIKELY(length != 0 && value == NULL)) { \
627 JniAbortF(#fn, #value " == null"); \
628 }
629
Elliott Hughescdf53122011-08-19 15:46:09 -0700630class JNI {
631 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700632 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700633 return JNI_VERSION_1_6;
634 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700635
Ian Rogers25e8b912012-09-07 11:31:36 -0700636 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700637 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700638 return NULL;
639 }
640
Elliott Hughescdf53122011-08-19 15:46:09 -0700641 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogersbc939662013-08-15 10:26:54 -0700642 CHECK_NON_NULL_ARGUMENT(FindClass, name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700643 Runtime* runtime = Runtime::Current();
644 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700645 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700646 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700647 Class* c = NULL;
648 if (runtime->IsStarted()) {
Ian Rogersef28b142012-11-30 14:22:18 -0800649 ClassLoader* cl = GetClassLoader(soa);
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800650 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700651 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800652 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700653 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700654 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700655 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700656
Elliott Hughescdf53122011-08-19 15:46:09 -0700657 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogersbc939662013-08-15 10:26:54 -0700658 CHECK_NON_NULL_ARGUMENT(FromReflectedMethod, java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700659 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700660 jobject art_method = env->GetObjectField(java_method,
661 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod);
662 ArtMethod* method = soa.Decode<ArtMethod*>(art_method);
663 DCHECK(method != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700664 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700665 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700666
Elliott Hughescdf53122011-08-19 15:46:09 -0700667 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogersbc939662013-08-15 10:26:54 -0700668 CHECK_NON_NULL_ARGUMENT(FromReflectedField, java_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700669 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700670 jobject art_field = env->GetObjectField(java_field,
671 WellKnownClasses::java_lang_reflect_Field_artField);
672 ArtField* field = soa.Decode<ArtField*>(art_field);
673 DCHECK(field != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700674 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700675 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700676
Elliott Hughescdf53122011-08-19 15:46:09 -0700677 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700678 CHECK_NON_NULL_ARGUMENT(ToReflectedMethod, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700679 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700680 ArtMethod* m = soa.DecodeMethod(mid);
681 jobject art_method = soa.AddLocalReference<jobject>(m);
682 jobject reflect_method = env->AllocObject(WellKnownClasses::java_lang_reflect_Method);
683 if (env->ExceptionCheck()) {
684 return NULL;
685 }
686 SetObjectField(env,
687 reflect_method,
688 WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod,
689 art_method);
690 return reflect_method;
Elliott Hughescdf53122011-08-19 15:46:09 -0700691 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700692
Elliott Hughescdf53122011-08-19 15:46:09 -0700693 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogersbc939662013-08-15 10:26:54 -0700694 CHECK_NON_NULL_ARGUMENT(ToReflectedField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700695 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -0700696 ArtField* f = soa.DecodeField(fid);
697 jobject art_field = soa.AddLocalReference<jobject>(f);
698 jobject reflect_field = env->AllocObject(WellKnownClasses::java_lang_reflect_Field);
699 if (env->ExceptionCheck()) {
700 return NULL;
701 }
702 SetObjectField(env,
703 reflect_field,
704 WellKnownClasses::java_lang_reflect_Field_artField,
705 art_field);
706 return reflect_field;
Elliott Hughescdf53122011-08-19 15:46:09 -0700707 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700708
Elliott Hughes37f7a402011-08-22 18:56:01 -0700709 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -0700710 CHECK_NON_NULL_ARGUMENT(GetObjectClass, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700711 ScopedObjectAccess soa(env);
712 Object* o = soa.Decode<Object*>(java_object);
713 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700714 }
715
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700716 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700717 CHECK_NON_NULL_ARGUMENT(GetSuperclass, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700718 ScopedObjectAccess soa(env);
719 Class* c = soa.Decode<Class*>(java_class);
720 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700721 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700722
Elliott Hughes37f7a402011-08-22 18:56:01 -0700723 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogersbc939662013-08-15 10:26:54 -0700724 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class1);
725 CHECK_NON_NULL_ARGUMENT(IsAssignableFrom, java_class2);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700726 ScopedObjectAccess soa(env);
727 Class* c1 = soa.Decode<Class*>(java_class1);
728 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700729 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700730 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700731
Elliott Hughese84278b2012-03-22 10:06:53 -0700732 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700733 CHECK_NON_NULL_ARGUMENT(IsInstanceOf, java_class);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700734 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700735 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700736 return JNI_TRUE;
737 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700738 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700739 Object* obj = soa.Decode<Object*>(jobj);
740 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700741 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700742 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700743 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700744
Elliott Hughes37f7a402011-08-22 18:56:01 -0700745 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700746 ScopedObjectAccess soa(env);
747 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700748 if (exception == NULL) {
749 return JNI_ERR;
750 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800751 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
752 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700753 return JNI_OK;
754 }
755
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700756 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogersbc939662013-08-15 10:26:54 -0700757 CHECK_NON_NULL_ARGUMENT(ThrowNew, c);
Elliott Hughesa4f94742012-05-29 16:28:38 -0700758 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700759 }
760
761 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700762 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700763 }
764
765 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700766 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700767 }
768
769 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700770 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700771
Brian Carlstromea46f952013-07-30 01:26:50 -0700772 SirtRef<Object> old_throw_this_object(soa.Self(), NULL);
773 SirtRef<ArtMethod> old_throw_method(soa.Self(), NULL);
774 SirtRef<Throwable> old_exception(soa.Self(), NULL);
Ian Rogers62d6c772013-02-27 08:32:07 -0800775 uint32_t old_throw_dex_pc;
776 {
777 ThrowLocation old_throw_location;
Brian Carlstromea46f952013-07-30 01:26:50 -0700778 Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
Ian Rogers62d6c772013-02-27 08:32:07 -0800779 old_throw_this_object.reset(old_throw_location.GetThis());
780 old_throw_method.reset(old_throw_location.GetMethod());
781 old_exception.reset(old_exception_obj);
782 old_throw_dex_pc = old_throw_location.GetDexPc();
783 soa.Self()->ClearException();
784 }
785 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(old_exception.get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700786 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
787 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
788 if (mid == NULL) {
789 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Ian Rogers62d6c772013-02-27 08:32:07 -0800790 << PrettyTypeOf(old_exception.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700791 } else {
792 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800793 if (soa.Self()->IsExceptionPending()) {
794 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(NULL))
Elliott Hughes72025e52011-08-23 17:50:30 -0700795 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800796 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700797 }
798 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800799 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
800 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700801
Ian Rogers62d6c772013-02-27 08:32:07 -0800802 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700803 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700804
Elliott Hughescdf53122011-08-19 15:46:09 -0700805 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700806 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800807 Object* exception = soa.Self()->GetException(NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700808 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700809 }
810
Ian Rogers25e8b912012-09-07 11:31:36 -0700811 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700812 LOG(FATAL) << "JNI FatalError called: " << msg;
813 }
814
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700815 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800816 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700817 return JNI_ERR;
818 }
Ian Rogersef28b142012-11-30 14:22:18 -0800819 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700820 return JNI_OK;
821 }
822
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700823 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700824 ScopedObjectAccess soa(env);
825 Object* survivor = soa.Decode<Object*>(java_survivor);
826 soa.Env()->PopFrame();
827 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700828 }
829
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700830 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800831 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700832 }
833
Elliott Hughescdf53122011-08-19 15:46:09 -0700834 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700835 if (obj == NULL) {
836 return NULL;
837 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700838 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700839 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700840 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700841 Object* decoded_obj = soa.Decode<Object*>(obj);
Ian Rogersb8a0b942013-08-20 18:09:52 -0700842 WriterMutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700843 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700844 return reinterpret_cast<jobject>(ref);
845 }
846
847 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700848 if (obj == NULL) {
849 return;
850 }
Ian Rogersef28b142012-11-30 14:22:18 -0800851 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700852 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800853 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogersb8a0b942013-08-20 18:09:52 -0700854 WriterMutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700855
856 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
857 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
858 << "failed to find entry";
859 }
860 }
861
862 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700863 ScopedObjectAccess soa(env);
864 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700865 }
866
867 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700868 if (obj != nullptr) {
869 ScopedObjectAccess soa(env);
870 soa.Vm()->DeleteWeakGlobalRef(soa.Self(), obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700871 }
872 }
873
874 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700875 if (obj == NULL) {
876 return NULL;
877 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700878 ScopedObjectAccess soa(env);
Elliott Hughes9dcd45c2013-07-29 14:40:52 -0700879 return soa.AddLocalReference<jobject>(soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700880 }
881
882 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700883 if (obj == NULL) {
884 return;
885 }
Ian Rogersef28b142012-11-30 14:22:18 -0800886 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700887
Ian Rogersef28b142012-11-30 14:22:18 -0800888 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700889 if (!locals.Remove(cookie, obj)) {
890 // Attempting to delete a local reference that is not in the
891 // topmost local reference frame is a no-op. DeleteLocalRef returns
892 // void and doesn't throw any exceptions, but we should probably
893 // complain about it so the user will notice that things aren't
894 // going quite the way they expect.
895 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
896 << "failed to find entry";
897 }
898 }
899
900 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700901 if (obj1 == obj2) {
902 return JNI_TRUE;
903 } else {
904 ScopedObjectAccess soa(env);
905 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
906 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700907 }
908
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700909 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -0700910 CHECK_NON_NULL_ARGUMENT(AllocObject, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700911 ScopedObjectAccess soa(env);
912 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700913 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700914 return NULL;
915 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700916 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700917 }
918
Ian Rogersbc939662013-08-15 10:26:54 -0700919 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700920 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700921 va_start(args, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700922 CHECK_NON_NULL_ARGUMENT(NewObject, java_class);
923 CHECK_NON_NULL_ARGUMENT(NewObject, mid);
924 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700925 va_end(args);
926 return result;
927 }
928
Elliott Hughes72025e52011-08-23 17:50:30 -0700929 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700930 CHECK_NON_NULL_ARGUMENT(NewObjectV, java_class);
931 CHECK_NON_NULL_ARGUMENT(NewObjectV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700932 ScopedObjectAccess soa(env);
933 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700934 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700935 return NULL;
936 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700937 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700938 if (result == NULL) {
939 return NULL;
940 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700941 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700942 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700943 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700944 return local_result;
945 } else {
946 return NULL;
947 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700948 }
949
Elliott Hughes72025e52011-08-23 17:50:30 -0700950 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -0700951 CHECK_NON_NULL_ARGUMENT(NewObjectA, java_class);
952 CHECK_NON_NULL_ARGUMENT(NewObjectA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700953 ScopedObjectAccess soa(env);
954 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700955 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700956 return NULL;
957 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700958 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700959 if (result == NULL) {
960 return NULL;
961 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700962 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700963 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700964 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700965 return local_result;
966 } else {
967 return NULL;
968 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700969 }
970
Ian Rogersbc939662013-08-15 10:26:54 -0700971 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
972 CHECK_NON_NULL_ARGUMENT(GetMethodID, java_class);
973 CHECK_NON_NULL_ARGUMENT(GetMethodID, name);
974 CHECK_NON_NULL_ARGUMENT(GetMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700975 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700976 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700977 }
978
Ian Rogersbc939662013-08-15 10:26:54 -0700979 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
980 const char* sig) {
981 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, java_class);
982 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, name);
983 CHECK_NON_NULL_ARGUMENT(GetStaticMethodID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700984 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700985 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 }
987
Elliott Hughes72025e52011-08-23 17:50:30 -0700988 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700989 va_list ap;
990 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700991 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, obj);
992 CHECK_NON_NULL_ARGUMENT(CallObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700993 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700994 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700995 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700996 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700997 }
998
Elliott Hughes72025e52011-08-23 17:50:30 -0700999 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001000 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, obj);
1001 CHECK_NON_NULL_ARGUMENT(CallObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001002 ScopedObjectAccess soa(env);
1003 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
1004 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001005 }
1006
Elliott Hughes72025e52011-08-23 17:50:30 -07001007 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001008 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, obj);
1009 CHECK_NON_NULL_ARGUMENT(CallObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001010 ScopedObjectAccess soa(env);
1011 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
1012 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001013 }
1014
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001016 va_list ap;
1017 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001018 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, obj);
1019 CHECK_NON_NULL_ARGUMENT(CallBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001020 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001021 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001023 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001024 }
1025
Elliott Hughes72025e52011-08-23 17:50:30 -07001026 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001027 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, obj);
1028 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001029 ScopedObjectAccess soa(env);
1030 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001031 }
1032
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001034 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, obj);
1035 CHECK_NON_NULL_ARGUMENT(CallBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001036 ScopedObjectAccess soa(env);
1037 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001038 }
1039
Elliott Hughes72025e52011-08-23 17:50:30 -07001040 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001041 va_list ap;
1042 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001043 CHECK_NON_NULL_ARGUMENT(CallByteMethod, obj);
1044 CHECK_NON_NULL_ARGUMENT(CallByteMethod, mid);
1045 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001046 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001048 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 }
1050
Elliott Hughes72025e52011-08-23 17:50:30 -07001051 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001052 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, obj);
1053 CHECK_NON_NULL_ARGUMENT(CallByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001054 ScopedObjectAccess soa(env);
1055 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001056 }
1057
Elliott Hughes72025e52011-08-23 17:50:30 -07001058 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001059 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, obj);
1060 CHECK_NON_NULL_ARGUMENT(CallByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001061 ScopedObjectAccess soa(env);
1062 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001063 }
1064
Elliott Hughes72025e52011-08-23 17:50:30 -07001065 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 va_list ap;
1067 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001068 CHECK_NON_NULL_ARGUMENT(CallCharMethod, obj);
1069 CHECK_NON_NULL_ARGUMENT(CallCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001070 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001071 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001072 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001073 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001074 }
1075
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001077 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, obj);
1078 CHECK_NON_NULL_ARGUMENT(CallCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001079 ScopedObjectAccess soa(env);
1080 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 }
1082
Elliott Hughes72025e52011-08-23 17:50:30 -07001083 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001084 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, obj);
1085 CHECK_NON_NULL_ARGUMENT(CallCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001086 ScopedObjectAccess soa(env);
1087 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 }
1089
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001091 va_list ap;
1092 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001093 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, obj);
1094 CHECK_NON_NULL_ARGUMENT(CallDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001095 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001096 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001097 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001098 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001099 }
1100
Elliott Hughes72025e52011-08-23 17:50:30 -07001101 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001102 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, obj);
1103 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001104 ScopedObjectAccess soa(env);
1105 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 }
1107
Elliott Hughes72025e52011-08-23 17:50:30 -07001108 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001109 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, obj);
1110 CHECK_NON_NULL_ARGUMENT(CallDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001111 ScopedObjectAccess soa(env);
1112 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 }
1114
Elliott Hughes72025e52011-08-23 17:50:30 -07001115 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001116 va_list ap;
1117 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001118 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, obj);
1119 CHECK_NON_NULL_ARGUMENT(CallFloatMethod, mid);
1120 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001121 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001122 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001123 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 }
1125
Elliott Hughes72025e52011-08-23 17:50:30 -07001126 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001127 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, obj);
1128 CHECK_NON_NULL_ARGUMENT(CallFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001129 ScopedObjectAccess soa(env);
1130 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001131 }
1132
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001134 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, obj);
1135 CHECK_NON_NULL_ARGUMENT(CallFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001136 ScopedObjectAccess soa(env);
1137 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001138 }
1139
Elliott Hughes72025e52011-08-23 17:50:30 -07001140 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001141 va_list ap;
1142 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001143 CHECK_NON_NULL_ARGUMENT(CallIntMethod, obj);
1144 CHECK_NON_NULL_ARGUMENT(CallIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001145 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001146 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001148 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001149 }
1150
Elliott Hughes72025e52011-08-23 17:50:30 -07001151 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001152 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, obj);
1153 CHECK_NON_NULL_ARGUMENT(CallIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001154 ScopedObjectAccess soa(env);
1155 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001156 }
1157
Elliott Hughes72025e52011-08-23 17:50:30 -07001158 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001159 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, obj);
1160 CHECK_NON_NULL_ARGUMENT(CallIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001161 ScopedObjectAccess soa(env);
1162 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 }
1164
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001166 va_list ap;
1167 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001168 CHECK_NON_NULL_ARGUMENT(CallLongMethod, obj);
1169 CHECK_NON_NULL_ARGUMENT(CallLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001170 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001171 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001172 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001173 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001174 }
1175
Elliott Hughes72025e52011-08-23 17:50:30 -07001176 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001177 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, obj);
1178 CHECK_NON_NULL_ARGUMENT(CallLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001179 ScopedObjectAccess soa(env);
1180 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001181 }
1182
Elliott Hughes72025e52011-08-23 17:50:30 -07001183 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001184 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, obj);
1185 CHECK_NON_NULL_ARGUMENT(CallLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001186 ScopedObjectAccess soa(env);
1187 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001188 }
1189
Elliott Hughes72025e52011-08-23 17:50:30 -07001190 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001191 va_list ap;
1192 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001193 CHECK_NON_NULL_ARGUMENT(CallShortMethod, obj);
1194 CHECK_NON_NULL_ARGUMENT(CallShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001195 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001196 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001197 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001198 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 }
1200
Elliott Hughes72025e52011-08-23 17:50:30 -07001201 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001202 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, obj);
1203 CHECK_NON_NULL_ARGUMENT(CallShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001204 ScopedObjectAccess soa(env);
1205 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 }
1207
Elliott Hughes72025e52011-08-23 17:50:30 -07001208 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001209 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, obj);
1210 CHECK_NON_NULL_ARGUMENT(CallShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001211 ScopedObjectAccess soa(env);
1212 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 }
1214
Elliott Hughes72025e52011-08-23 17:50:30 -07001215 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001216 va_list ap;
1217 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001218 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, obj);
1219 CHECK_NON_NULL_ARGUMENT(CallVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001220 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001221 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001222 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 }
1224
Elliott Hughes72025e52011-08-23 17:50:30 -07001225 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001226 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, obj);
1227 CHECK_NON_NULL_ARGUMENT(CallVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001228 ScopedObjectAccess soa(env);
1229 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001230 }
1231
Elliott Hughes72025e52011-08-23 17:50:30 -07001232 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001233 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, obj);
1234 CHECK_NON_NULL_ARGUMENT(CallVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001235 ScopedObjectAccess soa(env);
1236 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 }
1238
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001239 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001241 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001242 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, obj);
1243 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001244 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001245 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1246 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 va_end(ap);
1248 return local_result;
1249 }
1250
Ian Rogersbc939662013-08-15 10:26:54 -07001251 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1252 va_list args) {
1253 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, obj);
1254 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001255 ScopedObjectAccess soa(env);
1256 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1257 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 }
1259
Ian Rogersbc939662013-08-15 10:26:54 -07001260 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1261 jvalue* args) {
1262 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, obj);
1263 CHECK_NON_NULL_ARGUMENT(CallNonvirtualObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001264 ScopedObjectAccess soa(env);
1265 JValue result(InvokeWithJValues(soa, obj, mid, args));
1266 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001267 }
1268
Ian Rogersbc939662013-08-15 10:26:54 -07001269 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1270 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001271 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001272 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001273 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, obj);
1274 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001275 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001276 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001277 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001278 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 }
1280
Ian Rogersbc939662013-08-15 10:26:54 -07001281 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1282 va_list args) {
1283 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, obj);
1284 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001285 ScopedObjectAccess soa(env);
1286 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001287 }
1288
Ian Rogersbc939662013-08-15 10:26:54 -07001289 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1290 jvalue* args) {
1291 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, obj);
1292 CHECK_NON_NULL_ARGUMENT(CallNonvirtualBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001293 ScopedObjectAccess soa(env);
1294 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 }
1296
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001297 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001299 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001300 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, obj);
1301 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001302 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001303 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001304 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001305 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 }
1307
Ian Rogersbc939662013-08-15 10:26:54 -07001308 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1309 va_list args) {
1310 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, obj);
1311 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001312 ScopedObjectAccess soa(env);
1313 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001314 }
1315
Ian Rogersbc939662013-08-15 10:26:54 -07001316 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1317 jvalue* args) {
1318 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, obj);
1319 CHECK_NON_NULL_ARGUMENT(CallNonvirtualByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001320 ScopedObjectAccess soa(env);
1321 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001322 }
1323
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001324 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001325 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001326 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001327 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, obj);
1328 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethod, mid);
1329 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001330 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001331 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001332 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 }
1334
Ian Rogersbc939662013-08-15 10:26:54 -07001335 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1336 va_list args) {
1337 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, obj);
1338 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001339 ScopedObjectAccess soa(env);
1340 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 }
1342
Ian Rogersbc939662013-08-15 10:26:54 -07001343 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1344 jvalue* args) {
1345 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, obj);
1346 CHECK_NON_NULL_ARGUMENT(CallNonvirtualCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001347 ScopedObjectAccess soa(env);
1348 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001349 }
1350
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001351 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001352 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001353 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001354 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, obj);
1355 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001356 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001357 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001358 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001359 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001360 }
1361
Ian Rogersbc939662013-08-15 10:26:54 -07001362 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1363 va_list args) {
1364 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, obj);
1365 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001366 ScopedObjectAccess soa(env);
1367 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001368 }
1369
Ian Rogersbc939662013-08-15 10:26:54 -07001370 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1371 jvalue* args) {
1372 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, obj);
1373 CHECK_NON_NULL_ARGUMENT(CallNonvirtualShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001374 ScopedObjectAccess soa(env);
1375 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001376 }
1377
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001378 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001380 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001381 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, obj);
1382 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001383 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001384 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001385 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001386 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001387 }
1388
Ian Rogersbc939662013-08-15 10:26:54 -07001389 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1390 va_list args) {
1391 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, obj);
1392 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001393 ScopedObjectAccess soa(env);
1394 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001395 }
1396
Ian Rogersbc939662013-08-15 10:26:54 -07001397 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1398 jvalue* args) {
1399 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, obj);
1400 CHECK_NON_NULL_ARGUMENT(CallNonvirtualIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001401 ScopedObjectAccess soa(env);
1402 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001403 }
1404
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001405 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001407 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001408 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, obj);
1409 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001410 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001411 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001413 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001414 }
1415
Ian Rogersbc939662013-08-15 10:26:54 -07001416 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1417 va_list args) {
1418 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, obj);
1419 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001420 ScopedObjectAccess soa(env);
1421 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 }
1423
Ian Rogersbc939662013-08-15 10:26:54 -07001424 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1425 jvalue* args) {
1426 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, obj);
1427 CHECK_NON_NULL_ARGUMENT(CallNonvirtualLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001428 ScopedObjectAccess soa(env);
1429 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 }
1431
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001432 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001433 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001434 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001435 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, obj);
1436 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001437 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001438 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001439 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001440 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001441 }
1442
Ian Rogersbc939662013-08-15 10:26:54 -07001443 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1444 va_list args) {
1445 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, obj);
1446 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001447 ScopedObjectAccess soa(env);
1448 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001449 }
1450
Ian Rogersbc939662013-08-15 10:26:54 -07001451 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1452 jvalue* args) {
1453 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, obj);
1454 CHECK_NON_NULL_ARGUMENT(CallNonvirtualFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001455 ScopedObjectAccess soa(env);
1456 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001457 }
1458
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001459 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001460 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001461 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001462 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, obj);
1463 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001464 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001465 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001467 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001468 }
1469
Ian Rogersbc939662013-08-15 10:26:54 -07001470 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1471 va_list args) {
1472 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, obj);
1473 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001474 ScopedObjectAccess soa(env);
1475 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001476 }
1477
Ian Rogersbc939662013-08-15 10:26:54 -07001478 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1479 jvalue* args) {
1480 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, obj);
1481 CHECK_NON_NULL_ARGUMENT(CallNonvirtualDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001482 ScopedObjectAccess soa(env);
1483 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001484 }
1485
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001486 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001487 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001488 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001489 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, obj);
1490 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001491 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001492 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001493 va_end(ap);
1494 }
1495
Brian Carlstromea46f952013-07-30 01:26:50 -07001496 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1497 va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001498 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, obj);
1499 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001500 ScopedObjectAccess soa(env);
1501 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001502 }
1503
Ian Rogersbc939662013-08-15 10:26:54 -07001504 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1505 jvalue* args) {
1506 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, obj);
1507 CHECK_NON_NULL_ARGUMENT(CallNonvirtualVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001508 ScopedObjectAccess soa(env);
1509 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001510 }
1511
Ian Rogersbc939662013-08-15 10:26:54 -07001512 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
1513 CHECK_NON_NULL_ARGUMENT(GetFieldID, java_class);
1514 CHECK_NON_NULL_ARGUMENT(GetFieldID, name);
1515 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001516 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001517 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001518 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001519
Ian Rogersbc939662013-08-15 10:26:54 -07001520 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1521 const char* sig) {
1522 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, java_class);
1523 CHECK_NON_NULL_ARGUMENT(GetStaticFieldID, name);
1524 CHECK_NON_NULL_ARGUMENT(GetFieldID, sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001525 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001526 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001527 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001528
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001529 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001530 CHECK_NON_NULL_ARGUMENT(GetObjectField, obj);
1531 CHECK_NON_NULL_ARGUMENT(GetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001532 ScopedObjectAccess soa(env);
1533 Object* o = soa.Decode<Object*>(obj);
Brian Carlstromea46f952013-07-30 01:26:50 -07001534 ArtField* f = soa.DecodeField(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001535 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001536 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001537
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001538 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001539 CHECK_NON_NULL_ARGUMENT(GetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001540 ScopedObjectAccess soa(env);
Brian Carlstromea46f952013-07-30 01:26:50 -07001541 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001542 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001543 }
1544
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001545 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001546 CHECK_NON_NULL_ARGUMENT(SetObjectField, java_object);
1547 CHECK_NON_NULL_ARGUMENT(SetObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001548 ScopedObjectAccess soa(env);
1549 Object* o = soa.Decode<Object*>(java_object);
1550 Object* v = soa.Decode<Object*>(java_value);
Brian Carlstromea46f952013-07-30 01:26:50 -07001551 ArtField* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001552 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001553 }
1554
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001555 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogersbc939662013-08-15 10:26:54 -07001556 CHECK_NON_NULL_ARGUMENT(SetStaticObjectField, fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001557 ScopedObjectAccess soa(env);
1558 Object* v = soa.Decode<Object*>(java_value);
Brian Carlstromea46f952013-07-30 01:26:50 -07001559 ArtField* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001560 f->SetObject(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001561 }
1562
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001563#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogersbc939662013-08-15 10:26:54 -07001564 CHECK_NON_NULL_ARGUMENT(Get #fn Field, instance); \
1565 CHECK_NON_NULL_ARGUMENT(Get #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001566 ScopedObjectAccess soa(env); \
1567 Object* o = soa.Decode<Object*>(instance); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001568 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001569 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001570
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001571#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogersbc939662013-08-15 10:26:54 -07001572 CHECK_NON_NULL_ARGUMENT(GetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001573 ScopedObjectAccess soa(env); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001574 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001575 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001576
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001577#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001578 CHECK_NON_NULL_ARGUMENT(Set #fn Field, instance); \
1579 CHECK_NON_NULL_ARGUMENT(Set #fn Field, fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001580 ScopedObjectAccess soa(env); \
1581 Object* o = soa.Decode<Object*>(instance); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001582 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001583 f->Set ##fn(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001584
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001585#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogersbc939662013-08-15 10:26:54 -07001586 CHECK_NON_NULL_ARGUMENT(SetStatic #fn Field, fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001587 ScopedObjectAccess soa(env); \
Brian Carlstromea46f952013-07-30 01:26:50 -07001588 ArtField* f = soa.DecodeField(fid); \
Ian Rogersbc939662013-08-15 10:26:54 -07001589 f->Set ##fn(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001590
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001591 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001592 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001593 }
1594
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001595 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001596 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001597 }
1598
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001599 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001600 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001601 }
1602
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001603 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001604 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001605 }
1606
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001607 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001608 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001609 }
1610
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001611 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001612 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001613 }
1614
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001615 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001616 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001617 }
1618
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001619 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001620 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001621 }
1622
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001623 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001624 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001625 }
1626
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001627 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001628 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001629 }
1630
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001631 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001632 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 }
1634
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001635 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001636 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 }
1638
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001639 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001640 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 }
1642
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001643 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001644 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001645 }
1646
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001647 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001648 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001649 }
1650
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001651 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001652 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001653 }
1654
1655 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001656 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001657 }
1658
1659 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001660 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001661 }
1662
1663 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001664 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001665 }
1666
1667 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001668 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001669 }
1670
1671 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001672 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001673 }
1674
1675 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001676 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001677 }
1678
1679 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001680 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001681 }
1682
1683 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001684 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001685 }
1686
1687 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001688 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001689 }
1690
1691 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001692 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001693 }
1694
1695 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001696 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001697 }
1698
1699 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001700 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001701 }
1702
1703 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001704 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001705 }
1706
1707 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001708 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001709 }
1710
1711 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001712 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001713 }
1714
1715 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001716 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 }
1718
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001719 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001720 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001721 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001722 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001723 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001724 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1725 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001726 va_end(ap);
1727 return local_result;
1728 }
1729
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001730 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001731 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001732 ScopedObjectAccess soa(env);
1733 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1734 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001735 }
1736
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001737 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001738 CHECK_NON_NULL_ARGUMENT(CallStaticObjectMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001739 ScopedObjectAccess soa(env);
1740 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1741 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001742 }
1743
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001744 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001745 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001746 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001747 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001748 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001749 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001750 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001751 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001752 }
1753
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001754 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001755 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001756 ScopedObjectAccess soa(env);
1757 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001758 }
1759
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001760 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001761 CHECK_NON_NULL_ARGUMENT(CallStaticBooleanMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001762 ScopedObjectAccess soa(env);
1763 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001764 }
1765
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001766 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001767 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001768 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001769 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001770 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001771 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001773 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001774 }
1775
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001776 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001777 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001778 ScopedObjectAccess soa(env);
1779 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001780 }
1781
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001782 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001783 CHECK_NON_NULL_ARGUMENT(CallStaticByteMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001784 ScopedObjectAccess soa(env);
1785 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 }
1787
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001788 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001789 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001790 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001791 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001792 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001793 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001794 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001795 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001796 }
1797
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001798 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001799 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001800 ScopedObjectAccess soa(env);
1801 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001802 }
1803
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001804 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001805 CHECK_NON_NULL_ARGUMENT(CallStaticCharMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001806 ScopedObjectAccess soa(env);
1807 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001808 }
1809
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001810 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001811 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001812 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001813 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001814 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001815 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001816 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001817 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001818 }
1819
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001820 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001821 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001822 ScopedObjectAccess soa(env);
1823 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 }
1825
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001826 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001827 CHECK_NON_NULL_ARGUMENT(CallStaticShortMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001828 ScopedObjectAccess soa(env);
1829 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001830 }
1831
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001832 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001833 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001834 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001835 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001836 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001837 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001838 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001839 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001840 }
1841
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001842 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001843 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001844 ScopedObjectAccess soa(env);
1845 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001846 }
1847
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001848 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001849 CHECK_NON_NULL_ARGUMENT(CallStaticIntMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001850 ScopedObjectAccess soa(env);
1851 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001852 }
1853
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001854 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001855 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001856 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001857 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001858 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001859 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001860 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001861 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001862 }
1863
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001864 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001865 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001866 ScopedObjectAccess soa(env);
1867 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001868 }
1869
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001870 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001871 CHECK_NON_NULL_ARGUMENT(CallStaticLongMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001872 ScopedObjectAccess soa(env);
1873 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001874 }
1875
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001876 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001877 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001878 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001879 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001880 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001881 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001882 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001883 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001884 }
1885
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001886 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001887 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001888 ScopedObjectAccess soa(env);
1889 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001890 }
1891
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001892 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001893 CHECK_NON_NULL_ARGUMENT(CallStaticFloatMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001894 ScopedObjectAccess soa(env);
1895 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001896 }
1897
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001898 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001899 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001900 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001901 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001902 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001903 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001904 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001905 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001906 }
1907
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001908 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001909 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001910 ScopedObjectAccess soa(env);
1911 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001912 }
1913
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001914 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001915 CHECK_NON_NULL_ARGUMENT(CallStaticDoubleMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001916 ScopedObjectAccess soa(env);
1917 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001918 }
1919
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001920 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001921 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001922 va_start(ap, mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001923 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethod, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001924 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001925 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001926 va_end(ap);
1927 }
1928
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001929 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001930 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodV, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001931 ScopedObjectAccess soa(env);
1932 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001933 }
1934
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001935 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogersbc939662013-08-15 10:26:54 -07001936 CHECK_NON_NULL_ARGUMENT(CallStaticVoidMethodA, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001937 ScopedObjectAccess soa(env);
1938 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001939 }
1940
Elliott Hughes814e4032011-08-23 12:07:56 -07001941 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogersbc939662013-08-15 10:26:54 -07001942 if (UNLIKELY(chars == NULL && char_count > 0)) { \
1943 JniAbortF("NewString", "char == null && char_count > 0"); \
1944 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001945 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001946 String* result = String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001947 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001948 }
1949
1950 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001951 if (utf == NULL) {
1952 return NULL;
1953 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001954 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001955 String* result = String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001956 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001957 }
1958
Elliott Hughes814e4032011-08-23 12:07:56 -07001959 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001960 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001961 ScopedObjectAccess soa(env);
1962 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001963 }
1964
1965 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogersbc939662013-08-15 10:26:54 -07001966 CHECK_NON_NULL_ARGUMENT(GetStringLength, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001967 ScopedObjectAccess soa(env);
1968 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001969 }
1970
Ian Rogersbc939662013-08-15 10:26:54 -07001971 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1972 jchar* buf) {
1973 CHECK_NON_NULL_ARGUMENT(GetStringRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001974 ScopedObjectAccess soa(env);
1975 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001976 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001977 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001978 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07001979 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001980 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1981 memcpy(buf, chars + start, length * sizeof(jchar));
1982 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001983 }
1984
Ian Rogersbc939662013-08-15 10:26:54 -07001985 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1986 char* buf) {
1987 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001988 ScopedObjectAccess soa(env);
1989 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001990 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001991 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001992 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07001993 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringUTFRegion, length, buf);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001994 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1995 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1996 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001997 }
1998
Elliott Hughes75770752011-08-24 17:52:38 -07001999 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002000 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002001 ScopedObjectAccess soa(env);
2002 String* s = soa.Decode<String*>(java_string);
Mathieu Chartier423d2a32013-09-12 17:33:56 -07002003 CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002004 PinPrimitiveArray(soa, chars);
Elliott Hughes75770752011-08-24 17:52:38 -07002005 if (is_copy != NULL) {
2006 *is_copy = JNI_FALSE;
2007 }
2008 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07002009 }
2010
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002011 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Ian Rogersbc939662013-08-15 10:26:54 -07002012 CHECK_NON_NULL_ARGUMENT(GetStringUTFRegion, java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002013 ScopedObjectAccess soa(env);
2014 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07002015 }
2016
Elliott Hughes75770752011-08-24 17:52:38 -07002017 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07002018 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002019 }
2020
Elliott Hughes75770752011-08-24 17:52:38 -07002021 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002022 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07002023 }
2024
Elliott Hughes75770752011-08-24 17:52:38 -07002025 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07002026 if (java_string == NULL) {
2027 return NULL;
2028 }
2029 if (is_copy != NULL) {
2030 *is_copy = JNI_TRUE;
2031 }
Ian Rogersef28b142012-11-30 14:22:18 -08002032 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002033 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07002034 size_t byte_count = s->GetUtfLength();
2035 char* bytes = new char[byte_count + 1];
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002036 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07002037 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
2038 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
2039 bytes[byte_count] = '\0';
2040 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002041 }
2042
Elliott Hughes75770752011-08-24 17:52:38 -07002043 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07002044 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002045 }
2046
Elliott Hughesbd935992011-08-22 11:59:34 -07002047 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogersbc939662013-08-15 10:26:54 -07002048 CHECK_NON_NULL_ARGUMENT(GetArrayLength, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002049 ScopedObjectAccess soa(env);
2050 Object* obj = soa.Decode<Object*>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07002051 if (UNLIKELY(!obj->IsArrayInstance())) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002052 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
2053 }
Elliott Hughesbd935992011-08-22 11:59:34 -07002054 Array* array = obj->AsArray();
2055 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07002056 }
2057
Elliott Hughes814e4032011-08-23 12:07:56 -07002058 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogersbc939662013-08-15 10:26:54 -07002059 CHECK_NON_NULL_ARGUMENT(GetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002060 ScopedObjectAccess soa(env);
2061 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
2062 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 }
2064
Ian Rogersbc939662013-08-15 10:26:54 -07002065 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2066 jobject java_value) {
2067 CHECK_NON_NULL_ARGUMENT(SetObjectArrayElement, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002068 ScopedObjectAccess soa(env);
2069 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
2070 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07002071 array->Set(index, value);
2072 }
2073
2074 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002075 ScopedObjectAccess soa(env);
2076 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 }
2078
2079 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002080 ScopedObjectAccess soa(env);
2081 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 }
2083
2084 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002085 ScopedObjectAccess soa(env);
2086 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 }
2088
2089 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002090 ScopedObjectAccess soa(env);
2091 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 }
2093
2094 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002095 ScopedObjectAccess soa(env);
2096 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 }
2098
2099 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002100 ScopedObjectAccess soa(env);
2101 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 }
2103
2104 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002105 ScopedObjectAccess soa(env);
2106 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 }
2108
2109 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002110 if (length < 0) {
2111 JniAbortF("NewObjectArray", "negative array length: %d", length);
2112 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002113
2114 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002115 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002116 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 std::string descriptor;
2118 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002119 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07002120
2121 // Find the class.
Brian Carlstromce888532013-10-10 00:32:58 -07002122 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2123 Class* array_class = class_linker->FindClass(descriptor.c_str(),
2124 element_class->GetClassLoader());
2125 if (array_class == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002126 return NULL;
2127 }
2128
Elliott Hughes75770752011-08-24 17:52:38 -07002129 // Allocate and initialize if necessary.
Ian Rogers50b35e22012-10-04 10:09:15 -07002130 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07002131 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002132 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07002133 for (jsize i = 0; i < length; ++i) {
2134 result->Set(i, initial_object);
2135 }
2136 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002137 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002138 }
2139
2140 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002141 ScopedObjectAccess soa(env);
2142 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 }
2144
Ian Rogersa15e67d2012-02-28 13:51:55 -08002145 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002146 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayCritical, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002147 ScopedObjectAccess soa(env);
2148 Array* array = soa.Decode<Array*>(java_array);
2149 PinPrimitiveArray(soa, array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08002150 if (is_copy != NULL) {
2151 *is_copy = JNI_FALSE;
2152 }
2153 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07002154 }
2155
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002156 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Ian Rogersbc939662013-08-15 10:26:54 -07002157 CHECK_NON_NULL_ARGUMENT(ReleasePrimitiveArrayCritical, array);
Ian Rogersef28b142012-11-30 14:22:18 -08002158 ReleasePrimitiveArray(env, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002159 }
2160
Elliott Hughes75770752011-08-24 17:52:38 -07002161 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002162 CHECK_NON_NULL_ARGUMENT(GetBooleanArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002163 ScopedObjectAccess soa(env);
2164 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 }
2166
Elliott Hughes75770752011-08-24 17:52:38 -07002167 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002168 CHECK_NON_NULL_ARGUMENT(GetByteArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002169 ScopedObjectAccess soa(env);
2170 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002171 }
2172
Elliott Hughes75770752011-08-24 17:52:38 -07002173 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002174 CHECK_NON_NULL_ARGUMENT(GetCharArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002175 ScopedObjectAccess soa(env);
2176 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002177 }
2178
Elliott Hughes75770752011-08-24 17:52:38 -07002179 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002180 CHECK_NON_NULL_ARGUMENT(GetDoubleArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002181 ScopedObjectAccess soa(env);
2182 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002183 }
2184
Elliott Hughes75770752011-08-24 17:52:38 -07002185 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002186 CHECK_NON_NULL_ARGUMENT(GetFloatArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002187 ScopedObjectAccess soa(env);
2188 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 }
2190
Elliott Hughes75770752011-08-24 17:52:38 -07002191 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002192 CHECK_NON_NULL_ARGUMENT(GetIntArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002193 ScopedObjectAccess soa(env);
2194 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 }
2196
Elliott Hughes75770752011-08-24 17:52:38 -07002197 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002198 CHECK_NON_NULL_ARGUMENT(GetLongArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002199 ScopedObjectAccess soa(env);
2200 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002201 }
2202
Elliott Hughes75770752011-08-24 17:52:38 -07002203 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogersbc939662013-08-15 10:26:54 -07002204 CHECK_NON_NULL_ARGUMENT(GetShortArrayElements, array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002205 ScopedObjectAccess soa(env);
2206 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002207 }
2208
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002209 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002210 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002211 }
2212
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002213 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002214 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002215 }
2216
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002217 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002218 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002219 }
2220
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002221 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002222 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002223 }
2224
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002225 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002226 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002227 }
2228
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002229 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002230 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002231 }
2232
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002233 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002234 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002235 }
2236
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002237 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08002238 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002239 }
2240
Ian Rogersbc939662013-08-15 10:26:54 -07002241 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2242 jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002243 ScopedObjectAccess soa(env);
2244 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002245 }
2246
Ian Rogersbc939662013-08-15 10:26:54 -07002247 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2248 jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002249 ScopedObjectAccess soa(env);
2250 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002251 }
2252
Ian Rogersbc939662013-08-15 10:26:54 -07002253 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2254 jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002255 ScopedObjectAccess soa(env);
2256 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002257 }
2258
Ian Rogersbc939662013-08-15 10:26:54 -07002259 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2260 jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002261 ScopedObjectAccess soa(env);
2262 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002263 }
2264
Ian Rogersbc939662013-08-15 10:26:54 -07002265 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2266 jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002267 ScopedObjectAccess soa(env);
2268 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002269 }
2270
Ian Rogersbc939662013-08-15 10:26:54 -07002271 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2272 jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002273 ScopedObjectAccess soa(env);
2274 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002275 }
2276
Ian Rogersbc939662013-08-15 10:26:54 -07002277 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2278 jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002279 ScopedObjectAccess soa(env);
2280 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002281 }
2282
Ian Rogersbc939662013-08-15 10:26:54 -07002283 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2284 jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002285 ScopedObjectAccess soa(env);
2286 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002287 }
2288
Ian Rogersbc939662013-08-15 10:26:54 -07002289 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2290 const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002291 ScopedObjectAccess soa(env);
2292 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002293 }
2294
Ian Rogersbc939662013-08-15 10:26:54 -07002295 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2296 const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002297 ScopedObjectAccess soa(env);
2298 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002299 }
2300
Ian Rogersbc939662013-08-15 10:26:54 -07002301 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2302 const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002303 ScopedObjectAccess soa(env);
2304 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002305 }
2306
Ian Rogersbc939662013-08-15 10:26:54 -07002307 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2308 const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002309 ScopedObjectAccess soa(env);
2310 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002311 }
2312
Ian Rogersbc939662013-08-15 10:26:54 -07002313 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2314 const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002315 ScopedObjectAccess soa(env);
2316 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002317 }
2318
Ian Rogersbc939662013-08-15 10:26:54 -07002319 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2320 const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002321 ScopedObjectAccess soa(env);
2322 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002323 }
2324
Ian Rogersbc939662013-08-15 10:26:54 -07002325 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2326 const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002327 ScopedObjectAccess soa(env);
2328 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002329 }
2330
Ian Rogersbc939662013-08-15 10:26:54 -07002331 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2332 const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002333 ScopedObjectAccess soa(env);
2334 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002335 }
2336
Ian Rogersbc939662013-08-15 10:26:54 -07002337 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2338 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002339 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2340 }
2341
Ian Rogersbc939662013-08-15 10:26:54 -07002342 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods,
2343 jint method_count, bool return_errors) {
2344 if (UNLIKELY(method_count < 0)) {
Elliott Hughesaa836f72013-08-20 16:57:23 -07002345 JniAbortF("RegisterNatives", "negative method count: %d", method_count);
Ian Rogersbc939662013-08-15 10:26:54 -07002346 return JNI_ERR; // Not reached.
2347 }
2348 CHECK_NON_NULL_ARGUMENT(RegisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002349 ScopedObjectAccess soa(env);
2350 Class* c = soa.Decode<Class*>(java_class);
Ian Rogersbc939662013-08-15 10:26:54 -07002351 if (UNLIKELY(method_count == 0)) {
2352 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2353 << PrettyDescriptor(c);
2354 return JNI_OK;
2355 }
2356 CHECK_NON_NULL_ARGUMENT(RegisterNatives, methods);
2357 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002358 const char* name = methods[i].name;
2359 const char* sig = methods[i].signature;
Ian Rogers1eb512d2013-10-18 15:42:20 -07002360 bool is_fast = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002361 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002362 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002363 ++sig;
2364 }
2365
Brian Carlstromea46f952013-07-30 01:26:50 -07002366 ArtMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002367 if (m == NULL) {
2368 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002369 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002370 if (m == NULL) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002371 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002372 << PrettyDescriptor(c) << "." << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002373 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002374 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002375 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002376 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
Ian Rogersbc939662013-08-15 10:26:54 -07002377 << PrettyDescriptor(c) << "." << name << sig
2378 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002379 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002380 return JNI_ERR;
2381 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002382
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002383 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002384
Ian Rogers1eb512d2013-10-18 15:42:20 -07002385 m->RegisterNative(soa.Self(), methods[i].fnPtr, is_fast);
Elliott Hughescdf53122011-08-19 15:46:09 -07002386 }
2387 return JNI_OK;
2388 }
2389
Elliott Hughes5174fe62011-08-23 15:12:35 -07002390 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogersbc939662013-08-15 10:26:54 -07002391 CHECK_NON_NULL_ARGUMENT(UnregisterNatives, java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002392 ScopedObjectAccess soa(env);
2393 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002394
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002395 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002396
2397 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07002398 ArtMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002399 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002400 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002401 }
2402 }
2403 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07002404 ArtMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002405 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002406 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002407 }
2408 }
2409
2410 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002411 }
2412
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002413 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2414 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002415 CHECK_NON_NULL_ARGUMENT(MonitorEnter, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002416 ScopedObjectAccess soa(env);
2417 Object* o = soa.Decode<Object*>(java_object);
2418 o->MonitorEnter(soa.Self());
2419 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002420 return JNI_ERR;
2421 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002422 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002423 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002424 }
2425
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002426 static jint MonitorExit(JNIEnv* env, jobject java_object)
2427 UNLOCK_FUNCTION(monitor_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002428 CHECK_NON_NULL_ARGUMENT(MonitorExit, java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002429 ScopedObjectAccess soa(env);
2430 Object* o = soa.Decode<Object*>(java_object);
2431 o->MonitorExit(soa.Self());
2432 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002433 return JNI_ERR;
2434 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002435 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002436 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002437 }
2438
2439 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogersbc939662013-08-15 10:26:54 -07002440 CHECK_NON_NULL_ARGUMENT(GetJavaVM, vm);
Elliott Hughescdf53122011-08-19 15:46:09 -07002441 Runtime* runtime = Runtime::Current();
2442 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002443 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002444 } else {
2445 *vm = NULL;
2446 }
2447 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2448 }
2449
Elliott Hughescdf53122011-08-19 15:46:09 -07002450 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002451 if (capacity < 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002452 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002453 }
Elliott Hughes11a796e2012-12-19 14:42:57 -08002454 if (address == NULL && capacity != 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002455 JniAbortF("NewDirectByteBuffer", "non-zero capacity for NULL pointer: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002456 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002457
Elliott Hughes96a98872012-12-19 14:21:15 -08002458 // At the moment, the Java side is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002459 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2460 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002461 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002462 jint capacity_arg = static_cast<jint>(capacity);
2463
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002464 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2465 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002466 address_arg, capacity_arg);
Ian Rogersef28b142012-11-30 14:22:18 -08002467 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002468 }
2469
Elliott Hughesb465ab02011-08-24 11:21:21 -07002470 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Jeff Hao534f2b62013-07-10 15:29:36 -07002471 return reinterpret_cast<void*>(env->GetLongField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002472 }
2473
Elliott Hughesb465ab02011-08-24 11:21:21 -07002474 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002475 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002476 }
2477
Elliott Hughesb465ab02011-08-24 11:21:21 -07002478 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogersbc939662013-08-15 10:26:54 -07002479 CHECK_NON_NULL_ARGUMENT(GetObjectRefType, java_object);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002480
2481 // Do we definitely know what kind of reference this is?
2482 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2483 IndirectRefKind kind = GetIndirectRefKind(ref);
2484 switch (kind) {
2485 case kLocal:
Ian Rogersef28b142012-11-30 14:22:18 -08002486 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002487 return JNILocalRefType;
2488 }
2489 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002490 case kGlobal:
2491 return JNIGlobalRefType;
2492 case kWeakGlobal:
2493 return JNIWeakGlobalRefType;
2494 case kSirtOrInvalid:
2495 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002496 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002497 return JNILocalRefType;
2498 }
2499
Ian Rogersef28b142012-11-30 14:22:18 -08002500 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002501 return JNIInvalidRefType;
2502 }
2503
Elliott Hughesb465ab02011-08-24 11:21:21 -07002504 // If we're handing out direct pointers, check whether it's a direct pointer
2505 // to a local reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002506 {
2507 ScopedObjectAccess soa(env);
2508 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2509 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
2510 return JNILocalRefType;
2511 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002512 }
2513 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002514 return JNIInvalidRefType;
2515 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002516 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2517 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002518 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002519
2520 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002521 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2522 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002523 // TODO: we should try to expand the table if necessary.
Elliott Hughesaa836f72013-08-20 16:57:23 -07002524 if (desired_capacity < 0 || desired_capacity > static_cast<jint>(kLocalsMax)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002525 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2526 return JNI_ERR;
2527 }
2528 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002529 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002530 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2531 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002532 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002533 soa.Self()->ThrowOutOfMemoryError(caller);
2534 }
2535 return okay ? JNI_OK : JNI_ERR;
2536 }
2537
2538 template<typename JniT, typename ArtT>
2539 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002540 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002541 if (length < 0) {
2542 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
2543 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002544 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002545 return soa.AddLocalReference<JniT>(result);
2546 }
2547
2548 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2549 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2550 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002551 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002552 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2553 PinPrimitiveArray(soa, array);
2554 if (is_copy != NULL) {
2555 *is_copy = JNI_FALSE;
2556 }
2557 return array->GetData();
2558 }
2559
2560 template <typename ArrayT>
Ian Rogersef28b142012-11-30 14:22:18 -08002561 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002562 if (mode != JNI_COMMIT) {
Ian Rogersef28b142012-11-30 14:22:18 -08002563 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002564 Array* array = soa.Decode<Array*>(java_array);
2565 UnpinPrimitiveArray(soa, array);
2566 }
2567 }
2568
2569 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2570 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2571 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002572 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002573 CHECK_NON_NULL_ARGUMENT(GetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002574 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2575 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2576 ThrowAIOOBE(soa, array, start, length, "src");
2577 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002578 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002579 JavaT* data = array->GetData();
2580 memcpy(buf, data + start, length * sizeof(JavaT));
2581 }
2582 }
2583
2584 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2585 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2586 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002587 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersbc939662013-08-15 10:26:54 -07002588 CHECK_NON_NULL_ARGUMENT(SetPrimitiveArrayRegion, java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002589 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2590 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2591 ThrowAIOOBE(soa, array, start, length, "dst");
2592 } else {
Ian Rogers4ffdc6b2013-08-21 16:55:13 -07002593 CHECK_NON_NULL_MEMCPY_ARGUMENT(GetStringRegion, length, buf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002594 JavaT* data = array->GetData();
2595 memcpy(data + start, buf, length * sizeof(JavaT));
2596 }
2597 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002598};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002599
Elliott Hughes88c5c352012-03-15 18:49:48 -07002600const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002601 NULL, // reserved0.
2602 NULL, // reserved1.
2603 NULL, // reserved2.
2604 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002605 JNI::GetVersion,
2606 JNI::DefineClass,
2607 JNI::FindClass,
2608 JNI::FromReflectedMethod,
2609 JNI::FromReflectedField,
2610 JNI::ToReflectedMethod,
2611 JNI::GetSuperclass,
2612 JNI::IsAssignableFrom,
2613 JNI::ToReflectedField,
2614 JNI::Throw,
2615 JNI::ThrowNew,
2616 JNI::ExceptionOccurred,
2617 JNI::ExceptionDescribe,
2618 JNI::ExceptionClear,
2619 JNI::FatalError,
2620 JNI::PushLocalFrame,
2621 JNI::PopLocalFrame,
2622 JNI::NewGlobalRef,
2623 JNI::DeleteGlobalRef,
2624 JNI::DeleteLocalRef,
2625 JNI::IsSameObject,
2626 JNI::NewLocalRef,
2627 JNI::EnsureLocalCapacity,
2628 JNI::AllocObject,
2629 JNI::NewObject,
2630 JNI::NewObjectV,
2631 JNI::NewObjectA,
2632 JNI::GetObjectClass,
2633 JNI::IsInstanceOf,
2634 JNI::GetMethodID,
2635 JNI::CallObjectMethod,
2636 JNI::CallObjectMethodV,
2637 JNI::CallObjectMethodA,
2638 JNI::CallBooleanMethod,
2639 JNI::CallBooleanMethodV,
2640 JNI::CallBooleanMethodA,
2641 JNI::CallByteMethod,
2642 JNI::CallByteMethodV,
2643 JNI::CallByteMethodA,
2644 JNI::CallCharMethod,
2645 JNI::CallCharMethodV,
2646 JNI::CallCharMethodA,
2647 JNI::CallShortMethod,
2648 JNI::CallShortMethodV,
2649 JNI::CallShortMethodA,
2650 JNI::CallIntMethod,
2651 JNI::CallIntMethodV,
2652 JNI::CallIntMethodA,
2653 JNI::CallLongMethod,
2654 JNI::CallLongMethodV,
2655 JNI::CallLongMethodA,
2656 JNI::CallFloatMethod,
2657 JNI::CallFloatMethodV,
2658 JNI::CallFloatMethodA,
2659 JNI::CallDoubleMethod,
2660 JNI::CallDoubleMethodV,
2661 JNI::CallDoubleMethodA,
2662 JNI::CallVoidMethod,
2663 JNI::CallVoidMethodV,
2664 JNI::CallVoidMethodA,
2665 JNI::CallNonvirtualObjectMethod,
2666 JNI::CallNonvirtualObjectMethodV,
2667 JNI::CallNonvirtualObjectMethodA,
2668 JNI::CallNonvirtualBooleanMethod,
2669 JNI::CallNonvirtualBooleanMethodV,
2670 JNI::CallNonvirtualBooleanMethodA,
2671 JNI::CallNonvirtualByteMethod,
2672 JNI::CallNonvirtualByteMethodV,
2673 JNI::CallNonvirtualByteMethodA,
2674 JNI::CallNonvirtualCharMethod,
2675 JNI::CallNonvirtualCharMethodV,
2676 JNI::CallNonvirtualCharMethodA,
2677 JNI::CallNonvirtualShortMethod,
2678 JNI::CallNonvirtualShortMethodV,
2679 JNI::CallNonvirtualShortMethodA,
2680 JNI::CallNonvirtualIntMethod,
2681 JNI::CallNonvirtualIntMethodV,
2682 JNI::CallNonvirtualIntMethodA,
2683 JNI::CallNonvirtualLongMethod,
2684 JNI::CallNonvirtualLongMethodV,
2685 JNI::CallNonvirtualLongMethodA,
2686 JNI::CallNonvirtualFloatMethod,
2687 JNI::CallNonvirtualFloatMethodV,
2688 JNI::CallNonvirtualFloatMethodA,
2689 JNI::CallNonvirtualDoubleMethod,
2690 JNI::CallNonvirtualDoubleMethodV,
2691 JNI::CallNonvirtualDoubleMethodA,
2692 JNI::CallNonvirtualVoidMethod,
2693 JNI::CallNonvirtualVoidMethodV,
2694 JNI::CallNonvirtualVoidMethodA,
2695 JNI::GetFieldID,
2696 JNI::GetObjectField,
2697 JNI::GetBooleanField,
2698 JNI::GetByteField,
2699 JNI::GetCharField,
2700 JNI::GetShortField,
2701 JNI::GetIntField,
2702 JNI::GetLongField,
2703 JNI::GetFloatField,
2704 JNI::GetDoubleField,
2705 JNI::SetObjectField,
2706 JNI::SetBooleanField,
2707 JNI::SetByteField,
2708 JNI::SetCharField,
2709 JNI::SetShortField,
2710 JNI::SetIntField,
2711 JNI::SetLongField,
2712 JNI::SetFloatField,
2713 JNI::SetDoubleField,
2714 JNI::GetStaticMethodID,
2715 JNI::CallStaticObjectMethod,
2716 JNI::CallStaticObjectMethodV,
2717 JNI::CallStaticObjectMethodA,
2718 JNI::CallStaticBooleanMethod,
2719 JNI::CallStaticBooleanMethodV,
2720 JNI::CallStaticBooleanMethodA,
2721 JNI::CallStaticByteMethod,
2722 JNI::CallStaticByteMethodV,
2723 JNI::CallStaticByteMethodA,
2724 JNI::CallStaticCharMethod,
2725 JNI::CallStaticCharMethodV,
2726 JNI::CallStaticCharMethodA,
2727 JNI::CallStaticShortMethod,
2728 JNI::CallStaticShortMethodV,
2729 JNI::CallStaticShortMethodA,
2730 JNI::CallStaticIntMethod,
2731 JNI::CallStaticIntMethodV,
2732 JNI::CallStaticIntMethodA,
2733 JNI::CallStaticLongMethod,
2734 JNI::CallStaticLongMethodV,
2735 JNI::CallStaticLongMethodA,
2736 JNI::CallStaticFloatMethod,
2737 JNI::CallStaticFloatMethodV,
2738 JNI::CallStaticFloatMethodA,
2739 JNI::CallStaticDoubleMethod,
2740 JNI::CallStaticDoubleMethodV,
2741 JNI::CallStaticDoubleMethodA,
2742 JNI::CallStaticVoidMethod,
2743 JNI::CallStaticVoidMethodV,
2744 JNI::CallStaticVoidMethodA,
2745 JNI::GetStaticFieldID,
2746 JNI::GetStaticObjectField,
2747 JNI::GetStaticBooleanField,
2748 JNI::GetStaticByteField,
2749 JNI::GetStaticCharField,
2750 JNI::GetStaticShortField,
2751 JNI::GetStaticIntField,
2752 JNI::GetStaticLongField,
2753 JNI::GetStaticFloatField,
2754 JNI::GetStaticDoubleField,
2755 JNI::SetStaticObjectField,
2756 JNI::SetStaticBooleanField,
2757 JNI::SetStaticByteField,
2758 JNI::SetStaticCharField,
2759 JNI::SetStaticShortField,
2760 JNI::SetStaticIntField,
2761 JNI::SetStaticLongField,
2762 JNI::SetStaticFloatField,
2763 JNI::SetStaticDoubleField,
2764 JNI::NewString,
2765 JNI::GetStringLength,
2766 JNI::GetStringChars,
2767 JNI::ReleaseStringChars,
2768 JNI::NewStringUTF,
2769 JNI::GetStringUTFLength,
2770 JNI::GetStringUTFChars,
2771 JNI::ReleaseStringUTFChars,
2772 JNI::GetArrayLength,
2773 JNI::NewObjectArray,
2774 JNI::GetObjectArrayElement,
2775 JNI::SetObjectArrayElement,
2776 JNI::NewBooleanArray,
2777 JNI::NewByteArray,
2778 JNI::NewCharArray,
2779 JNI::NewShortArray,
2780 JNI::NewIntArray,
2781 JNI::NewLongArray,
2782 JNI::NewFloatArray,
2783 JNI::NewDoubleArray,
2784 JNI::GetBooleanArrayElements,
2785 JNI::GetByteArrayElements,
2786 JNI::GetCharArrayElements,
2787 JNI::GetShortArrayElements,
2788 JNI::GetIntArrayElements,
2789 JNI::GetLongArrayElements,
2790 JNI::GetFloatArrayElements,
2791 JNI::GetDoubleArrayElements,
2792 JNI::ReleaseBooleanArrayElements,
2793 JNI::ReleaseByteArrayElements,
2794 JNI::ReleaseCharArrayElements,
2795 JNI::ReleaseShortArrayElements,
2796 JNI::ReleaseIntArrayElements,
2797 JNI::ReleaseLongArrayElements,
2798 JNI::ReleaseFloatArrayElements,
2799 JNI::ReleaseDoubleArrayElements,
2800 JNI::GetBooleanArrayRegion,
2801 JNI::GetByteArrayRegion,
2802 JNI::GetCharArrayRegion,
2803 JNI::GetShortArrayRegion,
2804 JNI::GetIntArrayRegion,
2805 JNI::GetLongArrayRegion,
2806 JNI::GetFloatArrayRegion,
2807 JNI::GetDoubleArrayRegion,
2808 JNI::SetBooleanArrayRegion,
2809 JNI::SetByteArrayRegion,
2810 JNI::SetCharArrayRegion,
2811 JNI::SetShortArrayRegion,
2812 JNI::SetIntArrayRegion,
2813 JNI::SetLongArrayRegion,
2814 JNI::SetFloatArrayRegion,
2815 JNI::SetDoubleArrayRegion,
2816 JNI::RegisterNatives,
2817 JNI::UnregisterNatives,
2818 JNI::MonitorEnter,
2819 JNI::MonitorExit,
2820 JNI::GetJavaVM,
2821 JNI::GetStringRegion,
2822 JNI::GetStringUTFRegion,
2823 JNI::GetPrimitiveArrayCritical,
2824 JNI::ReleasePrimitiveArrayCritical,
2825 JNI::GetStringCritical,
2826 JNI::ReleaseStringCritical,
2827 JNI::NewWeakGlobalRef,
2828 JNI::DeleteWeakGlobalRef,
2829 JNI::ExceptionCheck,
2830 JNI::NewDirectByteBuffer,
2831 JNI::GetDirectBufferAddress,
2832 JNI::GetDirectBufferCapacity,
2833 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002834};
2835
Elliott Hughes75770752011-08-24 17:52:38 -07002836JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002837 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002838 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002839 local_ref_cookie(IRT_FIRST_SEGMENT),
2840 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002841 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002842 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002843 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002844 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002845 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002846 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002847 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002848 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2849 // errors will ensue.
2850 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2851 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002852}
2853
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002854JNIEnvExt::~JNIEnvExt() {
2855}
2856
Elliott Hughes88c5c352012-03-15 18:49:48 -07002857void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2858 check_jni = enabled;
2859 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002860}
2861
Elliott Hughes73e66f72012-05-09 09:34:45 -07002862void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2863 locals.Dump(os);
2864 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002865}
2866
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002867void JNIEnvExt::PushFrame(int /*capacity*/) {
2868 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002869 stacked_local_ref_cookies.push_back(local_ref_cookie);
2870 local_ref_cookie = locals.GetSegmentState();
2871}
2872
2873void JNIEnvExt::PopFrame() {
2874 locals.SetSegmentState(local_ref_cookie);
2875 local_ref_cookie = stacked_local_ref_cookies.back();
2876 stacked_local_ref_cookies.pop_back();
2877}
2878
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002879Offset JNIEnvExt::SegmentStateOffset() {
2880 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2881 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2882}
2883
Carl Shapiroea4dca82011-08-01 13:45:38 -07002884// JNI Invocation interface.
2885
Brian Carlstrombddf9762013-05-14 11:35:37 -07002886extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002887 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002888 if (IsBadJniVersion(args->version)) {
2889 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002890 return JNI_EVERSION;
2891 }
2892 Runtime::Options options;
2893 for (int i = 0; i < args->nOptions; ++i) {
2894 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002895 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002896 }
2897 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002898 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002899 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002900 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002901 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002902 bool started = runtime->Start();
2903 if (!started) {
2904 delete Thread::Current()->GetJniEnv();
2905 delete runtime->GetJavaVM();
2906 LOG(WARNING) << "CreateJavaVM failed";
2907 return JNI_ERR;
2908 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002909 *p_env = Thread::Current()->GetJniEnv();
2910 *p_vm = runtime->GetJavaVM();
2911 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002912}
2913
Elliott Hughesf2682d52011-08-15 16:37:04 -07002914extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002915 Runtime* runtime = Runtime::Current();
2916 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002917 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002918 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002919 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002920 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002921 }
2922 return JNI_OK;
2923}
2924
2925// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002926extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002927 return JNI_ERR;
2928}
2929
Elliott Hughescdf53122011-08-19 15:46:09 -07002930class JII {
2931 public:
2932 static jint DestroyJavaVM(JavaVM* vm) {
2933 if (vm == NULL) {
2934 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002935 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002936 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2937 delete raw_vm->runtime;
2938 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002939 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002940
Elliott Hughescdf53122011-08-19 15:46:09 -07002941 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002942 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002943 }
2944
2945 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002946 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002947 }
2948
2949 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002950 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002951 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002952 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002953 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2954 Runtime* runtime = raw_vm->runtime;
2955 runtime->DetachCurrentThread();
2956 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002957 }
2958
2959 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes3b7ffa12013-09-06 15:57:08 -07002960 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
2961 // and unlike other calls that take a JNI version doesn't care if you supply
2962 // JNI_VERSION_1_1, which we don't otherwise support.
2963 if (IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
Elliott Hughes83a25322013-03-14 11:18:53 -07002964 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07002965 return JNI_EVERSION;
2966 }
2967 if (vm == NULL || env == NULL) {
2968 return JNI_ERR;
2969 }
2970 Thread* thread = Thread::Current();
2971 if (thread == NULL) {
2972 *env = NULL;
2973 return JNI_EDETACHED;
2974 }
2975 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002976 return JNI_OK;
2977 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002978};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002979
Elliott Hughes88c5c352012-03-15 18:49:48 -07002980const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002981 NULL, // reserved0
2982 NULL, // reserved1
2983 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002984 JII::DestroyJavaVM,
2985 JII::AttachCurrentThread,
2986 JII::DetachCurrentThread,
2987 JII::GetEnv,
2988 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002989};
2990
Elliott Hughesa0957642011-09-02 14:27:33 -07002991JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002992 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002993 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002994 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002995 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002996 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002997 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002998 work_around_app_jni_bugs(false),
Ian Rogers62d6c772013-02-27 08:32:07 -08002999 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07003000 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07003001 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07003002 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003003 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003004 libraries(new Libraries),
3005 weak_globals_lock_("JNI weak global reference table lock"),
3006 weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
3007 allow_new_weak_globals_(true),
3008 weak_globals_add_condition_("weak globals add condition", weak_globals_lock_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003009 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003010 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07003011 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07003012 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07003013}
3014
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003015JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07003016 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07003017}
3018
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003019jweak JavaVMExt::AddWeakGlobalReference(Thread* self, mirror::Object* obj) {
3020 if (obj == nullptr) {
3021 return nullptr;
3022 }
3023 MutexLock mu(self, weak_globals_lock_);
3024 while (UNLIKELY(!allow_new_weak_globals_)) {
3025 weak_globals_add_condition_.WaitHoldingLocks(self);
3026 }
3027 IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj);
3028 return reinterpret_cast<jweak>(ref);
3029}
3030
3031void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
3032 MutexLock mu(self, weak_globals_lock_);
3033 if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) {
3034 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
3035 << "failed to find entry";
3036 }
3037}
3038
Elliott Hughes88c5c352012-03-15 18:49:48 -07003039void JavaVMExt::SetCheckJniEnabled(bool enabled) {
3040 check_jni = enabled;
3041 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07003042}
3043
Elliott Hughesae80b492012-04-24 10:43:17 -07003044void JavaVMExt::DumpForSigQuit(std::ostream& os) {
3045 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
3046 if (force_copy) {
3047 os << " (with forcecopy)";
3048 }
3049 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07003050 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07003051 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003052 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003053 os << "; pins=" << pin_table.Size();
3054 }
3055 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003056 ReaderMutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003057 os << "; globals=" << globals.Capacity();
3058 }
3059 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003060 MutexLock mu(self, weak_globals_lock_);
3061 if (weak_globals_.Capacity() > 0) {
3062 os << " (plus " << weak_globals_.Capacity() << " weak)";
Elliott Hughesae80b492012-04-24 10:43:17 -07003063 }
3064 }
3065 os << '\n';
3066
3067 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003068 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07003069 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
3070 }
3071}
3072
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003073void JavaVMExt::DisallowNewWeakGlobals() {
3074 MutexLock mu(Thread::Current(), weak_globals_lock_);
3075 allow_new_weak_globals_ = false;
3076}
3077
3078void JavaVMExt::AllowNewWeakGlobals() {
3079 Thread* self = Thread::Current();
3080 MutexLock mu(self, weak_globals_lock_);
3081 allow_new_weak_globals_ = true;
3082 weak_globals_add_condition_.Broadcast(self);
3083}
3084
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003085mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
3086 MutexLock mu(self, weak_globals_lock_);
3087 while (UNLIKELY(!allow_new_weak_globals_)) {
3088 weak_globals_add_condition_.WaitHoldingLocks(self);
3089 }
3090 return const_cast<mirror::Object*>(weak_globals_.Get(ref));
3091}
3092
Elliott Hughes73e66f72012-05-09 09:34:45 -07003093void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003094 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003095 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003096 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003097 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003098 }
3099 {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07003100 MutexLock mu(self, weak_globals_lock_);
3101 weak_globals_.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003102 }
3103 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003104 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07003105 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003106 }
3107}
3108
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003109bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -07003110 std::string* detail) {
3111 detail->clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07003112
3113 // See if we've already loaded this library. If we have, and the class loader
3114 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07003115 // TODO: for better results we should canonicalize the pathname (or even compare
3116 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07003117 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07003118 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07003119 {
3120 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07003121 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003122 library = libraries->Get(path);
3123 }
Elliott Hughescdf53122011-08-19 15:46:09 -07003124 if (library != NULL) {
3125 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07003126 // The library will be associated with class_loader. The JNI
3127 // spec says we can't load the same library into more than one
3128 // class loader.
Ian Rogers1eb512d2013-10-18 15:42:20 -07003129 StringAppendF(detail, "Shared library \"%s\" already opened by "
Elliott Hughes75770752011-08-24 17:52:38 -07003130 "ClassLoader %p; can't open in ClassLoader %p",
3131 path.c_str(), library->GetClassLoader(), class_loader);
3132 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003133 return false;
3134 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003135 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
3136 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07003137 if (!library->CheckOnLoadResult()) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003138 StringAppendF(detail, "JNI_OnLoad failed on a previous attempt "
Elliott Hughes75770752011-08-24 17:52:38 -07003139 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07003140 return false;
3141 }
3142 return true;
3143 }
3144
3145 // Open the shared library. Because we're using a full path, the system
3146 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
3147 // resolve this library's dependencies though.)
3148
3149 // Failures here are expected when java.library.path has several entries
3150 // and we have to hunt for the lib.
3151
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003152 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
3153 // class unloading. Libraries will only be unloaded when the reference count (incremented by
3154 // dlopen) becomes zero from dlclose.
3155
Elliott Hughescdf53122011-08-19 15:46:09 -07003156 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003157 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003158 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
3159 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
3160 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07003161
Elliott Hughes84b2f142012-09-27 09:16:28 -07003162 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07003163
3164 if (handle == NULL) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003165 *detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07003166 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07003167 return false;
3168 }
3169
3170 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003171 // TODO: move the locking (and more of this logic) into Libraries.
3172 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07003173 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003174 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07003175 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003176 if (library == NULL) { // We won race to get libraries_lock
3177 library = new SharedLibrary(path, handle, class_loader);
3178 libraries->Put(path, library);
3179 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07003180 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003181 }
3182 if (!created_library) {
3183 LOG(INFO) << "WOW: we lost a race to add shared library: "
3184 << "\"" << path << "\" ClassLoader=" << class_loader;
3185 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07003186 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003187
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003188 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003189
Elliott Hughes79353722013-08-02 16:52:18 -07003190 bool was_successful = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07003191 void* sym = dlsym(handle, "JNI_OnLoad");
3192 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003193 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes85affca2013-08-02 17:48:52 -07003194 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003195 } else {
3196 // Call JNI_OnLoad. We have to override the current class
3197 // loader, which will always be "null" since the stuff at the
3198 // top of the stack is around Runtime.loadLibrary(). (See
3199 // the comments in the JNI FindClass function.)
3200 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
3201 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07003202 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07003203 self->SetClassLoaderOverride(class_loader);
3204
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003205 int version = 0;
3206 {
Elliott Hughes34e06962012-04-09 13:55:55 -07003207 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08003208 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07003209 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07003210 }
Elliott Hughes79082e32011-08-25 12:07:32 -07003211
Brian Carlstromaded5f72011-10-07 17:15:04 -07003212 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07003213
Elliott Hughes79353722013-08-02 16:52:18 -07003214 if (version == JNI_ERR) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003215 StringAppendF(detail, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Elliott Hughes79353722013-08-02 16:52:18 -07003216 } else if (IsBadJniVersion(version)) {
Ian Rogers1eb512d2013-10-18 15:42:20 -07003217 StringAppendF(detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003218 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07003219 // It's unwise to call dlclose() here, but we can mark it
3220 // as bad and ensure that future load attempts will fail.
3221 // We don't know how far JNI_OnLoad got, so there could
3222 // be some partially-initialized stuff accessible through
3223 // newly-registered native method calls. We could try to
3224 // unregister them, but that doesn't seem worthwhile.
Elliott Hughes79353722013-08-02 16:52:18 -07003225 } else {
3226 was_successful = true;
Elliott Hughes79082e32011-08-25 12:07:32 -07003227 }
Elliott Hughes79353722013-08-02 16:52:18 -07003228 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07003229 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07003230 }
3231
Elliott Hughes79353722013-08-02 16:52:18 -07003232 library->SetResult(was_successful);
3233 return was_successful;
Elliott Hughes79082e32011-08-25 12:07:32 -07003234}
3235
Brian Carlstromea46f952013-07-30 01:26:50 -07003236void* JavaVMExt::FindCodeForNativeMethod(ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003237 CHECK(m->IsNative());
3238
3239 Class* c = m->GetDeclaringClass();
3240
3241 // If this is a static method, it could be called before the class
3242 // has been initialized.
3243 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07003244 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07003245 return NULL;
3246 }
3247 } else {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003248 CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07003249 }
3250
Brian Carlstrom16192862011-09-12 17:50:06 -07003251 std::string detail;
3252 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07003253 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07003254 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003255 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07003256 native_method = libraries->FindNativeMethod(m, detail);
3257 }
Ian Rogers62d6c772013-02-27 08:32:07 -08003258 // Throwing can cause libraries_lock to be reacquired.
Brian Carlstrom16192862011-09-12 17:50:06 -07003259 if (native_method == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -08003260 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
3261 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07003262 }
3263 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07003264}
3265
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003266void JavaVMExt::SweepJniWeakGlobals(RootVisitor visitor, void* arg) {
Mathieu Chartier810b1d72013-09-20 14:02:02 -07003267 MutexLock mu(Thread::Current(), weak_globals_lock_);
3268 for (mirror::Object** entry : weak_globals_) {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07003269 mirror::Object* obj = *entry;
3270 mirror::Object* new_obj = visitor(obj, arg);
3271 if (new_obj == nullptr) {
3272 new_obj = kClearedJniWeakGlobal;
3273 }
3274 *entry = new_obj;
3275 }
3276}
3277
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003278void JavaVMExt::VisitRoots(RootVisitor* visitor, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003279 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07003280 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07003281 ReaderMutexLock mu(self, globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003282 globals.VisitRoots(visitor, arg);
3283 }
3284 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003285 MutexLock mu(self, pins_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07003286 pin_table.VisitRoots(visitor, arg);
3287 }
3288 // The weak_globals table is visited by the GC itself (because it mutates the table).
3289}
3290
Elliott Hughesc8fece32013-01-02 11:27:23 -08003291void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07003292 jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08003293 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
3294 if (c.get() == NULL) {
3295 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
3296 }
3297 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
3298}
3299
Ian Rogersdf20fe02011-07-20 20:34:16 -07003300} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003301
3302std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3303 switch (rhs) {
3304 case JNIInvalidRefType:
3305 os << "JNIInvalidRefType";
3306 return os;
3307 case JNILocalRefType:
3308 os << "JNILocalRefType";
3309 return os;
3310 case JNIGlobalRefType:
3311 os << "JNIGlobalRefType";
3312 return os;
3313 case JNIWeakGlobalRefType:
3314 os << "JNIWeakGlobalRefType";
3315 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003316 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003317 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003318 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003319 }
3320}