blob: b387f5f610d3ec18c5debfcba4fb22290cc76757 [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 Rogers00f7d0e2012-07-19 15:28:27 -070058 {
59 MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_);
60 CHECK_NE(self->GetState(), kRunnable);
61 self->SetState(kNative); // Ensure that we get a native stack trace for this thread.
62 }
Elliott Hughesa2501992011-08-26 19:39:54 -070063 LOG(FATAL) << os.str();
64 }
65}
66
Elliott Hughes3f6635a2012-06-19 13:37:49 -070067static void JniAbortV(const char* jni_function_name, const char* fmt, va_list ap) {
68 std::string msg;
69 StringAppendV(&msg, fmt, ap);
70 JniAbort(jni_function_name, msg.c_str());
71}
72
73void JniAbortF(const char* jni_function_name, const char* fmt, ...) {
74 va_list args;
75 va_start(args, fmt);
76 JniAbortV(jni_function_name, fmt, args);
77 va_end(args);
78}
79
Elliott Hughesa2501992011-08-26 19:39:54 -070080/*
81 * ===========================================================================
82 * JNI function helpers
83 * ===========================================================================
84 */
85
Ian Rogers959f8ed2012-02-07 16:33:37 -080086static bool IsSirtLocalRef(JNIEnv* env, jobject localRef) {
87 return GetIndirectRefKind(localRef) == kSirtOrInvalid &&
Ian Rogers0399dde2012-06-06 17:09:28 -070088 reinterpret_cast<JNIEnvExt*>(env)->self->SirtContains(localRef);
Ian Rogers959f8ed2012-02-07 16:33:37 -080089}
90
Elliott Hughes3f6635a2012-06-19 13:37:49 -070091// Hack to allow forcecopy to work with jniGetNonMovableArrayElements.
92// The code deliberately uses an invalid sequence of operations, so we
93// need to pass it through unmodified. Review that code before making
94// any changes here.
Elliott Hughesa2501992011-08-26 19:39:54 -070095#define kNoCopyMagic 0xd5aab57f
96
Elliott Hughes3f6635a2012-06-19 13:37:49 -070097// Flags passed into ScopedCheck.
Elliott Hughesa2501992011-08-26 19:39:54 -070098#define kFlag_Default 0x0000
99
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700100#define kFlag_CritBad 0x0000 // Calling while in critical is not allowed.
101#define kFlag_CritOkay 0x0001 // Calling while in critical is allowed.
102#define kFlag_CritGet 0x0002 // This is a critical "get".
103#define kFlag_CritRelease 0x0003 // This is a critical "release".
104#define kFlag_CritMask 0x0003 // Bit mask to get "crit" value.
Elliott Hughesa2501992011-08-26 19:39:54 -0700105
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700106#define kFlag_ExcepBad 0x0000 // Raised exceptions are not allowed.
107#define kFlag_ExcepOkay 0x0004 // Raised exceptions are allowed.
Elliott Hughesa2501992011-08-26 19:39:54 -0700108
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700109#define kFlag_Release 0x0010 // Are we in a non-critical release function?
110#define kFlag_NullableUtf 0x0020 // Are our UTF parameters nullable?
Elliott Hughesa2501992011-08-26 19:39:54 -0700111
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700112#define kFlag_Invocation 0x8000 // Part of the invocation interface (JavaVM*).
Elliott Hughesa2501992011-08-26 19:39:54 -0700113
Elliott Hughes485cac42011-12-09 17:49:35 -0800114#define kFlag_ForceTrace 0x80000000 // Add this to a JNI function's flags if you want to trace every call.
115
Elliott Hughesa0957642011-09-02 14:27:33 -0700116static const char* gBuiltInPrefixes[] = {
117 "Landroid/",
118 "Lcom/android/",
119 "Lcom/google/android/",
120 "Ldalvik/",
121 "Ljava/",
122 "Ljavax/",
123 "Llibcore/",
124 "Lorg/apache/harmony/",
125 NULL
126};
127
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700128static bool ShouldTrace(JavaVMExt* vm, const Method* method)
129 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700130 // If both "-Xcheck:jni" and "-Xjnitrace:" are enabled, we print trace messages
131 // when a native method that matches the -Xjnitrace argument calls a JNI function
132 // such as NewByteArray.
133 // If -verbose:third-party-jni is on, we want to log any JNI function calls
134 // made by a third-party native method.
Elliott Hughes81ff3182012-03-23 20:35:56 -0700135 std::string class_name(MethodHelper(method).GetDeclaringClassDescriptor());
136 if (!vm->trace.empty() && class_name.find(vm->trace) != std::string::npos) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700137 return true;
138 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800139 if (VLOG_IS_ON(third_party_jni)) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700140 // Return true if we're trying to log all third-party JNI activity and 'method' doesn't look
141 // like part of Android.
Elliott Hughesa0957642011-09-02 14:27:33 -0700142 for (size_t i = 0; gBuiltInPrefixes[i] != NULL; ++i) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700143 if (StartsWith(class_name, gBuiltInPrefixes[i])) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700144 return false;
145 }
146 }
147 return true;
148 }
149 return false;
150}
151
Elliott Hughesa2501992011-08-26 19:39:54 -0700152class ScopedCheck {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800153 public:
Elliott Hughesa2501992011-08-26 19:39:54 -0700154 // For JNIEnv* functions.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700155 explicit ScopedCheck(JNIEnv* env, int flags, const char* functionName)
156 SHARED_LOCK_FUNCTION(GlobalSynchronization::mutator_lock_)
157 : soa_(env) {
Ian Rogers365c1022012-06-22 15:05:28 -0700158 Init(flags, functionName, true);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700159 CheckThread(flags);
Elliott Hughesa2501992011-08-26 19:39:54 -0700160 }
161
162 // For JavaVM* functions.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700163 // TODO: it's not correct that this is a lock function, but making it so aids annotalysis.
164 explicit ScopedCheck(JavaVM* vm, bool has_method, const char* functionName)
165 SHARED_LOCK_FUNCTION(GlobalSynchronization::mutator_lock_)
166 : soa_(vm) {
Ian Rogers365c1022012-06-22 15:05:28 -0700167 Init(kFlag_Invocation, functionName, has_method);
Elliott Hughesa2501992011-08-26 19:39:54 -0700168 }
169
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700170 ~ScopedCheck() UNLOCK_FUNCTION(GlobalSynchronization::mutator_lock_) {}
171
172 const ScopedObjectAccess& soa() {
173 return soa_;
174 }
175
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700176 bool ForceCopy() {
Elliott Hughesa2501992011-08-26 19:39:54 -0700177 return Runtime::Current()->GetJavaVM()->force_copy;
178 }
179
Elliott Hughes81ff3182012-03-23 20:35:56 -0700180 // Checks that 'class_name' is a valid "fully-qualified" JNI class name, like "java/lang/Thread"
181 // or "[Ljava/lang/Object;". A ClassLoader can actually normalize class names a couple of
182 // times, so using "java.lang.Thread" instead of "java/lang/Thread" might work in some
183 // circumstances, but this is incorrect.
184 void CheckClassName(const char* class_name) {
185 if (!IsValidJniClassName(class_name)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700186 JniAbortF(function_name_,
187 "illegal class name '%s'\n"
188 " (should be of the form 'package/Class', [Lpackage/Class;' or '[[B')",
189 class_name);
Elliott Hughesa2501992011-08-26 19:39:54 -0700190 }
191 }
192
193 /*
194 * Verify that the field is of the appropriate type. If the field has an
195 * object type, "java_object" is the object we're trying to assign into it.
196 *
197 * Works for both static and instance fields.
198 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700199 void CheckFieldType(jobject java_object, jfieldID fid, char prim, bool isStatic)
200 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700201 Field* f = CheckFieldID(fid);
202 if (f == NULL) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700203 return;
204 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800205 Class* field_type = FieldHelper(f).GetType();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700206 if (!field_type->IsPrimitive()) {
207 if (java_object != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700208 Object* obj = soa_.Decode<Object*>(java_object);
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700209 // If java_object is a weak global ref whose referent has been cleared,
210 // obj will be NULL. Otherwise, obj should always be non-NULL
211 // and valid.
Elliott Hughes88c5c352012-03-15 18:49:48 -0700212 if (!Runtime::Current()->GetHeap()->IsHeapAddress(obj)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700213 JniAbortF(function_name_, "field operation on invalid %s: %p",
214 ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700215 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700216 } else {
Brian Carlstrom16192862011-09-12 17:50:06 -0700217 if (!obj->InstanceOf(field_type)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700218 JniAbortF(function_name_, "attempt to set field %s with value of wrong type: %s",
219 PrettyField(f).c_str(), PrettyTypeOf(obj).c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700220 return;
221 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700222 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700223 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700224 } else if (field_type != Runtime::Current()->GetClassLinker()->FindPrimitiveClass(prim)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700225 JniAbortF(function_name_, "attempt to set field %s with value of wrong type: %c",
226 PrettyField(f).c_str(), prim);
Elliott Hughesa2501992011-08-26 19:39:54 -0700227 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700228 }
229
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700230 if (isStatic != f->IsStatic()) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700231 if (isStatic) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700232 JniAbortF(function_name_, "accessing non-static field %s as static", PrettyField(f).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700233 } else {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700234 JniAbortF(function_name_, "accessing static field %s as non-static", PrettyField(f).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700235 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700236 return;
237 }
238 }
239
240 /*
241 * Verify that this instance field ID is valid for this object.
242 *
243 * Assumes "jobj" has already been validated.
244 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700245 void CheckInstanceFieldID(jobject java_object, jfieldID fid)
246 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
247 Object* o = soa_.Decode<Object*>(java_object);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800248 if (o == NULL || !Runtime::Current()->GetHeap()->IsHeapAddress(o)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700249 JniAbortF(function_name_, "field operation on invalid %s: %p",
250 ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700251 return;
252 }
253
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700254 Field* f = CheckFieldID(fid);
255 if (f == NULL) {
256 return;
257 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700258 Class* c = o->GetClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800259 FieldHelper fh(f);
260 if (c->FindInstanceField(fh.GetName(), fh.GetTypeDescriptor()) == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700261 JniAbortF(function_name_, "jfieldID %s not valid for an object of class %s",
262 PrettyField(f).c_str(), PrettyTypeOf(o).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700263 }
264 }
265
266 /*
267 * Verify that the pointer value is non-NULL.
268 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700269 void CheckNonNull(const void* ptr) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700270 if (ptr == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700271 JniAbortF(function_name_, "non-nullable argument was NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -0700272 }
273 }
274
275 /*
276 * Verify that the method's return type matches the type of call.
277 * 'expectedType' will be "L" for all objects, including arrays.
278 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700279 void CheckSig(jmethodID mid, const char* expectedType, bool isStatic)
280 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800281 Method* m = CheckMethodID(mid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700282 if (m == NULL) {
283 return;
284 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800285 if (*expectedType != MethodHelper(m).GetShorty()[0]) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700286 JniAbortF(function_name_, "the return type of %s does not match %s",
287 function_name_, PrettyMethod(m).c_str());
288 }
289 if (isStatic != m->IsStatic()) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700290 if (isStatic) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700291 JniAbortF(function_name_, "calling non-static method %s with %s",
292 PrettyMethod(m).c_str(), function_name_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700293 } else {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700294 JniAbortF(function_name_, "calling static method %s with %s",
295 PrettyMethod(m).c_str(), function_name_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700296 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700297 }
298 }
299
300 /*
301 * Verify that this static field ID is valid for this class.
302 *
303 * Assumes "java_class" has already been validated.
304 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700305 void CheckStaticFieldID(jclass java_class, jfieldID fid)
306 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
307 Class* c = soa_.Decode<Class*>(java_class);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700308 const Field* f = CheckFieldID(fid);
309 if (f == NULL) {
310 return;
311 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700312 if (f->GetDeclaringClass() != c) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700313 JniAbortF(function_name_, "static jfieldID %p not valid for class %s",
314 fid, PrettyClass(c).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700315 }
316 }
317
318 /*
Elliott Hughese84278b2012-03-22 10:06:53 -0700319 * Verify that "mid" is appropriate for "java_class".
Elliott Hughesa2501992011-08-26 19:39:54 -0700320 *
321 * A mismatch isn't dangerous, because the jmethodID defines the class. In
Elliott Hughese84278b2012-03-22 10:06:53 -0700322 * fact, java_class is unused in the implementation. It's best if we don't
Elliott Hughesa2501992011-08-26 19:39:54 -0700323 * allow bad code in the system though.
324 *
Elliott Hughese84278b2012-03-22 10:06:53 -0700325 * Instances of "java_class" must be instances of the method's declaring class.
Elliott Hughesa2501992011-08-26 19:39:54 -0700326 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700327 void CheckStaticMethod(jclass java_class, jmethodID mid)
328 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700329 const Method* m = CheckMethodID(mid);
330 if (m == NULL) {
331 return;
332 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700333 Class* c = soa_.Decode<Class*>(java_class);
Elliott Hughesa2501992011-08-26 19:39:54 -0700334 if (!c->IsAssignableFrom(m->GetDeclaringClass())) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700335 JniAbortF(function_name_, "can't call static %s on class %s",
336 PrettyMethod(m).c_str(), PrettyClass(c).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700337 }
338 }
339
340 /*
341 * Verify that "mid" is appropriate for "jobj".
342 *
343 * Make sure the object is an instance of the method's declaring class.
344 * (Note the mid might point to a declaration in an interface; this
345 * will be handled automatically by the instanceof check.)
346 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700347 void CheckVirtualMethod(jobject java_object, jmethodID mid)
348 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700349 const Method* m = CheckMethodID(mid);
350 if (m == NULL) {
351 return;
352 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700353 Object* o = soa_.Decode<Object*>(java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700354 if (!o->InstanceOf(m->GetDeclaringClass())) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700355 JniAbortF(function_name_, "can't call %s on instance of %s",
356 PrettyMethod(m).c_str(), PrettyTypeOf(o).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700357 }
358 }
359
360 /**
361 * The format string is a sequence of the following characters,
362 * and must be followed by arguments of the corresponding types
363 * in the same order.
364 *
365 * Java primitive types:
366 * B - jbyte
367 * C - jchar
368 * D - jdouble
369 * F - jfloat
370 * I - jint
371 * J - jlong
372 * S - jshort
373 * Z - jboolean (shown as true and false)
374 * V - void
375 *
376 * Java reference types:
377 * L - jobject
378 * a - jarray
379 * c - jclass
380 * s - jstring
381 *
382 * JNI types:
383 * b - jboolean (shown as JNI_TRUE and JNI_FALSE)
384 * f - jfieldID
385 * m - jmethodID
386 * p - void*
387 * r - jint (for release mode arguments)
Elliott Hughes78090d12011-10-07 14:31:47 -0700388 * u - const char* (Modified UTF-8)
Elliott Hughesa2501992011-08-26 19:39:54 -0700389 * z - jsize (for lengths; use i if negative values are okay)
390 * v - JavaVM*
391 * E - JNIEnv*
392 * . - no argument; just print "..." (used for varargs JNI calls)
393 *
394 * Use the kFlag_NullableUtf flag where 'u' field(s) are nullable.
395 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700396 void Check(bool entry, const char* fmt0, ...)
397 SHARED_LOCKS_REQUIRED (GlobalSynchronization::mutator_lock_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700398 va_list ap;
399
Elliott Hughesa0957642011-09-02 14:27:33 -0700400 const Method* traceMethod = NULL;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700401 if ((!soa_.Vm()->trace.empty() || VLOG_IS_ON(third_party_jni)) && has_method_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700402 // We need to guard some of the invocation interface's calls: a bad caller might
403 // use DetachCurrentThread or GetEnv on a thread that's not yet attached.
Elliott Hughesa0957642011-09-02 14:27:33 -0700404 Thread* self = Thread::Current();
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700405 if ((flags_ & kFlag_Invocation) == 0 || self != NULL) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700406 traceMethod = self->GetCurrentMethod();
Elliott Hughesa2501992011-08-26 19:39:54 -0700407 }
408 }
Elliott Hughesa0957642011-09-02 14:27:33 -0700409
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700410 if (((flags_ & kFlag_ForceTrace) != 0) || (traceMethod != NULL && ShouldTrace(soa_.Vm(), traceMethod))) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700411 va_start(ap, fmt0);
412 std::string msg;
413 for (const char* fmt = fmt0; *fmt;) {
414 char ch = *fmt++;
415 if (ch == 'B') { // jbyte
416 jbyte b = va_arg(ap, int);
417 if (b >= 0 && b < 10) {
418 StringAppendF(&msg, "%d", b);
419 } else {
420 StringAppendF(&msg, "%#x (%d)", b, b);
421 }
422 } else if (ch == 'C') { // jchar
423 jchar c = va_arg(ap, int);
424 if (c < 0x7f && c >= ' ') {
425 StringAppendF(&msg, "U+%x ('%c')", c, c);
426 } else {
427 StringAppendF(&msg, "U+%x", c);
428 }
429 } else if (ch == 'F' || ch == 'D') { // jfloat, jdouble
430 StringAppendF(&msg, "%g", va_arg(ap, double));
431 } else if (ch == 'I' || ch == 'S') { // jint, jshort
432 StringAppendF(&msg, "%d", va_arg(ap, int));
433 } else if (ch == 'J') { // jlong
434 StringAppendF(&msg, "%lld", va_arg(ap, jlong));
435 } else if (ch == 'Z') { // jboolean
436 StringAppendF(&msg, "%s", va_arg(ap, int) ? "true" : "false");
437 } else if (ch == 'V') { // void
438 msg += "void";
439 } else if (ch == 'v') { // JavaVM*
440 JavaVM* vm = va_arg(ap, JavaVM*);
441 StringAppendF(&msg, "(JavaVM*)%p", vm);
442 } else if (ch == 'E') { // JNIEnv*
443 JNIEnv* env = va_arg(ap, JNIEnv*);
444 StringAppendF(&msg, "(JNIEnv*)%p", env);
445 } else if (ch == 'L' || ch == 'a' || ch == 's') { // jobject, jarray, jstring
446 // For logging purposes, these are identical.
447 jobject o = va_arg(ap, jobject);
448 if (o == NULL) {
449 msg += "NULL";
450 } else {
451 StringAppendF(&msg, "%p", o);
452 }
453 } else if (ch == 'b') { // jboolean (JNI-style)
454 jboolean b = va_arg(ap, int);
455 msg += (b ? "JNI_TRUE" : "JNI_FALSE");
456 } else if (ch == 'c') { // jclass
457 jclass jc = va_arg(ap, jclass);
458 Class* c = reinterpret_cast<Class*>(Thread::Current()->DecodeJObject(jc));
459 if (c == NULL) {
460 msg += "NULL";
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800461 } else if (c == kInvalidIndirectRefObject || !Runtime::Current()->GetHeap()->IsHeapAddress(c)) {
Elliott Hughes485cac42011-12-09 17:49:35 -0800462 StringAppendF(&msg, "INVALID POINTER:%p", jc);
463 } else if (!c->IsClass()) {
464 msg += "INVALID NON-CLASS OBJECT OF TYPE:" + PrettyTypeOf(c);
Elliott Hughesa2501992011-08-26 19:39:54 -0700465 } else {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700466 msg += PrettyClass(c);
Elliott Hughesa2501992011-08-26 19:39:54 -0700467 if (!entry) {
468 StringAppendF(&msg, " (%p)", jc);
469 }
470 }
471 } else if (ch == 'f') { // jfieldID
472 jfieldID fid = va_arg(ap, jfieldID);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700473 Field* f = reinterpret_cast<Field*>(fid);
Elliott Hughesa2501992011-08-26 19:39:54 -0700474 msg += PrettyField(f);
475 if (!entry) {
476 StringAppendF(&msg, " (%p)", fid);
477 }
478 } else if (ch == 'z') { // non-negative jsize
479 // You might expect jsize to be size_t, but it's not; it's the same as jint.
480 // We only treat this specially so we can do the non-negative check.
481 // TODO: maybe this wasn't worth it?
482 jint i = va_arg(ap, jint);
483 StringAppendF(&msg, "%d", i);
484 } else if (ch == 'm') { // jmethodID
485 jmethodID mid = va_arg(ap, jmethodID);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700486 Method* m = reinterpret_cast<Method*>(mid);
Elliott Hughesa2501992011-08-26 19:39:54 -0700487 msg += PrettyMethod(m);
488 if (!entry) {
489 StringAppendF(&msg, " (%p)", mid);
490 }
491 } else if (ch == 'p') { // void* ("pointer")
492 void* p = va_arg(ap, void*);
493 if (p == NULL) {
494 msg += "NULL";
495 } else {
496 StringAppendF(&msg, "(void*) %p", p);
497 }
498 } else if (ch == 'r') { // jint (release mode)
499 jint releaseMode = va_arg(ap, jint);
500 if (releaseMode == 0) {
501 msg += "0";
502 } else if (releaseMode == JNI_ABORT) {
503 msg += "JNI_ABORT";
504 } else if (releaseMode == JNI_COMMIT) {
505 msg += "JNI_COMMIT";
506 } else {
507 StringAppendF(&msg, "invalid release mode %d", releaseMode);
508 }
Elliott Hughes78090d12011-10-07 14:31:47 -0700509 } else if (ch == 'u') { // const char* (Modified UTF-8)
Elliott Hughesa2501992011-08-26 19:39:54 -0700510 const char* utf = va_arg(ap, const char*);
511 if (utf == NULL) {
512 msg += "NULL";
513 } else {
514 StringAppendF(&msg, "\"%s\"", utf);
515 }
516 } else if (ch == '.') {
517 msg += "...";
518 } else {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700519 JniAbortF(function_name_, "unknown trace format specifier: %c", ch);
Elliott Hughesa2501992011-08-26 19:39:54 -0700520 return;
521 }
522 if (*fmt) {
523 StringAppendF(&msg, ", ");
524 }
525 }
526 va_end(ap);
527
Elliott Hughes485cac42011-12-09 17:49:35 -0800528 if ((flags_ & kFlag_ForceTrace) != 0) {
529 LOG(INFO) << "JNI: call to " << function_name_ << "(" << msg << ")";
530 } else if (entry) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700531 if (has_method_) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700532 std::string methodName(PrettyMethod(traceMethod, false));
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700533 LOG(INFO) << "JNI: " << methodName << " -> " << function_name_ << "(" << msg << ")";
534 indent_ = methodName.size() + 1;
Elliott Hughesa2501992011-08-26 19:39:54 -0700535 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700536 LOG(INFO) << "JNI: -> " << function_name_ << "(" << msg << ")";
537 indent_ = 0;
Elliott Hughesa2501992011-08-26 19:39:54 -0700538 }
539 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700540 LOG(INFO) << StringPrintf("JNI: %*s<- %s returned %s", indent_, "", function_name_, msg.c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700541 }
542 }
543
544 // We always do the thorough checks on entry, and never on exit...
545 if (entry) {
546 va_start(ap, fmt0);
547 for (const char* fmt = fmt0; *fmt; ++fmt) {
548 char ch = *fmt;
549 if (ch == 'a') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700550 CheckArray(va_arg(ap, jarray));
Elliott Hughesa2501992011-08-26 19:39:54 -0700551 } else if (ch == 'c') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700552 CheckInstance(kClass, va_arg(ap, jclass));
Elliott Hughesa2501992011-08-26 19:39:54 -0700553 } else if (ch == 'L') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700554 CheckObject(va_arg(ap, jobject));
Elliott Hughesa2501992011-08-26 19:39:54 -0700555 } else if (ch == 'r') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700556 CheckReleaseMode(va_arg(ap, jint));
Elliott Hughesa2501992011-08-26 19:39:54 -0700557 } else if (ch == 's') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700558 CheckInstance(kString, va_arg(ap, jstring));
Elliott Hughesa2501992011-08-26 19:39:54 -0700559 } else if (ch == 'u') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700560 if ((flags_ & kFlag_Release) != 0) {
561 CheckNonNull(va_arg(ap, const char*));
Elliott Hughesa2501992011-08-26 19:39:54 -0700562 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700563 bool nullable = ((flags_ & kFlag_NullableUtf) != 0);
564 CheckUtfString(va_arg(ap, const char*), nullable);
Elliott Hughesa2501992011-08-26 19:39:54 -0700565 }
566 } else if (ch == 'z') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700567 CheckLengthPositive(va_arg(ap, jsize));
Elliott Hughesa2501992011-08-26 19:39:54 -0700568 } else if (strchr("BCISZbfmpEv", ch) != NULL) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800569 va_arg(ap, uint32_t); // Skip this argument.
Elliott Hughesa2501992011-08-26 19:39:54 -0700570 } else if (ch == 'D' || ch == 'F') {
571 va_arg(ap, double); // Skip this argument.
572 } else if (ch == 'J') {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800573 va_arg(ap, uint64_t); // Skip this argument.
Elliott Hughesa2501992011-08-26 19:39:54 -0700574 } else if (ch == '.') {
575 } else {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800576 LOG(FATAL) << "Unknown check format specifier: " << ch;
Elliott Hughesa2501992011-08-26 19:39:54 -0700577 }
578 }
579 va_end(ap);
580 }
581 }
582
Elliott Hughesa92853e2012-02-07 16:09:27 -0800583 enum InstanceKind {
584 kClass,
Elliott Hughes0f3c5532012-03-30 14:51:51 -0700585 kDirectByteBuffer,
586 kObject,
587 kString,
588 kThrowable,
Elliott Hughesa92853e2012-02-07 16:09:27 -0800589 };
590
591 /*
592 * Verify that "jobj" is a valid non-NULL object reference, and points to
593 * an instance of expectedClass.
594 *
595 * Because we're looking at an object on the GC heap, we have to switch
596 * to "running" mode before doing the checks.
597 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700598 bool CheckInstance(InstanceKind kind, jobject java_object)
599 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughesa92853e2012-02-07 16:09:27 -0800600 const char* what = NULL;
601 switch (kind) {
602 case kClass:
603 what = "jclass";
604 break;
605 case kDirectByteBuffer:
606 what = "direct ByteBuffer";
607 break;
608 case kObject:
609 what = "jobject";
610 break;
611 case kString:
612 what = "jstring";
613 break;
614 case kThrowable:
615 what = "jthrowable";
616 break;
617 default:
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700618 LOG(FATAL) << "Unknown kind " << static_cast<int>(kind);
Elliott Hughesa92853e2012-02-07 16:09:27 -0800619 }
620
621 if (java_object == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700622 JniAbortF(function_name_, "%s received null %s", function_name_, what);
Elliott Hughesa92853e2012-02-07 16:09:27 -0800623 return false;
624 }
625
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700626 Object* obj = soa_.Decode<Object*>(java_object);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800627 if (!Runtime::Current()->GetHeap()->IsHeapAddress(obj)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700628 JniAbortF(function_name_, "%s is an invalid %s: %p (%p)",
629 what, ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object, obj);
Elliott Hughesa92853e2012-02-07 16:09:27 -0800630 return false;
631 }
632
633 bool okay = true;
634 switch (kind) {
635 case kClass:
636 okay = obj->IsClass();
637 break;
638 case kDirectByteBuffer:
639 UNIMPLEMENTED(FATAL);
640 break;
641 case kString:
642 okay = obj->GetClass()->IsStringClass();
643 break;
644 case kThrowable:
645 okay = obj->GetClass()->IsThrowableClass();
646 break;
647 case kObject:
648 break;
649 }
650 if (!okay) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700651 JniAbortF(function_name_, "%s has wrong type: %s", what, PrettyTypeOf(obj).c_str());
Elliott Hughesa92853e2012-02-07 16:09:27 -0800652 return false;
653 }
654
655 return true;
656 }
657
Elliott Hughesba8eee12012-01-24 20:25:24 -0800658 private:
Elliott Hughes81ff3182012-03-23 20:35:56 -0700659 // Set "has_method" to true if we have a valid thread with a method pointer.
660 // We won't have one before attaching a thread, after detaching a thread, or
661 // when shutting down the runtime.
Ian Rogers365c1022012-06-22 15:05:28 -0700662 void Init(int flags, const char* functionName, bool has_method) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700663 flags_ = flags;
664 function_name_ = functionName;
Elliott Hughes81ff3182012-03-23 20:35:56 -0700665 has_method_ = has_method;
Elliott Hughesa2501992011-08-26 19:39:54 -0700666 }
667
668 /*
669 * Verify that "array" is non-NULL and points to an Array object.
670 *
671 * Since we're dealing with objects, switch to "running" mode.
672 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700673 void CheckArray(jarray java_array) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700674 if (java_array == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700675 JniAbortF(function_name_, "jarray was NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -0700676 return;
677 }
678
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700679 Array* a = soa_.Decode<Array*>(java_array);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800680 if (!Runtime::Current()->GetHeap()->IsHeapAddress(a)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700681 JniAbortF(function_name_, "jarray is an invalid %s: %p (%p)",
682 ToStr<IndirectRefKind>(GetIndirectRefKind(java_array)).c_str(), java_array, a);
Elliott Hughesa2501992011-08-26 19:39:54 -0700683 } else if (!a->IsArrayInstance()) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700684 JniAbortF(function_name_, "jarray argument has non-array type: %s", PrettyTypeOf(a).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700685 }
686 }
687
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700688 void CheckLengthPositive(jsize length) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700689 if (length < 0) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700690 JniAbortF(function_name_, "negative jsize: %d", length);
Elliott Hughesa2501992011-08-26 19:39:54 -0700691 }
692 }
693
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700694 Field* CheckFieldID(jfieldID fid) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700695 if (fid == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700696 JniAbortF(function_name_, "jfieldID was NULL");
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700697 return NULL;
698 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700699 Field* f = soa_.DecodeField(fid);
Ian Rogers365c1022012-06-22 15:05:28 -0700700 if (!Runtime::Current()->GetHeap()->IsHeapAddress(f) || !f->IsField()) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700701 JniAbortF(function_name_, "invalid jfieldID: %p", fid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700702 return NULL;
703 }
704 return f;
705 }
706
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700707 Method* CheckMethodID(jmethodID mid) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700708 if (mid == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700709 JniAbortF(function_name_, "jmethodID was NULL");
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700710 return NULL;
711 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700712 Method* m = soa_.DecodeMethod(mid);
Ian Rogers365c1022012-06-22 15:05:28 -0700713 if (!Runtime::Current()->GetHeap()->IsHeapAddress(m) || !m->IsMethod()) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700714 JniAbortF(function_name_, "invalid jmethodID: %p", mid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700715 return NULL;
716 }
717 return m;
718 }
719
Elliott Hughesa2501992011-08-26 19:39:54 -0700720 /*
721 * Verify that "jobj" is a valid object, and that it's an object that JNI
722 * is allowed to know about. We allow NULL references.
723 *
724 * Switches to "running" mode before performing checks.
725 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700726 void CheckObject(jobject java_object)
727 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700728 if (java_object == NULL) {
729 return;
730 }
731
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700732 Object* o = soa_.Decode<Object*>(java_object);
Elliott Hughes88c5c352012-03-15 18:49:48 -0700733 if (!Runtime::Current()->GetHeap()->IsHeapAddress(o)) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700734 // TODO: when we remove work_around_app_jni_bugs, this should be impossible.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700735 JniAbortF(function_name_, "native code passing in reference to invalid %s: %p",
736 ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700737 }
738 }
739
740 /*
741 * Verify that the "mode" argument passed to a primitive array Release
742 * function is one of the valid values.
743 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700744 void CheckReleaseMode(jint mode) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700745 if (mode != 0 && mode != JNI_COMMIT && mode != JNI_ABORT) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700746 JniAbortF(function_name_, "bad value for release mode: %d", mode);
Elliott Hughesa2501992011-08-26 19:39:54 -0700747 }
748 }
749
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700750 void CheckThread(int flags) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700751 Thread* self = Thread::Current();
752 if (self == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700753 JniAbortF(function_name_, "a thread (tid %d) is making JNI calls without being attached", GetTid());
Elliott Hughesa2501992011-08-26 19:39:54 -0700754 return;
755 }
756
757 // Get the *correct* JNIEnv by going through our TLS pointer.
758 JNIEnvExt* threadEnv = self->GetJniEnv();
759
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700760 // Verify that the current thread is (a) attached and (b) associated with
761 // this particular instance of JNIEnv.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700762 if (soa_.Env() != threadEnv) {
763 if (soa_.Vm()->work_around_app_jni_bugs) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700764 // If we're keeping broken code limping along, we need to suppress the abort...
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700765 LOG(ERROR) << "APP BUG DETECTED: thread " << *self << " using JNIEnv* from thread " << *soa_.Self();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700766 } else {
767 JniAbortF(function_name_, "thread %s using JNIEnv* from thread %s",
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700768 ToStr<Thread>(*self).c_str(), ToStr<Thread>(*soa_.Self()).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700769 return;
770 }
771 }
772
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700773 // Verify that, if this thread previously made a critical "get" call, we
774 // do the corresponding "release" call before we try anything else.
Elliott Hughesa2501992011-08-26 19:39:54 -0700775 switch (flags & kFlag_CritMask) {
776 case kFlag_CritOkay: // okay to call this method
777 break;
778 case kFlag_CritBad: // not okay to call
779 if (threadEnv->critical) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700780 JniAbortF(function_name_, "thread %s using JNI after critical get", ToStr<Thread>(*self).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700781 return;
782 }
783 break;
784 case kFlag_CritGet: // this is a "get" call
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700785 // Don't check here; we allow nested gets.
Elliott Hughesa2501992011-08-26 19:39:54 -0700786 threadEnv->critical++;
787 break;
788 case kFlag_CritRelease: // this is a "release" call
789 threadEnv->critical--;
790 if (threadEnv->critical < 0) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700791 JniAbortF(function_name_, "thread %s called too many critical releases", ToStr<Thread>(*self).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700792 return;
793 }
794 break;
795 default:
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800796 LOG(FATAL) << "Bad flags (internal error): " << flags;
Elliott Hughesa2501992011-08-26 19:39:54 -0700797 }
798
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700799 // Verify that, if an exception has been raised, the native code doesn't
800 // make any JNI calls other than the Exception* methods.
Elliott Hughesa2501992011-08-26 19:39:54 -0700801 if ((flags & kFlag_ExcepOkay) == 0 && self->IsExceptionPending()) {
Elliott Hughes30646832011-10-13 16:59:46 -0700802 std::string type(PrettyTypeOf(self->GetException()));
Elliott Hughes30646832011-10-13 16:59:46 -0700803 // TODO: write native code that doesn't require allocation for dumping an exception.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700804 // TODO: do we care any more? art always dumps pending exceptions on aborting threads.
Elliott Hughes30646832011-10-13 16:59:46 -0700805 if (type != "java.lang.OutOfMemoryError") {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700806 JniAbortF(function_name_, "JNI %s called with pending exception: %s",
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700807 function_name_, type.c_str(), jniGetStackTrace(soa_.Env()).c_str());
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700808 } else {
809 JniAbortF(function_name_, "JNI %s called with %s pending", function_name_, type.c_str());
Elliott Hughes30646832011-10-13 16:59:46 -0700810 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700811 return;
812 }
813 }
814
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700815 // Verifies that "bytes" points to valid Modified UTF-8 data.
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700816 void CheckUtfString(const char* bytes, bool nullable) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700817 if (bytes == NULL) {
818 if (!nullable) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700819 JniAbortF(function_name_, "non-nullable const char* was NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -0700820 return;
821 }
822 return;
823 }
824
825 const char* errorKind = NULL;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700826 uint8_t utf8 = CheckUtfBytes(bytes, &errorKind);
Elliott Hughesa2501992011-08-26 19:39:54 -0700827 if (errorKind != NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700828 JniAbortF(function_name_,
829 "input is not valid Modified UTF-8: illegal %s byte %#x\n"
830 " string: '%s'", errorKind, utf8, bytes);
Elliott Hughesa2501992011-08-26 19:39:54 -0700831 return;
832 }
833 }
834
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700835 static uint8_t CheckUtfBytes(const char* bytes, const char** errorKind) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700836 while (*bytes != '\0') {
837 uint8_t utf8 = *(bytes++);
838 // Switch on the high four bits.
839 switch (utf8 >> 4) {
840 case 0x00:
841 case 0x01:
842 case 0x02:
843 case 0x03:
844 case 0x04:
845 case 0x05:
846 case 0x06:
847 case 0x07:
848 // Bit pattern 0xxx. No need for any extra bytes.
849 break;
850 case 0x08:
851 case 0x09:
852 case 0x0a:
853 case 0x0b:
854 case 0x0f:
855 /*
856 * Bit pattern 10xx or 1111, which are illegal start bytes.
857 * Note: 1111 is valid for normal UTF-8, but not the
Elliott Hughes78090d12011-10-07 14:31:47 -0700858 * Modified UTF-8 used here.
Elliott Hughesa2501992011-08-26 19:39:54 -0700859 */
860 *errorKind = "start";
861 return utf8;
862 case 0x0e:
863 // Bit pattern 1110, so there are two additional bytes.
864 utf8 = *(bytes++);
865 if ((utf8 & 0xc0) != 0x80) {
866 *errorKind = "continuation";
867 return utf8;
868 }
869 // Fall through to take care of the final byte.
870 case 0x0c:
871 case 0x0d:
872 // Bit pattern 110x, so there is one additional byte.
873 utf8 = *(bytes++);
874 if ((utf8 & 0xc0) != 0x80) {
875 *errorKind = "continuation";
876 return utf8;
877 }
878 break;
879 }
880 }
881 return 0;
882 }
883
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700884 const ScopedObjectAccess soa_;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700885 const char* function_name_;
886 int flags_;
887 bool has_method_;
Elliott Hughes92cb4982011-12-16 16:57:28 -0800888 int indent_;
Elliott Hughesa2501992011-08-26 19:39:54 -0700889
890 DISALLOW_COPY_AND_ASSIGN(ScopedCheck);
891};
892
893#define CHECK_JNI_ENTRY(flags, types, args...) \
894 ScopedCheck sc(env, flags, __FUNCTION__); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700895 sc.Check(true, types, ##args)
Elliott Hughesa2501992011-08-26 19:39:54 -0700896
897#define CHECK_JNI_EXIT(type, exp) ({ \
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700898 typeof(exp) _rc = (exp); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700899 sc.Check(false, type, _rc); \
Elliott Hughesa2501992011-08-26 19:39:54 -0700900 _rc; })
901#define CHECK_JNI_EXIT_VOID() \
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700902 sc.Check(false, "V")
Elliott Hughesa2501992011-08-26 19:39:54 -0700903
904/*
905 * ===========================================================================
906 * Guarded arrays
907 * ===========================================================================
908 */
909
910#define kGuardLen 512 /* must be multiple of 2 */
911#define kGuardPattern 0xd5e3 /* uncommon values; d5e3d5e3 invalid addr */
912#define kGuardMagic 0xffd5aa96
913
914/* this gets tucked in at the start of the buffer; struct size must be even */
915struct GuardedCopy {
916 uint32_t magic;
917 uLong adler;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700918 size_t original_length;
919 const void* original_ptr;
Elliott Hughesa2501992011-08-26 19:39:54 -0700920
921 /* find the GuardedCopy given the pointer into the "live" data */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700922 static inline const GuardedCopy* FromData(const void* dataBuf) {
923 return reinterpret_cast<const GuardedCopy*>(ActualBuffer(dataBuf));
Elliott Hughesa2501992011-08-26 19:39:54 -0700924 }
925
926 /*
927 * Create an over-sized buffer to hold the contents of "buf". Copy it in,
928 * filling in the area around it with guard data.
929 *
930 * We use a 16-bit pattern to make a rogue memset less likely to elude us.
931 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700932 static void* Create(const void* buf, size_t len, bool modOkay) {
933 size_t newLen = ActualLength(len);
934 uint8_t* newBuf = DebugAlloc(newLen);
Elliott Hughesa2501992011-08-26 19:39:54 -0700935
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700936 // Fill it in with a pattern.
Elliott Hughesba8eee12012-01-24 20:25:24 -0800937 uint16_t* pat = reinterpret_cast<uint16_t*>(newBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -0700938 for (size_t i = 0; i < newLen / 2; i++) {
939 *pat++ = kGuardPattern;
940 }
941
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700942 // Copy the data in; note "len" could be zero.
Elliott Hughesa2501992011-08-26 19:39:54 -0700943 memcpy(newBuf + kGuardLen / 2, buf, len);
944
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700945 // If modification is not expected, grab a checksum.
Elliott Hughesa2501992011-08-26 19:39:54 -0700946 uLong adler = 0;
947 if (!modOkay) {
948 adler = adler32(0L, Z_NULL, 0);
Elliott Hughesba8eee12012-01-24 20:25:24 -0800949 adler = adler32(adler, reinterpret_cast<const Bytef*>(buf), len);
950 *reinterpret_cast<uLong*>(newBuf) = adler;
Elliott Hughesa2501992011-08-26 19:39:54 -0700951 }
952
953 GuardedCopy* pExtra = reinterpret_cast<GuardedCopy*>(newBuf);
954 pExtra->magic = kGuardMagic;
955 pExtra->adler = adler;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700956 pExtra->original_ptr = buf;
957 pExtra->original_length = len;
Elliott Hughesa2501992011-08-26 19:39:54 -0700958
959 return newBuf + kGuardLen / 2;
960 }
961
962 /*
963 * Free up the guard buffer, scrub it, and return the original pointer.
964 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700965 static void* Destroy(void* dataBuf) {
966 const GuardedCopy* pExtra = GuardedCopy::FromData(dataBuf);
Elliott Hughesba8eee12012-01-24 20:25:24 -0800967 void* original_ptr = const_cast<void*>(pExtra->original_ptr);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700968 size_t len = pExtra->original_length;
969 DebugFree(dataBuf, len);
970 return original_ptr;
Elliott Hughesa2501992011-08-26 19:39:54 -0700971 }
972
973 /*
974 * Verify the guard area and, if "modOkay" is false, that the data itself
975 * has not been altered.
976 *
977 * The caller has already checked that "dataBuf" is non-NULL.
978 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700979 static void Check(const char* functionName, const void* dataBuf, bool modOkay) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700980 static const uint32_t kMagicCmp = kGuardMagic;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700981 const uint8_t* fullBuf = ActualBuffer(dataBuf);
982 const GuardedCopy* pExtra = GuardedCopy::FromData(dataBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -0700983
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700984 // Before we do anything with "pExtra", check the magic number. We
985 // do the check with memcmp rather than "==" in case the pointer is
986 // unaligned. If it points to completely bogus memory we're going
987 // to crash, but there's no easy way around that.
Elliott Hughesa2501992011-08-26 19:39:54 -0700988 if (memcmp(&pExtra->magic, &kMagicCmp, 4) != 0) {
989 uint8_t buf[4];
990 memcpy(buf, &pExtra->magic, 4);
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700991 JniAbortF(functionName,
992 "guard magic does not match (found 0x%02x%02x%02x%02x) -- incorrect data pointer %p?",
993 buf[3], buf[2], buf[1], buf[0], dataBuf); // Assumes little-endian.
Elliott Hughesa2501992011-08-26 19:39:54 -0700994 }
995
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700996 size_t len = pExtra->original_length;
Elliott Hughesa2501992011-08-26 19:39:54 -0700997
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700998 // Check bottom half of guard; skip over optional checksum storage.
Elliott Hughesba8eee12012-01-24 20:25:24 -0800999 const uint16_t* pat = reinterpret_cast<const uint16_t*>(fullBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -07001000 for (size_t i = sizeof(GuardedCopy) / 2; i < (kGuardLen / 2 - sizeof(GuardedCopy)) / 2; i++) {
1001 if (pat[i] != kGuardPattern) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001002 JniAbortF(functionName, "guard pattern(1) disturbed at %p +%d", fullBuf, i*2);
Elliott Hughesa2501992011-08-26 19:39:54 -07001003 }
1004 }
1005
1006 int offset = kGuardLen / 2 + len;
1007 if (offset & 0x01) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001008 // Odd byte; expected value depends on endian.
Elliott Hughesa2501992011-08-26 19:39:54 -07001009 const uint16_t patSample = kGuardPattern;
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001010 uint8_t expected_byte = reinterpret_cast<const uint8_t*>(&patSample)[1];
1011 if (fullBuf[offset] != expected_byte) {
1012 JniAbortF(functionName, "guard pattern disturbed in odd byte after %p +%d 0x%02x 0x%02x",
1013 fullBuf, offset, fullBuf[offset], expected_byte);
Elliott Hughesa2501992011-08-26 19:39:54 -07001014 }
1015 offset++;
1016 }
1017
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001018 // Check top half of guard.
Elliott Hughesba8eee12012-01-24 20:25:24 -08001019 pat = reinterpret_cast<const uint16_t*>(fullBuf + offset);
Elliott Hughesa2501992011-08-26 19:39:54 -07001020 for (size_t i = 0; i < kGuardLen / 4; i++) {
1021 if (pat[i] != kGuardPattern) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001022 JniAbortF(functionName, "guard pattern(2) disturbed at %p +%d", fullBuf, offset + i*2);
Elliott Hughesa2501992011-08-26 19:39:54 -07001023 }
1024 }
1025
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001026 // If modification is not expected, verify checksum. Strictly speaking
1027 // this is wrong: if we told the client that we made a copy, there's no
1028 // reason they can't alter the buffer.
Elliott Hughesa2501992011-08-26 19:39:54 -07001029 if (!modOkay) {
1030 uLong adler = adler32(0L, Z_NULL, 0);
1031 adler = adler32(adler, (const Bytef*)dataBuf, len);
1032 if (pExtra->adler != adler) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001033 JniAbortF(functionName, "buffer modified (0x%08lx vs 0x%08lx) at address %p",
1034 pExtra->adler, adler, dataBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -07001035 }
1036 }
1037 }
1038
1039 private:
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001040 static uint8_t* DebugAlloc(size_t len) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001041 void* result = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
1042 if (result == MAP_FAILED) {
1043 PLOG(FATAL) << "GuardedCopy::create mmap(" << len << ") failed";
1044 }
1045 return reinterpret_cast<uint8_t*>(result);
1046 }
1047
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001048 static void DebugFree(void* dataBuf, size_t len) {
1049 uint8_t* fullBuf = ActualBuffer(dataBuf);
1050 size_t totalByteCount = ActualLength(len);
Elliott Hughesa2501992011-08-26 19:39:54 -07001051 // TODO: we could mprotect instead, and keep the allocation around for a while.
1052 // This would be even more expensive, but it might catch more errors.
1053 // if (mprotect(fullBuf, totalByteCount, PROT_NONE) != 0) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -07001054 // PLOG(WARNING) << "mprotect(PROT_NONE) failed";
Elliott Hughesa2501992011-08-26 19:39:54 -07001055 // }
1056 if (munmap(fullBuf, totalByteCount) != 0) {
Elliott Hughesba8eee12012-01-24 20:25:24 -08001057 PLOG(FATAL) << "munmap(" << reinterpret_cast<void*>(fullBuf) << ", " << totalByteCount << ") failed";
Elliott Hughesa2501992011-08-26 19:39:54 -07001058 }
1059 }
1060
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001061 static const uint8_t* ActualBuffer(const void* dataBuf) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001062 return reinterpret_cast<const uint8_t*>(dataBuf) - kGuardLen / 2;
1063 }
1064
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001065 static uint8_t* ActualBuffer(void* dataBuf) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001066 return reinterpret_cast<uint8_t*>(dataBuf) - kGuardLen / 2;
1067 }
1068
1069 // Underlying length of a user allocation of 'length' bytes.
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001070 static size_t ActualLength(size_t length) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001071 return (length + kGuardLen + 1) & ~0x01;
1072 }
1073};
1074
1075/*
1076 * Create a guarded copy of a primitive array. Modifications to the copied
1077 * data are allowed. Returns a pointer to the copied data.
1078 */
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001079static void* CreateGuardedPACopy(JNIEnv* env, const jarray java_array, jboolean* isCopy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001080 ScopedObjectAccess soa(env);
Elliott Hughesa2501992011-08-26 19:39:54 -07001081
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001082 Array* a = soa.Decode<Array*>(java_array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001083 size_t component_size = a->GetClass()->GetComponentSize();
1084 size_t byte_count = a->GetLength() * component_size;
1085 void* result = GuardedCopy::Create(a->GetRawData(component_size), byte_count, true);
Elliott Hughesa2501992011-08-26 19:39:54 -07001086 if (isCopy != NULL) {
1087 *isCopy = JNI_TRUE;
1088 }
1089 return result;
1090}
1091
1092/*
1093 * Perform the array "release" operation, which may or may not copy data
Elliott Hughes81ff3182012-03-23 20:35:56 -07001094 * back into the managed heap, and may or may not release the underlying storage.
Elliott Hughesa2501992011-08-26 19:39:54 -07001095 */
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001096static void ReleaseGuardedPACopy(JNIEnv* env, jarray java_array, void* dataBuf, int mode) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001097 if (reinterpret_cast<uintptr_t>(dataBuf) == kNoCopyMagic) {
1098 return;
1099 }
1100
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001101 ScopedObjectAccess soa(env);
1102 Array* a = soa.Decode<Array*>(java_array);
Elliott Hughesa2501992011-08-26 19:39:54 -07001103
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001104 GuardedCopy::Check(__FUNCTION__, dataBuf, true);
Elliott Hughesa2501992011-08-26 19:39:54 -07001105
1106 if (mode != JNI_ABORT) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001107 size_t len = GuardedCopy::FromData(dataBuf)->original_length;
Ian Rogersa15e67d2012-02-28 13:51:55 -08001108 memcpy(a->GetRawData(a->GetClass()->GetComponentSize()), dataBuf, len);
Elliott Hughesa2501992011-08-26 19:39:54 -07001109 }
1110 if (mode != JNI_COMMIT) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001111 GuardedCopy::Destroy(dataBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -07001112 }
1113}
1114
1115/*
1116 * ===========================================================================
1117 * JNI functions
1118 * ===========================================================================
1119 */
1120
1121class CheckJNI {
1122 public:
1123 static jint GetVersion(JNIEnv* env) {
1124 CHECK_JNI_ENTRY(kFlag_Default, "E", env);
1125 return CHECK_JNI_EXIT("I", baseEnv(env)->GetVersion(env));
1126 }
1127
1128 static jclass DefineClass(JNIEnv* env, const char* name, jobject loader, const jbyte* buf, jsize bufLen) {
1129 CHECK_JNI_ENTRY(kFlag_Default, "EuLpz", env, name, loader, buf, bufLen);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001130 sc.CheckClassName(name);
Elliott Hughesa2501992011-08-26 19:39:54 -07001131 return CHECK_JNI_EXIT("c", baseEnv(env)->DefineClass(env, name, loader, buf, bufLen));
1132 }
1133
1134 static jclass FindClass(JNIEnv* env, const char* name) {
1135 CHECK_JNI_ENTRY(kFlag_Default, "Eu", env, name);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001136 sc.CheckClassName(name);
Elliott Hughesa2501992011-08-26 19:39:54 -07001137 return CHECK_JNI_EXIT("c", baseEnv(env)->FindClass(env, name));
1138 }
1139
Elliott Hughese84278b2012-03-22 10:06:53 -07001140 static jclass GetSuperclass(JNIEnv* env, jclass c) {
1141 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1142 return CHECK_JNI_EXIT("c", baseEnv(env)->GetSuperclass(env, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001143 }
1144
Elliott Hughese84278b2012-03-22 10:06:53 -07001145 static jboolean IsAssignableFrom(JNIEnv* env, jclass c1, jclass c2) {
1146 CHECK_JNI_ENTRY(kFlag_Default, "Ecc", env, c1, c2);
1147 return CHECK_JNI_EXIT("b", baseEnv(env)->IsAssignableFrom(env, c1, c2));
Elliott Hughesa2501992011-08-26 19:39:54 -07001148 }
1149
1150 static jmethodID FromReflectedMethod(JNIEnv* env, jobject method) {
1151 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, method);
1152 // TODO: check that 'field' is a java.lang.reflect.Method.
1153 return CHECK_JNI_EXIT("m", baseEnv(env)->FromReflectedMethod(env, method));
1154 }
1155
1156 static jfieldID FromReflectedField(JNIEnv* env, jobject field) {
1157 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, field);
1158 // TODO: check that 'field' is a java.lang.reflect.Field.
1159 return CHECK_JNI_EXIT("f", baseEnv(env)->FromReflectedField(env, field));
1160 }
1161
1162 static jobject ToReflectedMethod(JNIEnv* env, jclass cls, jmethodID mid, jboolean isStatic) {
1163 CHECK_JNI_ENTRY(kFlag_Default, "Ecmb", env, cls, mid, isStatic);
1164 return CHECK_JNI_EXIT("L", baseEnv(env)->ToReflectedMethod(env, cls, mid, isStatic));
1165 }
1166
1167 static jobject ToReflectedField(JNIEnv* env, jclass cls, jfieldID fid, jboolean isStatic) {
1168 CHECK_JNI_ENTRY(kFlag_Default, "Ecfb", env, cls, fid, isStatic);
1169 return CHECK_JNI_EXIT("L", baseEnv(env)->ToReflectedField(env, cls, fid, isStatic));
1170 }
1171
1172 static jint Throw(JNIEnv* env, jthrowable obj) {
1173 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1174 // TODO: check that 'obj' is a java.lang.Throwable.
1175 return CHECK_JNI_EXIT("I", baseEnv(env)->Throw(env, obj));
1176 }
1177
Elliott Hughese84278b2012-03-22 10:06:53 -07001178 static jint ThrowNew(JNIEnv* env, jclass c, const char* message) {
1179 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Ecu", env, c, message);
1180 return CHECK_JNI_EXIT("I", baseEnv(env)->ThrowNew(env, c, message));
Elliott Hughesa2501992011-08-26 19:39:54 -07001181 }
1182
1183 static jthrowable ExceptionOccurred(JNIEnv* env) {
1184 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1185 return CHECK_JNI_EXIT("L", baseEnv(env)->ExceptionOccurred(env));
1186 }
1187
1188 static void ExceptionDescribe(JNIEnv* env) {
1189 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1190 baseEnv(env)->ExceptionDescribe(env);
1191 CHECK_JNI_EXIT_VOID();
1192 }
1193
1194 static void ExceptionClear(JNIEnv* env) {
1195 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1196 baseEnv(env)->ExceptionClear(env);
1197 CHECK_JNI_EXIT_VOID();
1198 }
1199
1200 static void FatalError(JNIEnv* env, const char* msg) {
1201 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Eu", env, msg);
1202 baseEnv(env)->FatalError(env, msg);
1203 CHECK_JNI_EXIT_VOID();
1204 }
1205
1206 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
1207 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EI", env, capacity);
1208 return CHECK_JNI_EXIT("I", baseEnv(env)->PushLocalFrame(env, capacity));
1209 }
1210
1211 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
1212 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, res);
1213 return CHECK_JNI_EXIT("L", baseEnv(env)->PopLocalFrame(env, res));
1214 }
1215
1216 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
1217 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1218 return CHECK_JNI_EXIT("L", baseEnv(env)->NewGlobalRef(env, obj));
1219 }
1220
1221 static jobject NewLocalRef(JNIEnv* env, jobject ref) {
1222 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, ref);
1223 return CHECK_JNI_EXIT("L", baseEnv(env)->NewLocalRef(env, ref));
1224 }
1225
1226 static void DeleteGlobalRef(JNIEnv* env, jobject globalRef) {
1227 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, globalRef);
1228 if (globalRef != NULL && GetIndirectRefKind(globalRef) != kGlobal) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001229 JniAbortF(__FUNCTION__, "DeleteGlobalRef on %s: %p",
1230 ToStr<IndirectRefKind>(GetIndirectRefKind(globalRef)).c_str(), globalRef);
Elliott Hughesa2501992011-08-26 19:39:54 -07001231 } else {
1232 baseEnv(env)->DeleteGlobalRef(env, globalRef);
1233 CHECK_JNI_EXIT_VOID();
1234 }
1235 }
1236
1237 static void DeleteWeakGlobalRef(JNIEnv* env, jweak weakGlobalRef) {
1238 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, weakGlobalRef);
1239 if (weakGlobalRef != NULL && GetIndirectRefKind(weakGlobalRef) != kWeakGlobal) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001240 JniAbortF(__FUNCTION__, "DeleteWeakGlobalRef on %s: %p",
1241 ToStr<IndirectRefKind>(GetIndirectRefKind(weakGlobalRef)).c_str(), weakGlobalRef);
Elliott Hughesa2501992011-08-26 19:39:54 -07001242 } else {
1243 baseEnv(env)->DeleteWeakGlobalRef(env, weakGlobalRef);
1244 CHECK_JNI_EXIT_VOID();
1245 }
1246 }
1247
1248 static void DeleteLocalRef(JNIEnv* env, jobject localRef) {
1249 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, localRef);
Ian Rogers959f8ed2012-02-07 16:33:37 -08001250 if (localRef != NULL && GetIndirectRefKind(localRef) != kLocal && !IsSirtLocalRef(env, localRef)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001251 JniAbortF(__FUNCTION__, "DeleteLocalRef on %s: %p",
1252 ToStr<IndirectRefKind>(GetIndirectRefKind(localRef)).c_str(), localRef);
Elliott Hughesa2501992011-08-26 19:39:54 -07001253 } else {
1254 baseEnv(env)->DeleteLocalRef(env, localRef);
1255 CHECK_JNI_EXIT_VOID();
1256 }
1257 }
1258
1259 static jint EnsureLocalCapacity(JNIEnv *env, jint capacity) {
1260 CHECK_JNI_ENTRY(kFlag_Default, "EI", env, capacity);
1261 return CHECK_JNI_EXIT("I", baseEnv(env)->EnsureLocalCapacity(env, capacity));
1262 }
1263
1264 static jboolean IsSameObject(JNIEnv* env, jobject ref1, jobject ref2) {
1265 CHECK_JNI_ENTRY(kFlag_Default, "ELL", env, ref1, ref2);
1266 return CHECK_JNI_EXIT("b", baseEnv(env)->IsSameObject(env, ref1, ref2));
1267 }
1268
Elliott Hughese84278b2012-03-22 10:06:53 -07001269 static jobject AllocObject(JNIEnv* env, jclass c) {
1270 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1271 return CHECK_JNI_EXIT("L", baseEnv(env)->AllocObject(env, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001272 }
1273
Elliott Hughese84278b2012-03-22 10:06:53 -07001274 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
1275 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
Elliott Hughesa2501992011-08-26 19:39:54 -07001276 va_list args;
1277 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -07001278 jobject result = baseEnv(env)->NewObjectV(env, c, mid, args);
Elliott Hughesa2501992011-08-26 19:39:54 -07001279 va_end(args);
1280 return CHECK_JNI_EXIT("L", result);
1281 }
1282
Elliott Hughese84278b2012-03-22 10:06:53 -07001283 static jobject NewObjectV(JNIEnv* env, jclass c, jmethodID mid, va_list args) {
1284 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
1285 return CHECK_JNI_EXIT("L", baseEnv(env)->NewObjectV(env, c, mid, args));
Elliott Hughesa2501992011-08-26 19:39:54 -07001286 }
1287
Elliott Hughese84278b2012-03-22 10:06:53 -07001288 static jobject NewObjectA(JNIEnv* env, jclass c, jmethodID mid, jvalue* args) {
1289 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
1290 return CHECK_JNI_EXIT("L", baseEnv(env)->NewObjectA(env, c, mid, args));
Elliott Hughesa2501992011-08-26 19:39:54 -07001291 }
1292
1293 static jclass GetObjectClass(JNIEnv* env, jobject obj) {
1294 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1295 return CHECK_JNI_EXIT("c", baseEnv(env)->GetObjectClass(env, obj));
1296 }
1297
Elliott Hughese84278b2012-03-22 10:06:53 -07001298 static jboolean IsInstanceOf(JNIEnv* env, jobject obj, jclass c) {
1299 CHECK_JNI_ENTRY(kFlag_Default, "ELc", env, obj, c);
1300 return CHECK_JNI_EXIT("b", baseEnv(env)->IsInstanceOf(env, obj, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001301 }
1302
Elliott Hughese84278b2012-03-22 10:06:53 -07001303 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1304 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1305 return CHECK_JNI_EXIT("m", baseEnv(env)->GetMethodID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001306 }
1307
Elliott Hughese84278b2012-03-22 10:06:53 -07001308 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1309 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1310 return CHECK_JNI_EXIT("f", baseEnv(env)->GetFieldID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001311 }
1312
Elliott Hughese84278b2012-03-22 10:06:53 -07001313 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1314 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1315 return CHECK_JNI_EXIT("m", baseEnv(env)->GetStaticMethodID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001316 }
1317
Elliott Hughese84278b2012-03-22 10:06:53 -07001318 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1319 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1320 return CHECK_JNI_EXIT("f", baseEnv(env)->GetStaticFieldID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001321 }
1322
1323#define FIELD_ACCESSORS(_ctype, _jname, _type) \
Elliott Hughese84278b2012-03-22 10:06:53 -07001324 static _ctype GetStatic##_jname##Field(JNIEnv* env, jclass c, jfieldID fid) { \
1325 CHECK_JNI_ENTRY(kFlag_Default, "Ecf", env, c, fid); \
1326 sc.CheckStaticFieldID(c, fid); \
1327 return CHECK_JNI_EXIT(_type, baseEnv(env)->GetStatic##_jname##Field(env, c, fid)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001328 } \
1329 static _ctype Get##_jname##Field(JNIEnv* env, jobject obj, jfieldID fid) { \
1330 CHECK_JNI_ENTRY(kFlag_Default, "ELf", env, obj, fid); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001331 sc.CheckInstanceFieldID(obj, fid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001332 return CHECK_JNI_EXIT(_type, baseEnv(env)->Get##_jname##Field(env, obj, fid)); \
1333 } \
Elliott Hughese84278b2012-03-22 10:06:53 -07001334 static void SetStatic##_jname##Field(JNIEnv* env, jclass c, jfieldID fid, _ctype value) { \
1335 CHECK_JNI_ENTRY(kFlag_Default, "Ecf" _type, env, c, fid, value); \
1336 sc.CheckStaticFieldID(c, fid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001337 /* "value" arg only used when type == ref */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001338 sc.CheckFieldType((jobject)(uint32_t)value, fid, _type[0], true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001339 baseEnv(env)->SetStatic##_jname##Field(env, c, fid, value); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001340 CHECK_JNI_EXIT_VOID(); \
1341 } \
1342 static void Set##_jname##Field(JNIEnv* env, jobject obj, jfieldID fid, _ctype value) { \
1343 CHECK_JNI_ENTRY(kFlag_Default, "ELf" _type, env, obj, fid, value); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001344 sc.CheckInstanceFieldID(obj, fid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001345 /* "value" arg only used when type == ref */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001346 sc.CheckFieldType((jobject)(uint32_t) value, fid, _type[0], false); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001347 baseEnv(env)->Set##_jname##Field(env, obj, fid, value); \
1348 CHECK_JNI_EXIT_VOID(); \
1349 }
1350
1351FIELD_ACCESSORS(jobject, Object, "L");
1352FIELD_ACCESSORS(jboolean, Boolean, "Z");
1353FIELD_ACCESSORS(jbyte, Byte, "B");
1354FIELD_ACCESSORS(jchar, Char, "C");
1355FIELD_ACCESSORS(jshort, Short, "S");
1356FIELD_ACCESSORS(jint, Int, "I");
1357FIELD_ACCESSORS(jlong, Long, "J");
1358FIELD_ACCESSORS(jfloat, Float, "F");
1359FIELD_ACCESSORS(jdouble, Double, "D");
1360
1361#define CALL(_ctype, _jname, _retdecl, _retasgn, _retok, _retsig) \
1362 /* Virtual... */ \
1363 static _ctype Call##_jname##Method(JNIEnv* env, jobject obj, \
1364 jmethodID mid, ...) \
1365 { \
1366 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001367 sc.CheckSig(mid, _retsig, false); \
1368 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001369 _retdecl; \
1370 va_list args; \
1371 va_start(args, mid); \
Elliott Hughesba8eee12012-01-24 20:25:24 -08001372 _retasgn(baseEnv(env)->Call##_jname##MethodV(env, obj, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001373 va_end(args); \
1374 _retok; \
1375 } \
1376 static _ctype Call##_jname##MethodV(JNIEnv* env, jobject obj, \
1377 jmethodID mid, va_list args) \
1378 { \
1379 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001380 sc.CheckSig(mid, _retsig, false); \
1381 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001382 _retdecl; \
Elliott Hughesba8eee12012-01-24 20:25:24 -08001383 _retasgn(baseEnv(env)->Call##_jname##MethodV(env, obj, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001384 _retok; \
1385 } \
1386 static _ctype Call##_jname##MethodA(JNIEnv* env, jobject obj, \
1387 jmethodID mid, jvalue* args) \
1388 { \
1389 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001390 sc.CheckSig(mid, _retsig, false); \
1391 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001392 _retdecl; \
Elliott Hughesba8eee12012-01-24 20:25:24 -08001393 _retasgn(baseEnv(env)->Call##_jname##MethodA(env, obj, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001394 _retok; \
1395 } \
1396 /* Non-virtual... */ \
1397 static _ctype CallNonvirtual##_jname##Method(JNIEnv* env, \
Elliott Hughese84278b2012-03-22 10:06:53 -07001398 jobject obj, jclass c, jmethodID mid, ...) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001399 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001400 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001401 sc.CheckSig(mid, _retsig, false); \
1402 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001403 _retdecl; \
1404 va_list args; \
1405 va_start(args, mid); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001406 _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodV(env, obj, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001407 va_end(args); \
1408 _retok; \
1409 } \
1410 static _ctype CallNonvirtual##_jname##MethodV(JNIEnv* env, \
Elliott Hughese84278b2012-03-22 10:06:53 -07001411 jobject obj, jclass c, jmethodID mid, va_list args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001412 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001413 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001414 sc.CheckSig(mid, _retsig, false); \
1415 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001416 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001417 _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodV(env, obj, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001418 _retok; \
1419 } \
1420 static _ctype CallNonvirtual##_jname##MethodA(JNIEnv* env, \
Elliott Hughese84278b2012-03-22 10:06:53 -07001421 jobject obj, jclass c, jmethodID mid, jvalue* args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001422 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001423 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001424 sc.CheckSig(mid, _retsig, false); \
1425 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001426 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001427 _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodA(env, obj, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001428 _retok; \
1429 } \
1430 /* Static... */ \
Elliott Hughese84278b2012-03-22 10:06:53 -07001431 static _ctype CallStatic##_jname##Method(JNIEnv* env, jclass c, jmethodID mid, ...) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001432 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001433 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001434 sc.CheckSig(mid, _retsig, true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001435 sc.CheckStaticMethod(c, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001436 _retdecl; \
1437 va_list args; \
1438 va_start(args, mid); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001439 _retasgn(baseEnv(env)->CallStatic##_jname##MethodV(env, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001440 va_end(args); \
1441 _retok; \
1442 } \
Elliott Hughese84278b2012-03-22 10:06:53 -07001443 static _ctype CallStatic##_jname##MethodV(JNIEnv* env, jclass c, jmethodID mid, va_list args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001444 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001445 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001446 sc.CheckSig(mid, _retsig, true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001447 sc.CheckStaticMethod(c, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001448 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001449 _retasgn(baseEnv(env)->CallStatic##_jname##MethodV(env, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001450 _retok; \
1451 } \
Elliott Hughese84278b2012-03-22 10:06:53 -07001452 static _ctype CallStatic##_jname##MethodA(JNIEnv* env, jclass c, jmethodID mid, jvalue* args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001453 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001454 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001455 sc.CheckSig(mid, _retsig, true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001456 sc.CheckStaticMethod(c, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001457 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001458 _retasgn(baseEnv(env)->CallStatic##_jname##MethodA(env, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001459 _retok; \
1460 }
1461
1462#define NON_VOID_RETURN(_retsig, _ctype) return CHECK_JNI_EXIT(_retsig, (_ctype) result)
1463#define VOID_RETURN CHECK_JNI_EXIT_VOID()
1464
Elliott Hughesba8eee12012-01-24 20:25:24 -08001465CALL(jobject, Object, Object* result, result = reinterpret_cast<Object*>, NON_VOID_RETURN("L", jobject), "L");
1466CALL(jboolean, Boolean, jboolean result, result =, NON_VOID_RETURN("Z", jboolean), "Z");
1467CALL(jbyte, Byte, jbyte result, result =, NON_VOID_RETURN("B", jbyte), "B");
1468CALL(jchar, Char, jchar result, result =, NON_VOID_RETURN("C", jchar), "C");
1469CALL(jshort, Short, jshort result, result =, NON_VOID_RETURN("S", jshort), "S");
1470CALL(jint, Int, jint result, result =, NON_VOID_RETURN("I", jint), "I");
1471CALL(jlong, Long, jlong result, result =, NON_VOID_RETURN("J", jlong), "J");
1472CALL(jfloat, Float, jfloat result, result =, NON_VOID_RETURN("F", jfloat), "F");
1473CALL(jdouble, Double, jdouble result, result =, NON_VOID_RETURN("D", jdouble), "D");
Elliott Hughesa2501992011-08-26 19:39:54 -07001474CALL(void, Void, , , VOID_RETURN, "V");
1475
1476 static jstring NewString(JNIEnv* env, const jchar* unicodeChars, jsize len) {
1477 CHECK_JNI_ENTRY(kFlag_Default, "Epz", env, unicodeChars, len);
1478 return CHECK_JNI_EXIT("s", baseEnv(env)->NewString(env, unicodeChars, len));
1479 }
1480
1481 static jsize GetStringLength(JNIEnv* env, jstring string) {
1482 CHECK_JNI_ENTRY(kFlag_CritOkay, "Es", env, string);
1483 return CHECK_JNI_EXIT("I", baseEnv(env)->GetStringLength(env, string));
1484 }
1485
1486 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* isCopy) {
1487 CHECK_JNI_ENTRY(kFlag_CritOkay, "Esp", env, java_string, isCopy);
1488 const jchar* result = baseEnv(env)->GetStringChars(env, java_string, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001489 if (sc.ForceCopy() && result != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001490 String* s = sc.soa().Decode<String*>(java_string);
Elliott Hughesa2501992011-08-26 19:39:54 -07001491 int byteCount = s->GetLength() * 2;
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001492 result = (const jchar*) GuardedCopy::Create(result, byteCount, false);
Elliott Hughesa2501992011-08-26 19:39:54 -07001493 if (isCopy != NULL) {
1494 *isCopy = JNI_TRUE;
1495 }
1496 }
1497 return CHECK_JNI_EXIT("p", result);
1498 }
1499
1500 static void ReleaseStringChars(JNIEnv* env, jstring string, const jchar* chars) {
1501 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "Esp", env, string, chars);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001502 sc.CheckNonNull(chars);
1503 if (sc.ForceCopy()) {
1504 GuardedCopy::Check(__FUNCTION__, chars, false);
Elliott Hughesba8eee12012-01-24 20:25:24 -08001505 chars = reinterpret_cast<const jchar*>(GuardedCopy::Destroy(const_cast<jchar*>(chars)));
Elliott Hughesa2501992011-08-26 19:39:54 -07001506 }
1507 baseEnv(env)->ReleaseStringChars(env, string, chars);
1508 CHECK_JNI_EXIT_VOID();
1509 }
1510
1511 static jstring NewStringUTF(JNIEnv* env, const char* bytes) {
1512 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Eu", env, bytes); // TODO: show pointer and truncate string.
1513 return CHECK_JNI_EXIT("s", baseEnv(env)->NewStringUTF(env, bytes));
1514 }
1515
1516 static jsize GetStringUTFLength(JNIEnv* env, jstring string) {
1517 CHECK_JNI_ENTRY(kFlag_CritOkay, "Es", env, string);
1518 return CHECK_JNI_EXIT("I", baseEnv(env)->GetStringUTFLength(env, string));
1519 }
1520
1521 static const char* GetStringUTFChars(JNIEnv* env, jstring string, jboolean* isCopy) {
1522 CHECK_JNI_ENTRY(kFlag_CritOkay, "Esp", env, string, isCopy);
1523 const char* result = baseEnv(env)->GetStringUTFChars(env, string, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001524 if (sc.ForceCopy() && result != NULL) {
1525 result = (const char*) GuardedCopy::Create(result, strlen(result) + 1, false);
Elliott Hughesa2501992011-08-26 19:39:54 -07001526 if (isCopy != NULL) {
1527 *isCopy = JNI_TRUE;
1528 }
1529 }
1530 return CHECK_JNI_EXIT("u", result); // TODO: show pointer and truncate string.
1531 }
1532
1533 static void ReleaseStringUTFChars(JNIEnv* env, jstring string, const char* utf) {
1534 CHECK_JNI_ENTRY(kFlag_ExcepOkay | kFlag_Release, "Esu", env, string, utf); // TODO: show pointer and truncate string.
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001535 if (sc.ForceCopy()) {
1536 GuardedCopy::Check(__FUNCTION__, utf, false);
Elliott Hughesba8eee12012-01-24 20:25:24 -08001537 utf = reinterpret_cast<const char*>(GuardedCopy::Destroy(const_cast<char*>(utf)));
Elliott Hughesa2501992011-08-26 19:39:54 -07001538 }
1539 baseEnv(env)->ReleaseStringUTFChars(env, string, utf);
1540 CHECK_JNI_EXIT_VOID();
1541 }
1542
1543 static jsize GetArrayLength(JNIEnv* env, jarray array) {
1544 CHECK_JNI_ENTRY(kFlag_CritOkay, "Ea", env, array);
1545 return CHECK_JNI_EXIT("I", baseEnv(env)->GetArrayLength(env, array));
1546 }
1547
1548 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass elementClass, jobject initialElement) {
1549 CHECK_JNI_ENTRY(kFlag_Default, "EzcL", env, length, elementClass, initialElement);
1550 return CHECK_JNI_EXIT("a", baseEnv(env)->NewObjectArray(env, length, elementClass, initialElement));
1551 }
1552
1553 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index) {
1554 CHECK_JNI_ENTRY(kFlag_Default, "EaI", env, array, index);
1555 return CHECK_JNI_EXIT("L", baseEnv(env)->GetObjectArrayElement(env, array, index));
1556 }
1557
1558 static void SetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index, jobject value) {
1559 CHECK_JNI_ENTRY(kFlag_Default, "EaIL", env, array, index, value);
1560 baseEnv(env)->SetObjectArrayElement(env, array, index, value);
1561 CHECK_JNI_EXIT_VOID();
1562 }
1563
1564#define NEW_PRIMITIVE_ARRAY(_artype, _jname) \
1565 static _artype New##_jname##Array(JNIEnv* env, jsize length) { \
1566 CHECK_JNI_ENTRY(kFlag_Default, "Ez", env, length); \
1567 return CHECK_JNI_EXIT("a", baseEnv(env)->New##_jname##Array(env, length)); \
1568 }
1569NEW_PRIMITIVE_ARRAY(jbooleanArray, Boolean);
1570NEW_PRIMITIVE_ARRAY(jbyteArray, Byte);
1571NEW_PRIMITIVE_ARRAY(jcharArray, Char);
1572NEW_PRIMITIVE_ARRAY(jshortArray, Short);
1573NEW_PRIMITIVE_ARRAY(jintArray, Int);
1574NEW_PRIMITIVE_ARRAY(jlongArray, Long);
1575NEW_PRIMITIVE_ARRAY(jfloatArray, Float);
1576NEW_PRIMITIVE_ARRAY(jdoubleArray, Double);
1577
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001578struct ForceCopyGetChecker {
Elliott Hughesba8eee12012-01-24 20:25:24 -08001579 public:
Elliott Hughesa2501992011-08-26 19:39:54 -07001580 ForceCopyGetChecker(ScopedCheck& sc, jboolean* isCopy) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001581 force_copy = sc.ForceCopy();
1582 no_copy = 0;
1583 if (force_copy && isCopy != NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001584 // Capture this before the base call tramples on it.
Elliott Hughesba8eee12012-01-24 20:25:24 -08001585 no_copy = *reinterpret_cast<uint32_t*>(isCopy);
Elliott Hughesa2501992011-08-26 19:39:54 -07001586 }
1587 }
1588
1589 template<typename ResultT>
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001590 ResultT Check(JNIEnv* env, jarray array, jboolean* isCopy, ResultT result) {
1591 if (force_copy && result != NULL) {
1592 if (no_copy != kNoCopyMagic) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001593 result = reinterpret_cast<ResultT>(CreateGuardedPACopy(env, array, isCopy));
1594 }
1595 }
1596 return result;
1597 }
1598
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001599 uint32_t no_copy;
1600 bool force_copy;
Elliott Hughesa2501992011-08-26 19:39:54 -07001601};
1602
1603#define GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \
1604 static _ctype* Get##_jname##ArrayElements(JNIEnv* env, _ctype##Array array, jboolean* isCopy) { \
1605 CHECK_JNI_ENTRY(kFlag_Default, "Eap", env, array, isCopy); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001606 _ctype* result = ForceCopyGetChecker(sc, isCopy).Check(env, array, isCopy, baseEnv(env)->Get##_jname##ArrayElements(env, array, isCopy)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001607 return CHECK_JNI_EXIT("p", result); \
1608 }
1609
1610#define RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \
1611 static void Release##_jname##ArrayElements(JNIEnv* env, _ctype##Array array, _ctype* elems, jint mode) { \
1612 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "Eapr", env, array, elems, mode); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001613 sc.CheckNonNull(elems); \
1614 if (sc.ForceCopy()) { \
Elliott Hughesa2501992011-08-26 19:39:54 -07001615 ReleaseGuardedPACopy(env, array, elems, mode); \
1616 } \
1617 baseEnv(env)->Release##_jname##ArrayElements(env, array, elems, mode); \
1618 CHECK_JNI_EXIT_VOID(); \
1619 }
1620
1621#define GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \
1622 static void Get##_jname##ArrayRegion(JNIEnv* env, _ctype##Array array, jsize start, jsize len, _ctype* buf) { \
1623 CHECK_JNI_ENTRY(kFlag_Default, "EaIIp", env, array, start, len, buf); \
1624 baseEnv(env)->Get##_jname##ArrayRegion(env, array, start, len, buf); \
1625 CHECK_JNI_EXIT_VOID(); \
1626 }
1627
1628#define SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \
1629 static void Set##_jname##ArrayRegion(JNIEnv* env, _ctype##Array array, jsize start, jsize len, const _ctype* buf) { \
1630 CHECK_JNI_ENTRY(kFlag_Default, "EaIIp", env, array, start, len, buf); \
1631 baseEnv(env)->Set##_jname##ArrayRegion(env, array, start, len, buf); \
1632 CHECK_JNI_EXIT_VOID(); \
1633 }
1634
1635#define PRIMITIVE_ARRAY_FUNCTIONS(_ctype, _jname, _typechar) \
1636 GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \
1637 RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \
1638 GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname); \
1639 SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname);
1640
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001641// TODO: verify primitive array type matches call type.
Elliott Hughesa2501992011-08-26 19:39:54 -07001642PRIMITIVE_ARRAY_FUNCTIONS(jboolean, Boolean, 'Z');
1643PRIMITIVE_ARRAY_FUNCTIONS(jbyte, Byte, 'B');
1644PRIMITIVE_ARRAY_FUNCTIONS(jchar, Char, 'C');
1645PRIMITIVE_ARRAY_FUNCTIONS(jshort, Short, 'S');
1646PRIMITIVE_ARRAY_FUNCTIONS(jint, Int, 'I');
1647PRIMITIVE_ARRAY_FUNCTIONS(jlong, Long, 'J');
1648PRIMITIVE_ARRAY_FUNCTIONS(jfloat, Float, 'F');
1649PRIMITIVE_ARRAY_FUNCTIONS(jdouble, Double, 'D');
1650
Elliott Hughese84278b2012-03-22 10:06:53 -07001651 static jint RegisterNatives(JNIEnv* env, jclass c, const JNINativeMethod* methods, jint nMethods) {
1652 CHECK_JNI_ENTRY(kFlag_Default, "EcpI", env, c, methods, nMethods);
1653 return CHECK_JNI_EXIT("I", baseEnv(env)->RegisterNatives(env, c, methods, nMethods));
Elliott Hughesa2501992011-08-26 19:39:54 -07001654 }
1655
Elliott Hughese84278b2012-03-22 10:06:53 -07001656 static jint UnregisterNatives(JNIEnv* env, jclass c) {
1657 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1658 return CHECK_JNI_EXIT("I", baseEnv(env)->UnregisterNatives(env, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001659 }
1660
1661 static jint MonitorEnter(JNIEnv* env, jobject obj) {
1662 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
Elliott Hughesa92853e2012-02-07 16:09:27 -08001663 if (!sc.CheckInstance(ScopedCheck::kObject, obj)) {
1664 return JNI_ERR; // Only for jni_internal_test. Real code will have aborted already.
1665 }
Elliott Hughesa2501992011-08-26 19:39:54 -07001666 return CHECK_JNI_EXIT("I", baseEnv(env)->MonitorEnter(env, obj));
1667 }
1668
1669 static jint MonitorExit(JNIEnv* env, jobject obj) {
1670 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, obj);
Elliott Hughesa92853e2012-02-07 16:09:27 -08001671 if (!sc.CheckInstance(ScopedCheck::kObject, obj)) {
1672 return JNI_ERR; // Only for jni_internal_test. Real code will have aborted already.
1673 }
Elliott Hughesa2501992011-08-26 19:39:54 -07001674 return CHECK_JNI_EXIT("I", baseEnv(env)->MonitorExit(env, obj));
1675 }
1676
1677 static jint GetJavaVM(JNIEnv *env, JavaVM **vm) {
1678 CHECK_JNI_ENTRY(kFlag_Default, "Ep", env, vm);
1679 return CHECK_JNI_EXIT("I", baseEnv(env)->GetJavaVM(env, vm));
1680 }
1681
1682 static void GetStringRegion(JNIEnv* env, jstring str, jsize start, jsize len, jchar* buf) {
1683 CHECK_JNI_ENTRY(kFlag_CritOkay, "EsIIp", env, str, start, len, buf);
1684 baseEnv(env)->GetStringRegion(env, str, start, len, buf);
1685 CHECK_JNI_EXIT_VOID();
1686 }
1687
1688 static void GetStringUTFRegion(JNIEnv* env, jstring str, jsize start, jsize len, char* buf) {
1689 CHECK_JNI_ENTRY(kFlag_CritOkay, "EsIIp", env, str, start, len, buf);
1690 baseEnv(env)->GetStringUTFRegion(env, str, start, len, buf);
1691 CHECK_JNI_EXIT_VOID();
1692 }
1693
1694 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* isCopy) {
1695 CHECK_JNI_ENTRY(kFlag_CritGet, "Eap", env, array, isCopy);
1696 void* result = baseEnv(env)->GetPrimitiveArrayCritical(env, array, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001697 if (sc.ForceCopy() && result != NULL) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001698 result = CreateGuardedPACopy(env, array, isCopy);
1699 }
1700 return CHECK_JNI_EXIT("p", result);
1701 }
1702
1703 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* carray, jint mode) {
1704 CHECK_JNI_ENTRY(kFlag_CritRelease | kFlag_ExcepOkay, "Eapr", env, array, carray, mode);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001705 sc.CheckNonNull(carray);
1706 if (sc.ForceCopy()) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001707 ReleaseGuardedPACopy(env, array, carray, mode);
1708 }
1709 baseEnv(env)->ReleasePrimitiveArrayCritical(env, array, carray, mode);
1710 CHECK_JNI_EXIT_VOID();
1711 }
1712
1713 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* isCopy) {
1714 CHECK_JNI_ENTRY(kFlag_CritGet, "Esp", env, java_string, isCopy);
1715 const jchar* result = baseEnv(env)->GetStringCritical(env, java_string, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001716 if (sc.ForceCopy() && result != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001717 String* s = sc.soa().Decode<String*>(java_string);
Elliott Hughesa2501992011-08-26 19:39:54 -07001718 int byteCount = s->GetLength() * 2;
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001719 result = (const jchar*) GuardedCopy::Create(result, byteCount, false);
Elliott Hughesa2501992011-08-26 19:39:54 -07001720 if (isCopy != NULL) {
1721 *isCopy = JNI_TRUE;
1722 }
1723 }
1724 return CHECK_JNI_EXIT("p", result);
1725 }
1726
1727 static void ReleaseStringCritical(JNIEnv* env, jstring string, const jchar* carray) {
1728 CHECK_JNI_ENTRY(kFlag_CritRelease | kFlag_ExcepOkay, "Esp", env, string, carray);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001729 sc.CheckNonNull(carray);
1730 if (sc.ForceCopy()) {
1731 GuardedCopy::Check(__FUNCTION__, carray, false);
Elliott Hughesba8eee12012-01-24 20:25:24 -08001732 carray = reinterpret_cast<const jchar*>(GuardedCopy::Destroy(const_cast<jchar*>(carray)));
Elliott Hughesa2501992011-08-26 19:39:54 -07001733 }
1734 baseEnv(env)->ReleaseStringCritical(env, string, carray);
1735 CHECK_JNI_EXIT_VOID();
1736 }
1737
1738 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
1739 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1740 return CHECK_JNI_EXIT("L", baseEnv(env)->NewWeakGlobalRef(env, obj));
1741 }
1742
1743 static jboolean ExceptionCheck(JNIEnv* env) {
1744 CHECK_JNI_ENTRY(kFlag_CritOkay | kFlag_ExcepOkay, "E", env);
1745 return CHECK_JNI_EXIT("b", baseEnv(env)->ExceptionCheck(env));
1746 }
1747
1748 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject obj) {
1749 // Note: we use "Ep" rather than "EL" because this is the one JNI function
1750 // that it's okay to pass an invalid reference to.
1751 CHECK_JNI_ENTRY(kFlag_Default, "Ep", env, obj);
1752 // TODO: proper decoding of jobjectRefType!
1753 return CHECK_JNI_EXIT("I", baseEnv(env)->GetObjectRefType(env, obj));
1754 }
1755
1756 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
1757 CHECK_JNI_ENTRY(kFlag_Default, "EpJ", env, address, capacity);
1758 if (address == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001759 JniAbortF(__FUNCTION__, "non-nullable address is NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -07001760 }
1761 if (capacity <= 0) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001762 JniAbortF(__FUNCTION__, "capacity must be greater than 0: %d", capacity);
Elliott Hughesa2501992011-08-26 19:39:54 -07001763 }
1764 return CHECK_JNI_EXIT("L", baseEnv(env)->NewDirectByteBuffer(env, address, capacity));
1765 }
1766
1767 static void* GetDirectBufferAddress(JNIEnv* env, jobject buf) {
1768 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, buf);
1769 // TODO: check that 'buf' is a java.nio.Buffer.
1770 return CHECK_JNI_EXIT("p", baseEnv(env)->GetDirectBufferAddress(env, buf));
1771 }
1772
1773 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject buf) {
1774 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, buf);
1775 // TODO: check that 'buf' is a java.nio.Buffer.
1776 return CHECK_JNI_EXIT("J", baseEnv(env)->GetDirectBufferCapacity(env, buf));
1777 }
1778
1779 private:
1780 static inline const JNINativeInterface* baseEnv(JNIEnv* env) {
1781 return reinterpret_cast<JNIEnvExt*>(env)->unchecked_functions;
1782 }
1783};
1784
1785const JNINativeInterface gCheckNativeInterface = {
1786 NULL, // reserved0.
1787 NULL, // reserved1.
1788 NULL, // reserved2.
1789 NULL, // reserved3.
1790 CheckJNI::GetVersion,
1791 CheckJNI::DefineClass,
1792 CheckJNI::FindClass,
1793 CheckJNI::FromReflectedMethod,
1794 CheckJNI::FromReflectedField,
1795 CheckJNI::ToReflectedMethod,
1796 CheckJNI::GetSuperclass,
1797 CheckJNI::IsAssignableFrom,
1798 CheckJNI::ToReflectedField,
1799 CheckJNI::Throw,
1800 CheckJNI::ThrowNew,
1801 CheckJNI::ExceptionOccurred,
1802 CheckJNI::ExceptionDescribe,
1803 CheckJNI::ExceptionClear,
1804 CheckJNI::FatalError,
1805 CheckJNI::PushLocalFrame,
1806 CheckJNI::PopLocalFrame,
1807 CheckJNI::NewGlobalRef,
1808 CheckJNI::DeleteGlobalRef,
1809 CheckJNI::DeleteLocalRef,
1810 CheckJNI::IsSameObject,
1811 CheckJNI::NewLocalRef,
1812 CheckJNI::EnsureLocalCapacity,
1813 CheckJNI::AllocObject,
1814 CheckJNI::NewObject,
1815 CheckJNI::NewObjectV,
1816 CheckJNI::NewObjectA,
1817 CheckJNI::GetObjectClass,
1818 CheckJNI::IsInstanceOf,
1819 CheckJNI::GetMethodID,
1820 CheckJNI::CallObjectMethod,
1821 CheckJNI::CallObjectMethodV,
1822 CheckJNI::CallObjectMethodA,
1823 CheckJNI::CallBooleanMethod,
1824 CheckJNI::CallBooleanMethodV,
1825 CheckJNI::CallBooleanMethodA,
1826 CheckJNI::CallByteMethod,
1827 CheckJNI::CallByteMethodV,
1828 CheckJNI::CallByteMethodA,
1829 CheckJNI::CallCharMethod,
1830 CheckJNI::CallCharMethodV,
1831 CheckJNI::CallCharMethodA,
1832 CheckJNI::CallShortMethod,
1833 CheckJNI::CallShortMethodV,
1834 CheckJNI::CallShortMethodA,
1835 CheckJNI::CallIntMethod,
1836 CheckJNI::CallIntMethodV,
1837 CheckJNI::CallIntMethodA,
1838 CheckJNI::CallLongMethod,
1839 CheckJNI::CallLongMethodV,
1840 CheckJNI::CallLongMethodA,
1841 CheckJNI::CallFloatMethod,
1842 CheckJNI::CallFloatMethodV,
1843 CheckJNI::CallFloatMethodA,
1844 CheckJNI::CallDoubleMethod,
1845 CheckJNI::CallDoubleMethodV,
1846 CheckJNI::CallDoubleMethodA,
1847 CheckJNI::CallVoidMethod,
1848 CheckJNI::CallVoidMethodV,
1849 CheckJNI::CallVoidMethodA,
1850 CheckJNI::CallNonvirtualObjectMethod,
1851 CheckJNI::CallNonvirtualObjectMethodV,
1852 CheckJNI::CallNonvirtualObjectMethodA,
1853 CheckJNI::CallNonvirtualBooleanMethod,
1854 CheckJNI::CallNonvirtualBooleanMethodV,
1855 CheckJNI::CallNonvirtualBooleanMethodA,
1856 CheckJNI::CallNonvirtualByteMethod,
1857 CheckJNI::CallNonvirtualByteMethodV,
1858 CheckJNI::CallNonvirtualByteMethodA,
1859 CheckJNI::CallNonvirtualCharMethod,
1860 CheckJNI::CallNonvirtualCharMethodV,
1861 CheckJNI::CallNonvirtualCharMethodA,
1862 CheckJNI::CallNonvirtualShortMethod,
1863 CheckJNI::CallNonvirtualShortMethodV,
1864 CheckJNI::CallNonvirtualShortMethodA,
1865 CheckJNI::CallNonvirtualIntMethod,
1866 CheckJNI::CallNonvirtualIntMethodV,
1867 CheckJNI::CallNonvirtualIntMethodA,
1868 CheckJNI::CallNonvirtualLongMethod,
1869 CheckJNI::CallNonvirtualLongMethodV,
1870 CheckJNI::CallNonvirtualLongMethodA,
1871 CheckJNI::CallNonvirtualFloatMethod,
1872 CheckJNI::CallNonvirtualFloatMethodV,
1873 CheckJNI::CallNonvirtualFloatMethodA,
1874 CheckJNI::CallNonvirtualDoubleMethod,
1875 CheckJNI::CallNonvirtualDoubleMethodV,
1876 CheckJNI::CallNonvirtualDoubleMethodA,
1877 CheckJNI::CallNonvirtualVoidMethod,
1878 CheckJNI::CallNonvirtualVoidMethodV,
1879 CheckJNI::CallNonvirtualVoidMethodA,
1880 CheckJNI::GetFieldID,
1881 CheckJNI::GetObjectField,
1882 CheckJNI::GetBooleanField,
1883 CheckJNI::GetByteField,
1884 CheckJNI::GetCharField,
1885 CheckJNI::GetShortField,
1886 CheckJNI::GetIntField,
1887 CheckJNI::GetLongField,
1888 CheckJNI::GetFloatField,
1889 CheckJNI::GetDoubleField,
1890 CheckJNI::SetObjectField,
1891 CheckJNI::SetBooleanField,
1892 CheckJNI::SetByteField,
1893 CheckJNI::SetCharField,
1894 CheckJNI::SetShortField,
1895 CheckJNI::SetIntField,
1896 CheckJNI::SetLongField,
1897 CheckJNI::SetFloatField,
1898 CheckJNI::SetDoubleField,
1899 CheckJNI::GetStaticMethodID,
1900 CheckJNI::CallStaticObjectMethod,
1901 CheckJNI::CallStaticObjectMethodV,
1902 CheckJNI::CallStaticObjectMethodA,
1903 CheckJNI::CallStaticBooleanMethod,
1904 CheckJNI::CallStaticBooleanMethodV,
1905 CheckJNI::CallStaticBooleanMethodA,
1906 CheckJNI::CallStaticByteMethod,
1907 CheckJNI::CallStaticByteMethodV,
1908 CheckJNI::CallStaticByteMethodA,
1909 CheckJNI::CallStaticCharMethod,
1910 CheckJNI::CallStaticCharMethodV,
1911 CheckJNI::CallStaticCharMethodA,
1912 CheckJNI::CallStaticShortMethod,
1913 CheckJNI::CallStaticShortMethodV,
1914 CheckJNI::CallStaticShortMethodA,
1915 CheckJNI::CallStaticIntMethod,
1916 CheckJNI::CallStaticIntMethodV,
1917 CheckJNI::CallStaticIntMethodA,
1918 CheckJNI::CallStaticLongMethod,
1919 CheckJNI::CallStaticLongMethodV,
1920 CheckJNI::CallStaticLongMethodA,
1921 CheckJNI::CallStaticFloatMethod,
1922 CheckJNI::CallStaticFloatMethodV,
1923 CheckJNI::CallStaticFloatMethodA,
1924 CheckJNI::CallStaticDoubleMethod,
1925 CheckJNI::CallStaticDoubleMethodV,
1926 CheckJNI::CallStaticDoubleMethodA,
1927 CheckJNI::CallStaticVoidMethod,
1928 CheckJNI::CallStaticVoidMethodV,
1929 CheckJNI::CallStaticVoidMethodA,
1930 CheckJNI::GetStaticFieldID,
1931 CheckJNI::GetStaticObjectField,
1932 CheckJNI::GetStaticBooleanField,
1933 CheckJNI::GetStaticByteField,
1934 CheckJNI::GetStaticCharField,
1935 CheckJNI::GetStaticShortField,
1936 CheckJNI::GetStaticIntField,
1937 CheckJNI::GetStaticLongField,
1938 CheckJNI::GetStaticFloatField,
1939 CheckJNI::GetStaticDoubleField,
1940 CheckJNI::SetStaticObjectField,
1941 CheckJNI::SetStaticBooleanField,
1942 CheckJNI::SetStaticByteField,
1943 CheckJNI::SetStaticCharField,
1944 CheckJNI::SetStaticShortField,
1945 CheckJNI::SetStaticIntField,
1946 CheckJNI::SetStaticLongField,
1947 CheckJNI::SetStaticFloatField,
1948 CheckJNI::SetStaticDoubleField,
1949 CheckJNI::NewString,
1950 CheckJNI::GetStringLength,
1951 CheckJNI::GetStringChars,
1952 CheckJNI::ReleaseStringChars,
1953 CheckJNI::NewStringUTF,
1954 CheckJNI::GetStringUTFLength,
1955 CheckJNI::GetStringUTFChars,
1956 CheckJNI::ReleaseStringUTFChars,
1957 CheckJNI::GetArrayLength,
1958 CheckJNI::NewObjectArray,
1959 CheckJNI::GetObjectArrayElement,
1960 CheckJNI::SetObjectArrayElement,
1961 CheckJNI::NewBooleanArray,
1962 CheckJNI::NewByteArray,
1963 CheckJNI::NewCharArray,
1964 CheckJNI::NewShortArray,
1965 CheckJNI::NewIntArray,
1966 CheckJNI::NewLongArray,
1967 CheckJNI::NewFloatArray,
1968 CheckJNI::NewDoubleArray,
1969 CheckJNI::GetBooleanArrayElements,
1970 CheckJNI::GetByteArrayElements,
1971 CheckJNI::GetCharArrayElements,
1972 CheckJNI::GetShortArrayElements,
1973 CheckJNI::GetIntArrayElements,
1974 CheckJNI::GetLongArrayElements,
1975 CheckJNI::GetFloatArrayElements,
1976 CheckJNI::GetDoubleArrayElements,
1977 CheckJNI::ReleaseBooleanArrayElements,
1978 CheckJNI::ReleaseByteArrayElements,
1979 CheckJNI::ReleaseCharArrayElements,
1980 CheckJNI::ReleaseShortArrayElements,
1981 CheckJNI::ReleaseIntArrayElements,
1982 CheckJNI::ReleaseLongArrayElements,
1983 CheckJNI::ReleaseFloatArrayElements,
1984 CheckJNI::ReleaseDoubleArrayElements,
1985 CheckJNI::GetBooleanArrayRegion,
1986 CheckJNI::GetByteArrayRegion,
1987 CheckJNI::GetCharArrayRegion,
1988 CheckJNI::GetShortArrayRegion,
1989 CheckJNI::GetIntArrayRegion,
1990 CheckJNI::GetLongArrayRegion,
1991 CheckJNI::GetFloatArrayRegion,
1992 CheckJNI::GetDoubleArrayRegion,
1993 CheckJNI::SetBooleanArrayRegion,
1994 CheckJNI::SetByteArrayRegion,
1995 CheckJNI::SetCharArrayRegion,
1996 CheckJNI::SetShortArrayRegion,
1997 CheckJNI::SetIntArrayRegion,
1998 CheckJNI::SetLongArrayRegion,
1999 CheckJNI::SetFloatArrayRegion,
2000 CheckJNI::SetDoubleArrayRegion,
2001 CheckJNI::RegisterNatives,
2002 CheckJNI::UnregisterNatives,
2003 CheckJNI::MonitorEnter,
2004 CheckJNI::MonitorExit,
2005 CheckJNI::GetJavaVM,
2006 CheckJNI::GetStringRegion,
2007 CheckJNI::GetStringUTFRegion,
2008 CheckJNI::GetPrimitiveArrayCritical,
2009 CheckJNI::ReleasePrimitiveArrayCritical,
2010 CheckJNI::GetStringCritical,
2011 CheckJNI::ReleaseStringCritical,
2012 CheckJNI::NewWeakGlobalRef,
2013 CheckJNI::DeleteWeakGlobalRef,
2014 CheckJNI::ExceptionCheck,
2015 CheckJNI::NewDirectByteBuffer,
2016 CheckJNI::GetDirectBufferAddress,
2017 CheckJNI::GetDirectBufferCapacity,
2018 CheckJNI::GetObjectRefType,
2019};
2020
2021const JNINativeInterface* GetCheckJniNativeInterface() {
2022 return &gCheckNativeInterface;
2023}
2024
2025class CheckJII {
Elliott Hughesba8eee12012-01-24 20:25:24 -08002026 public:
Elliott Hughesa2501992011-08-26 19:39:54 -07002027 static jint DestroyJavaVM(JavaVM* vm) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002028 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002029 sc.Check(true, "v", vm);
2030 return CHECK_JNI_EXIT("I", BaseVm(vm)->DestroyJavaVM(vm));
Elliott Hughesa2501992011-08-26 19:39:54 -07002031 }
2032
2033 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002034 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002035 sc.Check(true, "vpp", vm, p_env, thr_args);
2036 return CHECK_JNI_EXIT("I", BaseVm(vm)->AttachCurrentThread(vm, p_env, thr_args));
Elliott Hughesa2501992011-08-26 19:39:54 -07002037 }
2038
2039 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002040 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002041 sc.Check(true, "vpp", vm, p_env, thr_args);
2042 return CHECK_JNI_EXIT("I", BaseVm(vm)->AttachCurrentThreadAsDaemon(vm, p_env, thr_args));
Elliott Hughesa2501992011-08-26 19:39:54 -07002043 }
2044
2045 static jint DetachCurrentThread(JavaVM* vm) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002046 ScopedCheck sc(vm, true, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002047 sc.Check(true, "v", vm);
2048 return CHECK_JNI_EXIT("I", BaseVm(vm)->DetachCurrentThread(vm));
Elliott Hughesa2501992011-08-26 19:39:54 -07002049 }
2050
2051 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002052 ScopedCheck sc(vm, true, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002053 sc.Check(true, "v", vm);
2054 return CHECK_JNI_EXIT("I", BaseVm(vm)->GetEnv(vm, env, version));
Elliott Hughesa2501992011-08-26 19:39:54 -07002055 }
2056
2057 private:
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002058 static inline const JNIInvokeInterface* BaseVm(JavaVM* vm) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002059 return reinterpret_cast<JavaVMExt*>(vm)->unchecked_functions;
2060 }
2061};
2062
2063const JNIInvokeInterface gCheckInvokeInterface = {
2064 NULL, // reserved0
2065 NULL, // reserved1
2066 NULL, // reserved2
2067 CheckJII::DestroyJavaVM,
2068 CheckJII::AttachCurrentThread,
2069 CheckJII::DetachCurrentThread,
2070 CheckJII::GetEnv,
2071 CheckJII::AttachCurrentThreadAsDaemon
2072};
2073
2074const JNIInvokeInterface* GetCheckJniInvokeInterface() {
2075 return &gCheckInvokeInterface;
2076}
2077
2078} // namespace art