blob: c724eb24ea9f01438db1eaa963756dcf3928fb95 [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"
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070025#include "scoped_jni_thread_state.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
36void JniAbort(const char* jni_function_name) {
Elliott Hughesa0957642011-09-02 14:27:33 -070037 Thread* self = Thread::Current();
Elliott Hughesd07986f2011-12-06 18:27:45 -080038 Method* current_method = self->GetCurrentMethod();
Elliott Hughesa2501992011-08-26 19:39:54 -070039
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070040 std::ostringstream os;
Elliott Hughese6087632011-09-26 12:18:25 -070041 os << "Aborting because JNI app bug detected (see above for details)";
Elliott Hughesa2501992011-08-26 19:39:54 -070042
43 if (jni_function_name != NULL) {
44 os << "\n in call to " << jni_function_name;
45 }
Elliott Hughesa0957642011-09-02 14:27:33 -070046 // TODO: is this useful given that we're about to dump the calling thread's stack?
47 if (current_method != NULL) {
48 os << "\n from " << PrettyMethod(current_method);
49 }
50 os << "\n";
51 self->Dump(os);
Elliott Hughesa2501992011-08-26 19:39:54 -070052
53 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
54 if (vm->check_jni_abort_hook != NULL) {
55 vm->check_jni_abort_hook(os.str());
56 } else {
Elliott Hughesa8c74182012-03-14 14:35:41 -070057 self->SetState(Thread::kNative); // Ensure that we get a native stack trace for this thread.
Elliott Hughesa2501992011-08-26 19:39:54 -070058 LOG(FATAL) << os.str();
59 }
60}
61
62/*
63 * ===========================================================================
64 * JNI function helpers
65 * ===========================================================================
66 */
67
Ian Rogers959f8ed2012-02-07 16:33:37 -080068static bool IsSirtLocalRef(JNIEnv* env, jobject localRef) {
69 return GetIndirectRefKind(localRef) == kSirtOrInvalid &&
70 reinterpret_cast<JNIEnvExt*>(env)->self->SirtContains(localRef);
71}
72
Elliott Hughesa2501992011-08-26 19:39:54 -070073template<typename T>
74T Decode(ScopedJniThreadState& ts, jobject obj) {
75 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
76}
77
Elliott Hughesa2501992011-08-26 19:39:54 -070078/*
79 * Hack to allow forcecopy to work with jniGetNonMovableArrayElements.
80 * The code deliberately uses an invalid sequence of operations, so we
81 * need to pass it through unmodified. Review that code before making
82 * any changes here.
83 */
84#define kNoCopyMagic 0xd5aab57f
85
86/*
87 * Flags passed into ScopedCheck.
88 */
89#define kFlag_Default 0x0000
90
91#define kFlag_CritBad 0x0000 /* calling while in critical is bad */
92#define kFlag_CritOkay 0x0001 /* ...okay */
93#define kFlag_CritGet 0x0002 /* this is a critical "get" */
94#define kFlag_CritRelease 0x0003 /* this is a critical "release" */
95#define kFlag_CritMask 0x0003 /* bit mask to get "crit" value */
96
97#define kFlag_ExcepBad 0x0000 /* raised exceptions are bad */
98#define kFlag_ExcepOkay 0x0004 /* ...okay */
99
100#define kFlag_Release 0x0010 /* are we in a non-critical release function? */
101#define kFlag_NullableUtf 0x0020 /* are our UTF parameters nullable? */
102
103#define kFlag_Invocation 0x8000 /* Part of the invocation interface (JavaVM*) */
104
Elliott Hughes485cac42011-12-09 17:49:35 -0800105#define kFlag_ForceTrace 0x80000000 // Add this to a JNI function's flags if you want to trace every call.
106
Elliott Hughesa0957642011-09-02 14:27:33 -0700107static const char* gBuiltInPrefixes[] = {
108 "Landroid/",
109 "Lcom/android/",
110 "Lcom/google/android/",
111 "Ldalvik/",
112 "Ljava/",
113 "Ljavax/",
114 "Llibcore/",
115 "Lorg/apache/harmony/",
116 NULL
117};
118
119bool ShouldTrace(JavaVMExt* vm, const Method* method) {
120 // If both "-Xcheck:jni" and "-Xjnitrace:" are enabled, we print trace messages
121 // when a native method that matches the -Xjnitrace argument calls a JNI function
122 // such as NewByteArray.
123 // If -verbose:third-party-jni is on, we want to log any JNI function calls
124 // made by a third-party native method.
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800125 std::string className(MethodHelper(method).GetDeclaringClassDescriptor());
126 if (!vm->trace.empty() && className.find(vm->trace) != std::string::npos) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700127 return true;
128 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800129 if (VLOG_IS_ON(third_party_jni)) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700130 // Return true if we're trying to log all third-party JNI activity and 'method' doesn't look
131 // like part of Android.
Elliott Hughesa0957642011-09-02 14:27:33 -0700132 for (size_t i = 0; gBuiltInPrefixes[i] != NULL; ++i) {
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800133 if (StartsWith(className, gBuiltInPrefixes[i])) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700134 return false;
135 }
136 }
137 return true;
138 }
139 return false;
140}
141
Elliott Hughesa2501992011-08-26 19:39:54 -0700142class ScopedCheck {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800143 public:
Elliott Hughesa2501992011-08-26 19:39:54 -0700144 // For JNIEnv* functions.
145 explicit ScopedCheck(JNIEnv* env, int flags, const char* functionName) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700146 Init(env, reinterpret_cast<JNIEnvExt*>(env)->vm, flags, functionName, true);
147 CheckThread(flags);
Elliott Hughesa2501992011-08-26 19:39:54 -0700148 }
149
150 // For JavaVM* functions.
Elliott Hughesa0957642011-09-02 14:27:33 -0700151 explicit ScopedCheck(JavaVM* vm, bool hasMethod, const char* functionName) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700152 Init(NULL, vm, kFlag_Invocation, functionName, hasMethod);
Elliott Hughesa2501992011-08-26 19:39:54 -0700153 }
154
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700155 bool ForceCopy() {
Elliott Hughesa2501992011-08-26 19:39:54 -0700156 return Runtime::Current()->GetJavaVM()->force_copy;
157 }
158
159 /*
160 * In some circumstances the VM will screen class names, but it doesn't
161 * for class lookup. When things get bounced through a class loader, they
162 * can actually get normalized a couple of times; as a result, passing in
163 * a class name like "java.lang.Thread" instead of "java/lang/Thread" will
164 * work in some circumstances.
165 *
166 * This is incorrect and could cause strange behavior or compatibility
167 * problems, so we want to screen that out here.
168 *
169 * We expect "fully-qualified" class names, like "java/lang/Thread" or
170 * "[Ljava/lang/Object;".
171 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700172 void CheckClassName(const char* className) {
Elliott Hughes906e6852011-10-28 14:52:10 -0700173 if (!IsValidJniClassName(className)) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700174 LOG(ERROR) << "JNI ERROR: illegal class name '" << className << "' (" << function_name_ << ")\n"
Elliott Hughesa2501992011-08-26 19:39:54 -0700175 << " (should be of the form 'java/lang/String', [Ljava/lang/String;' or '[[B')\n";
176 JniAbort();
177 }
178 }
179
180 /*
181 * Verify that the field is of the appropriate type. If the field has an
182 * object type, "java_object" is the object we're trying to assign into it.
183 *
184 * Works for both static and instance fields.
185 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700186 void CheckFieldType(jobject java_object, jfieldID fid, char prim, bool isStatic) {
187 ScopedJniThreadState ts(env_);
188 Field* f = CheckFieldID(fid);
189 if (f == NULL) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700190 return;
191 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800192 Class* field_type = FieldHelper(f).GetType();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700193 if (!field_type->IsPrimitive()) {
194 if (java_object != NULL) {
195 Object* obj = Decode<Object*>(ts, java_object);
196 /*
197 * If java_object is a weak global ref whose referent has been cleared,
198 * obj will be NULL. Otherwise, obj should always be non-NULL
199 * and valid.
200 */
Elliott Hughes88c5c352012-03-15 18:49:48 -0700201 if (!Runtime::Current()->GetHeap()->IsHeapAddress(obj)) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700202 LOG(ERROR) << "JNI ERROR: field operation on invalid " << GetIndirectRefKind(java_object) << ": " << java_object;
Elliott Hughesa2501992011-08-26 19:39:54 -0700203 JniAbort();
204 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700205 } else {
Brian Carlstrom16192862011-09-12 17:50:06 -0700206 if (!obj->InstanceOf(field_type)) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700207 LOG(ERROR) << "JNI ERROR: attempt to set field " << PrettyField(f) << " with value of wrong type: " << PrettyTypeOf(obj);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700208 JniAbort();
209 return;
210 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700211 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700212 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700213 } else if (field_type != Runtime::Current()->GetClassLinker()->FindPrimitiveClass(prim)) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700214 LOG(ERROR) << "JNI ERROR: attempt to set field " << PrettyField(f) << " with value of wrong type: " << prim;
215 JniAbort();
216 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700217 }
218
219 if (isStatic && !f->IsStatic()) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700220 if (isStatic) {
221 LOG(ERROR) << "JNI ERROR: accessing non-static field " << PrettyField(f) << " as static";
222 } else {
223 LOG(ERROR) << "JNI ERROR: accessing static field " << PrettyField(f) << " as non-static";
224 }
225 JniAbort();
226 return;
227 }
228 }
229
230 /*
231 * Verify that this instance field ID is valid for this object.
232 *
233 * Assumes "jobj" has already been validated.
234 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700235 void CheckInstanceFieldID(jobject java_object, jfieldID fid) {
236 ScopedJniThreadState ts(env_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700237
238 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800239 if (o == NULL || !Runtime::Current()->GetHeap()->IsHeapAddress(o)) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700240 LOG(ERROR) << "JNI ERROR: field operation on invalid " << GetIndirectRefKind(java_object) << ": " << java_object;
241 JniAbort();
242 return;
243 }
244
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700245 Field* f = CheckFieldID(fid);
246 if (f == NULL) {
247 return;
248 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700249 Class* c = o->GetClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800250 FieldHelper fh(f);
251 if (c->FindInstanceField(fh.GetName(), fh.GetTypeDescriptor()) == NULL) {
Elliott Hughes906e6852011-10-28 14:52:10 -0700252 LOG(ERROR) << "JNI ERROR: jfieldID " << PrettyField(f)
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700253 << " not valid for an object of class " << PrettyTypeOf(o);
Elliott Hughesa2501992011-08-26 19:39:54 -0700254 JniAbort();
255 }
256 }
257
258 /*
259 * Verify that the pointer value is non-NULL.
260 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700261 void CheckNonNull(const void* ptr) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700262 if (ptr == NULL) {
263 LOG(ERROR) << "JNI ERROR: invalid null pointer";
264 JniAbort();
265 }
266 }
267
268 /*
269 * Verify that the method's return type matches the type of call.
270 * 'expectedType' will be "L" for all objects, including arrays.
271 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700272 void CheckSig(jmethodID mid, const char* expectedType, bool isStatic) {
273 ScopedJniThreadState ts(env_);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800274 Method* m = CheckMethodID(mid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700275 if (m == NULL) {
276 return;
277 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800278 if (*expectedType != MethodHelper(m).GetShorty()[0]) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700279 LOG(ERROR) << "JNI ERROR: the return type of " << function_name_ << " does not match "
280 << PrettyMethod(m);
Elliott Hughesa2501992011-08-26 19:39:54 -0700281 JniAbort();
282 } else if (isStatic && !m->IsStatic()) {
283 if (isStatic) {
Elliott Hughes726079d2011-10-07 18:43:44 -0700284 LOG(ERROR) << "JNI ERROR: calling non-static method "
285 << PrettyMethod(m) << " with " << function_name_;
Elliott Hughesa2501992011-08-26 19:39:54 -0700286 } else {
Elliott Hughes726079d2011-10-07 18:43:44 -0700287 LOG(ERROR) << "JNI ERROR: calling static method "
288 << PrettyMethod(m) << " with non-static " << function_name_;
Elliott Hughesa2501992011-08-26 19:39:54 -0700289 }
290 JniAbort();
291 }
292 }
293
294 /*
295 * Verify that this static field ID is valid for this class.
296 *
297 * Assumes "java_class" has already been validated.
298 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700299 void CheckStaticFieldID(jclass java_class, jfieldID fid) {
300 ScopedJniThreadState ts(env_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700301 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700302 const Field* f = CheckFieldID(fid);
303 if (f == NULL) {
304 return;
305 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700306 if (f->GetDeclaringClass() != c) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700307 LOG(ERROR) << "JNI ERROR: static jfieldID " << fid << " not valid for class " << PrettyClass(c);
Elliott Hughesa2501992011-08-26 19:39:54 -0700308 JniAbort();
309 }
310 }
311
312 /*
Elliott Hughese84278b2012-03-22 10:06:53 -0700313 * Verify that "mid" is appropriate for "java_class".
Elliott Hughesa2501992011-08-26 19:39:54 -0700314 *
315 * A mismatch isn't dangerous, because the jmethodID defines the class. In
Elliott Hughese84278b2012-03-22 10:06:53 -0700316 * fact, java_class is unused in the implementation. It's best if we don't
Elliott Hughesa2501992011-08-26 19:39:54 -0700317 * allow bad code in the system though.
318 *
Elliott Hughese84278b2012-03-22 10:06:53 -0700319 * Instances of "java_class" must be instances of the method's declaring class.
Elliott Hughesa2501992011-08-26 19:39:54 -0700320 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700321 void CheckStaticMethod(jclass java_class, jmethodID mid) {
322 ScopedJniThreadState ts(env_);
323 const Method* m = CheckMethodID(mid);
324 if (m == NULL) {
325 return;
326 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700327 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesa2501992011-08-26 19:39:54 -0700328 if (!c->IsAssignableFrom(m->GetDeclaringClass())) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700329 LOG(ERROR) << "JNI ERROR: can't call static " << PrettyMethod(m) << " on class " << PrettyClass(c);
Elliott Hughesa2501992011-08-26 19:39:54 -0700330 JniAbort();
331 }
332 }
333
334 /*
335 * Verify that "mid" is appropriate for "jobj".
336 *
337 * Make sure the object is an instance of the method's declaring class.
338 * (Note the mid might point to a declaration in an interface; this
339 * will be handled automatically by the instanceof check.)
340 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700341 void CheckVirtualMethod(jobject java_object, jmethodID mid) {
342 ScopedJniThreadState ts(env_);
343 const Method* m = CheckMethodID(mid);
344 if (m == NULL) {
345 return;
346 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700347 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700348 if (!o->InstanceOf(m->GetDeclaringClass())) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700349 LOG(ERROR) << "JNI ERROR: can't call " << PrettyMethod(m) << " on instance of " << PrettyTypeOf(o);
Elliott Hughesa2501992011-08-26 19:39:54 -0700350 JniAbort();
351 }
352 }
353
354 /**
355 * The format string is a sequence of the following characters,
356 * and must be followed by arguments of the corresponding types
357 * in the same order.
358 *
359 * Java primitive types:
360 * B - jbyte
361 * C - jchar
362 * D - jdouble
363 * F - jfloat
364 * I - jint
365 * J - jlong
366 * S - jshort
367 * Z - jboolean (shown as true and false)
368 * V - void
369 *
370 * Java reference types:
371 * L - jobject
372 * a - jarray
373 * c - jclass
374 * s - jstring
375 *
376 * JNI types:
377 * b - jboolean (shown as JNI_TRUE and JNI_FALSE)
378 * f - jfieldID
379 * m - jmethodID
380 * p - void*
381 * r - jint (for release mode arguments)
Elliott Hughes78090d12011-10-07 14:31:47 -0700382 * u - const char* (Modified UTF-8)
Elliott Hughesa2501992011-08-26 19:39:54 -0700383 * z - jsize (for lengths; use i if negative values are okay)
384 * v - JavaVM*
385 * E - JNIEnv*
386 * . - no argument; just print "..." (used for varargs JNI calls)
387 *
388 * Use the kFlag_NullableUtf flag where 'u' field(s) are nullable.
389 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700390 void Check(bool entry, const char* fmt0, ...) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700391 va_list ap;
392
Elliott Hughesa0957642011-09-02 14:27:33 -0700393 const Method* traceMethod = NULL;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800394 if ((!vm_->trace.empty() || VLOG_IS_ON(third_party_jni)) && has_method_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700395 // We need to guard some of the invocation interface's calls: a bad caller might
396 // use DetachCurrentThread or GetEnv on a thread that's not yet attached.
Elliott Hughesa0957642011-09-02 14:27:33 -0700397 Thread* self = Thread::Current();
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700398 if ((flags_ & kFlag_Invocation) == 0 || self != NULL) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700399 traceMethod = self->GetCurrentMethod();
Elliott Hughesa2501992011-08-26 19:39:54 -0700400 }
401 }
Elliott Hughesa0957642011-09-02 14:27:33 -0700402
Elliott Hughes485cac42011-12-09 17:49:35 -0800403 if (((flags_ & kFlag_ForceTrace) != 0) || (traceMethod != NULL && ShouldTrace(vm_, traceMethod))) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700404 va_start(ap, fmt0);
405 std::string msg;
406 for (const char* fmt = fmt0; *fmt;) {
407 char ch = *fmt++;
408 if (ch == 'B') { // jbyte
409 jbyte b = va_arg(ap, int);
410 if (b >= 0 && b < 10) {
411 StringAppendF(&msg, "%d", b);
412 } else {
413 StringAppendF(&msg, "%#x (%d)", b, b);
414 }
415 } else if (ch == 'C') { // jchar
416 jchar c = va_arg(ap, int);
417 if (c < 0x7f && c >= ' ') {
418 StringAppendF(&msg, "U+%x ('%c')", c, c);
419 } else {
420 StringAppendF(&msg, "U+%x", c);
421 }
422 } else if (ch == 'F' || ch == 'D') { // jfloat, jdouble
423 StringAppendF(&msg, "%g", va_arg(ap, double));
424 } else if (ch == 'I' || ch == 'S') { // jint, jshort
425 StringAppendF(&msg, "%d", va_arg(ap, int));
426 } else if (ch == 'J') { // jlong
427 StringAppendF(&msg, "%lld", va_arg(ap, jlong));
428 } else if (ch == 'Z') { // jboolean
429 StringAppendF(&msg, "%s", va_arg(ap, int) ? "true" : "false");
430 } else if (ch == 'V') { // void
431 msg += "void";
432 } else if (ch == 'v') { // JavaVM*
433 JavaVM* vm = va_arg(ap, JavaVM*);
434 StringAppendF(&msg, "(JavaVM*)%p", vm);
435 } else if (ch == 'E') { // JNIEnv*
436 JNIEnv* env = va_arg(ap, JNIEnv*);
437 StringAppendF(&msg, "(JNIEnv*)%p", env);
438 } else if (ch == 'L' || ch == 'a' || ch == 's') { // jobject, jarray, jstring
439 // For logging purposes, these are identical.
440 jobject o = va_arg(ap, jobject);
441 if (o == NULL) {
442 msg += "NULL";
443 } else {
444 StringAppendF(&msg, "%p", o);
445 }
446 } else if (ch == 'b') { // jboolean (JNI-style)
447 jboolean b = va_arg(ap, int);
448 msg += (b ? "JNI_TRUE" : "JNI_FALSE");
449 } else if (ch == 'c') { // jclass
450 jclass jc = va_arg(ap, jclass);
451 Class* c = reinterpret_cast<Class*>(Thread::Current()->DecodeJObject(jc));
452 if (c == NULL) {
453 msg += "NULL";
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800454 } else if (c == kInvalidIndirectRefObject || !Runtime::Current()->GetHeap()->IsHeapAddress(c)) {
Elliott Hughes485cac42011-12-09 17:49:35 -0800455 StringAppendF(&msg, "INVALID POINTER:%p", jc);
456 } else if (!c->IsClass()) {
457 msg += "INVALID NON-CLASS OBJECT OF TYPE:" + PrettyTypeOf(c);
Elliott Hughesa2501992011-08-26 19:39:54 -0700458 } else {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700459 msg += PrettyClass(c);
Elliott Hughesa2501992011-08-26 19:39:54 -0700460 if (!entry) {
461 StringAppendF(&msg, " (%p)", jc);
462 }
463 }
464 } else if (ch == 'f') { // jfieldID
465 jfieldID fid = va_arg(ap, jfieldID);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700466 Field* f = reinterpret_cast<Field*>(fid);
Elliott Hughesa2501992011-08-26 19:39:54 -0700467 msg += PrettyField(f);
468 if (!entry) {
469 StringAppendF(&msg, " (%p)", fid);
470 }
471 } else if (ch == 'z') { // non-negative jsize
472 // You might expect jsize to be size_t, but it's not; it's the same as jint.
473 // We only treat this specially so we can do the non-negative check.
474 // TODO: maybe this wasn't worth it?
475 jint i = va_arg(ap, jint);
476 StringAppendF(&msg, "%d", i);
477 } else if (ch == 'm') { // jmethodID
478 jmethodID mid = va_arg(ap, jmethodID);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700479 Method* m = reinterpret_cast<Method*>(mid);
Elliott Hughesa2501992011-08-26 19:39:54 -0700480 msg += PrettyMethod(m);
481 if (!entry) {
482 StringAppendF(&msg, " (%p)", mid);
483 }
484 } else if (ch == 'p') { // void* ("pointer")
485 void* p = va_arg(ap, void*);
486 if (p == NULL) {
487 msg += "NULL";
488 } else {
489 StringAppendF(&msg, "(void*) %p", p);
490 }
491 } else if (ch == 'r') { // jint (release mode)
492 jint releaseMode = va_arg(ap, jint);
493 if (releaseMode == 0) {
494 msg += "0";
495 } else if (releaseMode == JNI_ABORT) {
496 msg += "JNI_ABORT";
497 } else if (releaseMode == JNI_COMMIT) {
498 msg += "JNI_COMMIT";
499 } else {
500 StringAppendF(&msg, "invalid release mode %d", releaseMode);
501 }
Elliott Hughes78090d12011-10-07 14:31:47 -0700502 } else if (ch == 'u') { // const char* (Modified UTF-8)
Elliott Hughesa2501992011-08-26 19:39:54 -0700503 const char* utf = va_arg(ap, const char*);
504 if (utf == NULL) {
505 msg += "NULL";
506 } else {
507 StringAppendF(&msg, "\"%s\"", utf);
508 }
509 } else if (ch == '.') {
510 msg += "...";
511 } else {
512 LOG(ERROR) << "unknown trace format specifier: " << ch;
513 JniAbort();
514 return;
515 }
516 if (*fmt) {
517 StringAppendF(&msg, ", ");
518 }
519 }
520 va_end(ap);
521
Elliott Hughes485cac42011-12-09 17:49:35 -0800522 if ((flags_ & kFlag_ForceTrace) != 0) {
523 LOG(INFO) << "JNI: call to " << function_name_ << "(" << msg << ")";
524 } else if (entry) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700525 if (has_method_) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700526 std::string methodName(PrettyMethod(traceMethod, false));
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700527 LOG(INFO) << "JNI: " << methodName << " -> " << function_name_ << "(" << msg << ")";
528 indent_ = methodName.size() + 1;
Elliott Hughesa2501992011-08-26 19:39:54 -0700529 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700530 LOG(INFO) << "JNI: -> " << function_name_ << "(" << msg << ")";
531 indent_ = 0;
Elliott Hughesa2501992011-08-26 19:39:54 -0700532 }
533 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700534 LOG(INFO) << StringPrintf("JNI: %*s<- %s returned %s", indent_, "", function_name_, msg.c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700535 }
536 }
537
538 // We always do the thorough checks on entry, and never on exit...
539 if (entry) {
540 va_start(ap, fmt0);
541 for (const char* fmt = fmt0; *fmt; ++fmt) {
542 char ch = *fmt;
543 if (ch == 'a') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700544 CheckArray(va_arg(ap, jarray));
Elliott Hughesa2501992011-08-26 19:39:54 -0700545 } else if (ch == 'c') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700546 CheckInstance(kClass, va_arg(ap, jclass));
Elliott Hughesa2501992011-08-26 19:39:54 -0700547 } else if (ch == 'L') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700548 CheckObject(va_arg(ap, jobject));
Elliott Hughesa2501992011-08-26 19:39:54 -0700549 } else if (ch == 'r') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700550 CheckReleaseMode(va_arg(ap, jint));
Elliott Hughesa2501992011-08-26 19:39:54 -0700551 } else if (ch == 's') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700552 CheckInstance(kString, va_arg(ap, jstring));
Elliott Hughesa2501992011-08-26 19:39:54 -0700553 } else if (ch == 'u') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700554 if ((flags_ & kFlag_Release) != 0) {
555 CheckNonNull(va_arg(ap, const char*));
Elliott Hughesa2501992011-08-26 19:39:54 -0700556 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700557 bool nullable = ((flags_ & kFlag_NullableUtf) != 0);
558 CheckUtfString(va_arg(ap, const char*), nullable);
Elliott Hughesa2501992011-08-26 19:39:54 -0700559 }
560 } else if (ch == 'z') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700561 CheckLengthPositive(va_arg(ap, jsize));
Elliott Hughesa2501992011-08-26 19:39:54 -0700562 } else if (strchr("BCISZbfmpEv", ch) != NULL) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800563 va_arg(ap, uint32_t); // Skip this argument.
Elliott Hughesa2501992011-08-26 19:39:54 -0700564 } else if (ch == 'D' || ch == 'F') {
565 va_arg(ap, double); // Skip this argument.
566 } else if (ch == 'J') {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800567 va_arg(ap, uint64_t); // Skip this argument.
Elliott Hughesa2501992011-08-26 19:39:54 -0700568 } else if (ch == '.') {
569 } else {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800570 LOG(FATAL) << "Unknown check format specifier: " << ch;
Elliott Hughesa2501992011-08-26 19:39:54 -0700571 }
572 }
573 va_end(ap);
574 }
575 }
576
Elliott Hughesa92853e2012-02-07 16:09:27 -0800577 enum InstanceKind {
578 kClass,
579 kDirectByteBuffer,
580 kObject,
581 kString,
582 kThrowable,
583 };
584
585 /*
586 * Verify that "jobj" is a valid non-NULL object reference, and points to
587 * an instance of expectedClass.
588 *
589 * Because we're looking at an object on the GC heap, we have to switch
590 * to "running" mode before doing the checks.
591 */
592 bool CheckInstance(InstanceKind kind, jobject java_object) {
593 const char* what = NULL;
594 switch (kind) {
595 case kClass:
596 what = "jclass";
597 break;
598 case kDirectByteBuffer:
599 what = "direct ByteBuffer";
600 break;
601 case kObject:
602 what = "jobject";
603 break;
604 case kString:
605 what = "jstring";
606 break;
607 case kThrowable:
608 what = "jthrowable";
609 break;
610 default:
611 CHECK(false) << static_cast<int>(kind);
612 }
613
614 if (java_object == NULL) {
615 LOG(ERROR) << "JNI ERROR: " << function_name_ << " received null " << what;
616 JniAbort();
617 return false;
618 }
619
620 ScopedJniThreadState ts(env_);
621 Object* obj = Decode<Object*>(ts, java_object);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800622 if (!Runtime::Current()->GetHeap()->IsHeapAddress(obj)) {
623 LOG(ERROR) << "JNI ERROR: " << what << " is an invalid " << GetIndirectRefKind(java_object) << ": "
624 << java_object << " (" << obj << ")";
Elliott Hughesa92853e2012-02-07 16:09:27 -0800625 JniAbort();
626 return false;
627 }
628
629 bool okay = true;
630 switch (kind) {
631 case kClass:
632 okay = obj->IsClass();
633 break;
634 case kDirectByteBuffer:
635 UNIMPLEMENTED(FATAL);
636 break;
637 case kString:
638 okay = obj->GetClass()->IsStringClass();
639 break;
640 case kThrowable:
641 okay = obj->GetClass()->IsThrowableClass();
642 break;
643 case kObject:
644 break;
645 }
646 if (!okay) {
647 LOG(ERROR) << "JNI ERROR: " << what << " has wrong type: " << PrettyTypeOf(obj);
648 JniAbort();
649 return false;
650 }
651
652 return true;
653 }
654
Elliott Hughesba8eee12012-01-24 20:25:24 -0800655 private:
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700656 void Init(JNIEnv* env, JavaVM* vm, int flags, const char* functionName, bool hasMethod) {
657 env_ = reinterpret_cast<JNIEnvExt*>(env);
658 vm_ = reinterpret_cast<JavaVMExt*>(vm);
659 flags_ = flags;
660 function_name_ = functionName;
Elliott Hughesa2501992011-08-26 19:39:54 -0700661
662 // Set "hasMethod" to true if we have a valid thread with a method pointer.
663 // We won't have one before attaching a thread, after detaching a thread, or
664 // after destroying the VM.
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700665 has_method_ = hasMethod;
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 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700673 void CheckArray(jarray java_array) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700674 if (java_array == NULL) {
675 LOG(ERROR) << "JNI ERROR: received null array";
676 JniAbort();
677 return;
678 }
679
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700680 ScopedJniThreadState ts(env_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700681 Array* a = Decode<Array*>(ts, java_array);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800682 if (!Runtime::Current()->GetHeap()->IsHeapAddress(a)) {
683 LOG(ERROR) << "JNI ERROR: jarray is an invalid " << GetIndirectRefKind(java_array) << ": " << reinterpret_cast<void*>(java_array) << " (" << a << ")";
Elliott Hughesa2501992011-08-26 19:39:54 -0700684 JniAbort();
685 } else if (!a->IsArrayInstance()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700686 LOG(ERROR) << "JNI ERROR: jarray argument has non-array type: " << PrettyTypeOf(a);
Elliott Hughesa2501992011-08-26 19:39:54 -0700687 JniAbort();
688 }
689 }
690
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700691 void CheckLengthPositive(jsize length) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700692 if (length < 0) {
693 LOG(ERROR) << "JNI ERROR: negative jsize: " << length;
694 JniAbort();
695 }
696 }
697
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700698 Field* CheckFieldID(jfieldID fid) {
699 if (fid == NULL) {
700 LOG(ERROR) << "JNI ERROR: null jfieldID";
701 JniAbort();
702 return NULL;
703 }
704 Field* f = DecodeField(fid);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800705 if (!Runtime::Current()->GetHeap()->IsHeapAddress(f)) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700706 LOG(ERROR) << "JNI ERROR: invalid jfieldID: " << fid;
707 JniAbort();
708 return NULL;
709 }
710 return f;
711 }
712
713 Method* CheckMethodID(jmethodID mid) {
714 if (mid == NULL) {
715 LOG(ERROR) << "JNI ERROR: null jmethodID";
716 JniAbort();
717 return NULL;
718 }
719 Method* m = DecodeMethod(mid);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800720 if (!Runtime::Current()->GetHeap()->IsHeapAddress(m)) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700721 LOG(ERROR) << "JNI ERROR: invalid jmethodID: " << mid;
722 JniAbort();
723 return NULL;
724 }
725 return m;
726 }
727
Elliott Hughesa2501992011-08-26 19:39:54 -0700728 /*
729 * Verify that "jobj" is a valid object, and that it's an object that JNI
730 * is allowed to know about. We allow NULL references.
731 *
732 * Switches to "running" mode before performing checks.
733 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700734 void CheckObject(jobject java_object) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700735 if (java_object == NULL) {
736 return;
737 }
738
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700739 ScopedJniThreadState ts(env_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700740
741 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughes88c5c352012-03-15 18:49:48 -0700742 if (!Runtime::Current()->GetHeap()->IsHeapAddress(o)) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700743 // TODO: when we remove work_around_app_jni_bugs, this should be impossible.
Elliott Hughesa2501992011-08-26 19:39:54 -0700744 LOG(ERROR) << "JNI ERROR: native code passing in reference to invalid " << GetIndirectRefKind(java_object) << ": " << java_object;
745 JniAbort();
746 }
747 }
748
749 /*
750 * Verify that the "mode" argument passed to a primitive array Release
751 * function is one of the valid values.
752 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700753 void CheckReleaseMode(jint mode) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700754 if (mode != 0 && mode != JNI_COMMIT && mode != JNI_ABORT) {
755 LOG(ERROR) << "JNI ERROR: bad value for release mode: " << mode;
756 JniAbort();
757 }
758 }
759
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700760 void CheckThread(int flags) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700761 Thread* self = Thread::Current();
762 if (self == NULL) {
763 LOG(ERROR) << "JNI ERROR: non-VM thread making JNI calls";
764 JniAbort();
765 return;
766 }
767
768 // Get the *correct* JNIEnv by going through our TLS pointer.
769 JNIEnvExt* threadEnv = self->GetJniEnv();
770
771 /*
772 * Verify that the current thread is (a) attached and (b) associated with
773 * this particular instance of JNIEnv.
774 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700775 if (env_ != threadEnv) {
776 LOG(ERROR) << "JNI ERROR: thread " << *self << " using JNIEnv* from thread " << *env_->self;
Elliott Hughesa2501992011-08-26 19:39:54 -0700777 // If we're keeping broken code limping along, we need to suppress the abort...
Elliott Hughesc2dc62d2012-01-17 20:06:12 -0800778 if (!vm_->work_around_app_jni_bugs) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700779 JniAbort();
780 return;
781 }
782 }
783
784 /*
785 * Verify that, if this thread previously made a critical "get" call, we
786 * do the corresponding "release" call before we try anything else.
787 */
788 switch (flags & kFlag_CritMask) {
789 case kFlag_CritOkay: // okay to call this method
790 break;
791 case kFlag_CritBad: // not okay to call
792 if (threadEnv->critical) {
793 LOG(ERROR) << "JNI ERROR: thread " << *self << " using JNI after critical get";
794 JniAbort();
795 return;
796 }
797 break;
798 case kFlag_CritGet: // this is a "get" call
799 /* don't check here; we allow nested gets */
800 threadEnv->critical++;
801 break;
802 case kFlag_CritRelease: // this is a "release" call
803 threadEnv->critical--;
804 if (threadEnv->critical < 0) {
805 LOG(ERROR) << "JNI ERROR: thread " << *self << " called too many critical releases";
806 JniAbort();
807 return;
808 }
809 break;
810 default:
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800811 LOG(FATAL) << "Bad flags (internal error): " << flags;
Elliott Hughesa2501992011-08-26 19:39:54 -0700812 }
813
814 /*
815 * Verify that, if an exception has been raised, the native code doesn't
816 * make any JNI calls other than the Exception* methods.
817 */
818 if ((flags & kFlag_ExcepOkay) == 0 && self->IsExceptionPending()) {
Elliott Hughes30646832011-10-13 16:59:46 -0700819 std::string type(PrettyTypeOf(self->GetException()));
820 LOG(ERROR) << "JNI ERROR: JNI " << function_name_ << " called with " << type << " pending";
821 // TODO: write native code that doesn't require allocation for dumping an exception.
822 if (type != "java.lang.OutOfMemoryError") {
823 LOG(ERROR) << "Pending exception is: ";
824 LOG(ERROR) << jniGetStackTrace(env_);
825 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700826 JniAbort();
827 return;
828 }
829 }
830
831 /*
Elliott Hughes78090d12011-10-07 14:31:47 -0700832 * Verify that "bytes" points to valid Modified UTF-8 data.
Elliott Hughesa2501992011-08-26 19:39:54 -0700833 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700834 void CheckUtfString(const char* bytes, bool nullable) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700835 if (bytes == NULL) {
836 if (!nullable) {
837 LOG(ERROR) << "JNI ERROR: non-nullable const char* was NULL";
838 JniAbort();
839 return;
840 }
841 return;
842 }
843
844 const char* errorKind = NULL;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700845 uint8_t utf8 = CheckUtfBytes(bytes, &errorKind);
Elliott Hughesa2501992011-08-26 19:39:54 -0700846 if (errorKind != NULL) {
Elliott Hughes78090d12011-10-07 14:31:47 -0700847 LOG(ERROR) << "JNI ERROR: input is not valid Modified UTF-8: "
848 << "illegal " << errorKind << " byte " << StringPrintf("%#x", utf8) << "\n"
849 << " string: '" << bytes << "'";
Elliott Hughesa2501992011-08-26 19:39:54 -0700850 JniAbort();
851 return;
852 }
853 }
854
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700855 static uint8_t CheckUtfBytes(const char* bytes, const char** errorKind) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700856 while (*bytes != '\0') {
857 uint8_t utf8 = *(bytes++);
858 // Switch on the high four bits.
859 switch (utf8 >> 4) {
860 case 0x00:
861 case 0x01:
862 case 0x02:
863 case 0x03:
864 case 0x04:
865 case 0x05:
866 case 0x06:
867 case 0x07:
868 // Bit pattern 0xxx. No need for any extra bytes.
869 break;
870 case 0x08:
871 case 0x09:
872 case 0x0a:
873 case 0x0b:
874 case 0x0f:
875 /*
876 * Bit pattern 10xx or 1111, which are illegal start bytes.
877 * Note: 1111 is valid for normal UTF-8, but not the
Elliott Hughes78090d12011-10-07 14:31:47 -0700878 * Modified UTF-8 used here.
Elliott Hughesa2501992011-08-26 19:39:54 -0700879 */
880 *errorKind = "start";
881 return utf8;
882 case 0x0e:
883 // Bit pattern 1110, so there are two additional bytes.
884 utf8 = *(bytes++);
885 if ((utf8 & 0xc0) != 0x80) {
886 *errorKind = "continuation";
887 return utf8;
888 }
889 // Fall through to take care of the final byte.
890 case 0x0c:
891 case 0x0d:
892 // Bit pattern 110x, so there is one additional byte.
893 utf8 = *(bytes++);
894 if ((utf8 & 0xc0) != 0x80) {
895 *errorKind = "continuation";
896 return utf8;
897 }
898 break;
899 }
900 }
901 return 0;
902 }
903
904 void JniAbort() {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700905 ::art::JniAbort(function_name_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700906 }
907
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700908 JNIEnvExt* env_;
909 JavaVMExt* vm_;
910 const char* function_name_;
911 int flags_;
912 bool has_method_;
Elliott Hughes92cb4982011-12-16 16:57:28 -0800913 int indent_;
Elliott Hughesa2501992011-08-26 19:39:54 -0700914
915 DISALLOW_COPY_AND_ASSIGN(ScopedCheck);
916};
917
918#define CHECK_JNI_ENTRY(flags, types, args...) \
919 ScopedCheck sc(env, flags, __FUNCTION__); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700920 sc.Check(true, types, ##args)
Elliott Hughesa2501992011-08-26 19:39:54 -0700921
922#define CHECK_JNI_EXIT(type, exp) ({ \
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700923 typeof(exp) _rc = (exp); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700924 sc.Check(false, type, _rc); \
Elliott Hughesa2501992011-08-26 19:39:54 -0700925 _rc; })
926#define CHECK_JNI_EXIT_VOID() \
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700927 sc.Check(false, "V")
Elliott Hughesa2501992011-08-26 19:39:54 -0700928
929/*
930 * ===========================================================================
931 * Guarded arrays
932 * ===========================================================================
933 */
934
935#define kGuardLen 512 /* must be multiple of 2 */
936#define kGuardPattern 0xd5e3 /* uncommon values; d5e3d5e3 invalid addr */
937#define kGuardMagic 0xffd5aa96
938
939/* this gets tucked in at the start of the buffer; struct size must be even */
940struct GuardedCopy {
941 uint32_t magic;
942 uLong adler;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700943 size_t original_length;
944 const void* original_ptr;
Elliott Hughesa2501992011-08-26 19:39:54 -0700945
946 /* find the GuardedCopy given the pointer into the "live" data */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700947 static inline const GuardedCopy* FromData(const void* dataBuf) {
948 return reinterpret_cast<const GuardedCopy*>(ActualBuffer(dataBuf));
Elliott Hughesa2501992011-08-26 19:39:54 -0700949 }
950
951 /*
952 * Create an over-sized buffer to hold the contents of "buf". Copy it in,
953 * filling in the area around it with guard data.
954 *
955 * We use a 16-bit pattern to make a rogue memset less likely to elude us.
956 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700957 static void* Create(const void* buf, size_t len, bool modOkay) {
958 size_t newLen = ActualLength(len);
959 uint8_t* newBuf = DebugAlloc(newLen);
Elliott Hughesa2501992011-08-26 19:39:54 -0700960
961 /* fill it in with a pattern */
Elliott Hughesba8eee12012-01-24 20:25:24 -0800962 uint16_t* pat = reinterpret_cast<uint16_t*>(newBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -0700963 for (size_t i = 0; i < newLen / 2; i++) {
964 *pat++ = kGuardPattern;
965 }
966
967 /* copy the data in; note "len" could be zero */
968 memcpy(newBuf + kGuardLen / 2, buf, len);
969
970 /* if modification is not expected, grab a checksum */
971 uLong adler = 0;
972 if (!modOkay) {
973 adler = adler32(0L, Z_NULL, 0);
Elliott Hughesba8eee12012-01-24 20:25:24 -0800974 adler = adler32(adler, reinterpret_cast<const Bytef*>(buf), len);
975 *reinterpret_cast<uLong*>(newBuf) = adler;
Elliott Hughesa2501992011-08-26 19:39:54 -0700976 }
977
978 GuardedCopy* pExtra = reinterpret_cast<GuardedCopy*>(newBuf);
979 pExtra->magic = kGuardMagic;
980 pExtra->adler = adler;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700981 pExtra->original_ptr = buf;
982 pExtra->original_length = len;
Elliott Hughesa2501992011-08-26 19:39:54 -0700983
984 return newBuf + kGuardLen / 2;
985 }
986
987 /*
988 * Free up the guard buffer, scrub it, and return the original pointer.
989 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700990 static void* Destroy(void* dataBuf) {
991 const GuardedCopy* pExtra = GuardedCopy::FromData(dataBuf);
Elliott Hughesba8eee12012-01-24 20:25:24 -0800992 void* original_ptr = const_cast<void*>(pExtra->original_ptr);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700993 size_t len = pExtra->original_length;
994 DebugFree(dataBuf, len);
995 return original_ptr;
Elliott Hughesa2501992011-08-26 19:39:54 -0700996 }
997
998 /*
999 * Verify the guard area and, if "modOkay" is false, that the data itself
1000 * has not been altered.
1001 *
1002 * The caller has already checked that "dataBuf" is non-NULL.
1003 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001004 static void Check(const char* functionName, const void* dataBuf, bool modOkay) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001005 static const uint32_t kMagicCmp = kGuardMagic;
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001006 const uint8_t* fullBuf = ActualBuffer(dataBuf);
1007 const GuardedCopy* pExtra = GuardedCopy::FromData(dataBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -07001008
1009 /*
1010 * Before we do anything with "pExtra", check the magic number. We
1011 * do the check with memcmp rather than "==" in case the pointer is
1012 * unaligned. If it points to completely bogus memory we're going
1013 * to crash, but there's no easy way around that.
1014 */
1015 if (memcmp(&pExtra->magic, &kMagicCmp, 4) != 0) {
1016 uint8_t buf[4];
1017 memcpy(buf, &pExtra->magic, 4);
1018 LOG(ERROR) << StringPrintf("JNI: guard magic does not match "
1019 "(found 0x%02x%02x%02x%02x) -- incorrect data pointer %p?",
1020 buf[3], buf[2], buf[1], buf[0], dataBuf); /* assume little endian */
1021 JniAbort(functionName);
1022 }
1023
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001024 size_t len = pExtra->original_length;
Elliott Hughesa2501992011-08-26 19:39:54 -07001025
1026 /* check bottom half of guard; skip over optional checksum storage */
Elliott Hughesba8eee12012-01-24 20:25:24 -08001027 const uint16_t* pat = reinterpret_cast<const uint16_t*>(fullBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -07001028 for (size_t i = sizeof(GuardedCopy) / 2; i < (kGuardLen / 2 - sizeof(GuardedCopy)) / 2; i++) {
1029 if (pat[i] != kGuardPattern) {
Elliott Hughesba8eee12012-01-24 20:25:24 -08001030 LOG(ERROR) << "JNI: guard pattern(1) disturbed at " << reinterpret_cast<const void*>(fullBuf) << " + " << (i*2);
Elliott Hughesa2501992011-08-26 19:39:54 -07001031 JniAbort(functionName);
1032 }
1033 }
1034
1035 int offset = kGuardLen / 2 + len;
1036 if (offset & 0x01) {
1037 /* odd byte; expected value depends on endian-ness of host */
1038 const uint16_t patSample = kGuardPattern;
Elliott Hughesba8eee12012-01-24 20:25:24 -08001039 if (fullBuf[offset] != reinterpret_cast<const uint8_t*>(&patSample)[1]) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001040 LOG(ERROR) << "JNI: guard pattern disturbed in odd byte after "
Elliott Hughesba8eee12012-01-24 20:25:24 -08001041 << reinterpret_cast<const void*>(fullBuf) << " (+" << offset << ") "
Elliott Hughesa2501992011-08-26 19:39:54 -07001042 << StringPrintf("0x%02x 0x%02x", fullBuf[offset], ((const uint8_t*) &patSample)[1]);
1043 JniAbort(functionName);
1044 }
1045 offset++;
1046 }
1047
1048 /* check top half of guard */
Elliott Hughesba8eee12012-01-24 20:25:24 -08001049 pat = reinterpret_cast<const uint16_t*>(fullBuf + offset);
Elliott Hughesa2501992011-08-26 19:39:54 -07001050 for (size_t i = 0; i < kGuardLen / 4; i++) {
1051 if (pat[i] != kGuardPattern) {
Elliott Hughesba8eee12012-01-24 20:25:24 -08001052 LOG(ERROR) << "JNI: guard pattern(2) disturbed at " << reinterpret_cast<const void*>(fullBuf) << " + " << (offset + i*2);
Elliott Hughesa2501992011-08-26 19:39:54 -07001053 JniAbort(functionName);
1054 }
1055 }
1056
1057 /*
1058 * If modification is not expected, verify checksum. Strictly speaking
1059 * this is wrong: if we told the client that we made a copy, there's no
1060 * reason they can't alter the buffer.
1061 */
1062 if (!modOkay) {
1063 uLong adler = adler32(0L, Z_NULL, 0);
1064 adler = adler32(adler, (const Bytef*)dataBuf, len);
1065 if (pExtra->adler != adler) {
1066 LOG(ERROR) << StringPrintf("JNI: buffer modified (0x%08lx vs 0x%08lx) at addr %p", pExtra->adler, adler, dataBuf);
1067 JniAbort(functionName);
1068 }
1069 }
1070 }
1071
1072 private:
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001073 static uint8_t* DebugAlloc(size_t len) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001074 void* result = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
1075 if (result == MAP_FAILED) {
1076 PLOG(FATAL) << "GuardedCopy::create mmap(" << len << ") failed";
1077 }
1078 return reinterpret_cast<uint8_t*>(result);
1079 }
1080
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001081 static void DebugFree(void* dataBuf, size_t len) {
1082 uint8_t* fullBuf = ActualBuffer(dataBuf);
1083 size_t totalByteCount = ActualLength(len);
Elliott Hughesa2501992011-08-26 19:39:54 -07001084 // TODO: we could mprotect instead, and keep the allocation around for a while.
1085 // This would be even more expensive, but it might catch more errors.
1086 // if (mprotect(fullBuf, totalByteCount, PROT_NONE) != 0) {
1087 // LOGW("mprotect(PROT_NONE) failed: %s", strerror(errno));
1088 // }
1089 if (munmap(fullBuf, totalByteCount) != 0) {
Elliott Hughesba8eee12012-01-24 20:25:24 -08001090 PLOG(FATAL) << "munmap(" << reinterpret_cast<void*>(fullBuf) << ", " << totalByteCount << ") failed";
Elliott Hughesa2501992011-08-26 19:39:54 -07001091 }
1092 }
1093
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001094 static const uint8_t* ActualBuffer(const void* dataBuf) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001095 return reinterpret_cast<const uint8_t*>(dataBuf) - kGuardLen / 2;
1096 }
1097
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001098 static uint8_t* ActualBuffer(void* dataBuf) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001099 return reinterpret_cast<uint8_t*>(dataBuf) - kGuardLen / 2;
1100 }
1101
1102 // Underlying length of a user allocation of 'length' bytes.
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001103 static size_t ActualLength(size_t length) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001104 return (length + kGuardLen + 1) & ~0x01;
1105 }
1106};
1107
1108/*
1109 * Create a guarded copy of a primitive array. Modifications to the copied
1110 * data are allowed. Returns a pointer to the copied data.
1111 */
1112void* CreateGuardedPACopy(JNIEnv* env, const jarray java_array, jboolean* isCopy) {
1113 ScopedJniThreadState ts(env);
1114
1115 Array* a = Decode<Array*>(ts, java_array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001116 size_t component_size = a->GetClass()->GetComponentSize();
1117 size_t byte_count = a->GetLength() * component_size;
1118 void* result = GuardedCopy::Create(a->GetRawData(component_size), byte_count, true);
Elliott Hughesa2501992011-08-26 19:39:54 -07001119 if (isCopy != NULL) {
1120 *isCopy = JNI_TRUE;
1121 }
1122 return result;
1123}
1124
1125/*
1126 * Perform the array "release" operation, which may or may not copy data
1127 * back into the VM, and may or may not release the underlying storage.
1128 */
1129void ReleaseGuardedPACopy(JNIEnv* env, jarray java_array, void* dataBuf, int mode) {
1130 if (reinterpret_cast<uintptr_t>(dataBuf) == kNoCopyMagic) {
1131 return;
1132 }
1133
1134 ScopedJniThreadState ts(env);
1135 Array* a = Decode<Array*>(ts, java_array);
1136
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001137 GuardedCopy::Check(__FUNCTION__, dataBuf, true);
Elliott Hughesa2501992011-08-26 19:39:54 -07001138
1139 if (mode != JNI_ABORT) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001140 size_t len = GuardedCopy::FromData(dataBuf)->original_length;
Ian Rogersa15e67d2012-02-28 13:51:55 -08001141 memcpy(a->GetRawData(a->GetClass()->GetComponentSize()), dataBuf, len);
Elliott Hughesa2501992011-08-26 19:39:54 -07001142 }
1143 if (mode != JNI_COMMIT) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001144 GuardedCopy::Destroy(dataBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -07001145 }
1146}
1147
1148/*
1149 * ===========================================================================
1150 * JNI functions
1151 * ===========================================================================
1152 */
1153
1154class CheckJNI {
1155 public:
1156 static jint GetVersion(JNIEnv* env) {
1157 CHECK_JNI_ENTRY(kFlag_Default, "E", env);
1158 return CHECK_JNI_EXIT("I", baseEnv(env)->GetVersion(env));
1159 }
1160
1161 static jclass DefineClass(JNIEnv* env, const char* name, jobject loader, const jbyte* buf, jsize bufLen) {
1162 CHECK_JNI_ENTRY(kFlag_Default, "EuLpz", env, name, loader, buf, bufLen);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001163 sc.CheckClassName(name);
Elliott Hughesa2501992011-08-26 19:39:54 -07001164 return CHECK_JNI_EXIT("c", baseEnv(env)->DefineClass(env, name, loader, buf, bufLen));
1165 }
1166
1167 static jclass FindClass(JNIEnv* env, const char* name) {
1168 CHECK_JNI_ENTRY(kFlag_Default, "Eu", env, name);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001169 sc.CheckClassName(name);
Elliott Hughesa2501992011-08-26 19:39:54 -07001170 return CHECK_JNI_EXIT("c", baseEnv(env)->FindClass(env, name));
1171 }
1172
Elliott Hughese84278b2012-03-22 10:06:53 -07001173 static jclass GetSuperclass(JNIEnv* env, jclass c) {
1174 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1175 return CHECK_JNI_EXIT("c", baseEnv(env)->GetSuperclass(env, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001176 }
1177
Elliott Hughese84278b2012-03-22 10:06:53 -07001178 static jboolean IsAssignableFrom(JNIEnv* env, jclass c1, jclass c2) {
1179 CHECK_JNI_ENTRY(kFlag_Default, "Ecc", env, c1, c2);
1180 return CHECK_JNI_EXIT("b", baseEnv(env)->IsAssignableFrom(env, c1, c2));
Elliott Hughesa2501992011-08-26 19:39:54 -07001181 }
1182
1183 static jmethodID FromReflectedMethod(JNIEnv* env, jobject method) {
1184 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, method);
1185 // TODO: check that 'field' is a java.lang.reflect.Method.
1186 return CHECK_JNI_EXIT("m", baseEnv(env)->FromReflectedMethod(env, method));
1187 }
1188
1189 static jfieldID FromReflectedField(JNIEnv* env, jobject field) {
1190 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, field);
1191 // TODO: check that 'field' is a java.lang.reflect.Field.
1192 return CHECK_JNI_EXIT("f", baseEnv(env)->FromReflectedField(env, field));
1193 }
1194
1195 static jobject ToReflectedMethod(JNIEnv* env, jclass cls, jmethodID mid, jboolean isStatic) {
1196 CHECK_JNI_ENTRY(kFlag_Default, "Ecmb", env, cls, mid, isStatic);
1197 return CHECK_JNI_EXIT("L", baseEnv(env)->ToReflectedMethod(env, cls, mid, isStatic));
1198 }
1199
1200 static jobject ToReflectedField(JNIEnv* env, jclass cls, jfieldID fid, jboolean isStatic) {
1201 CHECK_JNI_ENTRY(kFlag_Default, "Ecfb", env, cls, fid, isStatic);
1202 return CHECK_JNI_EXIT("L", baseEnv(env)->ToReflectedField(env, cls, fid, isStatic));
1203 }
1204
1205 static jint Throw(JNIEnv* env, jthrowable obj) {
1206 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1207 // TODO: check that 'obj' is a java.lang.Throwable.
1208 return CHECK_JNI_EXIT("I", baseEnv(env)->Throw(env, obj));
1209 }
1210
Elliott Hughese84278b2012-03-22 10:06:53 -07001211 static jint ThrowNew(JNIEnv* env, jclass c, const char* message) {
1212 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Ecu", env, c, message);
1213 return CHECK_JNI_EXIT("I", baseEnv(env)->ThrowNew(env, c, message));
Elliott Hughesa2501992011-08-26 19:39:54 -07001214 }
1215
1216 static jthrowable ExceptionOccurred(JNIEnv* env) {
1217 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1218 return CHECK_JNI_EXIT("L", baseEnv(env)->ExceptionOccurred(env));
1219 }
1220
1221 static void ExceptionDescribe(JNIEnv* env) {
1222 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1223 baseEnv(env)->ExceptionDescribe(env);
1224 CHECK_JNI_EXIT_VOID();
1225 }
1226
1227 static void ExceptionClear(JNIEnv* env) {
1228 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1229 baseEnv(env)->ExceptionClear(env);
1230 CHECK_JNI_EXIT_VOID();
1231 }
1232
1233 static void FatalError(JNIEnv* env, const char* msg) {
1234 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Eu", env, msg);
1235 baseEnv(env)->FatalError(env, msg);
1236 CHECK_JNI_EXIT_VOID();
1237 }
1238
1239 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
1240 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EI", env, capacity);
1241 return CHECK_JNI_EXIT("I", baseEnv(env)->PushLocalFrame(env, capacity));
1242 }
1243
1244 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
1245 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, res);
1246 return CHECK_JNI_EXIT("L", baseEnv(env)->PopLocalFrame(env, res));
1247 }
1248
1249 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
1250 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1251 return CHECK_JNI_EXIT("L", baseEnv(env)->NewGlobalRef(env, obj));
1252 }
1253
1254 static jobject NewLocalRef(JNIEnv* env, jobject ref) {
1255 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, ref);
1256 return CHECK_JNI_EXIT("L", baseEnv(env)->NewLocalRef(env, ref));
1257 }
1258
1259 static void DeleteGlobalRef(JNIEnv* env, jobject globalRef) {
1260 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, globalRef);
1261 if (globalRef != NULL && GetIndirectRefKind(globalRef) != kGlobal) {
1262 LOG(ERROR) << "JNI ERROR: DeleteGlobalRef on " << GetIndirectRefKind(globalRef) << ": " << globalRef;
1263 JniAbort(__FUNCTION__);
1264 } else {
1265 baseEnv(env)->DeleteGlobalRef(env, globalRef);
1266 CHECK_JNI_EXIT_VOID();
1267 }
1268 }
1269
1270 static void DeleteWeakGlobalRef(JNIEnv* env, jweak weakGlobalRef) {
1271 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, weakGlobalRef);
1272 if (weakGlobalRef != NULL && GetIndirectRefKind(weakGlobalRef) != kWeakGlobal) {
1273 LOG(ERROR) << "JNI ERROR: DeleteWeakGlobalRef on " << GetIndirectRefKind(weakGlobalRef) << ": " << weakGlobalRef;
1274 JniAbort(__FUNCTION__);
1275 } else {
1276 baseEnv(env)->DeleteWeakGlobalRef(env, weakGlobalRef);
1277 CHECK_JNI_EXIT_VOID();
1278 }
1279 }
1280
1281 static void DeleteLocalRef(JNIEnv* env, jobject localRef) {
1282 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, localRef);
Ian Rogers959f8ed2012-02-07 16:33:37 -08001283 if (localRef != NULL && GetIndirectRefKind(localRef) != kLocal && !IsSirtLocalRef(env, localRef)) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001284 LOG(ERROR) << "JNI ERROR: DeleteLocalRef on " << GetIndirectRefKind(localRef) << ": " << localRef;
1285 JniAbort(__FUNCTION__);
1286 } else {
1287 baseEnv(env)->DeleteLocalRef(env, localRef);
1288 CHECK_JNI_EXIT_VOID();
1289 }
1290 }
1291
1292 static jint EnsureLocalCapacity(JNIEnv *env, jint capacity) {
1293 CHECK_JNI_ENTRY(kFlag_Default, "EI", env, capacity);
1294 return CHECK_JNI_EXIT("I", baseEnv(env)->EnsureLocalCapacity(env, capacity));
1295 }
1296
1297 static jboolean IsSameObject(JNIEnv* env, jobject ref1, jobject ref2) {
1298 CHECK_JNI_ENTRY(kFlag_Default, "ELL", env, ref1, ref2);
1299 return CHECK_JNI_EXIT("b", baseEnv(env)->IsSameObject(env, ref1, ref2));
1300 }
1301
Elliott Hughese84278b2012-03-22 10:06:53 -07001302 static jobject AllocObject(JNIEnv* env, jclass c) {
1303 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1304 return CHECK_JNI_EXIT("L", baseEnv(env)->AllocObject(env, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001305 }
1306
Elliott Hughese84278b2012-03-22 10:06:53 -07001307 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
1308 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
Elliott Hughesa2501992011-08-26 19:39:54 -07001309 va_list args;
1310 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -07001311 jobject result = baseEnv(env)->NewObjectV(env, c, mid, args);
Elliott Hughesa2501992011-08-26 19:39:54 -07001312 va_end(args);
1313 return CHECK_JNI_EXIT("L", result);
1314 }
1315
Elliott Hughese84278b2012-03-22 10:06:53 -07001316 static jobject NewObjectV(JNIEnv* env, jclass c, jmethodID mid, va_list args) {
1317 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
1318 return CHECK_JNI_EXIT("L", baseEnv(env)->NewObjectV(env, c, mid, args));
Elliott Hughesa2501992011-08-26 19:39:54 -07001319 }
1320
Elliott Hughese84278b2012-03-22 10:06:53 -07001321 static jobject NewObjectA(JNIEnv* env, jclass c, jmethodID mid, jvalue* args) {
1322 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
1323 return CHECK_JNI_EXIT("L", baseEnv(env)->NewObjectA(env, c, mid, args));
Elliott Hughesa2501992011-08-26 19:39:54 -07001324 }
1325
1326 static jclass GetObjectClass(JNIEnv* env, jobject obj) {
1327 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1328 return CHECK_JNI_EXIT("c", baseEnv(env)->GetObjectClass(env, obj));
1329 }
1330
Elliott Hughese84278b2012-03-22 10:06:53 -07001331 static jboolean IsInstanceOf(JNIEnv* env, jobject obj, jclass c) {
1332 CHECK_JNI_ENTRY(kFlag_Default, "ELc", env, obj, c);
1333 return CHECK_JNI_EXIT("b", baseEnv(env)->IsInstanceOf(env, obj, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001334 }
1335
Elliott Hughese84278b2012-03-22 10:06:53 -07001336 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1337 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1338 return CHECK_JNI_EXIT("m", baseEnv(env)->GetMethodID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001339 }
1340
Elliott Hughese84278b2012-03-22 10:06:53 -07001341 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1342 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1343 return CHECK_JNI_EXIT("f", baseEnv(env)->GetFieldID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001344 }
1345
Elliott Hughese84278b2012-03-22 10:06:53 -07001346 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1347 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1348 return CHECK_JNI_EXIT("m", baseEnv(env)->GetStaticMethodID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001349 }
1350
Elliott Hughese84278b2012-03-22 10:06:53 -07001351 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1352 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1353 return CHECK_JNI_EXIT("f", baseEnv(env)->GetStaticFieldID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001354 }
1355
1356#define FIELD_ACCESSORS(_ctype, _jname, _type) \
Elliott Hughese84278b2012-03-22 10:06:53 -07001357 static _ctype GetStatic##_jname##Field(JNIEnv* env, jclass c, jfieldID fid) { \
1358 CHECK_JNI_ENTRY(kFlag_Default, "Ecf", env, c, fid); \
1359 sc.CheckStaticFieldID(c, fid); \
1360 return CHECK_JNI_EXIT(_type, baseEnv(env)->GetStatic##_jname##Field(env, c, fid)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001361 } \
1362 static _ctype Get##_jname##Field(JNIEnv* env, jobject obj, jfieldID fid) { \
1363 CHECK_JNI_ENTRY(kFlag_Default, "ELf", env, obj, fid); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001364 sc.CheckInstanceFieldID(obj, fid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001365 return CHECK_JNI_EXIT(_type, baseEnv(env)->Get##_jname##Field(env, obj, fid)); \
1366 } \
Elliott Hughese84278b2012-03-22 10:06:53 -07001367 static void SetStatic##_jname##Field(JNIEnv* env, jclass c, jfieldID fid, _ctype value) { \
1368 CHECK_JNI_ENTRY(kFlag_Default, "Ecf" _type, env, c, fid, value); \
1369 sc.CheckStaticFieldID(c, fid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001370 /* "value" arg only used when type == ref */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001371 sc.CheckFieldType((jobject)(uint32_t)value, fid, _type[0], true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001372 baseEnv(env)->SetStatic##_jname##Field(env, c, fid, value); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001373 CHECK_JNI_EXIT_VOID(); \
1374 } \
1375 static void Set##_jname##Field(JNIEnv* env, jobject obj, jfieldID fid, _ctype value) { \
1376 CHECK_JNI_ENTRY(kFlag_Default, "ELf" _type, env, obj, fid, value); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001377 sc.CheckInstanceFieldID(obj, fid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001378 /* "value" arg only used when type == ref */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001379 sc.CheckFieldType((jobject)(uint32_t) value, fid, _type[0], false); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001380 baseEnv(env)->Set##_jname##Field(env, obj, fid, value); \
1381 CHECK_JNI_EXIT_VOID(); \
1382 }
1383
1384FIELD_ACCESSORS(jobject, Object, "L");
1385FIELD_ACCESSORS(jboolean, Boolean, "Z");
1386FIELD_ACCESSORS(jbyte, Byte, "B");
1387FIELD_ACCESSORS(jchar, Char, "C");
1388FIELD_ACCESSORS(jshort, Short, "S");
1389FIELD_ACCESSORS(jint, Int, "I");
1390FIELD_ACCESSORS(jlong, Long, "J");
1391FIELD_ACCESSORS(jfloat, Float, "F");
1392FIELD_ACCESSORS(jdouble, Double, "D");
1393
1394#define CALL(_ctype, _jname, _retdecl, _retasgn, _retok, _retsig) \
1395 /* Virtual... */ \
1396 static _ctype Call##_jname##Method(JNIEnv* env, jobject obj, \
1397 jmethodID mid, ...) \
1398 { \
1399 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001400 sc.CheckSig(mid, _retsig, false); \
1401 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001402 _retdecl; \
1403 va_list args; \
1404 va_start(args, mid); \
Elliott Hughesba8eee12012-01-24 20:25:24 -08001405 _retasgn(baseEnv(env)->Call##_jname##MethodV(env, obj, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001406 va_end(args); \
1407 _retok; \
1408 } \
1409 static _ctype Call##_jname##MethodV(JNIEnv* env, jobject obj, \
1410 jmethodID mid, va_list args) \
1411 { \
1412 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001413 sc.CheckSig(mid, _retsig, false); \
1414 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001415 _retdecl; \
Elliott Hughesba8eee12012-01-24 20:25:24 -08001416 _retasgn(baseEnv(env)->Call##_jname##MethodV(env, obj, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001417 _retok; \
1418 } \
1419 static _ctype Call##_jname##MethodA(JNIEnv* env, jobject obj, \
1420 jmethodID mid, jvalue* args) \
1421 { \
1422 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001423 sc.CheckSig(mid, _retsig, false); \
1424 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001425 _retdecl; \
Elliott Hughesba8eee12012-01-24 20:25:24 -08001426 _retasgn(baseEnv(env)->Call##_jname##MethodA(env, obj, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001427 _retok; \
1428 } \
1429 /* Non-virtual... */ \
1430 static _ctype CallNonvirtual##_jname##Method(JNIEnv* env, \
Elliott Hughese84278b2012-03-22 10:06:53 -07001431 jobject obj, 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, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001434 sc.CheckSig(mid, _retsig, false); \
1435 sc.CheckVirtualMethod(obj, 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)->CallNonvirtual##_jname##MethodV(env, obj, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001440 va_end(args); \
1441 _retok; \
1442 } \
1443 static _ctype CallNonvirtual##_jname##MethodV(JNIEnv* env, \
Elliott Hughese84278b2012-03-22 10:06:53 -07001444 jobject obj, jclass c, jmethodID mid, va_list args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001445 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001446 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001447 sc.CheckSig(mid, _retsig, false); \
1448 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001449 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001450 _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodV(env, obj, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001451 _retok; \
1452 } \
1453 static _ctype CallNonvirtual##_jname##MethodA(JNIEnv* env, \
Elliott Hughese84278b2012-03-22 10:06:53 -07001454 jobject obj, jclass c, jmethodID mid, jvalue* args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001455 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001456 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001457 sc.CheckSig(mid, _retsig, false); \
1458 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001459 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001460 _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodA(env, obj, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001461 _retok; \
1462 } \
1463 /* Static... */ \
Elliott Hughese84278b2012-03-22 10:06:53 -07001464 static _ctype CallStatic##_jname##Method(JNIEnv* env, jclass c, jmethodID mid, ...) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001465 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001466 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001467 sc.CheckSig(mid, _retsig, true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001468 sc.CheckStaticMethod(c, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001469 _retdecl; \
1470 va_list args; \
1471 va_start(args, mid); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001472 _retasgn(baseEnv(env)->CallStatic##_jname##MethodV(env, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001473 va_end(args); \
1474 _retok; \
1475 } \
Elliott Hughese84278b2012-03-22 10:06:53 -07001476 static _ctype CallStatic##_jname##MethodV(JNIEnv* env, jclass c, jmethodID mid, va_list args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001477 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001478 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001479 sc.CheckSig(mid, _retsig, true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001480 sc.CheckStaticMethod(c, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001481 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001482 _retasgn(baseEnv(env)->CallStatic##_jname##MethodV(env, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001483 _retok; \
1484 } \
Elliott Hughese84278b2012-03-22 10:06:53 -07001485 static _ctype CallStatic##_jname##MethodA(JNIEnv* env, jclass c, jmethodID mid, jvalue* args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001486 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001487 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001488 sc.CheckSig(mid, _retsig, true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001489 sc.CheckStaticMethod(c, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001490 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001491 _retasgn(baseEnv(env)->CallStatic##_jname##MethodA(env, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001492 _retok; \
1493 }
1494
1495#define NON_VOID_RETURN(_retsig, _ctype) return CHECK_JNI_EXIT(_retsig, (_ctype) result)
1496#define VOID_RETURN CHECK_JNI_EXIT_VOID()
1497
Elliott Hughesba8eee12012-01-24 20:25:24 -08001498CALL(jobject, Object, Object* result, result = reinterpret_cast<Object*>, NON_VOID_RETURN("L", jobject), "L");
1499CALL(jboolean, Boolean, jboolean result, result =, NON_VOID_RETURN("Z", jboolean), "Z");
1500CALL(jbyte, Byte, jbyte result, result =, NON_VOID_RETURN("B", jbyte), "B");
1501CALL(jchar, Char, jchar result, result =, NON_VOID_RETURN("C", jchar), "C");
1502CALL(jshort, Short, jshort result, result =, NON_VOID_RETURN("S", jshort), "S");
1503CALL(jint, Int, jint result, result =, NON_VOID_RETURN("I", jint), "I");
1504CALL(jlong, Long, jlong result, result =, NON_VOID_RETURN("J", jlong), "J");
1505CALL(jfloat, Float, jfloat result, result =, NON_VOID_RETURN("F", jfloat), "F");
1506CALL(jdouble, Double, jdouble result, result =, NON_VOID_RETURN("D", jdouble), "D");
Elliott Hughesa2501992011-08-26 19:39:54 -07001507CALL(void, Void, , , VOID_RETURN, "V");
1508
1509 static jstring NewString(JNIEnv* env, const jchar* unicodeChars, jsize len) {
1510 CHECK_JNI_ENTRY(kFlag_Default, "Epz", env, unicodeChars, len);
1511 return CHECK_JNI_EXIT("s", baseEnv(env)->NewString(env, unicodeChars, len));
1512 }
1513
1514 static jsize GetStringLength(JNIEnv* env, jstring string) {
1515 CHECK_JNI_ENTRY(kFlag_CritOkay, "Es", env, string);
1516 return CHECK_JNI_EXIT("I", baseEnv(env)->GetStringLength(env, string));
1517 }
1518
1519 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* isCopy) {
1520 CHECK_JNI_ENTRY(kFlag_CritOkay, "Esp", env, java_string, isCopy);
1521 const jchar* result = baseEnv(env)->GetStringChars(env, java_string, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001522 if (sc.ForceCopy() && result != NULL) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001523 ScopedJniThreadState ts(env);
1524 String* s = Decode<String*>(ts, java_string);
1525 int byteCount = s->GetLength() * 2;
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001526 result = (const jchar*) GuardedCopy::Create(result, byteCount, false);
Elliott Hughesa2501992011-08-26 19:39:54 -07001527 if (isCopy != NULL) {
1528 *isCopy = JNI_TRUE;
1529 }
1530 }
1531 return CHECK_JNI_EXIT("p", result);
1532 }
1533
1534 static void ReleaseStringChars(JNIEnv* env, jstring string, const jchar* chars) {
1535 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "Esp", env, string, chars);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001536 sc.CheckNonNull(chars);
1537 if (sc.ForceCopy()) {
1538 GuardedCopy::Check(__FUNCTION__, chars, false);
Elliott Hughesba8eee12012-01-24 20:25:24 -08001539 chars = reinterpret_cast<const jchar*>(GuardedCopy::Destroy(const_cast<jchar*>(chars)));
Elliott Hughesa2501992011-08-26 19:39:54 -07001540 }
1541 baseEnv(env)->ReleaseStringChars(env, string, chars);
1542 CHECK_JNI_EXIT_VOID();
1543 }
1544
1545 static jstring NewStringUTF(JNIEnv* env, const char* bytes) {
1546 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Eu", env, bytes); // TODO: show pointer and truncate string.
1547 return CHECK_JNI_EXIT("s", baseEnv(env)->NewStringUTF(env, bytes));
1548 }
1549
1550 static jsize GetStringUTFLength(JNIEnv* env, jstring string) {
1551 CHECK_JNI_ENTRY(kFlag_CritOkay, "Es", env, string);
1552 return CHECK_JNI_EXIT("I", baseEnv(env)->GetStringUTFLength(env, string));
1553 }
1554
1555 static const char* GetStringUTFChars(JNIEnv* env, jstring string, jboolean* isCopy) {
1556 CHECK_JNI_ENTRY(kFlag_CritOkay, "Esp", env, string, isCopy);
1557 const char* result = baseEnv(env)->GetStringUTFChars(env, string, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001558 if (sc.ForceCopy() && result != NULL) {
1559 result = (const char*) GuardedCopy::Create(result, strlen(result) + 1, false);
Elliott Hughesa2501992011-08-26 19:39:54 -07001560 if (isCopy != NULL) {
1561 *isCopy = JNI_TRUE;
1562 }
1563 }
1564 return CHECK_JNI_EXIT("u", result); // TODO: show pointer and truncate string.
1565 }
1566
1567 static void ReleaseStringUTFChars(JNIEnv* env, jstring string, const char* utf) {
1568 CHECK_JNI_ENTRY(kFlag_ExcepOkay | kFlag_Release, "Esu", env, string, utf); // TODO: show pointer and truncate string.
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001569 if (sc.ForceCopy()) {
1570 GuardedCopy::Check(__FUNCTION__, utf, false);
Elliott Hughesba8eee12012-01-24 20:25:24 -08001571 utf = reinterpret_cast<const char*>(GuardedCopy::Destroy(const_cast<char*>(utf)));
Elliott Hughesa2501992011-08-26 19:39:54 -07001572 }
1573 baseEnv(env)->ReleaseStringUTFChars(env, string, utf);
1574 CHECK_JNI_EXIT_VOID();
1575 }
1576
1577 static jsize GetArrayLength(JNIEnv* env, jarray array) {
1578 CHECK_JNI_ENTRY(kFlag_CritOkay, "Ea", env, array);
1579 return CHECK_JNI_EXIT("I", baseEnv(env)->GetArrayLength(env, array));
1580 }
1581
1582 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass elementClass, jobject initialElement) {
1583 CHECK_JNI_ENTRY(kFlag_Default, "EzcL", env, length, elementClass, initialElement);
1584 return CHECK_JNI_EXIT("a", baseEnv(env)->NewObjectArray(env, length, elementClass, initialElement));
1585 }
1586
1587 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index) {
1588 CHECK_JNI_ENTRY(kFlag_Default, "EaI", env, array, index);
1589 return CHECK_JNI_EXIT("L", baseEnv(env)->GetObjectArrayElement(env, array, index));
1590 }
1591
1592 static void SetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index, jobject value) {
1593 CHECK_JNI_ENTRY(kFlag_Default, "EaIL", env, array, index, value);
1594 baseEnv(env)->SetObjectArrayElement(env, array, index, value);
1595 CHECK_JNI_EXIT_VOID();
1596 }
1597
1598#define NEW_PRIMITIVE_ARRAY(_artype, _jname) \
1599 static _artype New##_jname##Array(JNIEnv* env, jsize length) { \
1600 CHECK_JNI_ENTRY(kFlag_Default, "Ez", env, length); \
1601 return CHECK_JNI_EXIT("a", baseEnv(env)->New##_jname##Array(env, length)); \
1602 }
1603NEW_PRIMITIVE_ARRAY(jbooleanArray, Boolean);
1604NEW_PRIMITIVE_ARRAY(jbyteArray, Byte);
1605NEW_PRIMITIVE_ARRAY(jcharArray, Char);
1606NEW_PRIMITIVE_ARRAY(jshortArray, Short);
1607NEW_PRIMITIVE_ARRAY(jintArray, Int);
1608NEW_PRIMITIVE_ARRAY(jlongArray, Long);
1609NEW_PRIMITIVE_ARRAY(jfloatArray, Float);
1610NEW_PRIMITIVE_ARRAY(jdoubleArray, Double);
1611
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001612struct ForceCopyGetChecker {
Elliott Hughesba8eee12012-01-24 20:25:24 -08001613 public:
Elliott Hughesa2501992011-08-26 19:39:54 -07001614 ForceCopyGetChecker(ScopedCheck& sc, jboolean* isCopy) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001615 force_copy = sc.ForceCopy();
1616 no_copy = 0;
1617 if (force_copy && isCopy != NULL) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001618 /* capture this before the base call tramples on it */
Elliott Hughesba8eee12012-01-24 20:25:24 -08001619 no_copy = *reinterpret_cast<uint32_t*>(isCopy);
Elliott Hughesa2501992011-08-26 19:39:54 -07001620 }
1621 }
1622
1623 template<typename ResultT>
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001624 ResultT Check(JNIEnv* env, jarray array, jboolean* isCopy, ResultT result) {
1625 if (force_copy && result != NULL) {
1626 if (no_copy != kNoCopyMagic) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001627 result = reinterpret_cast<ResultT>(CreateGuardedPACopy(env, array, isCopy));
1628 }
1629 }
1630 return result;
1631 }
1632
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001633 uint32_t no_copy;
1634 bool force_copy;
Elliott Hughesa2501992011-08-26 19:39:54 -07001635};
1636
1637#define GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \
1638 static _ctype* Get##_jname##ArrayElements(JNIEnv* env, _ctype##Array array, jboolean* isCopy) { \
1639 CHECK_JNI_ENTRY(kFlag_Default, "Eap", env, array, isCopy); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001640 _ctype* result = ForceCopyGetChecker(sc, isCopy).Check(env, array, isCopy, baseEnv(env)->Get##_jname##ArrayElements(env, array, isCopy)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001641 return CHECK_JNI_EXIT("p", result); \
1642 }
1643
1644#define RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \
1645 static void Release##_jname##ArrayElements(JNIEnv* env, _ctype##Array array, _ctype* elems, jint mode) { \
1646 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "Eapr", env, array, elems, mode); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001647 sc.CheckNonNull(elems); \
1648 if (sc.ForceCopy()) { \
Elliott Hughesa2501992011-08-26 19:39:54 -07001649 ReleaseGuardedPACopy(env, array, elems, mode); \
1650 } \
1651 baseEnv(env)->Release##_jname##ArrayElements(env, array, elems, mode); \
1652 CHECK_JNI_EXIT_VOID(); \
1653 }
1654
1655#define GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \
1656 static void Get##_jname##ArrayRegion(JNIEnv* env, _ctype##Array array, jsize start, jsize len, _ctype* buf) { \
1657 CHECK_JNI_ENTRY(kFlag_Default, "EaIIp", env, array, start, len, buf); \
1658 baseEnv(env)->Get##_jname##ArrayRegion(env, array, start, len, buf); \
1659 CHECK_JNI_EXIT_VOID(); \
1660 }
1661
1662#define SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \
1663 static void Set##_jname##ArrayRegion(JNIEnv* env, _ctype##Array array, jsize start, jsize len, const _ctype* buf) { \
1664 CHECK_JNI_ENTRY(kFlag_Default, "EaIIp", env, array, start, len, buf); \
1665 baseEnv(env)->Set##_jname##ArrayRegion(env, array, start, len, buf); \
1666 CHECK_JNI_EXIT_VOID(); \
1667 }
1668
1669#define PRIMITIVE_ARRAY_FUNCTIONS(_ctype, _jname, _typechar) \
1670 GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \
1671 RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \
1672 GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname); \
1673 SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname);
1674
1675/* TODO: verify primitive array type matches call type */
1676PRIMITIVE_ARRAY_FUNCTIONS(jboolean, Boolean, 'Z');
1677PRIMITIVE_ARRAY_FUNCTIONS(jbyte, Byte, 'B');
1678PRIMITIVE_ARRAY_FUNCTIONS(jchar, Char, 'C');
1679PRIMITIVE_ARRAY_FUNCTIONS(jshort, Short, 'S');
1680PRIMITIVE_ARRAY_FUNCTIONS(jint, Int, 'I');
1681PRIMITIVE_ARRAY_FUNCTIONS(jlong, Long, 'J');
1682PRIMITIVE_ARRAY_FUNCTIONS(jfloat, Float, 'F');
1683PRIMITIVE_ARRAY_FUNCTIONS(jdouble, Double, 'D');
1684
Elliott Hughese84278b2012-03-22 10:06:53 -07001685 static jint RegisterNatives(JNIEnv* env, jclass c, const JNINativeMethod* methods, jint nMethods) {
1686 CHECK_JNI_ENTRY(kFlag_Default, "EcpI", env, c, methods, nMethods);
1687 return CHECK_JNI_EXIT("I", baseEnv(env)->RegisterNatives(env, c, methods, nMethods));
Elliott Hughesa2501992011-08-26 19:39:54 -07001688 }
1689
Elliott Hughese84278b2012-03-22 10:06:53 -07001690 static jint UnregisterNatives(JNIEnv* env, jclass c) {
1691 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1692 return CHECK_JNI_EXIT("I", baseEnv(env)->UnregisterNatives(env, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001693 }
1694
1695 static jint MonitorEnter(JNIEnv* env, jobject obj) {
1696 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
Elliott Hughesa92853e2012-02-07 16:09:27 -08001697 if (!sc.CheckInstance(ScopedCheck::kObject, obj)) {
1698 return JNI_ERR; // Only for jni_internal_test. Real code will have aborted already.
1699 }
Elliott Hughesa2501992011-08-26 19:39:54 -07001700 return CHECK_JNI_EXIT("I", baseEnv(env)->MonitorEnter(env, obj));
1701 }
1702
1703 static jint MonitorExit(JNIEnv* env, jobject obj) {
1704 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, obj);
Elliott Hughesa92853e2012-02-07 16:09:27 -08001705 if (!sc.CheckInstance(ScopedCheck::kObject, obj)) {
1706 return JNI_ERR; // Only for jni_internal_test. Real code will have aborted already.
1707 }
Elliott Hughesa2501992011-08-26 19:39:54 -07001708 return CHECK_JNI_EXIT("I", baseEnv(env)->MonitorExit(env, obj));
1709 }
1710
1711 static jint GetJavaVM(JNIEnv *env, JavaVM **vm) {
1712 CHECK_JNI_ENTRY(kFlag_Default, "Ep", env, vm);
1713 return CHECK_JNI_EXIT("I", baseEnv(env)->GetJavaVM(env, vm));
1714 }
1715
1716 static void GetStringRegion(JNIEnv* env, jstring str, jsize start, jsize len, jchar* buf) {
1717 CHECK_JNI_ENTRY(kFlag_CritOkay, "EsIIp", env, str, start, len, buf);
1718 baseEnv(env)->GetStringRegion(env, str, start, len, buf);
1719 CHECK_JNI_EXIT_VOID();
1720 }
1721
1722 static void GetStringUTFRegion(JNIEnv* env, jstring str, jsize start, jsize len, char* buf) {
1723 CHECK_JNI_ENTRY(kFlag_CritOkay, "EsIIp", env, str, start, len, buf);
1724 baseEnv(env)->GetStringUTFRegion(env, str, start, len, buf);
1725 CHECK_JNI_EXIT_VOID();
1726 }
1727
1728 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* isCopy) {
1729 CHECK_JNI_ENTRY(kFlag_CritGet, "Eap", env, array, isCopy);
1730 void* result = baseEnv(env)->GetPrimitiveArrayCritical(env, array, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001731 if (sc.ForceCopy() && result != NULL) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001732 result = CreateGuardedPACopy(env, array, isCopy);
1733 }
1734 return CHECK_JNI_EXIT("p", result);
1735 }
1736
1737 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* carray, jint mode) {
1738 CHECK_JNI_ENTRY(kFlag_CritRelease | kFlag_ExcepOkay, "Eapr", env, array, carray, mode);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001739 sc.CheckNonNull(carray);
1740 if (sc.ForceCopy()) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001741 ReleaseGuardedPACopy(env, array, carray, mode);
1742 }
1743 baseEnv(env)->ReleasePrimitiveArrayCritical(env, array, carray, mode);
1744 CHECK_JNI_EXIT_VOID();
1745 }
1746
1747 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* isCopy) {
1748 CHECK_JNI_ENTRY(kFlag_CritGet, "Esp", env, java_string, isCopy);
1749 const jchar* result = baseEnv(env)->GetStringCritical(env, java_string, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001750 if (sc.ForceCopy() && result != NULL) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001751 ScopedJniThreadState ts(env);
1752 String* s = Decode<String*>(ts, java_string);
1753 int byteCount = s->GetLength() * 2;
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001754 result = (const jchar*) GuardedCopy::Create(result, byteCount, false);
Elliott Hughesa2501992011-08-26 19:39:54 -07001755 if (isCopy != NULL) {
1756 *isCopy = JNI_TRUE;
1757 }
1758 }
1759 return CHECK_JNI_EXIT("p", result);
1760 }
1761
1762 static void ReleaseStringCritical(JNIEnv* env, jstring string, const jchar* carray) {
1763 CHECK_JNI_ENTRY(kFlag_CritRelease | kFlag_ExcepOkay, "Esp", env, string, carray);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001764 sc.CheckNonNull(carray);
1765 if (sc.ForceCopy()) {
1766 GuardedCopy::Check(__FUNCTION__, carray, false);
Elliott Hughesba8eee12012-01-24 20:25:24 -08001767 carray = reinterpret_cast<const jchar*>(GuardedCopy::Destroy(const_cast<jchar*>(carray)));
Elliott Hughesa2501992011-08-26 19:39:54 -07001768 }
1769 baseEnv(env)->ReleaseStringCritical(env, string, carray);
1770 CHECK_JNI_EXIT_VOID();
1771 }
1772
1773 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
1774 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1775 return CHECK_JNI_EXIT("L", baseEnv(env)->NewWeakGlobalRef(env, obj));
1776 }
1777
1778 static jboolean ExceptionCheck(JNIEnv* env) {
1779 CHECK_JNI_ENTRY(kFlag_CritOkay | kFlag_ExcepOkay, "E", env);
1780 return CHECK_JNI_EXIT("b", baseEnv(env)->ExceptionCheck(env));
1781 }
1782
1783 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject obj) {
1784 // Note: we use "Ep" rather than "EL" because this is the one JNI function
1785 // that it's okay to pass an invalid reference to.
1786 CHECK_JNI_ENTRY(kFlag_Default, "Ep", env, obj);
1787 // TODO: proper decoding of jobjectRefType!
1788 return CHECK_JNI_EXIT("I", baseEnv(env)->GetObjectRefType(env, obj));
1789 }
1790
1791 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
1792 CHECK_JNI_ENTRY(kFlag_Default, "EpJ", env, address, capacity);
1793 if (address == NULL) {
1794 LOG(ERROR) << "JNI ERROR: non-nullable address is NULL";
1795 JniAbort(__FUNCTION__);
1796 }
1797 if (capacity <= 0) {
1798 LOG(ERROR) << "JNI ERROR: capacity must be greater than 0: " << capacity;
1799 JniAbort(__FUNCTION__);
1800 }
1801 return CHECK_JNI_EXIT("L", baseEnv(env)->NewDirectByteBuffer(env, address, capacity));
1802 }
1803
1804 static void* GetDirectBufferAddress(JNIEnv* env, jobject buf) {
1805 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, buf);
1806 // TODO: check that 'buf' is a java.nio.Buffer.
1807 return CHECK_JNI_EXIT("p", baseEnv(env)->GetDirectBufferAddress(env, buf));
1808 }
1809
1810 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject buf) {
1811 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, buf);
1812 // TODO: check that 'buf' is a java.nio.Buffer.
1813 return CHECK_JNI_EXIT("J", baseEnv(env)->GetDirectBufferCapacity(env, buf));
1814 }
1815
1816 private:
1817 static inline const JNINativeInterface* baseEnv(JNIEnv* env) {
1818 return reinterpret_cast<JNIEnvExt*>(env)->unchecked_functions;
1819 }
1820};
1821
1822const JNINativeInterface gCheckNativeInterface = {
1823 NULL, // reserved0.
1824 NULL, // reserved1.
1825 NULL, // reserved2.
1826 NULL, // reserved3.
1827 CheckJNI::GetVersion,
1828 CheckJNI::DefineClass,
1829 CheckJNI::FindClass,
1830 CheckJNI::FromReflectedMethod,
1831 CheckJNI::FromReflectedField,
1832 CheckJNI::ToReflectedMethod,
1833 CheckJNI::GetSuperclass,
1834 CheckJNI::IsAssignableFrom,
1835 CheckJNI::ToReflectedField,
1836 CheckJNI::Throw,
1837 CheckJNI::ThrowNew,
1838 CheckJNI::ExceptionOccurred,
1839 CheckJNI::ExceptionDescribe,
1840 CheckJNI::ExceptionClear,
1841 CheckJNI::FatalError,
1842 CheckJNI::PushLocalFrame,
1843 CheckJNI::PopLocalFrame,
1844 CheckJNI::NewGlobalRef,
1845 CheckJNI::DeleteGlobalRef,
1846 CheckJNI::DeleteLocalRef,
1847 CheckJNI::IsSameObject,
1848 CheckJNI::NewLocalRef,
1849 CheckJNI::EnsureLocalCapacity,
1850 CheckJNI::AllocObject,
1851 CheckJNI::NewObject,
1852 CheckJNI::NewObjectV,
1853 CheckJNI::NewObjectA,
1854 CheckJNI::GetObjectClass,
1855 CheckJNI::IsInstanceOf,
1856 CheckJNI::GetMethodID,
1857 CheckJNI::CallObjectMethod,
1858 CheckJNI::CallObjectMethodV,
1859 CheckJNI::CallObjectMethodA,
1860 CheckJNI::CallBooleanMethod,
1861 CheckJNI::CallBooleanMethodV,
1862 CheckJNI::CallBooleanMethodA,
1863 CheckJNI::CallByteMethod,
1864 CheckJNI::CallByteMethodV,
1865 CheckJNI::CallByteMethodA,
1866 CheckJNI::CallCharMethod,
1867 CheckJNI::CallCharMethodV,
1868 CheckJNI::CallCharMethodA,
1869 CheckJNI::CallShortMethod,
1870 CheckJNI::CallShortMethodV,
1871 CheckJNI::CallShortMethodA,
1872 CheckJNI::CallIntMethod,
1873 CheckJNI::CallIntMethodV,
1874 CheckJNI::CallIntMethodA,
1875 CheckJNI::CallLongMethod,
1876 CheckJNI::CallLongMethodV,
1877 CheckJNI::CallLongMethodA,
1878 CheckJNI::CallFloatMethod,
1879 CheckJNI::CallFloatMethodV,
1880 CheckJNI::CallFloatMethodA,
1881 CheckJNI::CallDoubleMethod,
1882 CheckJNI::CallDoubleMethodV,
1883 CheckJNI::CallDoubleMethodA,
1884 CheckJNI::CallVoidMethod,
1885 CheckJNI::CallVoidMethodV,
1886 CheckJNI::CallVoidMethodA,
1887 CheckJNI::CallNonvirtualObjectMethod,
1888 CheckJNI::CallNonvirtualObjectMethodV,
1889 CheckJNI::CallNonvirtualObjectMethodA,
1890 CheckJNI::CallNonvirtualBooleanMethod,
1891 CheckJNI::CallNonvirtualBooleanMethodV,
1892 CheckJNI::CallNonvirtualBooleanMethodA,
1893 CheckJNI::CallNonvirtualByteMethod,
1894 CheckJNI::CallNonvirtualByteMethodV,
1895 CheckJNI::CallNonvirtualByteMethodA,
1896 CheckJNI::CallNonvirtualCharMethod,
1897 CheckJNI::CallNonvirtualCharMethodV,
1898 CheckJNI::CallNonvirtualCharMethodA,
1899 CheckJNI::CallNonvirtualShortMethod,
1900 CheckJNI::CallNonvirtualShortMethodV,
1901 CheckJNI::CallNonvirtualShortMethodA,
1902 CheckJNI::CallNonvirtualIntMethod,
1903 CheckJNI::CallNonvirtualIntMethodV,
1904 CheckJNI::CallNonvirtualIntMethodA,
1905 CheckJNI::CallNonvirtualLongMethod,
1906 CheckJNI::CallNonvirtualLongMethodV,
1907 CheckJNI::CallNonvirtualLongMethodA,
1908 CheckJNI::CallNonvirtualFloatMethod,
1909 CheckJNI::CallNonvirtualFloatMethodV,
1910 CheckJNI::CallNonvirtualFloatMethodA,
1911 CheckJNI::CallNonvirtualDoubleMethod,
1912 CheckJNI::CallNonvirtualDoubleMethodV,
1913 CheckJNI::CallNonvirtualDoubleMethodA,
1914 CheckJNI::CallNonvirtualVoidMethod,
1915 CheckJNI::CallNonvirtualVoidMethodV,
1916 CheckJNI::CallNonvirtualVoidMethodA,
1917 CheckJNI::GetFieldID,
1918 CheckJNI::GetObjectField,
1919 CheckJNI::GetBooleanField,
1920 CheckJNI::GetByteField,
1921 CheckJNI::GetCharField,
1922 CheckJNI::GetShortField,
1923 CheckJNI::GetIntField,
1924 CheckJNI::GetLongField,
1925 CheckJNI::GetFloatField,
1926 CheckJNI::GetDoubleField,
1927 CheckJNI::SetObjectField,
1928 CheckJNI::SetBooleanField,
1929 CheckJNI::SetByteField,
1930 CheckJNI::SetCharField,
1931 CheckJNI::SetShortField,
1932 CheckJNI::SetIntField,
1933 CheckJNI::SetLongField,
1934 CheckJNI::SetFloatField,
1935 CheckJNI::SetDoubleField,
1936 CheckJNI::GetStaticMethodID,
1937 CheckJNI::CallStaticObjectMethod,
1938 CheckJNI::CallStaticObjectMethodV,
1939 CheckJNI::CallStaticObjectMethodA,
1940 CheckJNI::CallStaticBooleanMethod,
1941 CheckJNI::CallStaticBooleanMethodV,
1942 CheckJNI::CallStaticBooleanMethodA,
1943 CheckJNI::CallStaticByteMethod,
1944 CheckJNI::CallStaticByteMethodV,
1945 CheckJNI::CallStaticByteMethodA,
1946 CheckJNI::CallStaticCharMethod,
1947 CheckJNI::CallStaticCharMethodV,
1948 CheckJNI::CallStaticCharMethodA,
1949 CheckJNI::CallStaticShortMethod,
1950 CheckJNI::CallStaticShortMethodV,
1951 CheckJNI::CallStaticShortMethodA,
1952 CheckJNI::CallStaticIntMethod,
1953 CheckJNI::CallStaticIntMethodV,
1954 CheckJNI::CallStaticIntMethodA,
1955 CheckJNI::CallStaticLongMethod,
1956 CheckJNI::CallStaticLongMethodV,
1957 CheckJNI::CallStaticLongMethodA,
1958 CheckJNI::CallStaticFloatMethod,
1959 CheckJNI::CallStaticFloatMethodV,
1960 CheckJNI::CallStaticFloatMethodA,
1961 CheckJNI::CallStaticDoubleMethod,
1962 CheckJNI::CallStaticDoubleMethodV,
1963 CheckJNI::CallStaticDoubleMethodA,
1964 CheckJNI::CallStaticVoidMethod,
1965 CheckJNI::CallStaticVoidMethodV,
1966 CheckJNI::CallStaticVoidMethodA,
1967 CheckJNI::GetStaticFieldID,
1968 CheckJNI::GetStaticObjectField,
1969 CheckJNI::GetStaticBooleanField,
1970 CheckJNI::GetStaticByteField,
1971 CheckJNI::GetStaticCharField,
1972 CheckJNI::GetStaticShortField,
1973 CheckJNI::GetStaticIntField,
1974 CheckJNI::GetStaticLongField,
1975 CheckJNI::GetStaticFloatField,
1976 CheckJNI::GetStaticDoubleField,
1977 CheckJNI::SetStaticObjectField,
1978 CheckJNI::SetStaticBooleanField,
1979 CheckJNI::SetStaticByteField,
1980 CheckJNI::SetStaticCharField,
1981 CheckJNI::SetStaticShortField,
1982 CheckJNI::SetStaticIntField,
1983 CheckJNI::SetStaticLongField,
1984 CheckJNI::SetStaticFloatField,
1985 CheckJNI::SetStaticDoubleField,
1986 CheckJNI::NewString,
1987 CheckJNI::GetStringLength,
1988 CheckJNI::GetStringChars,
1989 CheckJNI::ReleaseStringChars,
1990 CheckJNI::NewStringUTF,
1991 CheckJNI::GetStringUTFLength,
1992 CheckJNI::GetStringUTFChars,
1993 CheckJNI::ReleaseStringUTFChars,
1994 CheckJNI::GetArrayLength,
1995 CheckJNI::NewObjectArray,
1996 CheckJNI::GetObjectArrayElement,
1997 CheckJNI::SetObjectArrayElement,
1998 CheckJNI::NewBooleanArray,
1999 CheckJNI::NewByteArray,
2000 CheckJNI::NewCharArray,
2001 CheckJNI::NewShortArray,
2002 CheckJNI::NewIntArray,
2003 CheckJNI::NewLongArray,
2004 CheckJNI::NewFloatArray,
2005 CheckJNI::NewDoubleArray,
2006 CheckJNI::GetBooleanArrayElements,
2007 CheckJNI::GetByteArrayElements,
2008 CheckJNI::GetCharArrayElements,
2009 CheckJNI::GetShortArrayElements,
2010 CheckJNI::GetIntArrayElements,
2011 CheckJNI::GetLongArrayElements,
2012 CheckJNI::GetFloatArrayElements,
2013 CheckJNI::GetDoubleArrayElements,
2014 CheckJNI::ReleaseBooleanArrayElements,
2015 CheckJNI::ReleaseByteArrayElements,
2016 CheckJNI::ReleaseCharArrayElements,
2017 CheckJNI::ReleaseShortArrayElements,
2018 CheckJNI::ReleaseIntArrayElements,
2019 CheckJNI::ReleaseLongArrayElements,
2020 CheckJNI::ReleaseFloatArrayElements,
2021 CheckJNI::ReleaseDoubleArrayElements,
2022 CheckJNI::GetBooleanArrayRegion,
2023 CheckJNI::GetByteArrayRegion,
2024 CheckJNI::GetCharArrayRegion,
2025 CheckJNI::GetShortArrayRegion,
2026 CheckJNI::GetIntArrayRegion,
2027 CheckJNI::GetLongArrayRegion,
2028 CheckJNI::GetFloatArrayRegion,
2029 CheckJNI::GetDoubleArrayRegion,
2030 CheckJNI::SetBooleanArrayRegion,
2031 CheckJNI::SetByteArrayRegion,
2032 CheckJNI::SetCharArrayRegion,
2033 CheckJNI::SetShortArrayRegion,
2034 CheckJNI::SetIntArrayRegion,
2035 CheckJNI::SetLongArrayRegion,
2036 CheckJNI::SetFloatArrayRegion,
2037 CheckJNI::SetDoubleArrayRegion,
2038 CheckJNI::RegisterNatives,
2039 CheckJNI::UnregisterNatives,
2040 CheckJNI::MonitorEnter,
2041 CheckJNI::MonitorExit,
2042 CheckJNI::GetJavaVM,
2043 CheckJNI::GetStringRegion,
2044 CheckJNI::GetStringUTFRegion,
2045 CheckJNI::GetPrimitiveArrayCritical,
2046 CheckJNI::ReleasePrimitiveArrayCritical,
2047 CheckJNI::GetStringCritical,
2048 CheckJNI::ReleaseStringCritical,
2049 CheckJNI::NewWeakGlobalRef,
2050 CheckJNI::DeleteWeakGlobalRef,
2051 CheckJNI::ExceptionCheck,
2052 CheckJNI::NewDirectByteBuffer,
2053 CheckJNI::GetDirectBufferAddress,
2054 CheckJNI::GetDirectBufferCapacity,
2055 CheckJNI::GetObjectRefType,
2056};
2057
2058const JNINativeInterface* GetCheckJniNativeInterface() {
2059 return &gCheckNativeInterface;
2060}
2061
2062class CheckJII {
Elliott Hughesba8eee12012-01-24 20:25:24 -08002063 public:
Elliott Hughesa2501992011-08-26 19:39:54 -07002064 static jint DestroyJavaVM(JavaVM* vm) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002065 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002066 sc.Check(true, "v", vm);
2067 return CHECK_JNI_EXIT("I", BaseVm(vm)->DestroyJavaVM(vm));
Elliott Hughesa2501992011-08-26 19:39:54 -07002068 }
2069
2070 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002071 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002072 sc.Check(true, "vpp", vm, p_env, thr_args);
2073 return CHECK_JNI_EXIT("I", BaseVm(vm)->AttachCurrentThread(vm, p_env, thr_args));
Elliott Hughesa2501992011-08-26 19:39:54 -07002074 }
2075
2076 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002077 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002078 sc.Check(true, "vpp", vm, p_env, thr_args);
2079 return CHECK_JNI_EXIT("I", BaseVm(vm)->AttachCurrentThreadAsDaemon(vm, p_env, thr_args));
Elliott Hughesa2501992011-08-26 19:39:54 -07002080 }
2081
2082 static jint DetachCurrentThread(JavaVM* vm) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002083 ScopedCheck sc(vm, true, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002084 sc.Check(true, "v", vm);
2085 return CHECK_JNI_EXIT("I", BaseVm(vm)->DetachCurrentThread(vm));
Elliott Hughesa2501992011-08-26 19:39:54 -07002086 }
2087
2088 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002089 ScopedCheck sc(vm, true, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002090 sc.Check(true, "v", vm);
2091 return CHECK_JNI_EXIT("I", BaseVm(vm)->GetEnv(vm, env, version));
Elliott Hughesa2501992011-08-26 19:39:54 -07002092 }
2093
2094 private:
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002095 static inline const JNIInvokeInterface* BaseVm(JavaVM* vm) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002096 return reinterpret_cast<JavaVMExt*>(vm)->unchecked_functions;
2097 }
2098};
2099
2100const JNIInvokeInterface gCheckInvokeInterface = {
2101 NULL, // reserved0
2102 NULL, // reserved1
2103 NULL, // reserved2
2104 CheckJII::DestroyJavaVM,
2105 CheckJII::AttachCurrentThread,
2106 CheckJII::DetachCurrentThread,
2107 CheckJII::GetEnv,
2108 CheckJII::AttachCurrentThreadAsDaemon
2109};
2110
2111const JNIInvokeInterface* GetCheckJniInvokeInterface() {
2112 return &gCheckInvokeInterface;
2113}
2114
2115} // namespace art