blob: 0e521af5aa0343e6ec3176854fd5c45d9fcc6fa5 [file] [log] [blame]
Elliott Hughesa2501992011-08-26 19:39:54 -07001/*
2 * Copyright (C) 2008 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 */
16
17#include "jni_internal.h"
18
19#include <sys/mman.h>
20#include <zlib.h>
21
22#include "class_linker.h"
23#include "logging.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080024#include "object_utils.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070025#include "scoped_thread_state_change.h"
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080026#include "space.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070027#include "thread.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070028#include "runtime.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070029
Elliott Hughese6087632011-09-26 12:18:25 -070030#define LIBCORE_CPP_JNI_HELPERS
31#include <JNIHelp.h> // from libcore
32#undef LIBCORE_CPP_JNI_HELPERS
33
Elliott Hughesa2501992011-08-26 19:39:54 -070034namespace art {
35
Elliott Hughes3f6635a2012-06-19 13:37:49 -070036static void JniAbort(const char* jni_function_name, const char* msg) {
Elliott Hughesa0957642011-09-02 14:27:33 -070037 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070038 ScopedObjectAccess soa(self);
Elliott Hughesd07986f2011-12-06 18:27:45 -080039 Method* current_method = self->GetCurrentMethod();
Elliott Hughesa2501992011-08-26 19:39:54 -070040
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070041 std::ostringstream os;
Elliott Hughes3f6635a2012-06-19 13:37:49 -070042 os << "JNI DETECTED ERROR IN APPLICATION: " << msg;
Elliott Hughesa2501992011-08-26 19:39:54 -070043
44 if (jni_function_name != NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -070045 os << "\n in call to " << jni_function_name;
Elliott Hughesa2501992011-08-26 19:39:54 -070046 }
Elliott Hughesa0957642011-09-02 14:27:33 -070047 // TODO: is this useful given that we're about to dump the calling thread's stack?
48 if (current_method != NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -070049 os << "\n from " << PrettyMethod(current_method);
Elliott Hughesa0957642011-09-02 14:27:33 -070050 }
51 os << "\n";
52 self->Dump(os);
Elliott Hughesa2501992011-08-26 19:39:54 -070053
54 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
55 if (vm->check_jni_abort_hook != NULL) {
Elliott Hughesb264f082012-04-06 17:10:10 -070056 vm->check_jni_abort_hook(vm->check_jni_abort_hook_data, os.str());
Elliott Hughesa2501992011-08-26 19:39:54 -070057 } else {
Ian Rogers9da7f592012-08-20 17:14:28 -070058 // Ensure that we get a native stack trace for this thread.
59 self->TransitionFromRunnableToSuspended(kNative);
Elliott Hughesa2501992011-08-26 19:39:54 -070060 LOG(FATAL) << os.str();
Ian Rogers9da7f592012-08-20 17:14:28 -070061 self->TransitionFromSuspendedToRunnable(); // Unreachable, keep annotalysis happy.
Elliott Hughesa2501992011-08-26 19:39:54 -070062 }
63}
64
Elliott Hughes3f6635a2012-06-19 13:37:49 -070065static void JniAbortV(const char* jni_function_name, const char* fmt, va_list ap) {
66 std::string msg;
67 StringAppendV(&msg, fmt, ap);
68 JniAbort(jni_function_name, msg.c_str());
69}
70
71void JniAbortF(const char* jni_function_name, const char* fmt, ...) {
72 va_list args;
73 va_start(args, fmt);
74 JniAbortV(jni_function_name, fmt, args);
75 va_end(args);
76}
77
Elliott Hughesa2501992011-08-26 19:39:54 -070078/*
79 * ===========================================================================
80 * JNI function helpers
81 * ===========================================================================
82 */
83
Ian Rogers959f8ed2012-02-07 16:33:37 -080084static bool IsSirtLocalRef(JNIEnv* env, jobject localRef) {
85 return GetIndirectRefKind(localRef) == kSirtOrInvalid &&
Ian Rogers0399dde2012-06-06 17:09:28 -070086 reinterpret_cast<JNIEnvExt*>(env)->self->SirtContains(localRef);
Ian Rogers959f8ed2012-02-07 16:33:37 -080087}
88
Elliott Hughes3f6635a2012-06-19 13:37:49 -070089// Hack to allow forcecopy to work with jniGetNonMovableArrayElements.
90// The code deliberately uses an invalid sequence of operations, so we
91// need to pass it through unmodified. Review that code before making
92// any changes here.
Elliott Hughesa2501992011-08-26 19:39:54 -070093#define kNoCopyMagic 0xd5aab57f
94
Elliott Hughes3f6635a2012-06-19 13:37:49 -070095// Flags passed into ScopedCheck.
Elliott Hughesa2501992011-08-26 19:39:54 -070096#define kFlag_Default 0x0000
97
Elliott Hughes3f6635a2012-06-19 13:37:49 -070098#define kFlag_CritBad 0x0000 // Calling while in critical is not allowed.
99#define kFlag_CritOkay 0x0001 // Calling while in critical is allowed.
100#define kFlag_CritGet 0x0002 // This is a critical "get".
101#define kFlag_CritRelease 0x0003 // This is a critical "release".
102#define kFlag_CritMask 0x0003 // Bit mask to get "crit" value.
Elliott Hughesa2501992011-08-26 19:39:54 -0700103
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700104#define kFlag_ExcepBad 0x0000 // Raised exceptions are not allowed.
105#define kFlag_ExcepOkay 0x0004 // Raised exceptions are allowed.
Elliott Hughesa2501992011-08-26 19:39:54 -0700106
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700107#define kFlag_Release 0x0010 // Are we in a non-critical release function?
108#define kFlag_NullableUtf 0x0020 // Are our UTF parameters nullable?
Elliott Hughesa2501992011-08-26 19:39:54 -0700109
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700110#define kFlag_Invocation 0x8000 // Part of the invocation interface (JavaVM*).
Elliott Hughesa2501992011-08-26 19:39:54 -0700111
Elliott Hughes485cac42011-12-09 17:49:35 -0800112#define kFlag_ForceTrace 0x80000000 // Add this to a JNI function's flags if you want to trace every call.
113
Elliott Hughesa0957642011-09-02 14:27:33 -0700114static const char* gBuiltInPrefixes[] = {
115 "Landroid/",
116 "Lcom/android/",
117 "Lcom/google/android/",
118 "Ldalvik/",
119 "Ljava/",
120 "Ljavax/",
121 "Llibcore/",
122 "Lorg/apache/harmony/",
123 NULL
124};
125
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700126static bool ShouldTrace(JavaVMExt* vm, const Method* method)
127 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700128 // If both "-Xcheck:jni" and "-Xjnitrace:" are enabled, we print trace messages
129 // when a native method that matches the -Xjnitrace argument calls a JNI function
130 // such as NewByteArray.
131 // If -verbose:third-party-jni is on, we want to log any JNI function calls
132 // made by a third-party native method.
Elliott Hughes81ff3182012-03-23 20:35:56 -0700133 std::string class_name(MethodHelper(method).GetDeclaringClassDescriptor());
134 if (!vm->trace.empty() && class_name.find(vm->trace) != std::string::npos) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700135 return true;
136 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800137 if (VLOG_IS_ON(third_party_jni)) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700138 // Return true if we're trying to log all third-party JNI activity and 'method' doesn't look
139 // like part of Android.
Elliott Hughesa0957642011-09-02 14:27:33 -0700140 for (size_t i = 0; gBuiltInPrefixes[i] != NULL; ++i) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700141 if (StartsWith(class_name, gBuiltInPrefixes[i])) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700142 return false;
143 }
144 }
145 return true;
146 }
147 return false;
148}
149
Elliott Hughesa2501992011-08-26 19:39:54 -0700150class ScopedCheck {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800151 public:
Elliott Hughesa2501992011-08-26 19:39:54 -0700152 // For JNIEnv* functions.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700153 explicit ScopedCheck(JNIEnv* env, int flags, const char* functionName)
154 SHARED_LOCK_FUNCTION(GlobalSynchronization::mutator_lock_)
155 : soa_(env) {
Ian Rogers365c1022012-06-22 15:05:28 -0700156 Init(flags, functionName, true);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700157 CheckThread(flags);
Elliott Hughesa2501992011-08-26 19:39:54 -0700158 }
159
160 // For JavaVM* functions.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700161 // TODO: it's not correct that this is a lock function, but making it so aids annotalysis.
162 explicit ScopedCheck(JavaVM* vm, bool has_method, const char* functionName)
163 SHARED_LOCK_FUNCTION(GlobalSynchronization::mutator_lock_)
164 : soa_(vm) {
Ian Rogers365c1022012-06-22 15:05:28 -0700165 Init(kFlag_Invocation, functionName, has_method);
Elliott Hughesa2501992011-08-26 19:39:54 -0700166 }
167
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700168 ~ScopedCheck() UNLOCK_FUNCTION(GlobalSynchronization::mutator_lock_) {}
169
170 const ScopedObjectAccess& soa() {
171 return soa_;
172 }
173
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700174 bool ForceCopy() {
Elliott Hughesa2501992011-08-26 19:39:54 -0700175 return Runtime::Current()->GetJavaVM()->force_copy;
176 }
177
Elliott Hughes81ff3182012-03-23 20:35:56 -0700178 // Checks that 'class_name' is a valid "fully-qualified" JNI class name, like "java/lang/Thread"
179 // or "[Ljava/lang/Object;". A ClassLoader can actually normalize class names a couple of
180 // times, so using "java.lang.Thread" instead of "java/lang/Thread" might work in some
181 // circumstances, but this is incorrect.
182 void CheckClassName(const char* class_name) {
183 if (!IsValidJniClassName(class_name)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700184 JniAbortF(function_name_,
185 "illegal class name '%s'\n"
186 " (should be of the form 'package/Class', [Lpackage/Class;' or '[[B')",
187 class_name);
Elliott Hughesa2501992011-08-26 19:39:54 -0700188 }
189 }
190
191 /*
192 * Verify that the field is of the appropriate type. If the field has an
193 * object type, "java_object" is the object we're trying to assign into it.
194 *
195 * Works for both static and instance fields.
196 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700197 void CheckFieldType(jobject java_object, jfieldID fid, char prim, bool isStatic)
198 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700199 Field* f = CheckFieldID(fid);
200 if (f == NULL) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700201 return;
202 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800203 Class* field_type = FieldHelper(f).GetType();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700204 if (!field_type->IsPrimitive()) {
205 if (java_object != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700206 Object* obj = soa_.Decode<Object*>(java_object);
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700207 // If java_object is a weak global ref whose referent has been cleared,
208 // obj will be NULL. Otherwise, obj should always be non-NULL
209 // and valid.
Elliott Hughes88c5c352012-03-15 18:49:48 -0700210 if (!Runtime::Current()->GetHeap()->IsHeapAddress(obj)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700211 JniAbortF(function_name_, "field operation on invalid %s: %p",
212 ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700213 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700214 } else {
Brian Carlstrom16192862011-09-12 17:50:06 -0700215 if (!obj->InstanceOf(field_type)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700216 JniAbortF(function_name_, "attempt to set field %s with value of wrong type: %s",
217 PrettyField(f).c_str(), PrettyTypeOf(obj).c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700218 return;
219 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700220 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700221 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700222 } else if (field_type != Runtime::Current()->GetClassLinker()->FindPrimitiveClass(prim)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700223 JniAbortF(function_name_, "attempt to set field %s with value of wrong type: %c",
224 PrettyField(f).c_str(), prim);
Elliott Hughesa2501992011-08-26 19:39:54 -0700225 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700226 }
227
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700228 if (isStatic != f->IsStatic()) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700229 if (isStatic) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700230 JniAbortF(function_name_, "accessing non-static field %s as static", PrettyField(f).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700231 } else {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700232 JniAbortF(function_name_, "accessing static field %s as non-static", PrettyField(f).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700233 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700234 return;
235 }
236 }
237
238 /*
239 * Verify that this instance field ID is valid for this object.
240 *
241 * Assumes "jobj" has already been validated.
242 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700243 void CheckInstanceFieldID(jobject java_object, jfieldID fid)
244 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
245 Object* o = soa_.Decode<Object*>(java_object);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800246 if (o == NULL || !Runtime::Current()->GetHeap()->IsHeapAddress(o)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700247 JniAbortF(function_name_, "field operation on invalid %s: %p",
248 ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700249 return;
250 }
251
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700252 Field* f = CheckFieldID(fid);
253 if (f == NULL) {
254 return;
255 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700256 Class* c = o->GetClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800257 FieldHelper fh(f);
258 if (c->FindInstanceField(fh.GetName(), fh.GetTypeDescriptor()) == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700259 JniAbortF(function_name_, "jfieldID %s not valid for an object of class %s",
260 PrettyField(f).c_str(), PrettyTypeOf(o).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700261 }
262 }
263
264 /*
265 * Verify that the pointer value is non-NULL.
266 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700267 void CheckNonNull(const void* ptr) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700268 if (ptr == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700269 JniAbortF(function_name_, "non-nullable argument was NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -0700270 }
271 }
272
273 /*
274 * Verify that the method's return type matches the type of call.
275 * 'expectedType' will be "L" for all objects, including arrays.
276 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700277 void CheckSig(jmethodID mid, const char* expectedType, bool isStatic)
278 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800279 Method* m = CheckMethodID(mid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700280 if (m == NULL) {
281 return;
282 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800283 if (*expectedType != MethodHelper(m).GetShorty()[0]) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700284 JniAbortF(function_name_, "the return type of %s does not match %s",
285 function_name_, PrettyMethod(m).c_str());
286 }
287 if (isStatic != m->IsStatic()) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700288 if (isStatic) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700289 JniAbortF(function_name_, "calling non-static method %s with %s",
290 PrettyMethod(m).c_str(), function_name_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700291 } else {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700292 JniAbortF(function_name_, "calling static method %s with %s",
293 PrettyMethod(m).c_str(), function_name_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700294 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700295 }
296 }
297
298 /*
299 * Verify that this static field ID is valid for this class.
300 *
301 * Assumes "java_class" has already been validated.
302 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700303 void CheckStaticFieldID(jclass java_class, jfieldID fid)
304 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
305 Class* c = soa_.Decode<Class*>(java_class);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700306 const Field* f = CheckFieldID(fid);
307 if (f == NULL) {
308 return;
309 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700310 if (f->GetDeclaringClass() != c) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700311 JniAbortF(function_name_, "static jfieldID %p not valid for class %s",
312 fid, PrettyClass(c).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700313 }
314 }
315
316 /*
Elliott Hughese84278b2012-03-22 10:06:53 -0700317 * Verify that "mid" is appropriate for "java_class".
Elliott Hughesa2501992011-08-26 19:39:54 -0700318 *
319 * A mismatch isn't dangerous, because the jmethodID defines the class. In
Elliott Hughese84278b2012-03-22 10:06:53 -0700320 * fact, java_class is unused in the implementation. It's best if we don't
Elliott Hughesa2501992011-08-26 19:39:54 -0700321 * allow bad code in the system though.
322 *
Elliott Hughese84278b2012-03-22 10:06:53 -0700323 * Instances of "java_class" must be instances of the method's declaring class.
Elliott Hughesa2501992011-08-26 19:39:54 -0700324 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700325 void CheckStaticMethod(jclass java_class, jmethodID mid)
326 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700327 const Method* m = CheckMethodID(mid);
328 if (m == NULL) {
329 return;
330 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700331 Class* c = soa_.Decode<Class*>(java_class);
Elliott Hughesa2501992011-08-26 19:39:54 -0700332 if (!c->IsAssignableFrom(m->GetDeclaringClass())) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700333 JniAbortF(function_name_, "can't call static %s on class %s",
334 PrettyMethod(m).c_str(), PrettyClass(c).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700335 }
336 }
337
338 /*
339 * Verify that "mid" is appropriate for "jobj".
340 *
341 * Make sure the object is an instance of the method's declaring class.
342 * (Note the mid might point to a declaration in an interface; this
343 * will be handled automatically by the instanceof check.)
344 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700345 void CheckVirtualMethod(jobject java_object, jmethodID mid)
346 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700347 const Method* m = CheckMethodID(mid);
348 if (m == NULL) {
349 return;
350 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700351 Object* o = soa_.Decode<Object*>(java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700352 if (!o->InstanceOf(m->GetDeclaringClass())) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700353 JniAbortF(function_name_, "can't call %s on instance of %s",
354 PrettyMethod(m).c_str(), PrettyTypeOf(o).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700355 }
356 }
357
358 /**
359 * The format string is a sequence of the following characters,
360 * and must be followed by arguments of the corresponding types
361 * in the same order.
362 *
363 * Java primitive types:
364 * B - jbyte
365 * C - jchar
366 * D - jdouble
367 * F - jfloat
368 * I - jint
369 * J - jlong
370 * S - jshort
371 * Z - jboolean (shown as true and false)
372 * V - void
373 *
374 * Java reference types:
375 * L - jobject
376 * a - jarray
377 * c - jclass
378 * s - jstring
379 *
380 * JNI types:
381 * b - jboolean (shown as JNI_TRUE and JNI_FALSE)
382 * f - jfieldID
383 * m - jmethodID
384 * p - void*
385 * r - jint (for release mode arguments)
Elliott Hughes78090d12011-10-07 14:31:47 -0700386 * u - const char* (Modified UTF-8)
Elliott Hughesa2501992011-08-26 19:39:54 -0700387 * z - jsize (for lengths; use i if negative values are okay)
388 * v - JavaVM*
389 * E - JNIEnv*
390 * . - no argument; just print "..." (used for varargs JNI calls)
391 *
392 * Use the kFlag_NullableUtf flag where 'u' field(s) are nullable.
393 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700394 void Check(bool entry, const char* fmt0, ...)
395 SHARED_LOCKS_REQUIRED (GlobalSynchronization::mutator_lock_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700396 va_list ap;
397
Elliott Hughesa0957642011-09-02 14:27:33 -0700398 const Method* traceMethod = NULL;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700399 if ((!soa_.Vm()->trace.empty() || VLOG_IS_ON(third_party_jni)) && has_method_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700400 // We need to guard some of the invocation interface's calls: a bad caller might
401 // use DetachCurrentThread or GetEnv on a thread that's not yet attached.
Elliott Hughesa0957642011-09-02 14:27:33 -0700402 Thread* self = Thread::Current();
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700403 if ((flags_ & kFlag_Invocation) == 0 || self != NULL) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700404 traceMethod = self->GetCurrentMethod();
Elliott Hughesa2501992011-08-26 19:39:54 -0700405 }
406 }
Elliott Hughesa0957642011-09-02 14:27:33 -0700407
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700408 if (((flags_ & kFlag_ForceTrace) != 0) || (traceMethod != NULL && ShouldTrace(soa_.Vm(), traceMethod))) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700409 va_start(ap, fmt0);
410 std::string msg;
411 for (const char* fmt = fmt0; *fmt;) {
412 char ch = *fmt++;
413 if (ch == 'B') { // jbyte
414 jbyte b = va_arg(ap, int);
415 if (b >= 0 && b < 10) {
416 StringAppendF(&msg, "%d", b);
417 } else {
418 StringAppendF(&msg, "%#x (%d)", b, b);
419 }
420 } else if (ch == 'C') { // jchar
421 jchar c = va_arg(ap, int);
422 if (c < 0x7f && c >= ' ') {
423 StringAppendF(&msg, "U+%x ('%c')", c, c);
424 } else {
425 StringAppendF(&msg, "U+%x", c);
426 }
427 } else if (ch == 'F' || ch == 'D') { // jfloat, jdouble
428 StringAppendF(&msg, "%g", va_arg(ap, double));
429 } else if (ch == 'I' || ch == 'S') { // jint, jshort
430 StringAppendF(&msg, "%d", va_arg(ap, int));
431 } else if (ch == 'J') { // jlong
432 StringAppendF(&msg, "%lld", va_arg(ap, jlong));
433 } else if (ch == 'Z') { // jboolean
434 StringAppendF(&msg, "%s", va_arg(ap, int) ? "true" : "false");
435 } else if (ch == 'V') { // void
436 msg += "void";
437 } else if (ch == 'v') { // JavaVM*
438 JavaVM* vm = va_arg(ap, JavaVM*);
439 StringAppendF(&msg, "(JavaVM*)%p", vm);
440 } else if (ch == 'E') { // JNIEnv*
441 JNIEnv* env = va_arg(ap, JNIEnv*);
442 StringAppendF(&msg, "(JNIEnv*)%p", env);
443 } else if (ch == 'L' || ch == 'a' || ch == 's') { // jobject, jarray, jstring
444 // For logging purposes, these are identical.
445 jobject o = va_arg(ap, jobject);
446 if (o == NULL) {
447 msg += "NULL";
448 } else {
449 StringAppendF(&msg, "%p", o);
450 }
451 } else if (ch == 'b') { // jboolean (JNI-style)
452 jboolean b = va_arg(ap, int);
453 msg += (b ? "JNI_TRUE" : "JNI_FALSE");
454 } else if (ch == 'c') { // jclass
455 jclass jc = va_arg(ap, jclass);
456 Class* c = reinterpret_cast<Class*>(Thread::Current()->DecodeJObject(jc));
457 if (c == NULL) {
458 msg += "NULL";
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800459 } else if (c == kInvalidIndirectRefObject || !Runtime::Current()->GetHeap()->IsHeapAddress(c)) {
Elliott Hughes485cac42011-12-09 17:49:35 -0800460 StringAppendF(&msg, "INVALID POINTER:%p", jc);
461 } else if (!c->IsClass()) {
462 msg += "INVALID NON-CLASS OBJECT OF TYPE:" + PrettyTypeOf(c);
Elliott Hughesa2501992011-08-26 19:39:54 -0700463 } else {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700464 msg += PrettyClass(c);
Elliott Hughesa2501992011-08-26 19:39:54 -0700465 if (!entry) {
466 StringAppendF(&msg, " (%p)", jc);
467 }
468 }
469 } else if (ch == 'f') { // jfieldID
470 jfieldID fid = va_arg(ap, jfieldID);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700471 Field* f = reinterpret_cast<Field*>(fid);
Elliott Hughesa2501992011-08-26 19:39:54 -0700472 msg += PrettyField(f);
473 if (!entry) {
474 StringAppendF(&msg, " (%p)", fid);
475 }
476 } else if (ch == 'z') { // non-negative jsize
477 // You might expect jsize to be size_t, but it's not; it's the same as jint.
478 // We only treat this specially so we can do the non-negative check.
479 // TODO: maybe this wasn't worth it?
480 jint i = va_arg(ap, jint);
481 StringAppendF(&msg, "%d", i);
482 } else if (ch == 'm') { // jmethodID
483 jmethodID mid = va_arg(ap, jmethodID);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700484 Method* m = reinterpret_cast<Method*>(mid);
Elliott Hughesa2501992011-08-26 19:39:54 -0700485 msg += PrettyMethod(m);
486 if (!entry) {
487 StringAppendF(&msg, " (%p)", mid);
488 }
489 } else if (ch == 'p') { // void* ("pointer")
490 void* p = va_arg(ap, void*);
491 if (p == NULL) {
492 msg += "NULL";
493 } else {
494 StringAppendF(&msg, "(void*) %p", p);
495 }
496 } else if (ch == 'r') { // jint (release mode)
497 jint releaseMode = va_arg(ap, jint);
498 if (releaseMode == 0) {
499 msg += "0";
500 } else if (releaseMode == JNI_ABORT) {
501 msg += "JNI_ABORT";
502 } else if (releaseMode == JNI_COMMIT) {
503 msg += "JNI_COMMIT";
504 } else {
505 StringAppendF(&msg, "invalid release mode %d", releaseMode);
506 }
Elliott Hughes78090d12011-10-07 14:31:47 -0700507 } else if (ch == 'u') { // const char* (Modified UTF-8)
Elliott Hughesa2501992011-08-26 19:39:54 -0700508 const char* utf = va_arg(ap, const char*);
509 if (utf == NULL) {
510 msg += "NULL";
511 } else {
512 StringAppendF(&msg, "\"%s\"", utf);
513 }
514 } else if (ch == '.') {
515 msg += "...";
516 } else {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700517 JniAbortF(function_name_, "unknown trace format specifier: %c", ch);
Elliott Hughesa2501992011-08-26 19:39:54 -0700518 return;
519 }
520 if (*fmt) {
521 StringAppendF(&msg, ", ");
522 }
523 }
524 va_end(ap);
525
Elliott Hughes485cac42011-12-09 17:49:35 -0800526 if ((flags_ & kFlag_ForceTrace) != 0) {
527 LOG(INFO) << "JNI: call to " << function_name_ << "(" << msg << ")";
528 } else if (entry) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700529 if (has_method_) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700530 std::string methodName(PrettyMethod(traceMethod, false));
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700531 LOG(INFO) << "JNI: " << methodName << " -> " << function_name_ << "(" << msg << ")";
532 indent_ = methodName.size() + 1;
Elliott Hughesa2501992011-08-26 19:39:54 -0700533 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700534 LOG(INFO) << "JNI: -> " << function_name_ << "(" << msg << ")";
535 indent_ = 0;
Elliott Hughesa2501992011-08-26 19:39:54 -0700536 }
537 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700538 LOG(INFO) << StringPrintf("JNI: %*s<- %s returned %s", indent_, "", function_name_, msg.c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700539 }
540 }
541
542 // We always do the thorough checks on entry, and never on exit...
543 if (entry) {
544 va_start(ap, fmt0);
545 for (const char* fmt = fmt0; *fmt; ++fmt) {
546 char ch = *fmt;
547 if (ch == 'a') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700548 CheckArray(va_arg(ap, jarray));
Elliott Hughesa2501992011-08-26 19:39:54 -0700549 } else if (ch == 'c') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700550 CheckInstance(kClass, va_arg(ap, jclass));
Elliott Hughesa2501992011-08-26 19:39:54 -0700551 } else if (ch == 'L') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700552 CheckObject(va_arg(ap, jobject));
Elliott Hughesa2501992011-08-26 19:39:54 -0700553 } else if (ch == 'r') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700554 CheckReleaseMode(va_arg(ap, jint));
Elliott Hughesa2501992011-08-26 19:39:54 -0700555 } else if (ch == 's') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700556 CheckInstance(kString, va_arg(ap, jstring));
Elliott Hughesa2501992011-08-26 19:39:54 -0700557 } else if (ch == 'u') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700558 if ((flags_ & kFlag_Release) != 0) {
559 CheckNonNull(va_arg(ap, const char*));
Elliott Hughesa2501992011-08-26 19:39:54 -0700560 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700561 bool nullable = ((flags_ & kFlag_NullableUtf) != 0);
562 CheckUtfString(va_arg(ap, const char*), nullable);
Elliott Hughesa2501992011-08-26 19:39:54 -0700563 }
564 } else if (ch == 'z') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700565 CheckLengthPositive(va_arg(ap, jsize));
Elliott Hughesa2501992011-08-26 19:39:54 -0700566 } else if (strchr("BCISZbfmpEv", ch) != NULL) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800567 va_arg(ap, uint32_t); // Skip this argument.
Elliott Hughesa2501992011-08-26 19:39:54 -0700568 } else if (ch == 'D' || ch == 'F') {
569 va_arg(ap, double); // Skip this argument.
570 } else if (ch == 'J') {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800571 va_arg(ap, uint64_t); // Skip this argument.
Elliott Hughesa2501992011-08-26 19:39:54 -0700572 } else if (ch == '.') {
573 } else {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800574 LOG(FATAL) << "Unknown check format specifier: " << ch;
Elliott Hughesa2501992011-08-26 19:39:54 -0700575 }
576 }
577 va_end(ap);
578 }
579 }
580
Elliott Hughesa92853e2012-02-07 16:09:27 -0800581 enum InstanceKind {
582 kClass,
Elliott Hughes0f3c5532012-03-30 14:51:51 -0700583 kDirectByteBuffer,
584 kObject,
585 kString,
586 kThrowable,
Elliott Hughesa92853e2012-02-07 16:09:27 -0800587 };
588
589 /*
590 * Verify that "jobj" is a valid non-NULL object reference, and points to
591 * an instance of expectedClass.
592 *
593 * Because we're looking at an object on the GC heap, we have to switch
594 * to "running" mode before doing the checks.
595 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700596 bool CheckInstance(InstanceKind kind, jobject java_object)
597 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughesa92853e2012-02-07 16:09:27 -0800598 const char* what = NULL;
599 switch (kind) {
600 case kClass:
601 what = "jclass";
602 break;
603 case kDirectByteBuffer:
604 what = "direct ByteBuffer";
605 break;
606 case kObject:
607 what = "jobject";
608 break;
609 case kString:
610 what = "jstring";
611 break;
612 case kThrowable:
613 what = "jthrowable";
614 break;
615 default:
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700616 LOG(FATAL) << "Unknown kind " << static_cast<int>(kind);
Elliott Hughesa92853e2012-02-07 16:09:27 -0800617 }
618
619 if (java_object == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700620 JniAbortF(function_name_, "%s received null %s", function_name_, what);
Elliott Hughesa92853e2012-02-07 16:09:27 -0800621 return false;
622 }
623
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700624 Object* obj = soa_.Decode<Object*>(java_object);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800625 if (!Runtime::Current()->GetHeap()->IsHeapAddress(obj)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700626 JniAbortF(function_name_, "%s is an invalid %s: %p (%p)",
627 what, ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object, obj);
Elliott Hughesa92853e2012-02-07 16:09:27 -0800628 return false;
629 }
630
631 bool okay = true;
632 switch (kind) {
633 case kClass:
634 okay = obj->IsClass();
635 break;
636 case kDirectByteBuffer:
637 UNIMPLEMENTED(FATAL);
638 break;
639 case kString:
640 okay = obj->GetClass()->IsStringClass();
641 break;
642 case kThrowable:
643 okay = obj->GetClass()->IsThrowableClass();
644 break;
645 case kObject:
646 break;
647 }
648 if (!okay) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700649 JniAbortF(function_name_, "%s has wrong type: %s", what, PrettyTypeOf(obj).c_str());
Elliott Hughesa92853e2012-02-07 16:09:27 -0800650 return false;
651 }
652
653 return true;
654 }
655
Elliott Hughesba8eee12012-01-24 20:25:24 -0800656 private:
Elliott Hughes81ff3182012-03-23 20:35:56 -0700657 // Set "has_method" to true if we have a valid thread with a method pointer.
658 // We won't have one before attaching a thread, after detaching a thread, or
659 // when shutting down the runtime.
Ian Rogers365c1022012-06-22 15:05:28 -0700660 void Init(int flags, const char* functionName, bool has_method) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700661 flags_ = flags;
662 function_name_ = functionName;
Elliott Hughes81ff3182012-03-23 20:35:56 -0700663 has_method_ = has_method;
Elliott Hughesa2501992011-08-26 19:39:54 -0700664 }
665
666 /*
667 * Verify that "array" is non-NULL and points to an Array object.
668 *
669 * Since we're dealing with objects, switch to "running" mode.
670 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700671 void CheckArray(jarray java_array) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700672 if (java_array == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700673 JniAbortF(function_name_, "jarray was NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -0700674 return;
675 }
676
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700677 Array* a = soa_.Decode<Array*>(java_array);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800678 if (!Runtime::Current()->GetHeap()->IsHeapAddress(a)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700679 JniAbortF(function_name_, "jarray is an invalid %s: %p (%p)",
680 ToStr<IndirectRefKind>(GetIndirectRefKind(java_array)).c_str(), java_array, a);
Elliott Hughesa2501992011-08-26 19:39:54 -0700681 } else if (!a->IsArrayInstance()) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700682 JniAbortF(function_name_, "jarray argument has non-array type: %s", PrettyTypeOf(a).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700683 }
684 }
685
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700686 void CheckLengthPositive(jsize length) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700687 if (length < 0) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700688 JniAbortF(function_name_, "negative jsize: %d", length);
Elliott Hughesa2501992011-08-26 19:39:54 -0700689 }
690 }
691
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700692 Field* CheckFieldID(jfieldID fid) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700693 if (fid == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700694 JniAbortF(function_name_, "jfieldID was NULL");
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700695 return NULL;
696 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700697 Field* f = soa_.DecodeField(fid);
Ian Rogers365c1022012-06-22 15:05:28 -0700698 if (!Runtime::Current()->GetHeap()->IsHeapAddress(f) || !f->IsField()) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700699 JniAbortF(function_name_, "invalid jfieldID: %p", fid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700700 return NULL;
701 }
702 return f;
703 }
704
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700705 Method* CheckMethodID(jmethodID mid) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700706 if (mid == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700707 JniAbortF(function_name_, "jmethodID was NULL");
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700708 return NULL;
709 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700710 Method* m = soa_.DecodeMethod(mid);
Ian Rogers365c1022012-06-22 15:05:28 -0700711 if (!Runtime::Current()->GetHeap()->IsHeapAddress(m) || !m->IsMethod()) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700712 JniAbortF(function_name_, "invalid jmethodID: %p", mid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700713 return NULL;
714 }
715 return m;
716 }
717
Elliott Hughesa2501992011-08-26 19:39:54 -0700718 /*
719 * Verify that "jobj" is a valid object, and that it's an object that JNI
720 * is allowed to know about. We allow NULL references.
721 *
722 * Switches to "running" mode before performing checks.
723 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700724 void CheckObject(jobject java_object)
725 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700726 if (java_object == NULL) {
727 return;
728 }
729
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700730 Object* o = soa_.Decode<Object*>(java_object);
Elliott Hughes88c5c352012-03-15 18:49:48 -0700731 if (!Runtime::Current()->GetHeap()->IsHeapAddress(o)) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700732 // TODO: when we remove work_around_app_jni_bugs, this should be impossible.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700733 JniAbortF(function_name_, "native code passing in reference to invalid %s: %p",
734 ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700735 }
736 }
737
738 /*
739 * Verify that the "mode" argument passed to a primitive array Release
740 * function is one of the valid values.
741 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700742 void CheckReleaseMode(jint mode) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700743 if (mode != 0 && mode != JNI_COMMIT && mode != JNI_ABORT) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700744 JniAbortF(function_name_, "bad value for release mode: %d", mode);
Elliott Hughesa2501992011-08-26 19:39:54 -0700745 }
746 }
747
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700748 void CheckThread(int flags) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700749 Thread* self = Thread::Current();
750 if (self == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700751 JniAbortF(function_name_, "a thread (tid %d) is making JNI calls without being attached", GetTid());
Elliott Hughesa2501992011-08-26 19:39:54 -0700752 return;
753 }
754
755 // Get the *correct* JNIEnv by going through our TLS pointer.
756 JNIEnvExt* threadEnv = self->GetJniEnv();
757
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700758 // Verify that the current thread is (a) attached and (b) associated with
759 // this particular instance of JNIEnv.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700760 if (soa_.Env() != threadEnv) {
761 if (soa_.Vm()->work_around_app_jni_bugs) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700762 // If we're keeping broken code limping along, we need to suppress the abort...
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700763 LOG(ERROR) << "APP BUG DETECTED: thread " << *self << " using JNIEnv* from thread " << *soa_.Self();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700764 } else {
765 JniAbortF(function_name_, "thread %s using JNIEnv* from thread %s",
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700766 ToStr<Thread>(*self).c_str(), ToStr<Thread>(*soa_.Self()).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700767 return;
768 }
769 }
770
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700771 // Verify that, if this thread previously made a critical "get" call, we
772 // do the corresponding "release" call before we try anything else.
Elliott Hughesa2501992011-08-26 19:39:54 -0700773 switch (flags & kFlag_CritMask) {
774 case kFlag_CritOkay: // okay to call this method
775 break;
776 case kFlag_CritBad: // not okay to call
777 if (threadEnv->critical) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700778 JniAbortF(function_name_, "thread %s using JNI after critical get", ToStr<Thread>(*self).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700779 return;
780 }
781 break;
782 case kFlag_CritGet: // this is a "get" call
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700783 // Don't check here; we allow nested gets.
Elliott Hughesa2501992011-08-26 19:39:54 -0700784 threadEnv->critical++;
785 break;
786 case kFlag_CritRelease: // this is a "release" call
787 threadEnv->critical--;
788 if (threadEnv->critical < 0) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700789 JniAbortF(function_name_, "thread %s called too many critical releases", ToStr<Thread>(*self).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700790 return;
791 }
792 break;
793 default:
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800794 LOG(FATAL) << "Bad flags (internal error): " << flags;
Elliott Hughesa2501992011-08-26 19:39:54 -0700795 }
796
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700797 // Verify that, if an exception has been raised, the native code doesn't
798 // make any JNI calls other than the Exception* methods.
Elliott Hughesa2501992011-08-26 19:39:54 -0700799 if ((flags & kFlag_ExcepOkay) == 0 && self->IsExceptionPending()) {
Elliott Hughes30646832011-10-13 16:59:46 -0700800 std::string type(PrettyTypeOf(self->GetException()));
Elliott Hughes30646832011-10-13 16:59:46 -0700801 // TODO: write native code that doesn't require allocation for dumping an exception.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700802 // TODO: do we care any more? art always dumps pending exceptions on aborting threads.
Elliott Hughes30646832011-10-13 16:59:46 -0700803 if (type != "java.lang.OutOfMemoryError") {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700804 JniAbortF(function_name_, "JNI %s called with pending exception: %s",
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700805 function_name_, type.c_str(), jniGetStackTrace(soa_.Env()).c_str());
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700806 } else {
807 JniAbortF(function_name_, "JNI %s called with %s pending", function_name_, type.c_str());
Elliott Hughes30646832011-10-13 16:59:46 -0700808 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700809 return;
810 }
811 }
812
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700813 // Verifies that "bytes" points to valid Modified UTF-8 data.
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700814 void CheckUtfString(const char* bytes, bool nullable) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700815 if (bytes == NULL) {
816 if (!nullable) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700817 JniAbortF(function_name_, "non-nullable const char* was NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -0700818 return;
819 }
820 return;
821 }
822
823 const char* errorKind = NULL;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700824 uint8_t utf8 = CheckUtfBytes(bytes, &errorKind);
Elliott Hughesa2501992011-08-26 19:39:54 -0700825 if (errorKind != NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700826 JniAbortF(function_name_,
827 "input is not valid Modified UTF-8: illegal %s byte %#x\n"
828 " string: '%s'", errorKind, utf8, bytes);
Elliott Hughesa2501992011-08-26 19:39:54 -0700829 return;
830 }
831 }
832
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700833 static uint8_t CheckUtfBytes(const char* bytes, const char** errorKind) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700834 while (*bytes != '\0') {
835 uint8_t utf8 = *(bytes++);
836 // Switch on the high four bits.
837 switch (utf8 >> 4) {
838 case 0x00:
839 case 0x01:
840 case 0x02:
841 case 0x03:
842 case 0x04:
843 case 0x05:
844 case 0x06:
845 case 0x07:
846 // Bit pattern 0xxx. No need for any extra bytes.
847 break;
848 case 0x08:
849 case 0x09:
850 case 0x0a:
851 case 0x0b:
852 case 0x0f:
853 /*
854 * Bit pattern 10xx or 1111, which are illegal start bytes.
855 * Note: 1111 is valid for normal UTF-8, but not the
Elliott Hughes78090d12011-10-07 14:31:47 -0700856 * Modified UTF-8 used here.
Elliott Hughesa2501992011-08-26 19:39:54 -0700857 */
858 *errorKind = "start";
859 return utf8;
860 case 0x0e:
861 // Bit pattern 1110, so there are two additional bytes.
862 utf8 = *(bytes++);
863 if ((utf8 & 0xc0) != 0x80) {
864 *errorKind = "continuation";
865 return utf8;
866 }
867 // Fall through to take care of the final byte.
868 case 0x0c:
869 case 0x0d:
870 // Bit pattern 110x, so there is one additional byte.
871 utf8 = *(bytes++);
872 if ((utf8 & 0xc0) != 0x80) {
873 *errorKind = "continuation";
874 return utf8;
875 }
876 break;
877 }
878 }
879 return 0;
880 }
881
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700882 const ScopedObjectAccess soa_;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700883 const char* function_name_;
884 int flags_;
885 bool has_method_;
Elliott Hughes92cb4982011-12-16 16:57:28 -0800886 int indent_;
Elliott Hughesa2501992011-08-26 19:39:54 -0700887
888 DISALLOW_COPY_AND_ASSIGN(ScopedCheck);
889};
890
891#define CHECK_JNI_ENTRY(flags, types, args...) \
892 ScopedCheck sc(env, flags, __FUNCTION__); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700893 sc.Check(true, types, ##args)
Elliott Hughesa2501992011-08-26 19:39:54 -0700894
895#define CHECK_JNI_EXIT(type, exp) ({ \
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700896 typeof(exp) _rc = (exp); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700897 sc.Check(false, type, _rc); \
Elliott Hughesa2501992011-08-26 19:39:54 -0700898 _rc; })
899#define CHECK_JNI_EXIT_VOID() \
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700900 sc.Check(false, "V")
Elliott Hughesa2501992011-08-26 19:39:54 -0700901
902/*
903 * ===========================================================================
904 * Guarded arrays
905 * ===========================================================================
906 */
907
908#define kGuardLen 512 /* must be multiple of 2 */
909#define kGuardPattern 0xd5e3 /* uncommon values; d5e3d5e3 invalid addr */
910#define kGuardMagic 0xffd5aa96
911
912/* this gets tucked in at the start of the buffer; struct size must be even */
913struct GuardedCopy {
914 uint32_t magic;
915 uLong adler;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700916 size_t original_length;
917 const void* original_ptr;
Elliott Hughesa2501992011-08-26 19:39:54 -0700918
919 /* find the GuardedCopy given the pointer into the "live" data */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700920 static inline const GuardedCopy* FromData(const void* dataBuf) {
921 return reinterpret_cast<const GuardedCopy*>(ActualBuffer(dataBuf));
Elliott Hughesa2501992011-08-26 19:39:54 -0700922 }
923
924 /*
925 * Create an over-sized buffer to hold the contents of "buf". Copy it in,
926 * filling in the area around it with guard data.
927 *
928 * We use a 16-bit pattern to make a rogue memset less likely to elude us.
929 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700930 static void* Create(const void* buf, size_t len, bool modOkay) {
931 size_t newLen = ActualLength(len);
932 uint8_t* newBuf = DebugAlloc(newLen);
Elliott Hughesa2501992011-08-26 19:39:54 -0700933
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700934 // Fill it in with a pattern.
Elliott Hughesba8eee12012-01-24 20:25:24 -0800935 uint16_t* pat = reinterpret_cast<uint16_t*>(newBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -0700936 for (size_t i = 0; i < newLen / 2; i++) {
937 *pat++ = kGuardPattern;
938 }
939
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700940 // Copy the data in; note "len" could be zero.
Elliott Hughesa2501992011-08-26 19:39:54 -0700941 memcpy(newBuf + kGuardLen / 2, buf, len);
942
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700943 // If modification is not expected, grab a checksum.
Elliott Hughesa2501992011-08-26 19:39:54 -0700944 uLong adler = 0;
945 if (!modOkay) {
946 adler = adler32(0L, Z_NULL, 0);
Elliott Hughesba8eee12012-01-24 20:25:24 -0800947 adler = adler32(adler, reinterpret_cast<const Bytef*>(buf), len);
948 *reinterpret_cast<uLong*>(newBuf) = adler;
Elliott Hughesa2501992011-08-26 19:39:54 -0700949 }
950
951 GuardedCopy* pExtra = reinterpret_cast<GuardedCopy*>(newBuf);
952 pExtra->magic = kGuardMagic;
953 pExtra->adler = adler;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700954 pExtra->original_ptr = buf;
955 pExtra->original_length = len;
Elliott Hughesa2501992011-08-26 19:39:54 -0700956
957 return newBuf + kGuardLen / 2;
958 }
959
960 /*
961 * Free up the guard buffer, scrub it, and return the original pointer.
962 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700963 static void* Destroy(void* dataBuf) {
964 const GuardedCopy* pExtra = GuardedCopy::FromData(dataBuf);
Elliott Hughesba8eee12012-01-24 20:25:24 -0800965 void* original_ptr = const_cast<void*>(pExtra->original_ptr);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700966 size_t len = pExtra->original_length;
967 DebugFree(dataBuf, len);
968 return original_ptr;
Elliott Hughesa2501992011-08-26 19:39:54 -0700969 }
970
971 /*
972 * Verify the guard area and, if "modOkay" is false, that the data itself
973 * has not been altered.
974 *
975 * The caller has already checked that "dataBuf" is non-NULL.
976 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700977 static void Check(const char* functionName, const void* dataBuf, bool modOkay) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700978 static const uint32_t kMagicCmp = kGuardMagic;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700979 const uint8_t* fullBuf = ActualBuffer(dataBuf);
980 const GuardedCopy* pExtra = GuardedCopy::FromData(dataBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -0700981
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700982 // Before we do anything with "pExtra", check the magic number. We
983 // do the check with memcmp rather than "==" in case the pointer is
984 // unaligned. If it points to completely bogus memory we're going
985 // to crash, but there's no easy way around that.
Elliott Hughesa2501992011-08-26 19:39:54 -0700986 if (memcmp(&pExtra->magic, &kMagicCmp, 4) != 0) {
987 uint8_t buf[4];
988 memcpy(buf, &pExtra->magic, 4);
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700989 JniAbortF(functionName,
990 "guard magic does not match (found 0x%02x%02x%02x%02x) -- incorrect data pointer %p?",
991 buf[3], buf[2], buf[1], buf[0], dataBuf); // Assumes little-endian.
Elliott Hughesa2501992011-08-26 19:39:54 -0700992 }
993
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700994 size_t len = pExtra->original_length;
Elliott Hughesa2501992011-08-26 19:39:54 -0700995
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700996 // Check bottom half of guard; skip over optional checksum storage.
Elliott Hughesba8eee12012-01-24 20:25:24 -0800997 const uint16_t* pat = reinterpret_cast<const uint16_t*>(fullBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -0700998 for (size_t i = sizeof(GuardedCopy) / 2; i < (kGuardLen / 2 - sizeof(GuardedCopy)) / 2; i++) {
999 if (pat[i] != kGuardPattern) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001000 JniAbortF(functionName, "guard pattern(1) disturbed at %p +%d", fullBuf, i*2);
Elliott Hughesa2501992011-08-26 19:39:54 -07001001 }
1002 }
1003
1004 int offset = kGuardLen / 2 + len;
1005 if (offset & 0x01) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001006 // Odd byte; expected value depends on endian.
Elliott Hughesa2501992011-08-26 19:39:54 -07001007 const uint16_t patSample = kGuardPattern;
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001008 uint8_t expected_byte = reinterpret_cast<const uint8_t*>(&patSample)[1];
1009 if (fullBuf[offset] != expected_byte) {
1010 JniAbortF(functionName, "guard pattern disturbed in odd byte after %p +%d 0x%02x 0x%02x",
1011 fullBuf, offset, fullBuf[offset], expected_byte);
Elliott Hughesa2501992011-08-26 19:39:54 -07001012 }
1013 offset++;
1014 }
1015
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001016 // Check top half of guard.
Elliott Hughesba8eee12012-01-24 20:25:24 -08001017 pat = reinterpret_cast<const uint16_t*>(fullBuf + offset);
Elliott Hughesa2501992011-08-26 19:39:54 -07001018 for (size_t i = 0; i < kGuardLen / 4; i++) {
1019 if (pat[i] != kGuardPattern) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001020 JniAbortF(functionName, "guard pattern(2) disturbed at %p +%d", fullBuf, offset + i*2);
Elliott Hughesa2501992011-08-26 19:39:54 -07001021 }
1022 }
1023
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001024 // If modification is not expected, verify checksum. Strictly speaking
1025 // this is wrong: if we told the client that we made a copy, there's no
1026 // reason they can't alter the buffer.
Elliott Hughesa2501992011-08-26 19:39:54 -07001027 if (!modOkay) {
1028 uLong adler = adler32(0L, Z_NULL, 0);
1029 adler = adler32(adler, (const Bytef*)dataBuf, len);
1030 if (pExtra->adler != adler) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001031 JniAbortF(functionName, "buffer modified (0x%08lx vs 0x%08lx) at address %p",
1032 pExtra->adler, adler, dataBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -07001033 }
1034 }
1035 }
1036
1037 private:
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001038 static uint8_t* DebugAlloc(size_t len) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001039 void* result = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
1040 if (result == MAP_FAILED) {
1041 PLOG(FATAL) << "GuardedCopy::create mmap(" << len << ") failed";
1042 }
1043 return reinterpret_cast<uint8_t*>(result);
1044 }
1045
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001046 static void DebugFree(void* dataBuf, size_t len) {
1047 uint8_t* fullBuf = ActualBuffer(dataBuf);
1048 size_t totalByteCount = ActualLength(len);
Elliott Hughesa2501992011-08-26 19:39:54 -07001049 // TODO: we could mprotect instead, and keep the allocation around for a while.
1050 // This would be even more expensive, but it might catch more errors.
1051 // if (mprotect(fullBuf, totalByteCount, PROT_NONE) != 0) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -07001052 // PLOG(WARNING) << "mprotect(PROT_NONE) failed";
Elliott Hughesa2501992011-08-26 19:39:54 -07001053 // }
1054 if (munmap(fullBuf, totalByteCount) != 0) {
Elliott Hughesba8eee12012-01-24 20:25:24 -08001055 PLOG(FATAL) << "munmap(" << reinterpret_cast<void*>(fullBuf) << ", " << totalByteCount << ") failed";
Elliott Hughesa2501992011-08-26 19:39:54 -07001056 }
1057 }
1058
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001059 static const uint8_t* ActualBuffer(const void* dataBuf) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001060 return reinterpret_cast<const uint8_t*>(dataBuf) - kGuardLen / 2;
1061 }
1062
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001063 static uint8_t* ActualBuffer(void* dataBuf) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001064 return reinterpret_cast<uint8_t*>(dataBuf) - kGuardLen / 2;
1065 }
1066
1067 // Underlying length of a user allocation of 'length' bytes.
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001068 static size_t ActualLength(size_t length) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001069 return (length + kGuardLen + 1) & ~0x01;
1070 }
1071};
1072
1073/*
1074 * Create a guarded copy of a primitive array. Modifications to the copied
1075 * data are allowed. Returns a pointer to the copied data.
1076 */
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001077static void* CreateGuardedPACopy(JNIEnv* env, const jarray java_array, jboolean* isCopy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001078 ScopedObjectAccess soa(env);
Elliott Hughesa2501992011-08-26 19:39:54 -07001079
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001080 Array* a = soa.Decode<Array*>(java_array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001081 size_t component_size = a->GetClass()->GetComponentSize();
1082 size_t byte_count = a->GetLength() * component_size;
1083 void* result = GuardedCopy::Create(a->GetRawData(component_size), byte_count, true);
Elliott Hughesa2501992011-08-26 19:39:54 -07001084 if (isCopy != NULL) {
1085 *isCopy = JNI_TRUE;
1086 }
1087 return result;
1088}
1089
1090/*
1091 * Perform the array "release" operation, which may or may not copy data
Elliott Hughes81ff3182012-03-23 20:35:56 -07001092 * back into the managed heap, and may or may not release the underlying storage.
Elliott Hughesa2501992011-08-26 19:39:54 -07001093 */
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001094static void ReleaseGuardedPACopy(JNIEnv* env, jarray java_array, void* dataBuf, int mode) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001095 if (reinterpret_cast<uintptr_t>(dataBuf) == kNoCopyMagic) {
1096 return;
1097 }
1098
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001099 ScopedObjectAccess soa(env);
1100 Array* a = soa.Decode<Array*>(java_array);
Elliott Hughesa2501992011-08-26 19:39:54 -07001101
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001102 GuardedCopy::Check(__FUNCTION__, dataBuf, true);
Elliott Hughesa2501992011-08-26 19:39:54 -07001103
1104 if (mode != JNI_ABORT) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001105 size_t len = GuardedCopy::FromData(dataBuf)->original_length;
Ian Rogersa15e67d2012-02-28 13:51:55 -08001106 memcpy(a->GetRawData(a->GetClass()->GetComponentSize()), dataBuf, len);
Elliott Hughesa2501992011-08-26 19:39:54 -07001107 }
1108 if (mode != JNI_COMMIT) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001109 GuardedCopy::Destroy(dataBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -07001110 }
1111}
1112
1113/*
1114 * ===========================================================================
1115 * JNI functions
1116 * ===========================================================================
1117 */
1118
1119class CheckJNI {
1120 public:
1121 static jint GetVersion(JNIEnv* env) {
1122 CHECK_JNI_ENTRY(kFlag_Default, "E", env);
1123 return CHECK_JNI_EXIT("I", baseEnv(env)->GetVersion(env));
1124 }
1125
1126 static jclass DefineClass(JNIEnv* env, const char* name, jobject loader, const jbyte* buf, jsize bufLen) {
1127 CHECK_JNI_ENTRY(kFlag_Default, "EuLpz", env, name, loader, buf, bufLen);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001128 sc.CheckClassName(name);
Elliott Hughesa2501992011-08-26 19:39:54 -07001129 return CHECK_JNI_EXIT("c", baseEnv(env)->DefineClass(env, name, loader, buf, bufLen));
1130 }
1131
1132 static jclass FindClass(JNIEnv* env, const char* name) {
1133 CHECK_JNI_ENTRY(kFlag_Default, "Eu", env, name);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001134 sc.CheckClassName(name);
Elliott Hughesa2501992011-08-26 19:39:54 -07001135 return CHECK_JNI_EXIT("c", baseEnv(env)->FindClass(env, name));
1136 }
1137
Elliott Hughese84278b2012-03-22 10:06:53 -07001138 static jclass GetSuperclass(JNIEnv* env, jclass c) {
1139 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1140 return CHECK_JNI_EXIT("c", baseEnv(env)->GetSuperclass(env, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001141 }
1142
Elliott Hughese84278b2012-03-22 10:06:53 -07001143 static jboolean IsAssignableFrom(JNIEnv* env, jclass c1, jclass c2) {
1144 CHECK_JNI_ENTRY(kFlag_Default, "Ecc", env, c1, c2);
1145 return CHECK_JNI_EXIT("b", baseEnv(env)->IsAssignableFrom(env, c1, c2));
Elliott Hughesa2501992011-08-26 19:39:54 -07001146 }
1147
1148 static jmethodID FromReflectedMethod(JNIEnv* env, jobject method) {
1149 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, method);
1150 // TODO: check that 'field' is a java.lang.reflect.Method.
1151 return CHECK_JNI_EXIT("m", baseEnv(env)->FromReflectedMethod(env, method));
1152 }
1153
1154 static jfieldID FromReflectedField(JNIEnv* env, jobject field) {
1155 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, field);
1156 // TODO: check that 'field' is a java.lang.reflect.Field.
1157 return CHECK_JNI_EXIT("f", baseEnv(env)->FromReflectedField(env, field));
1158 }
1159
1160 static jobject ToReflectedMethod(JNIEnv* env, jclass cls, jmethodID mid, jboolean isStatic) {
1161 CHECK_JNI_ENTRY(kFlag_Default, "Ecmb", env, cls, mid, isStatic);
1162 return CHECK_JNI_EXIT("L", baseEnv(env)->ToReflectedMethod(env, cls, mid, isStatic));
1163 }
1164
1165 static jobject ToReflectedField(JNIEnv* env, jclass cls, jfieldID fid, jboolean isStatic) {
1166 CHECK_JNI_ENTRY(kFlag_Default, "Ecfb", env, cls, fid, isStatic);
1167 return CHECK_JNI_EXIT("L", baseEnv(env)->ToReflectedField(env, cls, fid, isStatic));
1168 }
1169
1170 static jint Throw(JNIEnv* env, jthrowable obj) {
1171 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1172 // TODO: check that 'obj' is a java.lang.Throwable.
1173 return CHECK_JNI_EXIT("I", baseEnv(env)->Throw(env, obj));
1174 }
1175
Elliott Hughese84278b2012-03-22 10:06:53 -07001176 static jint ThrowNew(JNIEnv* env, jclass c, const char* message) {
1177 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Ecu", env, c, message);
1178 return CHECK_JNI_EXIT("I", baseEnv(env)->ThrowNew(env, c, message));
Elliott Hughesa2501992011-08-26 19:39:54 -07001179 }
1180
1181 static jthrowable ExceptionOccurred(JNIEnv* env) {
1182 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1183 return CHECK_JNI_EXIT("L", baseEnv(env)->ExceptionOccurred(env));
1184 }
1185
1186 static void ExceptionDescribe(JNIEnv* env) {
1187 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1188 baseEnv(env)->ExceptionDescribe(env);
1189 CHECK_JNI_EXIT_VOID();
1190 }
1191
1192 static void ExceptionClear(JNIEnv* env) {
1193 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1194 baseEnv(env)->ExceptionClear(env);
1195 CHECK_JNI_EXIT_VOID();
1196 }
1197
1198 static void FatalError(JNIEnv* env, const char* msg) {
1199 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Eu", env, msg);
1200 baseEnv(env)->FatalError(env, msg);
1201 CHECK_JNI_EXIT_VOID();
1202 }
1203
1204 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
1205 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EI", env, capacity);
1206 return CHECK_JNI_EXIT("I", baseEnv(env)->PushLocalFrame(env, capacity));
1207 }
1208
1209 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
1210 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, res);
1211 return CHECK_JNI_EXIT("L", baseEnv(env)->PopLocalFrame(env, res));
1212 }
1213
1214 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
1215 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1216 return CHECK_JNI_EXIT("L", baseEnv(env)->NewGlobalRef(env, obj));
1217 }
1218
1219 static jobject NewLocalRef(JNIEnv* env, jobject ref) {
1220 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, ref);
1221 return CHECK_JNI_EXIT("L", baseEnv(env)->NewLocalRef(env, ref));
1222 }
1223
1224 static void DeleteGlobalRef(JNIEnv* env, jobject globalRef) {
1225 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, globalRef);
1226 if (globalRef != NULL && GetIndirectRefKind(globalRef) != kGlobal) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001227 JniAbortF(__FUNCTION__, "DeleteGlobalRef on %s: %p",
1228 ToStr<IndirectRefKind>(GetIndirectRefKind(globalRef)).c_str(), globalRef);
Elliott Hughesa2501992011-08-26 19:39:54 -07001229 } else {
1230 baseEnv(env)->DeleteGlobalRef(env, globalRef);
1231 CHECK_JNI_EXIT_VOID();
1232 }
1233 }
1234
1235 static void DeleteWeakGlobalRef(JNIEnv* env, jweak weakGlobalRef) {
1236 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, weakGlobalRef);
1237 if (weakGlobalRef != NULL && GetIndirectRefKind(weakGlobalRef) != kWeakGlobal) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001238 JniAbortF(__FUNCTION__, "DeleteWeakGlobalRef on %s: %p",
1239 ToStr<IndirectRefKind>(GetIndirectRefKind(weakGlobalRef)).c_str(), weakGlobalRef);
Elliott Hughesa2501992011-08-26 19:39:54 -07001240 } else {
1241 baseEnv(env)->DeleteWeakGlobalRef(env, weakGlobalRef);
1242 CHECK_JNI_EXIT_VOID();
1243 }
1244 }
1245
1246 static void DeleteLocalRef(JNIEnv* env, jobject localRef) {
1247 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, localRef);
Ian Rogers959f8ed2012-02-07 16:33:37 -08001248 if (localRef != NULL && GetIndirectRefKind(localRef) != kLocal && !IsSirtLocalRef(env, localRef)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001249 JniAbortF(__FUNCTION__, "DeleteLocalRef on %s: %p",
1250 ToStr<IndirectRefKind>(GetIndirectRefKind(localRef)).c_str(), localRef);
Elliott Hughesa2501992011-08-26 19:39:54 -07001251 } else {
1252 baseEnv(env)->DeleteLocalRef(env, localRef);
1253 CHECK_JNI_EXIT_VOID();
1254 }
1255 }
1256
1257 static jint EnsureLocalCapacity(JNIEnv *env, jint capacity) {
1258 CHECK_JNI_ENTRY(kFlag_Default, "EI", env, capacity);
1259 return CHECK_JNI_EXIT("I", baseEnv(env)->EnsureLocalCapacity(env, capacity));
1260 }
1261
1262 static jboolean IsSameObject(JNIEnv* env, jobject ref1, jobject ref2) {
1263 CHECK_JNI_ENTRY(kFlag_Default, "ELL", env, ref1, ref2);
1264 return CHECK_JNI_EXIT("b", baseEnv(env)->IsSameObject(env, ref1, ref2));
1265 }
1266
Elliott Hughese84278b2012-03-22 10:06:53 -07001267 static jobject AllocObject(JNIEnv* env, jclass c) {
1268 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1269 return CHECK_JNI_EXIT("L", baseEnv(env)->AllocObject(env, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001270 }
1271
Elliott Hughese84278b2012-03-22 10:06:53 -07001272 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
1273 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
Elliott Hughesa2501992011-08-26 19:39:54 -07001274 va_list args;
1275 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -07001276 jobject result = baseEnv(env)->NewObjectV(env, c, mid, args);
Elliott Hughesa2501992011-08-26 19:39:54 -07001277 va_end(args);
1278 return CHECK_JNI_EXIT("L", result);
1279 }
1280
Elliott Hughese84278b2012-03-22 10:06:53 -07001281 static jobject NewObjectV(JNIEnv* env, jclass c, jmethodID mid, va_list args) {
1282 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
1283 return CHECK_JNI_EXIT("L", baseEnv(env)->NewObjectV(env, c, mid, args));
Elliott Hughesa2501992011-08-26 19:39:54 -07001284 }
1285
Elliott Hughese84278b2012-03-22 10:06:53 -07001286 static jobject NewObjectA(JNIEnv* env, jclass c, jmethodID mid, jvalue* args) {
1287 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
1288 return CHECK_JNI_EXIT("L", baseEnv(env)->NewObjectA(env, c, mid, args));
Elliott Hughesa2501992011-08-26 19:39:54 -07001289 }
1290
1291 static jclass GetObjectClass(JNIEnv* env, jobject obj) {
1292 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1293 return CHECK_JNI_EXIT("c", baseEnv(env)->GetObjectClass(env, obj));
1294 }
1295
Elliott Hughese84278b2012-03-22 10:06:53 -07001296 static jboolean IsInstanceOf(JNIEnv* env, jobject obj, jclass c) {
1297 CHECK_JNI_ENTRY(kFlag_Default, "ELc", env, obj, c);
1298 return CHECK_JNI_EXIT("b", baseEnv(env)->IsInstanceOf(env, obj, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001299 }
1300
Elliott Hughese84278b2012-03-22 10:06:53 -07001301 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1302 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1303 return CHECK_JNI_EXIT("m", baseEnv(env)->GetMethodID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001304 }
1305
Elliott Hughese84278b2012-03-22 10:06:53 -07001306 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1307 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1308 return CHECK_JNI_EXIT("f", baseEnv(env)->GetFieldID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001309 }
1310
Elliott Hughese84278b2012-03-22 10:06:53 -07001311 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1312 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1313 return CHECK_JNI_EXIT("m", baseEnv(env)->GetStaticMethodID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001314 }
1315
Elliott Hughese84278b2012-03-22 10:06:53 -07001316 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1317 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1318 return CHECK_JNI_EXIT("f", baseEnv(env)->GetStaticFieldID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001319 }
1320
1321#define FIELD_ACCESSORS(_ctype, _jname, _type) \
Elliott Hughese84278b2012-03-22 10:06:53 -07001322 static _ctype GetStatic##_jname##Field(JNIEnv* env, jclass c, jfieldID fid) { \
1323 CHECK_JNI_ENTRY(kFlag_Default, "Ecf", env, c, fid); \
1324 sc.CheckStaticFieldID(c, fid); \
1325 return CHECK_JNI_EXIT(_type, baseEnv(env)->GetStatic##_jname##Field(env, c, fid)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001326 } \
1327 static _ctype Get##_jname##Field(JNIEnv* env, jobject obj, jfieldID fid) { \
1328 CHECK_JNI_ENTRY(kFlag_Default, "ELf", env, obj, fid); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001329 sc.CheckInstanceFieldID(obj, fid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001330 return CHECK_JNI_EXIT(_type, baseEnv(env)->Get##_jname##Field(env, obj, fid)); \
1331 } \
Elliott Hughese84278b2012-03-22 10:06:53 -07001332 static void SetStatic##_jname##Field(JNIEnv* env, jclass c, jfieldID fid, _ctype value) { \
1333 CHECK_JNI_ENTRY(kFlag_Default, "Ecf" _type, env, c, fid, value); \
1334 sc.CheckStaticFieldID(c, fid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001335 /* "value" arg only used when type == ref */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001336 sc.CheckFieldType((jobject)(uint32_t)value, fid, _type[0], true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001337 baseEnv(env)->SetStatic##_jname##Field(env, c, fid, value); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001338 CHECK_JNI_EXIT_VOID(); \
1339 } \
1340 static void Set##_jname##Field(JNIEnv* env, jobject obj, jfieldID fid, _ctype value) { \
1341 CHECK_JNI_ENTRY(kFlag_Default, "ELf" _type, env, obj, fid, value); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001342 sc.CheckInstanceFieldID(obj, fid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001343 /* "value" arg only used when type == ref */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001344 sc.CheckFieldType((jobject)(uint32_t) value, fid, _type[0], false); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001345 baseEnv(env)->Set##_jname##Field(env, obj, fid, value); \
1346 CHECK_JNI_EXIT_VOID(); \
1347 }
1348
1349FIELD_ACCESSORS(jobject, Object, "L");
1350FIELD_ACCESSORS(jboolean, Boolean, "Z");
1351FIELD_ACCESSORS(jbyte, Byte, "B");
1352FIELD_ACCESSORS(jchar, Char, "C");
1353FIELD_ACCESSORS(jshort, Short, "S");
1354FIELD_ACCESSORS(jint, Int, "I");
1355FIELD_ACCESSORS(jlong, Long, "J");
1356FIELD_ACCESSORS(jfloat, Float, "F");
1357FIELD_ACCESSORS(jdouble, Double, "D");
1358
1359#define CALL(_ctype, _jname, _retdecl, _retasgn, _retok, _retsig) \
1360 /* Virtual... */ \
1361 static _ctype Call##_jname##Method(JNIEnv* env, jobject obj, \
1362 jmethodID mid, ...) \
1363 { \
1364 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001365 sc.CheckSig(mid, _retsig, false); \
1366 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001367 _retdecl; \
1368 va_list args; \
1369 va_start(args, mid); \
Elliott Hughesba8eee12012-01-24 20:25:24 -08001370 _retasgn(baseEnv(env)->Call##_jname##MethodV(env, obj, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001371 va_end(args); \
1372 _retok; \
1373 } \
1374 static _ctype Call##_jname##MethodV(JNIEnv* env, jobject obj, \
1375 jmethodID mid, va_list args) \
1376 { \
1377 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001378 sc.CheckSig(mid, _retsig, false); \
1379 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001380 _retdecl; \
Elliott Hughesba8eee12012-01-24 20:25:24 -08001381 _retasgn(baseEnv(env)->Call##_jname##MethodV(env, obj, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001382 _retok; \
1383 } \
1384 static _ctype Call##_jname##MethodA(JNIEnv* env, jobject obj, \
1385 jmethodID mid, jvalue* args) \
1386 { \
1387 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001388 sc.CheckSig(mid, _retsig, false); \
1389 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001390 _retdecl; \
Elliott Hughesba8eee12012-01-24 20:25:24 -08001391 _retasgn(baseEnv(env)->Call##_jname##MethodA(env, obj, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001392 _retok; \
1393 } \
1394 /* Non-virtual... */ \
1395 static _ctype CallNonvirtual##_jname##Method(JNIEnv* env, \
Elliott Hughese84278b2012-03-22 10:06:53 -07001396 jobject obj, jclass c, jmethodID mid, ...) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001397 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001398 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001399 sc.CheckSig(mid, _retsig, false); \
1400 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001401 _retdecl; \
1402 va_list args; \
1403 va_start(args, mid); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001404 _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodV(env, obj, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001405 va_end(args); \
1406 _retok; \
1407 } \
1408 static _ctype CallNonvirtual##_jname##MethodV(JNIEnv* env, \
Elliott Hughese84278b2012-03-22 10:06:53 -07001409 jobject obj, jclass c, jmethodID mid, va_list args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001410 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001411 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001412 sc.CheckSig(mid, _retsig, false); \
1413 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001414 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001415 _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodV(env, obj, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001416 _retok; \
1417 } \
1418 static _ctype CallNonvirtual##_jname##MethodA(JNIEnv* env, \
Elliott Hughese84278b2012-03-22 10:06:53 -07001419 jobject obj, jclass c, jmethodID mid, jvalue* args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001420 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001421 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001422 sc.CheckSig(mid, _retsig, false); \
1423 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001424 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001425 _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodA(env, obj, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001426 _retok; \
1427 } \
1428 /* Static... */ \
Elliott Hughese84278b2012-03-22 10:06:53 -07001429 static _ctype CallStatic##_jname##Method(JNIEnv* env, jclass c, jmethodID mid, ...) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001430 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001431 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001432 sc.CheckSig(mid, _retsig, true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001433 sc.CheckStaticMethod(c, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001434 _retdecl; \
1435 va_list args; \
1436 va_start(args, mid); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001437 _retasgn(baseEnv(env)->CallStatic##_jname##MethodV(env, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001438 va_end(args); \
1439 _retok; \
1440 } \
Elliott Hughese84278b2012-03-22 10:06:53 -07001441 static _ctype CallStatic##_jname##MethodV(JNIEnv* env, jclass c, jmethodID mid, va_list args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001442 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001443 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001444 sc.CheckSig(mid, _retsig, true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001445 sc.CheckStaticMethod(c, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001446 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001447 _retasgn(baseEnv(env)->CallStatic##_jname##MethodV(env, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001448 _retok; \
1449 } \
Elliott Hughese84278b2012-03-22 10:06:53 -07001450 static _ctype CallStatic##_jname##MethodA(JNIEnv* env, jclass c, jmethodID mid, jvalue* args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001451 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001452 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001453 sc.CheckSig(mid, _retsig, true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001454 sc.CheckStaticMethod(c, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001455 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001456 _retasgn(baseEnv(env)->CallStatic##_jname##MethodA(env, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001457 _retok; \
1458 }
1459
1460#define NON_VOID_RETURN(_retsig, _ctype) return CHECK_JNI_EXIT(_retsig, (_ctype) result)
1461#define VOID_RETURN CHECK_JNI_EXIT_VOID()
1462
Elliott Hughesba8eee12012-01-24 20:25:24 -08001463CALL(jobject, Object, Object* result, result = reinterpret_cast<Object*>, NON_VOID_RETURN("L", jobject), "L");
1464CALL(jboolean, Boolean, jboolean result, result =, NON_VOID_RETURN("Z", jboolean), "Z");
1465CALL(jbyte, Byte, jbyte result, result =, NON_VOID_RETURN("B", jbyte), "B");
1466CALL(jchar, Char, jchar result, result =, NON_VOID_RETURN("C", jchar), "C");
1467CALL(jshort, Short, jshort result, result =, NON_VOID_RETURN("S", jshort), "S");
1468CALL(jint, Int, jint result, result =, NON_VOID_RETURN("I", jint), "I");
1469CALL(jlong, Long, jlong result, result =, NON_VOID_RETURN("J", jlong), "J");
1470CALL(jfloat, Float, jfloat result, result =, NON_VOID_RETURN("F", jfloat), "F");
1471CALL(jdouble, Double, jdouble result, result =, NON_VOID_RETURN("D", jdouble), "D");
Elliott Hughesa2501992011-08-26 19:39:54 -07001472CALL(void, Void, , , VOID_RETURN, "V");
1473
1474 static jstring NewString(JNIEnv* env, const jchar* unicodeChars, jsize len) {
1475 CHECK_JNI_ENTRY(kFlag_Default, "Epz", env, unicodeChars, len);
1476 return CHECK_JNI_EXIT("s", baseEnv(env)->NewString(env, unicodeChars, len));
1477 }
1478
1479 static jsize GetStringLength(JNIEnv* env, jstring string) {
1480 CHECK_JNI_ENTRY(kFlag_CritOkay, "Es", env, string);
1481 return CHECK_JNI_EXIT("I", baseEnv(env)->GetStringLength(env, string));
1482 }
1483
1484 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* isCopy) {
1485 CHECK_JNI_ENTRY(kFlag_CritOkay, "Esp", env, java_string, isCopy);
1486 const jchar* result = baseEnv(env)->GetStringChars(env, java_string, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001487 if (sc.ForceCopy() && result != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001488 String* s = sc.soa().Decode<String*>(java_string);
Elliott Hughesa2501992011-08-26 19:39:54 -07001489 int byteCount = s->GetLength() * 2;
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001490 result = (const jchar*) GuardedCopy::Create(result, byteCount, false);
Elliott Hughesa2501992011-08-26 19:39:54 -07001491 if (isCopy != NULL) {
1492 *isCopy = JNI_TRUE;
1493 }
1494 }
1495 return CHECK_JNI_EXIT("p", result);
1496 }
1497
1498 static void ReleaseStringChars(JNIEnv* env, jstring string, const jchar* chars) {
1499 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "Esp", env, string, chars);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001500 sc.CheckNonNull(chars);
1501 if (sc.ForceCopy()) {
1502 GuardedCopy::Check(__FUNCTION__, chars, false);
Elliott Hughesba8eee12012-01-24 20:25:24 -08001503 chars = reinterpret_cast<const jchar*>(GuardedCopy::Destroy(const_cast<jchar*>(chars)));
Elliott Hughesa2501992011-08-26 19:39:54 -07001504 }
1505 baseEnv(env)->ReleaseStringChars(env, string, chars);
1506 CHECK_JNI_EXIT_VOID();
1507 }
1508
1509 static jstring NewStringUTF(JNIEnv* env, const char* bytes) {
1510 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Eu", env, bytes); // TODO: show pointer and truncate string.
1511 return CHECK_JNI_EXIT("s", baseEnv(env)->NewStringUTF(env, bytes));
1512 }
1513
1514 static jsize GetStringUTFLength(JNIEnv* env, jstring string) {
1515 CHECK_JNI_ENTRY(kFlag_CritOkay, "Es", env, string);
1516 return CHECK_JNI_EXIT("I", baseEnv(env)->GetStringUTFLength(env, string));
1517 }
1518
1519 static const char* GetStringUTFChars(JNIEnv* env, jstring string, jboolean* isCopy) {
1520 CHECK_JNI_ENTRY(kFlag_CritOkay, "Esp", env, string, isCopy);
1521 const char* result = baseEnv(env)->GetStringUTFChars(env, string, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001522 if (sc.ForceCopy() && result != NULL) {
1523 result = (const char*) GuardedCopy::Create(result, strlen(result) + 1, false);
Elliott Hughesa2501992011-08-26 19:39:54 -07001524 if (isCopy != NULL) {
1525 *isCopy = JNI_TRUE;
1526 }
1527 }
1528 return CHECK_JNI_EXIT("u", result); // TODO: show pointer and truncate string.
1529 }
1530
1531 static void ReleaseStringUTFChars(JNIEnv* env, jstring string, const char* utf) {
1532 CHECK_JNI_ENTRY(kFlag_ExcepOkay | kFlag_Release, "Esu", env, string, utf); // TODO: show pointer and truncate string.
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001533 if (sc.ForceCopy()) {
1534 GuardedCopy::Check(__FUNCTION__, utf, false);
Elliott Hughesba8eee12012-01-24 20:25:24 -08001535 utf = reinterpret_cast<const char*>(GuardedCopy::Destroy(const_cast<char*>(utf)));
Elliott Hughesa2501992011-08-26 19:39:54 -07001536 }
1537 baseEnv(env)->ReleaseStringUTFChars(env, string, utf);
1538 CHECK_JNI_EXIT_VOID();
1539 }
1540
1541 static jsize GetArrayLength(JNIEnv* env, jarray array) {
1542 CHECK_JNI_ENTRY(kFlag_CritOkay, "Ea", env, array);
1543 return CHECK_JNI_EXIT("I", baseEnv(env)->GetArrayLength(env, array));
1544 }
1545
1546 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass elementClass, jobject initialElement) {
1547 CHECK_JNI_ENTRY(kFlag_Default, "EzcL", env, length, elementClass, initialElement);
1548 return CHECK_JNI_EXIT("a", baseEnv(env)->NewObjectArray(env, length, elementClass, initialElement));
1549 }
1550
1551 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index) {
1552 CHECK_JNI_ENTRY(kFlag_Default, "EaI", env, array, index);
1553 return CHECK_JNI_EXIT("L", baseEnv(env)->GetObjectArrayElement(env, array, index));
1554 }
1555
1556 static void SetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index, jobject value) {
1557 CHECK_JNI_ENTRY(kFlag_Default, "EaIL", env, array, index, value);
1558 baseEnv(env)->SetObjectArrayElement(env, array, index, value);
1559 CHECK_JNI_EXIT_VOID();
1560 }
1561
1562#define NEW_PRIMITIVE_ARRAY(_artype, _jname) \
1563 static _artype New##_jname##Array(JNIEnv* env, jsize length) { \
1564 CHECK_JNI_ENTRY(kFlag_Default, "Ez", env, length); \
1565 return CHECK_JNI_EXIT("a", baseEnv(env)->New##_jname##Array(env, length)); \
1566 }
1567NEW_PRIMITIVE_ARRAY(jbooleanArray, Boolean);
1568NEW_PRIMITIVE_ARRAY(jbyteArray, Byte);
1569NEW_PRIMITIVE_ARRAY(jcharArray, Char);
1570NEW_PRIMITIVE_ARRAY(jshortArray, Short);
1571NEW_PRIMITIVE_ARRAY(jintArray, Int);
1572NEW_PRIMITIVE_ARRAY(jlongArray, Long);
1573NEW_PRIMITIVE_ARRAY(jfloatArray, Float);
1574NEW_PRIMITIVE_ARRAY(jdoubleArray, Double);
1575
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001576struct ForceCopyGetChecker {
Elliott Hughesba8eee12012-01-24 20:25:24 -08001577 public:
Elliott Hughesa2501992011-08-26 19:39:54 -07001578 ForceCopyGetChecker(ScopedCheck& sc, jboolean* isCopy) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001579 force_copy = sc.ForceCopy();
1580 no_copy = 0;
1581 if (force_copy && isCopy != NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001582 // Capture this before the base call tramples on it.
Elliott Hughesba8eee12012-01-24 20:25:24 -08001583 no_copy = *reinterpret_cast<uint32_t*>(isCopy);
Elliott Hughesa2501992011-08-26 19:39:54 -07001584 }
1585 }
1586
1587 template<typename ResultT>
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001588 ResultT Check(JNIEnv* env, jarray array, jboolean* isCopy, ResultT result) {
1589 if (force_copy && result != NULL) {
1590 if (no_copy != kNoCopyMagic) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001591 result = reinterpret_cast<ResultT>(CreateGuardedPACopy(env, array, isCopy));
1592 }
1593 }
1594 return result;
1595 }
1596
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001597 uint32_t no_copy;
1598 bool force_copy;
Elliott Hughesa2501992011-08-26 19:39:54 -07001599};
1600
1601#define GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \
1602 static _ctype* Get##_jname##ArrayElements(JNIEnv* env, _ctype##Array array, jboolean* isCopy) { \
1603 CHECK_JNI_ENTRY(kFlag_Default, "Eap", env, array, isCopy); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001604 _ctype* result = ForceCopyGetChecker(sc, isCopy).Check(env, array, isCopy, baseEnv(env)->Get##_jname##ArrayElements(env, array, isCopy)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001605 return CHECK_JNI_EXIT("p", result); \
1606 }
1607
1608#define RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \
1609 static void Release##_jname##ArrayElements(JNIEnv* env, _ctype##Array array, _ctype* elems, jint mode) { \
1610 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "Eapr", env, array, elems, mode); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001611 sc.CheckNonNull(elems); \
1612 if (sc.ForceCopy()) { \
Elliott Hughesa2501992011-08-26 19:39:54 -07001613 ReleaseGuardedPACopy(env, array, elems, mode); \
1614 } \
1615 baseEnv(env)->Release##_jname##ArrayElements(env, array, elems, mode); \
1616 CHECK_JNI_EXIT_VOID(); \
1617 }
1618
1619#define GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \
1620 static void Get##_jname##ArrayRegion(JNIEnv* env, _ctype##Array array, jsize start, jsize len, _ctype* buf) { \
1621 CHECK_JNI_ENTRY(kFlag_Default, "EaIIp", env, array, start, len, buf); \
1622 baseEnv(env)->Get##_jname##ArrayRegion(env, array, start, len, buf); \
1623 CHECK_JNI_EXIT_VOID(); \
1624 }
1625
1626#define SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \
1627 static void Set##_jname##ArrayRegion(JNIEnv* env, _ctype##Array array, jsize start, jsize len, const _ctype* buf) { \
1628 CHECK_JNI_ENTRY(kFlag_Default, "EaIIp", env, array, start, len, buf); \
1629 baseEnv(env)->Set##_jname##ArrayRegion(env, array, start, len, buf); \
1630 CHECK_JNI_EXIT_VOID(); \
1631 }
1632
1633#define PRIMITIVE_ARRAY_FUNCTIONS(_ctype, _jname, _typechar) \
1634 GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \
1635 RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \
1636 GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname); \
1637 SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname);
1638
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001639// TODO: verify primitive array type matches call type.
Elliott Hughesa2501992011-08-26 19:39:54 -07001640PRIMITIVE_ARRAY_FUNCTIONS(jboolean, Boolean, 'Z');
1641PRIMITIVE_ARRAY_FUNCTIONS(jbyte, Byte, 'B');
1642PRIMITIVE_ARRAY_FUNCTIONS(jchar, Char, 'C');
1643PRIMITIVE_ARRAY_FUNCTIONS(jshort, Short, 'S');
1644PRIMITIVE_ARRAY_FUNCTIONS(jint, Int, 'I');
1645PRIMITIVE_ARRAY_FUNCTIONS(jlong, Long, 'J');
1646PRIMITIVE_ARRAY_FUNCTIONS(jfloat, Float, 'F');
1647PRIMITIVE_ARRAY_FUNCTIONS(jdouble, Double, 'D');
1648
Elliott Hughese84278b2012-03-22 10:06:53 -07001649 static jint RegisterNatives(JNIEnv* env, jclass c, const JNINativeMethod* methods, jint nMethods) {
1650 CHECK_JNI_ENTRY(kFlag_Default, "EcpI", env, c, methods, nMethods);
1651 return CHECK_JNI_EXIT("I", baseEnv(env)->RegisterNatives(env, c, methods, nMethods));
Elliott Hughesa2501992011-08-26 19:39:54 -07001652 }
1653
Elliott Hughese84278b2012-03-22 10:06:53 -07001654 static jint UnregisterNatives(JNIEnv* env, jclass c) {
1655 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1656 return CHECK_JNI_EXIT("I", baseEnv(env)->UnregisterNatives(env, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001657 }
1658
1659 static jint MonitorEnter(JNIEnv* env, jobject obj) {
1660 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
Elliott Hughesa92853e2012-02-07 16:09:27 -08001661 if (!sc.CheckInstance(ScopedCheck::kObject, obj)) {
1662 return JNI_ERR; // Only for jni_internal_test. Real code will have aborted already.
1663 }
Elliott Hughesa2501992011-08-26 19:39:54 -07001664 return CHECK_JNI_EXIT("I", baseEnv(env)->MonitorEnter(env, obj));
1665 }
1666
1667 static jint MonitorExit(JNIEnv* env, jobject obj) {
1668 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, obj);
Elliott Hughesa92853e2012-02-07 16:09:27 -08001669 if (!sc.CheckInstance(ScopedCheck::kObject, obj)) {
1670 return JNI_ERR; // Only for jni_internal_test. Real code will have aborted already.
1671 }
Elliott Hughesa2501992011-08-26 19:39:54 -07001672 return CHECK_JNI_EXIT("I", baseEnv(env)->MonitorExit(env, obj));
1673 }
1674
1675 static jint GetJavaVM(JNIEnv *env, JavaVM **vm) {
1676 CHECK_JNI_ENTRY(kFlag_Default, "Ep", env, vm);
1677 return CHECK_JNI_EXIT("I", baseEnv(env)->GetJavaVM(env, vm));
1678 }
1679
1680 static void GetStringRegion(JNIEnv* env, jstring str, jsize start, jsize len, jchar* buf) {
1681 CHECK_JNI_ENTRY(kFlag_CritOkay, "EsIIp", env, str, start, len, buf);
1682 baseEnv(env)->GetStringRegion(env, str, start, len, buf);
1683 CHECK_JNI_EXIT_VOID();
1684 }
1685
1686 static void GetStringUTFRegion(JNIEnv* env, jstring str, jsize start, jsize len, char* buf) {
1687 CHECK_JNI_ENTRY(kFlag_CritOkay, "EsIIp", env, str, start, len, buf);
1688 baseEnv(env)->GetStringUTFRegion(env, str, start, len, buf);
1689 CHECK_JNI_EXIT_VOID();
1690 }
1691
1692 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* isCopy) {
1693 CHECK_JNI_ENTRY(kFlag_CritGet, "Eap", env, array, isCopy);
1694 void* result = baseEnv(env)->GetPrimitiveArrayCritical(env, array, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001695 if (sc.ForceCopy() && result != NULL) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001696 result = CreateGuardedPACopy(env, array, isCopy);
1697 }
1698 return CHECK_JNI_EXIT("p", result);
1699 }
1700
1701 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* carray, jint mode) {
1702 CHECK_JNI_ENTRY(kFlag_CritRelease | kFlag_ExcepOkay, "Eapr", env, array, carray, mode);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001703 sc.CheckNonNull(carray);
1704 if (sc.ForceCopy()) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001705 ReleaseGuardedPACopy(env, array, carray, mode);
1706 }
1707 baseEnv(env)->ReleasePrimitiveArrayCritical(env, array, carray, mode);
1708 CHECK_JNI_EXIT_VOID();
1709 }
1710
1711 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* isCopy) {
1712 CHECK_JNI_ENTRY(kFlag_CritGet, "Esp", env, java_string, isCopy);
1713 const jchar* result = baseEnv(env)->GetStringCritical(env, java_string, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001714 if (sc.ForceCopy() && result != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001715 String* s = sc.soa().Decode<String*>(java_string);
Elliott Hughesa2501992011-08-26 19:39:54 -07001716 int byteCount = s->GetLength() * 2;
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001717 result = (const jchar*) GuardedCopy::Create(result, byteCount, false);
Elliott Hughesa2501992011-08-26 19:39:54 -07001718 if (isCopy != NULL) {
1719 *isCopy = JNI_TRUE;
1720 }
1721 }
1722 return CHECK_JNI_EXIT("p", result);
1723 }
1724
1725 static void ReleaseStringCritical(JNIEnv* env, jstring string, const jchar* carray) {
1726 CHECK_JNI_ENTRY(kFlag_CritRelease | kFlag_ExcepOkay, "Esp", env, string, carray);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001727 sc.CheckNonNull(carray);
1728 if (sc.ForceCopy()) {
1729 GuardedCopy::Check(__FUNCTION__, carray, false);
Elliott Hughesba8eee12012-01-24 20:25:24 -08001730 carray = reinterpret_cast<const jchar*>(GuardedCopy::Destroy(const_cast<jchar*>(carray)));
Elliott Hughesa2501992011-08-26 19:39:54 -07001731 }
1732 baseEnv(env)->ReleaseStringCritical(env, string, carray);
1733 CHECK_JNI_EXIT_VOID();
1734 }
1735
1736 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
1737 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1738 return CHECK_JNI_EXIT("L", baseEnv(env)->NewWeakGlobalRef(env, obj));
1739 }
1740
1741 static jboolean ExceptionCheck(JNIEnv* env) {
1742 CHECK_JNI_ENTRY(kFlag_CritOkay | kFlag_ExcepOkay, "E", env);
1743 return CHECK_JNI_EXIT("b", baseEnv(env)->ExceptionCheck(env));
1744 }
1745
1746 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject obj) {
1747 // Note: we use "Ep" rather than "EL" because this is the one JNI function
1748 // that it's okay to pass an invalid reference to.
1749 CHECK_JNI_ENTRY(kFlag_Default, "Ep", env, obj);
1750 // TODO: proper decoding of jobjectRefType!
1751 return CHECK_JNI_EXIT("I", baseEnv(env)->GetObjectRefType(env, obj));
1752 }
1753
1754 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
1755 CHECK_JNI_ENTRY(kFlag_Default, "EpJ", env, address, capacity);
1756 if (address == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001757 JniAbortF(__FUNCTION__, "non-nullable address is NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -07001758 }
1759 if (capacity <= 0) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001760 JniAbortF(__FUNCTION__, "capacity must be greater than 0: %d", capacity);
Elliott Hughesa2501992011-08-26 19:39:54 -07001761 }
1762 return CHECK_JNI_EXIT("L", baseEnv(env)->NewDirectByteBuffer(env, address, capacity));
1763 }
1764
1765 static void* GetDirectBufferAddress(JNIEnv* env, jobject buf) {
1766 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, buf);
1767 // TODO: check that 'buf' is a java.nio.Buffer.
1768 return CHECK_JNI_EXIT("p", baseEnv(env)->GetDirectBufferAddress(env, buf));
1769 }
1770
1771 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject buf) {
1772 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, buf);
1773 // TODO: check that 'buf' is a java.nio.Buffer.
1774 return CHECK_JNI_EXIT("J", baseEnv(env)->GetDirectBufferCapacity(env, buf));
1775 }
1776
1777 private:
1778 static inline const JNINativeInterface* baseEnv(JNIEnv* env) {
1779 return reinterpret_cast<JNIEnvExt*>(env)->unchecked_functions;
1780 }
1781};
1782
1783const JNINativeInterface gCheckNativeInterface = {
1784 NULL, // reserved0.
1785 NULL, // reserved1.
1786 NULL, // reserved2.
1787 NULL, // reserved3.
1788 CheckJNI::GetVersion,
1789 CheckJNI::DefineClass,
1790 CheckJNI::FindClass,
1791 CheckJNI::FromReflectedMethod,
1792 CheckJNI::FromReflectedField,
1793 CheckJNI::ToReflectedMethod,
1794 CheckJNI::GetSuperclass,
1795 CheckJNI::IsAssignableFrom,
1796 CheckJNI::ToReflectedField,
1797 CheckJNI::Throw,
1798 CheckJNI::ThrowNew,
1799 CheckJNI::ExceptionOccurred,
1800 CheckJNI::ExceptionDescribe,
1801 CheckJNI::ExceptionClear,
1802 CheckJNI::FatalError,
1803 CheckJNI::PushLocalFrame,
1804 CheckJNI::PopLocalFrame,
1805 CheckJNI::NewGlobalRef,
1806 CheckJNI::DeleteGlobalRef,
1807 CheckJNI::DeleteLocalRef,
1808 CheckJNI::IsSameObject,
1809 CheckJNI::NewLocalRef,
1810 CheckJNI::EnsureLocalCapacity,
1811 CheckJNI::AllocObject,
1812 CheckJNI::NewObject,
1813 CheckJNI::NewObjectV,
1814 CheckJNI::NewObjectA,
1815 CheckJNI::GetObjectClass,
1816 CheckJNI::IsInstanceOf,
1817 CheckJNI::GetMethodID,
1818 CheckJNI::CallObjectMethod,
1819 CheckJNI::CallObjectMethodV,
1820 CheckJNI::CallObjectMethodA,
1821 CheckJNI::CallBooleanMethod,
1822 CheckJNI::CallBooleanMethodV,
1823 CheckJNI::CallBooleanMethodA,
1824 CheckJNI::CallByteMethod,
1825 CheckJNI::CallByteMethodV,
1826 CheckJNI::CallByteMethodA,
1827 CheckJNI::CallCharMethod,
1828 CheckJNI::CallCharMethodV,
1829 CheckJNI::CallCharMethodA,
1830 CheckJNI::CallShortMethod,
1831 CheckJNI::CallShortMethodV,
1832 CheckJNI::CallShortMethodA,
1833 CheckJNI::CallIntMethod,
1834 CheckJNI::CallIntMethodV,
1835 CheckJNI::CallIntMethodA,
1836 CheckJNI::CallLongMethod,
1837 CheckJNI::CallLongMethodV,
1838 CheckJNI::CallLongMethodA,
1839 CheckJNI::CallFloatMethod,
1840 CheckJNI::CallFloatMethodV,
1841 CheckJNI::CallFloatMethodA,
1842 CheckJNI::CallDoubleMethod,
1843 CheckJNI::CallDoubleMethodV,
1844 CheckJNI::CallDoubleMethodA,
1845 CheckJNI::CallVoidMethod,
1846 CheckJNI::CallVoidMethodV,
1847 CheckJNI::CallVoidMethodA,
1848 CheckJNI::CallNonvirtualObjectMethod,
1849 CheckJNI::CallNonvirtualObjectMethodV,
1850 CheckJNI::CallNonvirtualObjectMethodA,
1851 CheckJNI::CallNonvirtualBooleanMethod,
1852 CheckJNI::CallNonvirtualBooleanMethodV,
1853 CheckJNI::CallNonvirtualBooleanMethodA,
1854 CheckJNI::CallNonvirtualByteMethod,
1855 CheckJNI::CallNonvirtualByteMethodV,
1856 CheckJNI::CallNonvirtualByteMethodA,
1857 CheckJNI::CallNonvirtualCharMethod,
1858 CheckJNI::CallNonvirtualCharMethodV,
1859 CheckJNI::CallNonvirtualCharMethodA,
1860 CheckJNI::CallNonvirtualShortMethod,
1861 CheckJNI::CallNonvirtualShortMethodV,
1862 CheckJNI::CallNonvirtualShortMethodA,
1863 CheckJNI::CallNonvirtualIntMethod,
1864 CheckJNI::CallNonvirtualIntMethodV,
1865 CheckJNI::CallNonvirtualIntMethodA,
1866 CheckJNI::CallNonvirtualLongMethod,
1867 CheckJNI::CallNonvirtualLongMethodV,
1868 CheckJNI::CallNonvirtualLongMethodA,
1869 CheckJNI::CallNonvirtualFloatMethod,
1870 CheckJNI::CallNonvirtualFloatMethodV,
1871 CheckJNI::CallNonvirtualFloatMethodA,
1872 CheckJNI::CallNonvirtualDoubleMethod,
1873 CheckJNI::CallNonvirtualDoubleMethodV,
1874 CheckJNI::CallNonvirtualDoubleMethodA,
1875 CheckJNI::CallNonvirtualVoidMethod,
1876 CheckJNI::CallNonvirtualVoidMethodV,
1877 CheckJNI::CallNonvirtualVoidMethodA,
1878 CheckJNI::GetFieldID,
1879 CheckJNI::GetObjectField,
1880 CheckJNI::GetBooleanField,
1881 CheckJNI::GetByteField,
1882 CheckJNI::GetCharField,
1883 CheckJNI::GetShortField,
1884 CheckJNI::GetIntField,
1885 CheckJNI::GetLongField,
1886 CheckJNI::GetFloatField,
1887 CheckJNI::GetDoubleField,
1888 CheckJNI::SetObjectField,
1889 CheckJNI::SetBooleanField,
1890 CheckJNI::SetByteField,
1891 CheckJNI::SetCharField,
1892 CheckJNI::SetShortField,
1893 CheckJNI::SetIntField,
1894 CheckJNI::SetLongField,
1895 CheckJNI::SetFloatField,
1896 CheckJNI::SetDoubleField,
1897 CheckJNI::GetStaticMethodID,
1898 CheckJNI::CallStaticObjectMethod,
1899 CheckJNI::CallStaticObjectMethodV,
1900 CheckJNI::CallStaticObjectMethodA,
1901 CheckJNI::CallStaticBooleanMethod,
1902 CheckJNI::CallStaticBooleanMethodV,
1903 CheckJNI::CallStaticBooleanMethodA,
1904 CheckJNI::CallStaticByteMethod,
1905 CheckJNI::CallStaticByteMethodV,
1906 CheckJNI::CallStaticByteMethodA,
1907 CheckJNI::CallStaticCharMethod,
1908 CheckJNI::CallStaticCharMethodV,
1909 CheckJNI::CallStaticCharMethodA,
1910 CheckJNI::CallStaticShortMethod,
1911 CheckJNI::CallStaticShortMethodV,
1912 CheckJNI::CallStaticShortMethodA,
1913 CheckJNI::CallStaticIntMethod,
1914 CheckJNI::CallStaticIntMethodV,
1915 CheckJNI::CallStaticIntMethodA,
1916 CheckJNI::CallStaticLongMethod,
1917 CheckJNI::CallStaticLongMethodV,
1918 CheckJNI::CallStaticLongMethodA,
1919 CheckJNI::CallStaticFloatMethod,
1920 CheckJNI::CallStaticFloatMethodV,
1921 CheckJNI::CallStaticFloatMethodA,
1922 CheckJNI::CallStaticDoubleMethod,
1923 CheckJNI::CallStaticDoubleMethodV,
1924 CheckJNI::CallStaticDoubleMethodA,
1925 CheckJNI::CallStaticVoidMethod,
1926 CheckJNI::CallStaticVoidMethodV,
1927 CheckJNI::CallStaticVoidMethodA,
1928 CheckJNI::GetStaticFieldID,
1929 CheckJNI::GetStaticObjectField,
1930 CheckJNI::GetStaticBooleanField,
1931 CheckJNI::GetStaticByteField,
1932 CheckJNI::GetStaticCharField,
1933 CheckJNI::GetStaticShortField,
1934 CheckJNI::GetStaticIntField,
1935 CheckJNI::GetStaticLongField,
1936 CheckJNI::GetStaticFloatField,
1937 CheckJNI::GetStaticDoubleField,
1938 CheckJNI::SetStaticObjectField,
1939 CheckJNI::SetStaticBooleanField,
1940 CheckJNI::SetStaticByteField,
1941 CheckJNI::SetStaticCharField,
1942 CheckJNI::SetStaticShortField,
1943 CheckJNI::SetStaticIntField,
1944 CheckJNI::SetStaticLongField,
1945 CheckJNI::SetStaticFloatField,
1946 CheckJNI::SetStaticDoubleField,
1947 CheckJNI::NewString,
1948 CheckJNI::GetStringLength,
1949 CheckJNI::GetStringChars,
1950 CheckJNI::ReleaseStringChars,
1951 CheckJNI::NewStringUTF,
1952 CheckJNI::GetStringUTFLength,
1953 CheckJNI::GetStringUTFChars,
1954 CheckJNI::ReleaseStringUTFChars,
1955 CheckJNI::GetArrayLength,
1956 CheckJNI::NewObjectArray,
1957 CheckJNI::GetObjectArrayElement,
1958 CheckJNI::SetObjectArrayElement,
1959 CheckJNI::NewBooleanArray,
1960 CheckJNI::NewByteArray,
1961 CheckJNI::NewCharArray,
1962 CheckJNI::NewShortArray,
1963 CheckJNI::NewIntArray,
1964 CheckJNI::NewLongArray,
1965 CheckJNI::NewFloatArray,
1966 CheckJNI::NewDoubleArray,
1967 CheckJNI::GetBooleanArrayElements,
1968 CheckJNI::GetByteArrayElements,
1969 CheckJNI::GetCharArrayElements,
1970 CheckJNI::GetShortArrayElements,
1971 CheckJNI::GetIntArrayElements,
1972 CheckJNI::GetLongArrayElements,
1973 CheckJNI::GetFloatArrayElements,
1974 CheckJNI::GetDoubleArrayElements,
1975 CheckJNI::ReleaseBooleanArrayElements,
1976 CheckJNI::ReleaseByteArrayElements,
1977 CheckJNI::ReleaseCharArrayElements,
1978 CheckJNI::ReleaseShortArrayElements,
1979 CheckJNI::ReleaseIntArrayElements,
1980 CheckJNI::ReleaseLongArrayElements,
1981 CheckJNI::ReleaseFloatArrayElements,
1982 CheckJNI::ReleaseDoubleArrayElements,
1983 CheckJNI::GetBooleanArrayRegion,
1984 CheckJNI::GetByteArrayRegion,
1985 CheckJNI::GetCharArrayRegion,
1986 CheckJNI::GetShortArrayRegion,
1987 CheckJNI::GetIntArrayRegion,
1988 CheckJNI::GetLongArrayRegion,
1989 CheckJNI::GetFloatArrayRegion,
1990 CheckJNI::GetDoubleArrayRegion,
1991 CheckJNI::SetBooleanArrayRegion,
1992 CheckJNI::SetByteArrayRegion,
1993 CheckJNI::SetCharArrayRegion,
1994 CheckJNI::SetShortArrayRegion,
1995 CheckJNI::SetIntArrayRegion,
1996 CheckJNI::SetLongArrayRegion,
1997 CheckJNI::SetFloatArrayRegion,
1998 CheckJNI::SetDoubleArrayRegion,
1999 CheckJNI::RegisterNatives,
2000 CheckJNI::UnregisterNatives,
2001 CheckJNI::MonitorEnter,
2002 CheckJNI::MonitorExit,
2003 CheckJNI::GetJavaVM,
2004 CheckJNI::GetStringRegion,
2005 CheckJNI::GetStringUTFRegion,
2006 CheckJNI::GetPrimitiveArrayCritical,
2007 CheckJNI::ReleasePrimitiveArrayCritical,
2008 CheckJNI::GetStringCritical,
2009 CheckJNI::ReleaseStringCritical,
2010 CheckJNI::NewWeakGlobalRef,
2011 CheckJNI::DeleteWeakGlobalRef,
2012 CheckJNI::ExceptionCheck,
2013 CheckJNI::NewDirectByteBuffer,
2014 CheckJNI::GetDirectBufferAddress,
2015 CheckJNI::GetDirectBufferCapacity,
2016 CheckJNI::GetObjectRefType,
2017};
2018
2019const JNINativeInterface* GetCheckJniNativeInterface() {
2020 return &gCheckNativeInterface;
2021}
2022
2023class CheckJII {
Elliott Hughesba8eee12012-01-24 20:25:24 -08002024 public:
Elliott Hughesa2501992011-08-26 19:39:54 -07002025 static jint DestroyJavaVM(JavaVM* vm) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002026 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002027 sc.Check(true, "v", vm);
2028 return CHECK_JNI_EXIT("I", BaseVm(vm)->DestroyJavaVM(vm));
Elliott Hughesa2501992011-08-26 19:39:54 -07002029 }
2030
2031 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002032 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002033 sc.Check(true, "vpp", vm, p_env, thr_args);
2034 return CHECK_JNI_EXIT("I", BaseVm(vm)->AttachCurrentThread(vm, p_env, thr_args));
Elliott Hughesa2501992011-08-26 19:39:54 -07002035 }
2036
2037 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002038 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002039 sc.Check(true, "vpp", vm, p_env, thr_args);
2040 return CHECK_JNI_EXIT("I", BaseVm(vm)->AttachCurrentThreadAsDaemon(vm, p_env, thr_args));
Elliott Hughesa2501992011-08-26 19:39:54 -07002041 }
2042
2043 static jint DetachCurrentThread(JavaVM* vm) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002044 ScopedCheck sc(vm, true, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002045 sc.Check(true, "v", vm);
2046 return CHECK_JNI_EXIT("I", BaseVm(vm)->DetachCurrentThread(vm));
Elliott Hughesa2501992011-08-26 19:39:54 -07002047 }
2048
2049 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002050 ScopedCheck sc(vm, true, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002051 sc.Check(true, "v", vm);
2052 return CHECK_JNI_EXIT("I", BaseVm(vm)->GetEnv(vm, env, version));
Elliott Hughesa2501992011-08-26 19:39:54 -07002053 }
2054
2055 private:
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002056 static inline const JNIInvokeInterface* BaseVm(JavaVM* vm) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002057 return reinterpret_cast<JavaVMExt*>(vm)->unchecked_functions;
2058 }
2059};
2060
2061const JNIInvokeInterface gCheckInvokeInterface = {
2062 NULL, // reserved0
2063 NULL, // reserved1
2064 NULL, // reserved2
2065 CheckJII::DestroyJavaVM,
2066 CheckJII::AttachCurrentThread,
2067 CheckJII::DetachCurrentThread,
2068 CheckJII::GetEnv,
2069 CheckJII::AttachCurrentThreadAsDaemon
2070};
2071
2072const JNIInvokeInterface* GetCheckJniInvokeInterface() {
2073 return &gCheckInvokeInterface;
2074}
2075
2076} // namespace art