blob: 60b49de83f884d76a179c0a29563f47519621c3d [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 Hughes40ef99e2011-08-11 17:44:34 -070025#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070026#include "class_loader.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070027#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070028#include "logging.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070029#include "mutex.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070030#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080031#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070032#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070033#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070034#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070035#include "ScopedLocalRef.h"
Elliott Hughesc31664f2011-09-29 15:58:28 -070036#include "stl_util.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070037#include "stringpiece.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070038#include "thread.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070039#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070040#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070041
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070042namespace art {
43
Elliott Hughes2ced6a52011-10-16 18:44:48 -070044static const size_t kMonitorsInitial = 32; // Arbitrary.
45static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
46
47static const size_t kLocalsInitial = 64; // Arbitrary.
48static const size_t kLocalsMax = 512; // Arbitrary sanity check.
49
50static const size_t kPinTableInitial = 16; // Arbitrary.
51static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
52
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070053static size_t gGlobalsInitial = 512; // Arbitrary.
54static size_t gGlobalsMax = 51200; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070055
56static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
57static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check.
58
Ian Rogers00f7d0e2012-07-19 15:28:27 -070059void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
60 size_t method_count) {
Elliott Hugheseac76672012-05-24 21:56:51 -070061 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
62 if (c.get() == NULL) {
63 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
64 }
65 if (env->RegisterNatives(c.get(), methods, method_count) != JNI_OK) {
66 LOG(FATAL) << "Failed to register natives methods: " << jni_class_name;
67 }
68}
69
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070070void SetJniGlobalsMax(size_t max) {
71 if (max != 0) {
72 gGlobalsMax = max;
73 gGlobalsInitial = std::min(gGlobalsInitial, gGlobalsMax);
74 }
75}
Ian Rogersdf20fe02011-07-20 20:34:16 -070076
Ian Rogers45619fc2012-02-29 11:15:25 -080077size_t NumArgArrayBytes(const char* shorty, uint32_t shorty_len) {
78 size_t num_bytes = 0;
79 for (size_t i = 1; i < shorty_len; ++i) {
80 char ch = shorty[i];
81 if (ch == 'D' || ch == 'J') {
82 num_bytes += 8;
83 } else if (ch == 'L') {
84 // Argument is a reference or an array. The shorty descriptor
85 // does not distinguish between these types.
86 num_bytes += sizeof(Object*);
87 } else {
88 num_bytes += 4;
89 }
90 }
91 return num_bytes;
92}
93
94class ArgArray {
95 public:
Mathieu Chartier66f19252012-09-18 08:57:04 -070096 explicit ArgArray(AbstractMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers45619fc2012-02-29 11:15:25 -080097 MethodHelper mh(method);
98 shorty_ = mh.GetShorty();
99 shorty_len_ = mh.GetShortyLength();
Elliott Hughes77405792012-03-15 15:22:12 -0700100 if (shorty_len_ - 1 < kSmallArgArraySize) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800101 arg_array_ = small_arg_array_;
102 } else {
Elliott Hughes77405792012-03-15 15:22:12 -0700103 large_arg_array_.reset(new JValue[shorty_len_ - 1]);
Ian Rogers45619fc2012-02-29 11:15:25 -0800104 arg_array_ = large_arg_array_.get();
105 }
106 }
107
Elliott Hughes77405792012-03-15 15:22:12 -0700108 JValue* get() {
Ian Rogers45619fc2012-02-29 11:15:25 -0800109 return arg_array_;
110 }
111
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700112 void BuildArgArray(const ScopedObjectAccess& soa, va_list ap)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700113 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes77405792012-03-15 15:22:12 -0700114 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800115 switch (shorty_[i]) {
116 case 'Z':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700117 arg_array_[offset].SetZ(va_arg(ap, jint));
118 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800119 case 'B':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700120 arg_array_[offset].SetB(va_arg(ap, jint));
121 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800122 case 'C':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700123 arg_array_[offset].SetC(va_arg(ap, jint));
124 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800125 case 'S':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700126 arg_array_[offset].SetS(va_arg(ap, jint));
127 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800128 case 'I':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700129 arg_array_[offset].SetI(va_arg(ap, jint));
Ian Rogers45619fc2012-02-29 11:15:25 -0800130 break;
131 case 'F':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700132 arg_array_[offset].SetF(va_arg(ap, jdouble));
Ian Rogers45619fc2012-02-29 11:15:25 -0800133 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700134 case 'L':
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700135 arg_array_[offset].SetL(soa.Decode<Object*>(va_arg(ap, jobject)));
Ian Rogers45619fc2012-02-29 11:15:25 -0800136 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800137 case 'D':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700138 arg_array_[offset].SetD(va_arg(ap, jdouble));
Ian Rogers45619fc2012-02-29 11:15:25 -0800139 break;
140 case 'J':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700141 arg_array_[offset].SetJ(va_arg(ap, jlong));
Ian Rogers45619fc2012-02-29 11:15:25 -0800142 break;
143 }
144 }
145 }
146
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700147 void BuildArgArray(const ScopedObjectAccess& soa, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700148 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes77405792012-03-15 15:22:12 -0700149 for (size_t i = 1, offset = 0; i < shorty_len_; ++i, ++offset) {
Ian Rogers45619fc2012-02-29 11:15:25 -0800150 switch (shorty_[i]) {
151 case 'Z':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700152 arg_array_[offset].SetZ(args[offset].z);
153 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800154 case 'B':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700155 arg_array_[offset].SetB(args[offset].b);
156 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800157 case 'C':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700158 arg_array_[offset].SetC(args[offset].c);
159 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800160 case 'S':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700161 arg_array_[offset].SetS(args[offset].s);
162 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800163 case 'I':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700164 arg_array_[offset].SetI(args[offset].i);
Ian Rogers45619fc2012-02-29 11:15:25 -0800165 break;
166 case 'F':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700167 arg_array_[offset].SetF(args[offset].f);
Ian Rogers45619fc2012-02-29 11:15:25 -0800168 break;
Elliott Hughes77405792012-03-15 15:22:12 -0700169 case 'L':
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700170 arg_array_[offset].SetL(soa.Decode<Object*>(args[offset].l));
Ian Rogers45619fc2012-02-29 11:15:25 -0800171 break;
Ian Rogers45619fc2012-02-29 11:15:25 -0800172 case 'D':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700173 arg_array_[offset].SetD(args[offset].d);
Ian Rogers45619fc2012-02-29 11:15:25 -0800174 break;
175 case 'J':
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700176 arg_array_[offset].SetJ(args[offset].j);
Ian Rogers45619fc2012-02-29 11:15:25 -0800177 break;
178 }
179 }
180 }
181
Ian Rogers45619fc2012-02-29 11:15:25 -0800182 private:
Elliott Hughes77405792012-03-15 15:22:12 -0700183 enum { kSmallArgArraySize = 16 };
Ian Rogers45619fc2012-02-29 11:15:25 -0800184 const char* shorty_;
185 uint32_t shorty_len_;
Elliott Hughes77405792012-03-15 15:22:12 -0700186 JValue* arg_array_;
187 JValue small_arg_array_[kSmallArgArraySize];
188 UniquePtr<JValue[]> large_arg_array_;
Ian Rogers45619fc2012-02-29 11:15:25 -0800189};
190
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700191static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700192 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700193 if (obj == NULL) {
194 return NULL;
195 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700196 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700197 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -0700198 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700199 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
200 return reinterpret_cast<jweak>(ref);
201}
202
Mathieu Chartier66f19252012-09-18 08:57:04 -0700203static void CheckMethodArguments(AbstractMethod* m, JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700204 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700205 MethodHelper mh(m);
Ian Rogers50b35e22012-10-04 10:09:15 -0700206 const DexFile::TypeList* params = mh.GetParameterTypeList();
207 if (params == NULL) {
208 return; // No arguments so nothing to check.
209 }
210 uint32_t num_params = params->Size();
Elliott Hughesb264f082012-04-06 17:10:10 -0700211 size_t error_count = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -0700212 for (uint32_t i = 0; i < num_params; i++) {
213 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
214 Class* param_type = mh.GetClassFromTypeIdx(type_idx);
215 if (param_type == NULL) {
216 Thread* self = Thread::Current();
217 CHECK(self->IsExceptionPending());
218 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
219 << mh.GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
220 << self->GetException()->Dump();
221 self->ClearException();
222 ++error_count;
223 } else if (!param_type->IsPrimitive()) {
224 // TODO: check primitives are in range.
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700225 Object* argument = args[i].GetL();
Ian Rogers50b35e22012-10-04 10:09:15 -0700226 if (argument != NULL && !argument->InstanceOf(param_type)) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700227 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
228 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
229 ++error_count;
230 }
231 }
232 }
233 if (error_count > 0) {
234 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
235 // with an argument.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700236 JniAbortF(NULL, "bad arguments passed to %s (see above for details)", PrettyMethod(m).c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -0700237 }
238}
Elliott Hughesb264f082012-04-06 17:10:10 -0700239
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700240static JValue InvokeWithArgArray(const ScopedObjectAccess& soa, Object* receiver,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700241 AbstractMethod* method, JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700242 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700243 if (UNLIKELY(soa.Env()->check_jni)) {
Elliott Hughes4cacde82012-04-11 18:32:27 -0700244 CheckMethodArguments(method, args);
245 }
Elliott Hughes1d878f32012-04-11 15:17:54 -0700246 JValue result;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700247 method->Invoke(soa.Self(), receiver, args, &result);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700248 return result;
249}
250
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700251static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
252 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700253 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700254 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700255 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800256 ArgArray arg_array(method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700257 arg_array.BuildArgArray(soa, args);
258 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700259}
260
Mathieu Chartier66f19252012-09-18 08:57:04 -0700261static AbstractMethod* FindVirtualMethod(Object* receiver, AbstractMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700262 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700263 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700264}
265
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700266static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
267 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700268 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700269 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700270 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800271 ArgArray arg_array(method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272 arg_array.BuildArgArray(soa, args);
273 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700274}
275
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700276static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
277 jobject obj, jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700278 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700279 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700280 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers45619fc2012-02-29 11:15:25 -0800281 ArgArray arg_array(method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700282 arg_array.BuildArgArray(soa, args);
283 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Carl Shapiroea4dca82011-08-01 13:45:38 -0700284}
285
Elliott Hughes6b436852011-08-12 10:16:44 -0700286// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
287// separated with slashes but aren't wrapped with "L;" like regular descriptors
288// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
289// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
290// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700291static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700292 std::string result;
293 // Add the missing "L;" if necessary.
294 if (name[0] == '[') {
295 result = name;
296 } else {
297 result += 'L';
298 result += name;
299 result += ';';
300 }
301 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700302 if (result.find('.') != std::string::npos) {
303 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
304 << "\"" << name << "\"";
305 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700306 }
307 return result;
308}
309
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700310static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, Class* c,
311 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700312 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700313 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Elliott Hughes91250e02011-12-13 22:30:35 -0800314 "no %s method \"%s.%s%s\"", kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700315}
316
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700317static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
318 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700319 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700320 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700321 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700322 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700323 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700324
Mathieu Chartier66f19252012-09-18 08:57:04 -0700325 AbstractMethod* method = NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700326 if (is_static) {
327 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700328 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700329 method = c->FindVirtualMethod(name, sig);
330 if (method == NULL) {
331 // No virtual method matching the signature. Search declared
332 // private methods and constructors.
333 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700334 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700335 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700336
Elliott Hughescdf53122011-08-19 15:46:09 -0700337 if (method == NULL || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700338 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700339 return NULL;
340 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700341
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700342 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700343}
344
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700345static ClassLoader* GetClassLoader(Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700346 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700347 AbstractMethod* method = self->GetCurrentMethod();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700348 if (method == NULL || PrettyMethod(method, false) == "java.lang.Runtime.nativeLoad") {
349 return self->GetClassLoaderOverride();
350 }
351 return method->GetDeclaringClass()->GetClassLoader();
352}
353
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700354static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
355 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700356 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700357 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700358 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700359 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700360 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700361
362 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700363 Class* field_type;
364 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
365 if (sig[1] != '\0') {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700366 ClassLoader* cl = GetClassLoader(soa.Self());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700367 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700368 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700369 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700370 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700371 if (field_type == NULL) {
372 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700373 DCHECK(soa.Self()->IsExceptionPending());
374 soa.Self()->ClearException();
375 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogersb17d08b2011-09-02 16:16:49 -0700376 "no type \"%s\" found and so no field \"%s\" could be found in class "
Elliott Hughes91250e02011-12-13 22:30:35 -0800377 "\"%s\" or its superclasses", sig, name, ClassHelper(c).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700378 return NULL;
379 }
380 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800381 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700382 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800383 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700384 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700385 if (field == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700386 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughescdf53122011-08-19 15:46:09 -0700387 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
Elliott Hughes91250e02011-12-13 22:30:35 -0800388 name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700389 return NULL;
390 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700391 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700392}
393
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700394static void PinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700395 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700396 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700397 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700398 vm->pin_table.Add(array);
399}
400
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700401static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700402 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700403 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700404 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700405 vm->pin_table.Remove(array);
406}
407
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700408static void ThrowAIOOBE(ScopedObjectAccess& soa, Array* array, jsize start,
409 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700410 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700411 std::string type(PrettyTypeOf(array));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700412 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes814e4032011-08-23 12:07:56 -0700413 "%s offset=%d length=%d %s.length=%d",
414 type.c_str(), start, length, identifier, array->GetLength());
415}
Ian Rogers0571d352011-11-03 19:51:38 -0700416
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700417static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
418 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700419 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700420 soa.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Elliott Hughesb465ab02011-08-24 11:21:21 -0700421 "offset=%d length=%d string.length()=%d", start, length, array_length);
422}
Elliott Hughes814e4032011-08-23 12:07:56 -0700423
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700424int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700425 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700426 ScopedObjectAccess soa(env);
427
428 // Turn the const char* into a java.lang.String.
429 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
430 if (msg != NULL && s.get() == NULL) {
431 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700432 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700433
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700434 // Choose an appropriate constructor and set up the arguments.
435 jvalue args[2];
436 const char* signature;
437 if (msg == NULL && cause == NULL) {
438 signature = "()V";
439 } else if (msg != NULL && cause == NULL) {
440 signature = "(Ljava/lang/String;)V";
441 args[0].l = s.get();
442 } else if (msg == NULL && cause != NULL) {
443 signature = "(Ljava/lang/Throwable;)V";
444 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700445 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700446 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
447 args[0].l = s.get();
448 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700449 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700450 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
451 if (mid == NULL) {
452 LOG(ERROR) << "No <init>" << signature << " in "
453 << PrettyClass(soa.Decode<Class*>(exception_class));
454 return JNI_ERR;
455 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700456
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700457 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
458 if (exception.get() == NULL) {
459 return JNI_ERR;
460 }
Elliott Hughesa4f94742012-05-29 16:28:38 -0700461
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700462 soa.Self()->SetException(soa.Decode<Throwable*>(exception.get()));
Elliott Hughesa4f94742012-05-29 16:28:38 -0700463
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700464 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700465}
466
Elliott Hughes462c9442012-03-23 18:47:50 -0700467static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700468 if (vm == NULL || p_env == NULL) {
469 return JNI_ERR;
470 }
471
Elliott Hughes462c9442012-03-23 18:47:50 -0700472 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700473 Thread* self = Thread::Current();
474 if (self != NULL) {
475 *p_env = self->GetJniEnv();
476 return JNI_OK;
477 }
478
479 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
480
481 // No threads allowed in zygote mode.
482 if (runtime->IsZygote()) {
483 LOG(ERROR) << "Attempt to attach a thread in the zygote";
484 return JNI_ERR;
485 }
486
Elliott Hughes462c9442012-03-23 18:47:50 -0700487 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
488 const char* thread_name = NULL;
Ian Rogers365c1022012-06-22 15:05:28 -0700489 jobject thread_group = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -0700490 if (args != NULL) {
491 CHECK_GE(args->version, JNI_VERSION_1_2);
492 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700493 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700494 }
Elliott Hughes75770752011-08-24 17:52:38 -0700495
Ian Rogers120f1c72012-09-28 17:17:10 -0700496 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group)) {
497 *p_env = NULL;
498 return JNI_ERR;
499 } else {
500 *p_env = Thread::Current()->GetJniEnv();
501 return JNI_OK;
502 }
Elliott Hughes75770752011-08-24 17:52:38 -0700503}
504
Elliott Hughes79082e32011-08-25 12:07:32 -0700505class SharedLibrary {
506 public:
507 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
508 : path_(path),
509 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700510 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700511 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700512 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700513 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700514 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700515 }
516
Elliott Hughes79082e32011-08-25 12:07:32 -0700517 Object* GetClassLoader() {
518 return class_loader_;
519 }
520
521 std::string GetPath() {
522 return path_;
523 }
524
525 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700526 * Check the result of an earlier call to JNI_OnLoad on this library.
527 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700528 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700529 bool CheckOnLoadResult()
530 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700531 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700532 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700533 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
534 bool okay;
535 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700536 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700537
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700538 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
539 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
540 // caller can continue.
541 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
542 okay = true;
543 } else {
544 while (jni_on_load_result_ == kPending) {
545 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700546 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700547 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700548
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700549 okay = (jni_on_load_result_ == kOkay);
550 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
551 << (okay ? "succeeded" : "failed") << "]";
552 }
553 }
554 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700555 return okay;
556 }
557
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700558 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700559 Thread* self = Thread::Current();
560 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700561
Elliott Hughes79082e32011-08-25 12:07:32 -0700562 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700563 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700564
565 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700566 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700567 }
568
569 void* FindSymbol(const std::string& symbol_name) {
570 return dlsym(handle_, symbol_name.c_str());
571 }
572
573 private:
574 enum JNI_OnLoadState {
575 kPending,
576 kFailed,
577 kOkay,
578 };
579
580 // Path to library "/system/lib/libjni.so".
581 std::string path_;
582
583 // The void* returned by dlopen(3).
584 void* handle_;
585
586 // The ClassLoader this library is associated with.
587 Object* class_loader_;
588
589 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700590 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700591 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700592 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700593 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700594 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700595 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700596 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700597};
598
Elliott Hughes79082e32011-08-25 12:07:32 -0700599// This exists mainly to keep implementation details out of the header file.
600class Libraries {
601 public:
602 Libraries() {
603 }
604
605 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700606 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700607 }
608
Elliott Hughesae80b492012-04-24 10:43:17 -0700609 void Dump(std::ostream& os) const {
610 bool first = true;
611 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
612 if (!first) {
613 os << ' ';
614 }
615 first = false;
616 os << it->first;
617 }
618 }
619
620 size_t size() const {
621 return libraries_.size();
622 }
623
Elliott Hughes79082e32011-08-25 12:07:32 -0700624 SharedLibrary* Get(const std::string& path) {
Ian Rogers712462a2012-04-12 16:32:29 -0700625 It it = libraries_.find(path);
626 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700627 }
628
629 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700630 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700631 }
632
633 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700634 void* FindNativeMethod(const AbstractMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700635 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700636 std::string jni_short_name(JniShortName(m));
637 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700638 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700639 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
640 SharedLibrary* library = it->second;
641 if (library->GetClassLoader() != declaring_class_loader) {
642 // We only search libraries loaded by the appropriate ClassLoader.
643 continue;
644 }
645 // Try the short name then the long name...
646 void* fn = library->FindSymbol(jni_short_name);
647 if (fn == NULL) {
648 fn = library->FindSymbol(jni_long_name);
649 }
650 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800651 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
652 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700653 return fn;
654 }
655 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700656 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700657 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700658 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700659 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700660 return NULL;
661 }
662
663 private:
Elliott Hughesae80b492012-04-24 10:43:17 -0700664 typedef SafeMap<std::string, SharedLibrary*>::const_iterator It; // TODO: C++0x auto
Elliott Hughes79082e32011-08-25 12:07:32 -0700665
Elliott Hughesa0e18062012-04-13 15:59:59 -0700666 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700667};
668
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700669JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
670 jvalue* args) {
671 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700672 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers45619fc2012-02-29 11:15:25 -0800673 ArgArray arg_array(method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700674 arg_array.BuildArgArray(soa, args);
675 return InvokeWithArgArray(soa, receiver, method, arg_array.get());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700676}
677
Mathieu Chartier66f19252012-09-18 08:57:04 -0700678JValue InvokeWithJValues(const ScopedObjectAccess& soa, Object* receiver, AbstractMethod* m,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700679 JValue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700680 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700681 return InvokeWithArgArray(soa, receiver, m, args);
Elliott Hughesd07986f2011-12-06 18:27:45 -0800682}
683
Elliott Hughescdf53122011-08-19 15:46:09 -0700684class JNI {
685 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700686 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700687 return JNI_VERSION_1_6;
688 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700689
Ian Rogers25e8b912012-09-07 11:31:36 -0700690 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700691 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700692 return NULL;
693 }
694
Elliott Hughescdf53122011-08-19 15:46:09 -0700695 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700696 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700697 Runtime* runtime = Runtime::Current();
698 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700699 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700700 Class* c = NULL;
701 if (runtime->IsStarted()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700702 ClassLoader* cl = GetClassLoader(soa.Self());
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800703 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700704 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800705 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700706 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700707 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700708 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700709
Elliott Hughescdf53122011-08-19 15:46:09 -0700710 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700711 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700712 AbstractMethod* method = soa.Decode<AbstractMethod*>(java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700713 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700714 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700715
Elliott Hughescdf53122011-08-19 15:46:09 -0700716 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700717 ScopedObjectAccess soa(env);
718 Field* field = soa.Decode<Field*>(java_field);
719 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700720 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700721
Elliott Hughescdf53122011-08-19 15:46:09 -0700722 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700723 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700724 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700725 return soa.AddLocalReference<jobject>(method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700726 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700727
Elliott Hughescdf53122011-08-19 15:46:09 -0700728 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700729 ScopedObjectAccess soa(env);
730 Field* field = soa.DecodeField(fid);
731 return soa.AddLocalReference<jobject>(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700732 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700733
Elliott Hughes37f7a402011-08-22 18:56:01 -0700734 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700735 ScopedObjectAccess soa(env);
736 Object* o = soa.Decode<Object*>(java_object);
737 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700738 }
739
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700740 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700741 ScopedObjectAccess soa(env);
742 Class* c = soa.Decode<Class*>(java_class);
743 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700744 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700745
Elliott Hughes37f7a402011-08-22 18:56:01 -0700746 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700747 ScopedObjectAccess soa(env);
748 Class* c1 = soa.Decode<Class*>(java_class1);
749 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700750 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700751 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700752
Elliott Hughese84278b2012-03-22 10:06:53 -0700753 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700754 ScopedObjectAccess soa(env);
Elliott Hughese84278b2012-03-22 10:06:53 -0700755 CHECK_NE(static_cast<jclass>(NULL), java_class); // TODO: ReportJniError
Elliott Hughes37f7a402011-08-22 18:56:01 -0700756 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700757 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700758 return JNI_TRUE;
759 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700760 Object* obj = soa.Decode<Object*>(jobj);
761 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700762 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700763 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700764 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700765
Elliott Hughes37f7a402011-08-22 18:56:01 -0700766 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700767 ScopedObjectAccess soa(env);
768 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700769 if (exception == NULL) {
770 return JNI_ERR;
771 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700772 soa.Self()->SetException(exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700773 return JNI_OK;
774 }
775
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700776 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700777 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700778 }
779
780 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700781 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700782 }
783
784 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700785 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700786 }
787
788 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700789 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700790
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700791 Thread* self = soa.Self();
Elliott Hughes72025e52011-08-23 17:50:30 -0700792 Throwable* original_exception = self->GetException();
793 self->ClearException();
794
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700795 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(original_exception));
Elliott Hughes72025e52011-08-23 17:50:30 -0700796 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
797 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
798 if (mid == NULL) {
799 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Elliott Hughes54e7df12011-09-16 11:47:04 -0700800 << PrettyTypeOf(original_exception);
Elliott Hughes72025e52011-08-23 17:50:30 -0700801 } else {
802 env->CallVoidMethod(exception.get(), mid);
803 if (self->IsExceptionPending()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700804 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(self->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700805 << " thrown while calling printStackTrace";
806 self->ClearException();
807 }
808 }
809
810 self->SetException(original_exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700811 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700812
Elliott Hughescdf53122011-08-19 15:46:09 -0700813 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700814 ScopedObjectAccess soa(env);
815 Object* exception = soa.Self()->GetException();
816 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700817 }
818
Ian Rogers25e8b912012-09-07 11:31:36 -0700819 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700820 LOG(FATAL) << "JNI FatalError called: " << msg;
821 }
822
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700823 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700824 ScopedObjectAccess soa(env);
825 if (EnsureLocalCapacity(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700826 return JNI_ERR;
827 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700828 soa.Env()->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700829 return JNI_OK;
830 }
831
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700832 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700833 ScopedObjectAccess soa(env);
834 Object* survivor = soa.Decode<Object*>(java_survivor);
835 soa.Env()->PopFrame();
836 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700837 }
838
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700839 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700840 ScopedObjectAccess soa(env);
841 return EnsureLocalCapacity(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700842 }
843
Elliott Hughescdf53122011-08-19 15:46:09 -0700844 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700845 if (obj == NULL) {
846 return NULL;
847 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700848 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700849 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700850 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700851 Object* decoded_obj = soa.Decode<Object*>(obj);
Ian Rogers50b35e22012-10-04 10:09:15 -0700852 MutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700853 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700854 return reinterpret_cast<jobject>(ref);
855 }
856
857 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700858 if (obj == NULL) {
859 return;
860 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700861 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700862 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700863 IndirectReferenceTable& globals = vm->globals;
Ian Rogers50b35e22012-10-04 10:09:15 -0700864 MutexLock mu(soa.Self(), vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700865
866 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
867 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
868 << "failed to find entry";
869 }
870 }
871
872 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700873 ScopedObjectAccess soa(env);
874 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700875 }
876
877 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700878 if (obj == NULL) {
879 return;
880 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700881 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700882 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700883 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -0700884 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700885
886 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
887 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
888 << "failed to find entry";
889 }
890 }
891
892 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700893 if (obj == NULL) {
894 return NULL;
895 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700896 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700897 IndirectReferenceTable& locals = soa.Env()->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700898
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700899 uint32_t cookie = soa.Env()->local_ref_cookie;
900 IndirectRef ref = locals.Add(cookie, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700901 return reinterpret_cast<jobject>(ref);
902 }
903
904 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700905 if (obj == NULL) {
906 return;
907 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700908 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700909 IndirectReferenceTable& locals = soa.Env()->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700910
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700911 uint32_t cookie = soa.Env()->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700912 if (!locals.Remove(cookie, obj)) {
913 // Attempting to delete a local reference that is not in the
914 // topmost local reference frame is a no-op. DeleteLocalRef returns
915 // void and doesn't throw any exceptions, but we should probably
916 // complain about it so the user will notice that things aren't
917 // going quite the way they expect.
918 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
919 << "failed to find entry";
920 }
921 }
922
923 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700924 ScopedObjectAccess soa(env);
Ian Rogers120f1c72012-09-28 17:17:10 -0700925 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700926 }
927
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700928 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700929 ScopedObjectAccess soa(env);
930 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700931 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700932 return NULL;
933 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700934 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700935 }
936
Elliott Hughese84278b2012-03-22 10:06:53 -0700937 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700938 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700939 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -0700940 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700941 va_end(args);
942 return result;
943 }
944
Elliott Hughes72025e52011-08-23 17:50:30 -0700945 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700946 ScopedObjectAccess soa(env);
947 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700948 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700949 return NULL;
950 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700951 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700952 if (result == NULL) {
953 return NULL;
954 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700955 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700956 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700957 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700958 return local_result;
959 } else {
960 return NULL;
961 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700962 }
963
Elliott Hughes72025e52011-08-23 17:50:30 -0700964 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700965 ScopedObjectAccess soa(env);
966 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700967 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700968 return NULL;
969 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700970 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700971 if (result == NULL) {
972 return NULL;
973 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700974 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700975 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700976 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700977 return local_result;
978 } else {
979 return NULL;
980 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700981 }
982
Elliott Hughescdf53122011-08-19 15:46:09 -0700983 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700984 ScopedObjectAccess soa(env);
985 return FindMethodID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700986 }
987
988 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700989 ScopedObjectAccess soa(env);
990 return FindMethodID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 }
992
Elliott Hughes72025e52011-08-23 17:50:30 -0700993 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700994 va_list ap;
995 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700996 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700997 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700998 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700999 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001000 }
1001
Elliott Hughes72025e52011-08-23 17:50:30 -07001002 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001003 ScopedObjectAccess soa(env);
1004 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
1005 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001006 }
1007
Elliott Hughes72025e52011-08-23 17:50:30 -07001008 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001009 ScopedObjectAccess soa(env);
1010 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
1011 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001012 }
1013
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 va_list ap;
1016 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001017 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001018 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001019 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001020 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 }
1022
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001024 ScopedObjectAccess soa(env);
1025 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 }
1027
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001029 ScopedObjectAccess soa(env);
1030 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001031 }
1032
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001034 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001035 va_list ap;
1036 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001037 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001038 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001039 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 }
1041
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001043 ScopedObjectAccess soa(env);
1044 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 }
1046
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001048 ScopedObjectAccess soa(env);
1049 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001050 }
1051
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001053 va_list ap;
1054 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001055 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001056 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001057 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001058 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 }
1060
Elliott Hughes72025e52011-08-23 17:50:30 -07001061 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001062 ScopedObjectAccess soa(env);
1063 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 }
1065
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001067 ScopedObjectAccess soa(env);
1068 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001069 }
1070
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001072 va_list ap;
1073 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001074 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001075 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001076 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001077 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 }
1079
Elliott Hughes72025e52011-08-23 17:50:30 -07001080 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001081 ScopedObjectAccess soa(env);
1082 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 }
1084
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001086 ScopedObjectAccess soa(env);
1087 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001088 }
1089
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001091 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001092 va_list ap;
1093 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001094 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001095 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001096 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 }
1098
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001100 ScopedObjectAccess soa(env);
1101 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 }
1103
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001105 ScopedObjectAccess soa(env);
1106 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001107 }
1108
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001110 va_list ap;
1111 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001112 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001113 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001114 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001115 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 }
1117
Elliott Hughes72025e52011-08-23 17:50:30 -07001118 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001119 ScopedObjectAccess soa(env);
1120 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001121 }
1122
Elliott Hughes72025e52011-08-23 17:50:30 -07001123 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001124 ScopedObjectAccess soa(env);
1125 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001126 }
1127
Elliott Hughes72025e52011-08-23 17:50:30 -07001128 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001129 va_list ap;
1130 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001131 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001132 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001133 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001134 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001135 }
1136
Elliott Hughes72025e52011-08-23 17:50:30 -07001137 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001138 ScopedObjectAccess soa(env);
1139 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 }
1141
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001143 ScopedObjectAccess soa(env);
1144 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001145 }
1146
Elliott Hughes72025e52011-08-23 17:50:30 -07001147 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001148 va_list ap;
1149 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001150 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001151 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001152 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001153 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001154 }
1155
Elliott Hughes72025e52011-08-23 17:50:30 -07001156 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001157 ScopedObjectAccess soa(env);
1158 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 }
1160
Elliott Hughes72025e52011-08-23 17:50:30 -07001161 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001162 ScopedObjectAccess soa(env);
1163 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 }
1165
Elliott Hughes72025e52011-08-23 17:50:30 -07001166 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001167 va_list ap;
1168 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001169 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001170 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001171 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001172 }
1173
Elliott Hughes72025e52011-08-23 17:50:30 -07001174 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001175 ScopedObjectAccess soa(env);
1176 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001177 }
1178
Elliott Hughes72025e52011-08-23 17:50:30 -07001179 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001180 ScopedObjectAccess soa(env);
1181 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 }
1183
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001184 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001185 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001186 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001187 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001188 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1189 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 va_end(ap);
1191 return local_result;
1192 }
1193
1194 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001195 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001196 ScopedObjectAccess soa(env);
1197 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1198 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001199 }
1200
1201 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001202 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001203 ScopedObjectAccess soa(env);
1204 JValue result(InvokeWithJValues(soa, obj, mid, args));
1205 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 }
1207
1208 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001209 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001211 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001212 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001213 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001214 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001215 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001216 }
1217
1218 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001219 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001220 ScopedObjectAccess soa(env);
1221 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001222 }
1223
1224 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001225 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001226 ScopedObjectAccess soa(env);
1227 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 }
1229
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001230 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001231 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001232 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001233 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001234 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001235 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001236 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 }
1238
1239 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001240 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001241 ScopedObjectAccess soa(env);
1242 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001243 }
1244
1245 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001246 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001247 ScopedObjectAccess soa(env);
1248 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001249 }
1250
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001251 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001252 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001254 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001255 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001256 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001257 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 }
1259
1260 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001261 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001262 ScopedObjectAccess soa(env);
1263 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001264 }
1265
1266 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001267 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001268 ScopedObjectAccess soa(env);
1269 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001270 }
1271
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001272 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001273 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001274 va_start(ap, 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.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001279 }
1280
1281 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001282 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001283 ScopedObjectAccess soa(env);
1284 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 }
1286
1287 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001288 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001289 ScopedObjectAccess soa(env);
1290 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001291 }
1292
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001293 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001294 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001295 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001296 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001297 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001299 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001300 }
1301
1302 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001303 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001304 ScopedObjectAccess soa(env);
1305 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001306 }
1307
1308 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001309 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001310 ScopedObjectAccess soa(env);
1311 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001312 }
1313
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001314 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001315 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001316 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001317 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001318 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001319 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001320 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001321 }
1322
1323 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001324 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001325 ScopedObjectAccess soa(env);
1326 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 }
1328
1329 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001330 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001331 ScopedObjectAccess soa(env);
1332 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 }
1334
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001335 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001336 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001337 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001338 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001339 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001340 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001341 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001342 }
1343
1344 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001345 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001346 ScopedObjectAccess soa(env);
1347 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001348 }
1349
1350 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001351 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001352 ScopedObjectAccess soa(env);
1353 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001354 }
1355
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001356 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001358 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001359 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001360 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001361 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001362 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001363 }
1364
1365 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001366 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001367 ScopedObjectAccess soa(env);
1368 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001369 }
1370
1371 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001372 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001373 ScopedObjectAccess soa(env);
1374 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001375 }
1376
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001377 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001378 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001379 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001380 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001381 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001382 va_end(ap);
1383 }
1384
1385 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001386 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001387 ScopedObjectAccess soa(env);
1388 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001389 }
1390
1391 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001392 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001393 ScopedObjectAccess soa(env);
1394 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001395 }
1396
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001397 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001398 ScopedObjectAccess soa(env);
1399 return FindFieldID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001401
1402
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001403 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001404 ScopedObjectAccess soa(env);
1405 return FindFieldID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001407
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001408 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001409 ScopedObjectAccess soa(env);
1410 Object* o = soa.Decode<Object*>(obj);
1411 Field* f = soa.DecodeField(fid);
1412 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001413 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001414
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001415 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001416 ScopedObjectAccess soa(env);
1417 Field* f = soa.DecodeField(fid);
1418 return soa.AddLocalReference<jobject>(f->GetObject(NULL));
Elliott Hughescdf53122011-08-19 15:46:09 -07001419 }
1420
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001421 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001422 ScopedObjectAccess soa(env);
1423 Object* o = soa.Decode<Object*>(java_object);
1424 Object* v = soa.Decode<Object*>(java_value);
1425 Field* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001426 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001427 }
1428
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001429 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001430 ScopedObjectAccess soa(env);
1431 Object* v = soa.Decode<Object*>(java_value);
1432 Field* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001433 f->SetObject(NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 }
1435
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001436#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001437 ScopedObjectAccess soa(env); \
1438 Object* o = soa.Decode<Object*>(instance); \
1439 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001440 return f->fn(o)
1441
1442#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001443 ScopedObjectAccess soa(env); \
1444 Object* o = soa.Decode<Object*>(instance); \
1445 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001446 f->fn(o, value)
1447
1448 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1449 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001450 }
1451
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001452 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1453 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001454 }
1455
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001456 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1457 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001458 }
1459
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001460 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1461 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001462 }
1463
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001464 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1465 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001466 }
1467
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001468 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1469 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001470 }
1471
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001472 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1473 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001474 }
1475
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001476 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1477 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001478 }
1479
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001480 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001481 GET_PRIMITIVE_FIELD(GetBoolean, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001482 }
1483
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001484 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001485 GET_PRIMITIVE_FIELD(GetByte, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001486 }
1487
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001488 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001489 GET_PRIMITIVE_FIELD(GetChar, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001490 }
1491
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001492 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001493 GET_PRIMITIVE_FIELD(GetShort, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001494 }
1495
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001496 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001497 GET_PRIMITIVE_FIELD(GetInt, NULL);
Elliott Hughescdf53122011-08-19 15:46:09 -07001498 }
1499
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001500 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001501 GET_PRIMITIVE_FIELD(GetLong, NULL);
1502 }
1503
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001504 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001505 GET_PRIMITIVE_FIELD(GetFloat, NULL);
1506 }
1507
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001508 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001509 GET_PRIMITIVE_FIELD(GetDouble, NULL);
1510 }
1511
1512 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1513 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1514 }
1515
1516 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1517 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1518 }
1519
1520 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1521 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1522 }
1523
1524 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1525 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1526 }
1527
1528 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1529 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1530 }
1531
1532 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1533 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1534 }
1535
1536 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1537 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1538 }
1539
1540 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1541 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1542 }
1543
1544 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1545 SET_PRIMITIVE_FIELD(SetBoolean, NULL, v);
1546 }
1547
1548 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1549 SET_PRIMITIVE_FIELD(SetByte, NULL, v);
1550 }
1551
1552 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1553 SET_PRIMITIVE_FIELD(SetChar, NULL, v);
1554 }
1555
1556 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1557 SET_PRIMITIVE_FIELD(SetFloat, NULL, v);
1558 }
1559
1560 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1561 SET_PRIMITIVE_FIELD(SetDouble, NULL, v);
1562 }
1563
1564 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1565 SET_PRIMITIVE_FIELD(SetInt, NULL, v);
1566 }
1567
1568 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1569 SET_PRIMITIVE_FIELD(SetLong, NULL, v);
1570 }
1571
1572 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1573 SET_PRIMITIVE_FIELD(SetShort, NULL, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001574 }
1575
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001576 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001577 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001578 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001579 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001580 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1581 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001582 va_end(ap);
1583 return local_result;
1584 }
1585
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001586 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001587 ScopedObjectAccess soa(env);
1588 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1589 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001590 }
1591
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001592 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001593 ScopedObjectAccess soa(env);
1594 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1595 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001596 }
1597
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001598 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001599 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001600 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001601 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001602 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001603 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001604 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001605 }
1606
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001607 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001608 ScopedObjectAccess soa(env);
1609 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 }
1611
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001612 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001613 ScopedObjectAccess soa(env);
1614 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001615 }
1616
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001617 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001619 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001620 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001621 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001622 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001623 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 }
1625
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001626 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001627 ScopedObjectAccess soa(env);
1628 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001629 }
1630
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001631 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001632 ScopedObjectAccess soa(env);
1633 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001634 }
1635
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001636 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001637 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001638 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001639 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001640 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001641 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001642 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001643 }
1644
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001645 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001646 ScopedObjectAccess soa(env);
1647 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 }
1649
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001650 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001651 ScopedObjectAccess soa(env);
1652 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001653 }
1654
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001655 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001656 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001657 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001658 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001659 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001660 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001661 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 }
1663
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001664 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001665 ScopedObjectAccess soa(env);
1666 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 }
1668
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001669 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001670 ScopedObjectAccess soa(env);
1671 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001672 }
1673
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001674 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001675 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001676 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001677 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001678 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001679 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001680 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001681 }
1682
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001683 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001684 ScopedObjectAccess soa(env);
1685 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 }
1687
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001688 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001689 ScopedObjectAccess soa(env);
1690 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 }
1692
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001693 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001694 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001695 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001696 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001697 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001699 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001700 }
1701
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001702 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001703 ScopedObjectAccess soa(env);
1704 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 }
1706
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001707 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001708 ScopedObjectAccess soa(env);
1709 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001710 }
1711
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001712 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001713 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001714 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001715 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001716 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001718 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001719 }
1720
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001721 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001722 ScopedObjectAccess soa(env);
1723 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001724 }
1725
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001726 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001727 ScopedObjectAccess soa(env);
1728 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 }
1730
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001731 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001733 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001734 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001735 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001736 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001737 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 }
1739
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001740 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001741 ScopedObjectAccess soa(env);
1742 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 }
1744
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001745 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001746 ScopedObjectAccess soa(env);
1747 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001748 }
1749
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001750 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001751 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001752 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001753 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001754 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 va_end(ap);
1756 }
1757
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001758 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001759 ScopedObjectAccess soa(env);
1760 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001761 }
1762
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001763 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001764 ScopedObjectAccess soa(env);
1765 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001766 }
1767
Elliott Hughes814e4032011-08-23 12:07:56 -07001768 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001769 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001770 String* result = String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001771 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001772 }
1773
1774 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 if (utf == NULL) {
1776 return NULL;
1777 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001778 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001779 String* result = String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001780 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001781 }
1782
Elliott Hughes814e4032011-08-23 12:07:56 -07001783 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001784 ScopedObjectAccess soa(env);
1785 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001786 }
1787
1788 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001789 ScopedObjectAccess soa(env);
1790 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001791 }
1792
Elliott Hughesb465ab02011-08-24 11:21:21 -07001793 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001794 ScopedObjectAccess soa(env);
1795 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001796 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001797 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001798 } else {
1799 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1800 memcpy(buf, chars + start, length * sizeof(jchar));
1801 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001802 }
1803
Elliott Hughesb465ab02011-08-24 11:21:21 -07001804 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001805 ScopedObjectAccess soa(env);
1806 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001807 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001808 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001809 } else {
1810 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1811 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1812 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001813 }
1814
Elliott Hughes75770752011-08-24 17:52:38 -07001815 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001816 ScopedObjectAccess soa(env);
1817 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001818 const CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001819 PinPrimitiveArray(soa, chars);
Elliott Hughes75770752011-08-24 17:52:38 -07001820 if (is_copy != NULL) {
1821 *is_copy = JNI_FALSE;
1822 }
1823 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001824 }
1825
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001826 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001827 ScopedObjectAccess soa(env);
1828 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001829 }
1830
Elliott Hughes75770752011-08-24 17:52:38 -07001831 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001832 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001833 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001834 }
1835
Elliott Hughes75770752011-08-24 17:52:38 -07001836 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001837 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001838 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001839 }
1840
Elliott Hughes75770752011-08-24 17:52:38 -07001841 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001842 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001843 if (java_string == NULL) {
1844 return NULL;
1845 }
1846 if (is_copy != NULL) {
1847 *is_copy = JNI_TRUE;
1848 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001849 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001850 size_t byte_count = s->GetUtfLength();
1851 char* bytes = new char[byte_count + 1];
Elliott Hughes418dfe72011-10-06 18:56:27 -07001852 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001853 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1854 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1855 bytes[byte_count] = '\0';
1856 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001857 }
1858
Elliott Hughes75770752011-08-24 17:52:38 -07001859 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001860 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001861 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001862 }
1863
Elliott Hughesbd935992011-08-22 11:59:34 -07001864 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001865 ScopedObjectAccess soa(env);
1866 Object* obj = soa.Decode<Object*>(java_array);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001867 CHECK(obj->IsArrayInstance()); // TODO: ReportJniError
Elliott Hughesbd935992011-08-22 11:59:34 -07001868 Array* array = obj->AsArray();
1869 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001870 }
1871
Elliott Hughes814e4032011-08-23 12:07:56 -07001872 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001873 ScopedObjectAccess soa(env);
1874 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1875 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001876 }
1877
1878 static void SetObjectArrayElement(JNIEnv* env,
1879 jobjectArray java_array, jsize index, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001880 ScopedObjectAccess soa(env);
1881 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1882 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001883 array->Set(index, value);
1884 }
1885
1886 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001887 ScopedObjectAccess soa(env);
1888 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001889 }
1890
1891 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001892 ScopedObjectAccess soa(env);
1893 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001894 }
1895
1896 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001897 ScopedObjectAccess soa(env);
1898 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001899 }
1900
1901 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001902 ScopedObjectAccess soa(env);
1903 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001904 }
1905
1906 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001907 ScopedObjectAccess soa(env);
1908 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001909 }
1910
1911 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001912 ScopedObjectAccess soa(env);
1913 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001914 }
1915
1916 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001917 ScopedObjectAccess soa(env);
1918 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001919 }
1920
1921 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001922 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001923 CHECK_GE(length, 0); // TODO: ReportJniError
1924
1925 // Compute the array class corresponding to the given element class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001926 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001927 std::string descriptor;
1928 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001929 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001930
1931 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001932 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1933 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001934 return NULL;
1935 }
1936
Elliott Hughes75770752011-08-24 17:52:38 -07001937 // Allocate and initialize if necessary.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001938 Class* array_class = soa.Decode<Class*>(java_array_class.get());
Ian Rogers50b35e22012-10-04 10:09:15 -07001939 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001940 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001941 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07001942 for (jsize i = 0; i < length; ++i) {
1943 result->Set(i, initial_object);
1944 }
1945 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001946 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001947 }
1948
1949 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001950 ScopedObjectAccess soa(env);
1951 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001952 }
1953
Ian Rogersa15e67d2012-02-28 13:51:55 -08001954 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001955 ScopedObjectAccess soa(env);
1956 Array* array = soa.Decode<Array*>(java_array);
1957 PinPrimitiveArray(soa, array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001958 if (is_copy != NULL) {
1959 *is_copy = JNI_FALSE;
1960 }
1961 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001962 }
1963
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001964 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001965 ScopedObjectAccess soa(env);
1966 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001967 }
1968
Elliott Hughes75770752011-08-24 17:52:38 -07001969 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001970 ScopedObjectAccess soa(env);
1971 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001972 }
1973
Elliott Hughes75770752011-08-24 17:52:38 -07001974 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001975 ScopedObjectAccess soa(env);
1976 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001977 }
1978
Elliott Hughes75770752011-08-24 17:52:38 -07001979 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001980 ScopedObjectAccess soa(env);
1981 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001982 }
1983
Elliott Hughes75770752011-08-24 17:52:38 -07001984 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001985 ScopedObjectAccess soa(env);
1986 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 }
1988
Elliott Hughes75770752011-08-24 17:52:38 -07001989 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001990 ScopedObjectAccess soa(env);
1991 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001992 }
1993
Elliott Hughes75770752011-08-24 17:52:38 -07001994 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001995 ScopedObjectAccess soa(env);
1996 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001997 }
1998
Elliott Hughes75770752011-08-24 17:52:38 -07001999 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002000 ScopedObjectAccess soa(env);
2001 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002002 }
2003
Elliott Hughes75770752011-08-24 17:52:38 -07002004 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002005 ScopedObjectAccess soa(env);
2006 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002007 }
2008
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002009 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002010 ScopedObjectAccess soa(env);
2011 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002012 }
2013
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002014 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002015 ScopedObjectAccess soa(env);
2016 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002017 }
2018
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002019 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002020 ScopedObjectAccess soa(env);
2021 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002022 }
2023
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002024 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002025 ScopedObjectAccess soa(env);
2026 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002027 }
2028
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002029 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002030 ScopedObjectAccess soa(env);
2031 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002032 }
2033
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002034 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002035 ScopedObjectAccess soa(env);
2036 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 }
2038
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002039 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002040 ScopedObjectAccess soa(env);
2041 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002042 }
2043
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002044 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002045 ScopedObjectAccess soa(env);
2046 ReleasePrimitiveArray(soa, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002047 }
2048
Elliott Hughes814e4032011-08-23 12:07:56 -07002049 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002050 ScopedObjectAccess soa(env);
2051 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002052 }
2053
Elliott Hughes814e4032011-08-23 12:07:56 -07002054 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002055 ScopedObjectAccess soa(env);
2056 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002057 }
2058
Elliott Hughes814e4032011-08-23 12:07:56 -07002059 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002060 ScopedObjectAccess soa(env);
2061 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002062 }
2063
Elliott Hughes814e4032011-08-23 12:07:56 -07002064 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002065 ScopedObjectAccess soa(env);
2066 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002067 }
2068
Elliott Hughes814e4032011-08-23 12:07:56 -07002069 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002070 ScopedObjectAccess soa(env);
2071 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002072 }
2073
Elliott Hughes814e4032011-08-23 12:07:56 -07002074 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002075 ScopedObjectAccess soa(env);
2076 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002077 }
2078
Elliott Hughes814e4032011-08-23 12:07:56 -07002079 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002080 ScopedObjectAccess soa(env);
2081 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002082 }
2083
Elliott Hughes814e4032011-08-23 12:07:56 -07002084 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002085 ScopedObjectAccess soa(env);
2086 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002087 }
2088
Elliott Hughes814e4032011-08-23 12:07:56 -07002089 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002090 ScopedObjectAccess soa(env);
2091 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002092 }
2093
Elliott Hughes814e4032011-08-23 12:07:56 -07002094 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002095 ScopedObjectAccess soa(env);
2096 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002097 }
2098
Elliott Hughes814e4032011-08-23 12:07:56 -07002099 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002100 ScopedObjectAccess soa(env);
2101 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 }
2103
Elliott Hughes814e4032011-08-23 12:07:56 -07002104 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002105 ScopedObjectAccess soa(env);
2106 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 }
2108
Elliott Hughes814e4032011-08-23 12:07:56 -07002109 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002110 ScopedObjectAccess soa(env);
2111 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002112 }
2113
Elliott Hughes814e4032011-08-23 12:07:56 -07002114 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002115 ScopedObjectAccess soa(env);
2116 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 }
2118
Elliott Hughes814e4032011-08-23 12:07:56 -07002119 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002120 ScopedObjectAccess soa(env);
2121 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002122 }
2123
Elliott Hughes814e4032011-08-23 12:07:56 -07002124 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002125 ScopedObjectAccess soa(env);
2126 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002127 }
2128
Elliott Hughes5174fe62011-08-23 15:12:35 -07002129 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002130 ScopedObjectAccess soa(env);
2131 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002132
Elliott Hughes5174fe62011-08-23 15:12:35 -07002133 for (int i = 0; i < method_count; i++) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002134 const char* name = methods[i].name;
2135 const char* sig = methods[i].signature;
2136
2137 if (*sig == '!') {
2138 // TODO: fast jni. it's too noisy to log all these.
2139 ++sig;
2140 }
2141
Mathieu Chartier66f19252012-09-18 08:57:04 -07002142 AbstractMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002143 if (m == NULL) {
2144 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002145 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002146 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002147 LOG(INFO) << "Failed to register native method " << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002148 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002149 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002150 } else if (!m->IsNative()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002151 LOG(INFO) << "Failed to register non-native method " << name << sig << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002152 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002153 return JNI_ERR;
2154 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002155
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002156 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002157
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002158 m->RegisterNative(soa.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002159 }
2160 return JNI_OK;
2161 }
2162
Elliott Hughes5174fe62011-08-23 15:12:35 -07002163 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002164 ScopedObjectAccess soa(env);
2165 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002166
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002167 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002168
2169 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002170 AbstractMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002171 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002172 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002173 }
2174 }
2175 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002176 AbstractMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002177 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002178 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002179 }
2180 }
2181
2182 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002183 }
2184
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002185 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2186 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
2187 ScopedObjectAccess soa(env);
2188 Object* o = soa.Decode<Object*>(java_object);
2189 o->MonitorEnter(soa.Self());
2190 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002191 return JNI_ERR;
2192 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002193 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002194 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 }
2196
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002197 static jint MonitorExit(JNIEnv* env, jobject java_object)
2198 UNLOCK_FUNCTION(monitor_lock_) {
2199 ScopedObjectAccess soa(env);
2200 Object* o = soa.Decode<Object*>(java_object);
2201 o->MonitorExit(soa.Self());
2202 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002203 return JNI_ERR;
2204 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002205 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002206 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002207 }
2208
2209 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002210 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07002211 Runtime* runtime = Runtime::Current();
2212 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002213 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002214 } else {
2215 *vm = NULL;
2216 }
2217 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2218 }
2219
Elliott Hughescdf53122011-08-19 15:46:09 -07002220 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002221 ScopedObjectAccess soa(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002222
2223 // The address may not be NULL, and the capacity must be > 0.
Elliott Hughes75770752011-08-24 17:52:38 -07002224 CHECK(address != NULL); // TODO: ReportJniError
2225 CHECK_GT(capacity, 0); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002226
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002227 // At the moment, the Java side is limited to 32 bisoa.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002228 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2229 CHECK_LE(capacity, 0xffffffff);
2230 jint address_arg = reinterpret_cast<jint>(address);
2231 jint capacity_arg = static_cast<jint>(capacity);
2232
Elliott Hugheseac76672012-05-24 21:56:51 -07002233 jobject result = env->NewObject(WellKnownClasses::java_nio_ReadWriteDirectByteBuffer,
2234 WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_init,
2235 address_arg, capacity_arg);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002236 return soa.Self()->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002237 }
2238
Elliott Hughesb465ab02011-08-24 11:21:21 -07002239 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002240 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -07002241 return reinterpret_cast<void*>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002242 }
2243
Elliott Hughesb465ab02011-08-24 11:21:21 -07002244 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002245 ScopedObjectAccess soa(env);
Elliott Hugheseac76672012-05-24 21:56:51 -07002246 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002247 }
2248
Elliott Hughesb465ab02011-08-24 11:21:21 -07002249 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002250 ScopedObjectAccess soa(env);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002251
Elliott Hughes75770752011-08-24 17:52:38 -07002252 CHECK(java_object != NULL); // TODO: ReportJniError
Elliott Hughesb465ab02011-08-24 11:21:21 -07002253
2254 // Do we definitely know what kind of reference this is?
2255 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2256 IndirectRefKind kind = GetIndirectRefKind(ref);
2257 switch (kind) {
2258 case kLocal:
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002259 if (soa.Env()->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002260 return JNILocalRefType;
2261 }
2262 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002263 case kGlobal:
2264 return JNIGlobalRefType;
2265 case kWeakGlobal:
2266 return JNIWeakGlobalRefType;
2267 case kSirtOrInvalid:
2268 // Is it in a stack IRT?
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002269 if (soa.Self()->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002270 return JNILocalRefType;
2271 }
2272
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002273 if (!soa.Vm()->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002274 return JNIInvalidRefType;
2275 }
2276
Elliott Hughesb465ab02011-08-24 11:21:21 -07002277 // If we're handing out direct pointers, check whether it's a direct pointer
2278 // to a local reference.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002279 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2280 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002281 return JNILocalRefType;
2282 }
2283 }
2284
2285 return JNIInvalidRefType;
2286 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002287 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2288 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002289 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002290
2291 private:
2292 static jint EnsureLocalCapacity(const ScopedObjectAccess& soa, jint desired_capacity,
2293 const char* caller)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002294 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002295 // TODO: we should try to expand the table if necessary.
2296 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
2297 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2298 return JNI_ERR;
2299 }
2300 // TODO: this isn't quite right, since "capacity" includes holes.
2301 size_t capacity = soa.Env()->locals.Capacity();
2302 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2303 if (!okay) {
2304 soa.Self()->ThrowOutOfMemoryError(caller);
2305 }
2306 return okay ? JNI_OK : JNI_ERR;
2307 }
2308
2309 template<typename JniT, typename ArtT>
2310 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002311 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002312 CHECK_GE(length, 0); // TODO: ReportJniError
Ian Rogers50b35e22012-10-04 10:09:15 -07002313 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002314 return soa.AddLocalReference<JniT>(result);
2315 }
2316
2317 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2318 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2319 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002320 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002321 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2322 PinPrimitiveArray(soa, array);
2323 if (is_copy != NULL) {
2324 *is_copy = JNI_FALSE;
2325 }
2326 return array->GetData();
2327 }
2328
2329 template <typename ArrayT>
2330 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2331 jint mode)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002332 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002333 if (mode != JNI_COMMIT) {
2334 Array* array = soa.Decode<Array*>(java_array);
2335 UnpinPrimitiveArray(soa, array);
2336 }
2337 }
2338
2339 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2340 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2341 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002342 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002343 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2344 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2345 ThrowAIOOBE(soa, array, start, length, "src");
2346 } else {
2347 JavaT* data = array->GetData();
2348 memcpy(buf, data + start, length * sizeof(JavaT));
2349 }
2350 }
2351
2352 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2353 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2354 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002355 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002356 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2357 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2358 ThrowAIOOBE(soa, array, start, length, "dst");
2359 } else {
2360 JavaT* data = array->GetData();
2361 memcpy(data + start, buf, length * sizeof(JavaT));
2362 }
2363 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002364};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002365
Elliott Hughes88c5c352012-03-15 18:49:48 -07002366const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002367 NULL, // reserved0.
2368 NULL, // reserved1.
2369 NULL, // reserved2.
2370 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002371 JNI::GetVersion,
2372 JNI::DefineClass,
2373 JNI::FindClass,
2374 JNI::FromReflectedMethod,
2375 JNI::FromReflectedField,
2376 JNI::ToReflectedMethod,
2377 JNI::GetSuperclass,
2378 JNI::IsAssignableFrom,
2379 JNI::ToReflectedField,
2380 JNI::Throw,
2381 JNI::ThrowNew,
2382 JNI::ExceptionOccurred,
2383 JNI::ExceptionDescribe,
2384 JNI::ExceptionClear,
2385 JNI::FatalError,
2386 JNI::PushLocalFrame,
2387 JNI::PopLocalFrame,
2388 JNI::NewGlobalRef,
2389 JNI::DeleteGlobalRef,
2390 JNI::DeleteLocalRef,
2391 JNI::IsSameObject,
2392 JNI::NewLocalRef,
2393 JNI::EnsureLocalCapacity,
2394 JNI::AllocObject,
2395 JNI::NewObject,
2396 JNI::NewObjectV,
2397 JNI::NewObjectA,
2398 JNI::GetObjectClass,
2399 JNI::IsInstanceOf,
2400 JNI::GetMethodID,
2401 JNI::CallObjectMethod,
2402 JNI::CallObjectMethodV,
2403 JNI::CallObjectMethodA,
2404 JNI::CallBooleanMethod,
2405 JNI::CallBooleanMethodV,
2406 JNI::CallBooleanMethodA,
2407 JNI::CallByteMethod,
2408 JNI::CallByteMethodV,
2409 JNI::CallByteMethodA,
2410 JNI::CallCharMethod,
2411 JNI::CallCharMethodV,
2412 JNI::CallCharMethodA,
2413 JNI::CallShortMethod,
2414 JNI::CallShortMethodV,
2415 JNI::CallShortMethodA,
2416 JNI::CallIntMethod,
2417 JNI::CallIntMethodV,
2418 JNI::CallIntMethodA,
2419 JNI::CallLongMethod,
2420 JNI::CallLongMethodV,
2421 JNI::CallLongMethodA,
2422 JNI::CallFloatMethod,
2423 JNI::CallFloatMethodV,
2424 JNI::CallFloatMethodA,
2425 JNI::CallDoubleMethod,
2426 JNI::CallDoubleMethodV,
2427 JNI::CallDoubleMethodA,
2428 JNI::CallVoidMethod,
2429 JNI::CallVoidMethodV,
2430 JNI::CallVoidMethodA,
2431 JNI::CallNonvirtualObjectMethod,
2432 JNI::CallNonvirtualObjectMethodV,
2433 JNI::CallNonvirtualObjectMethodA,
2434 JNI::CallNonvirtualBooleanMethod,
2435 JNI::CallNonvirtualBooleanMethodV,
2436 JNI::CallNonvirtualBooleanMethodA,
2437 JNI::CallNonvirtualByteMethod,
2438 JNI::CallNonvirtualByteMethodV,
2439 JNI::CallNonvirtualByteMethodA,
2440 JNI::CallNonvirtualCharMethod,
2441 JNI::CallNonvirtualCharMethodV,
2442 JNI::CallNonvirtualCharMethodA,
2443 JNI::CallNonvirtualShortMethod,
2444 JNI::CallNonvirtualShortMethodV,
2445 JNI::CallNonvirtualShortMethodA,
2446 JNI::CallNonvirtualIntMethod,
2447 JNI::CallNonvirtualIntMethodV,
2448 JNI::CallNonvirtualIntMethodA,
2449 JNI::CallNonvirtualLongMethod,
2450 JNI::CallNonvirtualLongMethodV,
2451 JNI::CallNonvirtualLongMethodA,
2452 JNI::CallNonvirtualFloatMethod,
2453 JNI::CallNonvirtualFloatMethodV,
2454 JNI::CallNonvirtualFloatMethodA,
2455 JNI::CallNonvirtualDoubleMethod,
2456 JNI::CallNonvirtualDoubleMethodV,
2457 JNI::CallNonvirtualDoubleMethodA,
2458 JNI::CallNonvirtualVoidMethod,
2459 JNI::CallNonvirtualVoidMethodV,
2460 JNI::CallNonvirtualVoidMethodA,
2461 JNI::GetFieldID,
2462 JNI::GetObjectField,
2463 JNI::GetBooleanField,
2464 JNI::GetByteField,
2465 JNI::GetCharField,
2466 JNI::GetShortField,
2467 JNI::GetIntField,
2468 JNI::GetLongField,
2469 JNI::GetFloatField,
2470 JNI::GetDoubleField,
2471 JNI::SetObjectField,
2472 JNI::SetBooleanField,
2473 JNI::SetByteField,
2474 JNI::SetCharField,
2475 JNI::SetShortField,
2476 JNI::SetIntField,
2477 JNI::SetLongField,
2478 JNI::SetFloatField,
2479 JNI::SetDoubleField,
2480 JNI::GetStaticMethodID,
2481 JNI::CallStaticObjectMethod,
2482 JNI::CallStaticObjectMethodV,
2483 JNI::CallStaticObjectMethodA,
2484 JNI::CallStaticBooleanMethod,
2485 JNI::CallStaticBooleanMethodV,
2486 JNI::CallStaticBooleanMethodA,
2487 JNI::CallStaticByteMethod,
2488 JNI::CallStaticByteMethodV,
2489 JNI::CallStaticByteMethodA,
2490 JNI::CallStaticCharMethod,
2491 JNI::CallStaticCharMethodV,
2492 JNI::CallStaticCharMethodA,
2493 JNI::CallStaticShortMethod,
2494 JNI::CallStaticShortMethodV,
2495 JNI::CallStaticShortMethodA,
2496 JNI::CallStaticIntMethod,
2497 JNI::CallStaticIntMethodV,
2498 JNI::CallStaticIntMethodA,
2499 JNI::CallStaticLongMethod,
2500 JNI::CallStaticLongMethodV,
2501 JNI::CallStaticLongMethodA,
2502 JNI::CallStaticFloatMethod,
2503 JNI::CallStaticFloatMethodV,
2504 JNI::CallStaticFloatMethodA,
2505 JNI::CallStaticDoubleMethod,
2506 JNI::CallStaticDoubleMethodV,
2507 JNI::CallStaticDoubleMethodA,
2508 JNI::CallStaticVoidMethod,
2509 JNI::CallStaticVoidMethodV,
2510 JNI::CallStaticVoidMethodA,
2511 JNI::GetStaticFieldID,
2512 JNI::GetStaticObjectField,
2513 JNI::GetStaticBooleanField,
2514 JNI::GetStaticByteField,
2515 JNI::GetStaticCharField,
2516 JNI::GetStaticShortField,
2517 JNI::GetStaticIntField,
2518 JNI::GetStaticLongField,
2519 JNI::GetStaticFloatField,
2520 JNI::GetStaticDoubleField,
2521 JNI::SetStaticObjectField,
2522 JNI::SetStaticBooleanField,
2523 JNI::SetStaticByteField,
2524 JNI::SetStaticCharField,
2525 JNI::SetStaticShortField,
2526 JNI::SetStaticIntField,
2527 JNI::SetStaticLongField,
2528 JNI::SetStaticFloatField,
2529 JNI::SetStaticDoubleField,
2530 JNI::NewString,
2531 JNI::GetStringLength,
2532 JNI::GetStringChars,
2533 JNI::ReleaseStringChars,
2534 JNI::NewStringUTF,
2535 JNI::GetStringUTFLength,
2536 JNI::GetStringUTFChars,
2537 JNI::ReleaseStringUTFChars,
2538 JNI::GetArrayLength,
2539 JNI::NewObjectArray,
2540 JNI::GetObjectArrayElement,
2541 JNI::SetObjectArrayElement,
2542 JNI::NewBooleanArray,
2543 JNI::NewByteArray,
2544 JNI::NewCharArray,
2545 JNI::NewShortArray,
2546 JNI::NewIntArray,
2547 JNI::NewLongArray,
2548 JNI::NewFloatArray,
2549 JNI::NewDoubleArray,
2550 JNI::GetBooleanArrayElements,
2551 JNI::GetByteArrayElements,
2552 JNI::GetCharArrayElements,
2553 JNI::GetShortArrayElements,
2554 JNI::GetIntArrayElements,
2555 JNI::GetLongArrayElements,
2556 JNI::GetFloatArrayElements,
2557 JNI::GetDoubleArrayElements,
2558 JNI::ReleaseBooleanArrayElements,
2559 JNI::ReleaseByteArrayElements,
2560 JNI::ReleaseCharArrayElements,
2561 JNI::ReleaseShortArrayElements,
2562 JNI::ReleaseIntArrayElements,
2563 JNI::ReleaseLongArrayElements,
2564 JNI::ReleaseFloatArrayElements,
2565 JNI::ReleaseDoubleArrayElements,
2566 JNI::GetBooleanArrayRegion,
2567 JNI::GetByteArrayRegion,
2568 JNI::GetCharArrayRegion,
2569 JNI::GetShortArrayRegion,
2570 JNI::GetIntArrayRegion,
2571 JNI::GetLongArrayRegion,
2572 JNI::GetFloatArrayRegion,
2573 JNI::GetDoubleArrayRegion,
2574 JNI::SetBooleanArrayRegion,
2575 JNI::SetByteArrayRegion,
2576 JNI::SetCharArrayRegion,
2577 JNI::SetShortArrayRegion,
2578 JNI::SetIntArrayRegion,
2579 JNI::SetLongArrayRegion,
2580 JNI::SetFloatArrayRegion,
2581 JNI::SetDoubleArrayRegion,
2582 JNI::RegisterNatives,
2583 JNI::UnregisterNatives,
2584 JNI::MonitorEnter,
2585 JNI::MonitorExit,
2586 JNI::GetJavaVM,
2587 JNI::GetStringRegion,
2588 JNI::GetStringUTFRegion,
2589 JNI::GetPrimitiveArrayCritical,
2590 JNI::ReleasePrimitiveArrayCritical,
2591 JNI::GetStringCritical,
2592 JNI::ReleaseStringCritical,
2593 JNI::NewWeakGlobalRef,
2594 JNI::DeleteWeakGlobalRef,
2595 JNI::ExceptionCheck,
2596 JNI::NewDirectByteBuffer,
2597 JNI::GetDirectBufferAddress,
2598 JNI::GetDirectBufferCapacity,
2599 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002600};
2601
Elliott Hughes75770752011-08-24 17:52:38 -07002602JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002603 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002604 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002605 local_ref_cookie(IRT_FIRST_SEGMENT),
2606 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002607 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002608 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002609 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002610 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002611 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002612 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002613 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002614 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2615 // errors will ensue.
2616 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2617 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002618}
2619
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002620JNIEnvExt::~JNIEnvExt() {
2621}
2622
Elliott Hughes88c5c352012-03-15 18:49:48 -07002623void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2624 check_jni = enabled;
2625 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002626}
2627
Elliott Hughes73e66f72012-05-09 09:34:45 -07002628void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2629 locals.Dump(os);
2630 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002631}
2632
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002633void JNIEnvExt::PushFrame(int /*capacity*/) {
2634 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002635 stacked_local_ref_cookies.push_back(local_ref_cookie);
2636 local_ref_cookie = locals.GetSegmentState();
2637}
2638
2639void JNIEnvExt::PopFrame() {
2640 locals.SetSegmentState(local_ref_cookie);
2641 local_ref_cookie = stacked_local_ref_cookies.back();
2642 stacked_local_ref_cookies.pop_back();
2643}
2644
Carl Shapiroea4dca82011-08-01 13:45:38 -07002645// JNI Invocation interface.
2646
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002647extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, void** p_env, void* vm_args) {
2648 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
2649 if (args->version < JNI_VERSION_1_2) {
2650 return JNI_EVERSION;
2651 }
2652 Runtime::Options options;
2653 for (int i = 0; i < args->nOptions; ++i) {
2654 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002655 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002656 }
2657 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002658 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002659 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002660 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002661 Runtime* runtime = Runtime::Current();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002662 runtime->Start();
2663 *p_env = Thread::Current()->GetJniEnv();
2664 *p_vm = runtime->GetJavaVM();
2665 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002666}
2667
Elliott Hughesf2682d52011-08-15 16:37:04 -07002668extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002669 Runtime* runtime = Runtime::Current();
2670 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002671 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002672 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002673 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002674 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002675 }
2676 return JNI_OK;
2677}
2678
2679// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002680extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002681 return JNI_ERR;
2682}
2683
Elliott Hughescdf53122011-08-19 15:46:09 -07002684class JII {
2685 public:
2686 static jint DestroyJavaVM(JavaVM* vm) {
2687 if (vm == NULL) {
2688 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002689 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002690 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2691 delete raw_vm->runtime;
2692 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002693 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002694
Elliott Hughescdf53122011-08-19 15:46:09 -07002695 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002696 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002697 }
2698
2699 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002700 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002701 }
2702
2703 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002704 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002705 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002706 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002707 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2708 Runtime* runtime = raw_vm->runtime;
2709 runtime->DetachCurrentThread();
2710 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002711 }
2712
2713 static jint GetEnv(JavaVM* vm, void** env, jint version) {
2714 if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
2715 return JNI_EVERSION;
2716 }
2717 if (vm == NULL || env == NULL) {
2718 return JNI_ERR;
2719 }
2720 Thread* thread = Thread::Current();
2721 if (thread == NULL) {
2722 *env = NULL;
2723 return JNI_EDETACHED;
2724 }
2725 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002726 return JNI_OK;
2727 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002728};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002729
Elliott Hughes88c5c352012-03-15 18:49:48 -07002730const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002731 NULL, // reserved0
2732 NULL, // reserved1
2733 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002734 JII::DestroyJavaVM,
2735 JII::AttachCurrentThread,
2736 JII::DetachCurrentThread,
2737 JII::GetEnv,
2738 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002739};
2740
Elliott Hughesa0957642011-09-02 14:27:33 -07002741JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002742 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002743 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002744 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002745 check_jni(false),
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002746 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002747 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002748 work_around_app_jni_bugs(false),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002749 pins_lock("JNI pin table lock"),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002750 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002751 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002752 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002753 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002754 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002755 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Elliott Hughes79082e32011-08-25 12:07:32 -07002756 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002757 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002758 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002759 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002760 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002761}
2762
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002763JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002764 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002765}
2766
Elliott Hughes88c5c352012-03-15 18:49:48 -07002767void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2768 check_jni = enabled;
2769 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002770}
2771
Elliott Hughesae80b492012-04-24 10:43:17 -07002772void JavaVMExt::DumpForSigQuit(std::ostream& os) {
2773 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
2774 if (force_copy) {
2775 os << " (with forcecopy)";
2776 }
2777 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07002778 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07002779 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002780 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002781 os << "; pins=" << pin_table.Size();
2782 }
2783 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002784 MutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002785 os << "; globals=" << globals.Capacity();
2786 }
2787 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002788 MutexLock mu(self, weak_globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002789 if (weak_globals.Capacity() > 0) {
2790 os << " (plus " << weak_globals.Capacity() << " weak)";
2791 }
2792 }
2793 os << '\n';
2794
2795 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002796 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002797 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
2798 }
2799}
2800
Elliott Hughes73e66f72012-05-09 09:34:45 -07002801void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002802 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002803 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002804 MutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002805 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002806 }
2807 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002808 MutexLock mu(self, weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002809 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002810 }
2811 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002812 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002813 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002814 }
2815}
2816
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002817bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
2818 std::string& detail) {
Elliott Hughes75770752011-08-24 17:52:38 -07002819 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002820
2821 // See if we've already loaded this library. If we have, and the class loader
2822 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002823 // TODO: for better results we should canonicalize the pathname (or even compare
2824 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002825 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07002826 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07002827 {
2828 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07002829 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002830 library = libraries->Get(path);
2831 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002832 if (library != NULL) {
2833 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002834 // The library will be associated with class_loader. The JNI
2835 // spec says we can't load the same library into more than one
2836 // class loader.
2837 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2838 "ClassLoader %p; can't open in ClassLoader %p",
2839 path.c_str(), library->GetClassLoader(), class_loader);
2840 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002841 return false;
2842 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002843 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2844 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002845 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002846 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2847 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002848 return false;
2849 }
2850 return true;
2851 }
2852
2853 // Open the shared library. Because we're using a full path, the system
2854 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2855 // resolve this library's dependencies though.)
2856
2857 // Failures here are expected when java.library.path has several entries
2858 // and we have to hunt for the lib.
2859
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002860 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
2861 // class unloading. Libraries will only be unloaded when the reference count (incremented by
2862 // dlopen) becomes zero from dlclose.
2863
Elliott Hughescdf53122011-08-19 15:46:09 -07002864 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002865 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002866 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
2867 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
2868 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07002869
Elliott Hughes84b2f142012-09-27 09:16:28 -07002870 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002871
2872 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002873 detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07002874 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002875 return false;
2876 }
2877
2878 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002879 // TODO: move the locking (and more of this logic) into Libraries.
2880 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002881 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002882 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002883 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002884 if (library == NULL) { // We won race to get libraries_lock
2885 library = new SharedLibrary(path, handle, class_loader);
2886 libraries->Put(path, library);
2887 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002888 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002889 }
2890 if (!created_library) {
2891 LOG(INFO) << "WOW: we lost a race to add shared library: "
2892 << "\"" << path << "\" ClassLoader=" << class_loader;
2893 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002894 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002895
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002896 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002897
2898 bool result = true;
2899 void* sym = dlsym(handle, "JNI_OnLoad");
2900 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002901 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002902 } else {
2903 // Call JNI_OnLoad. We have to override the current class
2904 // loader, which will always be "null" since the stuff at the
2905 // top of the stack is around Runtime.loadLibrary(). (See
2906 // the comments in the JNI FindClass function.)
2907 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2908 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07002909 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002910 self->SetClassLoaderOverride(class_loader);
2911
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002912 int version = 0;
2913 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002914 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002915 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002916 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002917 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002918
Brian Carlstromaded5f72011-10-07 17:15:04 -07002919 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002920
2921 if (version != JNI_VERSION_1_2 &&
2922 version != JNI_VERSION_1_4 &&
2923 version != JNI_VERSION_1_6) {
2924 LOG(WARNING) << "JNI_OnLoad in \"" << path << "\" returned "
2925 << "bad version: " << version;
2926 // It's unwise to call dlclose() here, but we can mark it
2927 // as bad and ensure that future load attempts will fail.
2928 // We don't know how far JNI_OnLoad got, so there could
2929 // be some partially-initialized stuff accessible through
2930 // newly-registered native method calls. We could try to
2931 // unregister them, but that doesn't seem worthwhile.
2932 result = false;
2933 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002934 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2935 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002936 }
2937 }
2938
2939 library->SetResult(result);
2940 return result;
2941}
2942
Mathieu Chartier66f19252012-09-18 08:57:04 -07002943void* JavaVMExt::FindCodeForNativeMethod(AbstractMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002944 CHECK(m->IsNative());
2945
2946 Class* c = m->GetDeclaringClass();
2947
2948 // If this is a static method, it could be called before the class
2949 // has been initialized.
2950 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002951 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002952 return NULL;
2953 }
2954 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002955 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002956 }
2957
Brian Carlstrom16192862011-09-12 17:50:06 -07002958 std::string detail;
2959 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07002960 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07002961 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002962 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07002963 native_method = libraries->FindNativeMethod(m, detail);
2964 }
2965 // throwing can cause libraries_lock to be reacquired
2966 if (native_method == NULL) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002967 self->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002968 }
2969 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002970}
2971
Elliott Hughes410c0c82011-09-01 17:58:25 -07002972void JavaVMExt::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002973 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07002974 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002975 MutexLock mu(self, globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002976 globals.VisitRoots(visitor, arg);
2977 }
2978 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002979 MutexLock mu(self, pins_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002980 pin_table.VisitRoots(visitor, arg);
2981 }
2982 // The weak_globals table is visited by the GC itself (because it mutates the table).
2983}
2984
Ian Rogersdf20fe02011-07-20 20:34:16 -07002985} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002986
2987std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2988 switch (rhs) {
2989 case JNIInvalidRefType:
2990 os << "JNIInvalidRefType";
2991 return os;
2992 case JNILocalRefType:
2993 os << "JNILocalRefType";
2994 return os;
2995 case JNIGlobalRefType:
2996 os << "JNIGlobalRefType";
2997 return os;
2998 case JNIWeakGlobalRefType:
2999 os << "JNIWeakGlobalRefType";
3000 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003001 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08003002 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003003 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07003004 }
3005}