blob: 8a8862d32884b42544e9821ed41f0b268c369513 [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"
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070024#include "scoped_jni_thread_state.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070025#include "thread.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070026#include "runtime.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070027
28namespace art {
29
30void JniAbort(const char* jni_function_name) {
Elliott Hughesa0957642011-09-02 14:27:33 -070031 Thread* self = Thread::Current();
32 const Method* current_method = self->GetCurrentMethod();
Elliott Hughesa2501992011-08-26 19:39:54 -070033
Elliott Hughesa0957642011-09-02 14:27:33 -070034 std::stringstream os;
Elliott Hughesa2501992011-08-26 19:39:54 -070035 os << "JNI app bug detected";
36
37 if (jni_function_name != NULL) {
38 os << "\n in call to " << jni_function_name;
39 }
Elliott Hughesa0957642011-09-02 14:27:33 -070040 // TODO: is this useful given that we're about to dump the calling thread's stack?
41 if (current_method != NULL) {
42 os << "\n from " << PrettyMethod(current_method);
43 }
44 os << "\n";
45 self->Dump(os);
Elliott Hughesa2501992011-08-26 19:39:54 -070046
47 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
48 if (vm->check_jni_abort_hook != NULL) {
49 vm->check_jni_abort_hook(os.str());
50 } else {
51 LOG(FATAL) << os.str();
52 }
53}
54
55/*
56 * ===========================================================================
57 * JNI function helpers
58 * ===========================================================================
59 */
60
Elliott Hughesa2501992011-08-26 19:39:54 -070061template<typename T>
62T Decode(ScopedJniThreadState& ts, jobject obj) {
63 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
64}
65
Elliott Hughesa2501992011-08-26 19:39:54 -070066/*
67 * Hack to allow forcecopy to work with jniGetNonMovableArrayElements.
68 * The code deliberately uses an invalid sequence of operations, so we
69 * need to pass it through unmodified. Review that code before making
70 * any changes here.
71 */
72#define kNoCopyMagic 0xd5aab57f
73
74/*
75 * Flags passed into ScopedCheck.
76 */
77#define kFlag_Default 0x0000
78
79#define kFlag_CritBad 0x0000 /* calling while in critical is bad */
80#define kFlag_CritOkay 0x0001 /* ...okay */
81#define kFlag_CritGet 0x0002 /* this is a critical "get" */
82#define kFlag_CritRelease 0x0003 /* this is a critical "release" */
83#define kFlag_CritMask 0x0003 /* bit mask to get "crit" value */
84
85#define kFlag_ExcepBad 0x0000 /* raised exceptions are bad */
86#define kFlag_ExcepOkay 0x0004 /* ...okay */
87
88#define kFlag_Release 0x0010 /* are we in a non-critical release function? */
89#define kFlag_NullableUtf 0x0020 /* are our UTF parameters nullable? */
90
91#define kFlag_Invocation 0x8000 /* Part of the invocation interface (JavaVM*) */
92
Elliott Hughesa0957642011-09-02 14:27:33 -070093static const char* gBuiltInPrefixes[] = {
94 "Landroid/",
95 "Lcom/android/",
96 "Lcom/google/android/",
97 "Ldalvik/",
98 "Ljava/",
99 "Ljavax/",
100 "Llibcore/",
101 "Lorg/apache/harmony/",
102 NULL
103};
104
105bool ShouldTrace(JavaVMExt* vm, const Method* method) {
106 // If both "-Xcheck:jni" and "-Xjnitrace:" are enabled, we print trace messages
107 // when a native method that matches the -Xjnitrace argument calls a JNI function
108 // such as NewByteArray.
109 // If -verbose:third-party-jni is on, we want to log any JNI function calls
110 // made by a third-party native method.
111 std::string classNameStr(method->GetDeclaringClass()->GetDescriptor()->ToModifiedUtf8());
112 if (!vm->trace.empty() && classNameStr.find(vm->trace) != std::string::npos) {
113 return true;
114 }
115 if (vm->log_third_party_jni) {
116 // Return true if we're trying to log all third-party JNI activity and 'method' doesn't look
117 // like part of Android.
118 StringPiece className(classNameStr);
119 for (size_t i = 0; gBuiltInPrefixes[i] != NULL; ++i) {
120 if (className.starts_with(gBuiltInPrefixes[i])) {
121 return false;
122 }
123 }
124 return true;
125 }
126 return false;
127}
128
Elliott Hughesa2501992011-08-26 19:39:54 -0700129class ScopedCheck {
130public:
131 // For JNIEnv* functions.
132 explicit ScopedCheck(JNIEnv* env, int flags, const char* functionName) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700133 init(env, reinterpret_cast<JNIEnvExt*>(env)->vm, flags, functionName, true);
Elliott Hughesa2501992011-08-26 19:39:54 -0700134 checkThread(flags);
135 }
136
137 // For JavaVM* functions.
Elliott Hughesa0957642011-09-02 14:27:33 -0700138 explicit ScopedCheck(JavaVM* vm, bool hasMethod, const char* functionName) {
139 init(NULL, vm, kFlag_Invocation, functionName, hasMethod);
Elliott Hughesa2501992011-08-26 19:39:54 -0700140 }
141
142 bool forceCopy() {
143 return Runtime::Current()->GetJavaVM()->force_copy;
144 }
145
146 /*
147 * In some circumstances the VM will screen class names, but it doesn't
148 * for class lookup. When things get bounced through a class loader, they
149 * can actually get normalized a couple of times; as a result, passing in
150 * a class name like "java.lang.Thread" instead of "java/lang/Thread" will
151 * work in some circumstances.
152 *
153 * This is incorrect and could cause strange behavior or compatibility
154 * problems, so we want to screen that out here.
155 *
156 * We expect "fully-qualified" class names, like "java/lang/Thread" or
157 * "[Ljava/lang/Object;".
158 */
159 void checkClassName(const char* className) {
160 if (!IsValidClassName(className, true, false)) {
161 LOG(ERROR) << "JNI ERROR: illegal class name '" << className << "' (" << mFunctionName << ")\n"
162 << " (should be of the form 'java/lang/String', [Ljava/lang/String;' or '[[B')\n";
163 JniAbort();
164 }
165 }
166
167 /*
168 * Verify that the field is of the appropriate type. If the field has an
169 * object type, "java_object" is the object we're trying to assign into it.
170 *
171 * Works for both static and instance fields.
172 */
173 void checkFieldType(jobject java_object, jfieldID fid, char prim, bool isStatic) {
174 if (fid == NULL) {
175 LOG(ERROR) << "JNI ERROR: null jfieldID";
176 JniAbort();
177 return;
178 }
179
180 ScopedJniThreadState ts(mEnv);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700181 Field* f = DecodeField(fid);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700182 Class* field_type = f->GetType();
183 if (!field_type->IsPrimitive()) {
184 if (java_object != NULL) {
185 Object* obj = Decode<Object*>(ts, java_object);
186 /*
187 * If java_object is a weak global ref whose referent has been cleared,
188 * obj will be NULL. Otherwise, obj should always be non-NULL
189 * and valid.
190 */
191 if (obj != NULL && !Heap::IsHeapAddress(obj)) {
192 LOG(ERROR) << "JNI ERROR: field operation on invalid " << GetIndirectRefKind(java_object) << ": " << java_object;
Elliott Hughesa2501992011-08-26 19:39:54 -0700193 JniAbort();
194 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700195 } else {
Brian Carlstrom16192862011-09-12 17:50:06 -0700196 if (!obj->InstanceOf(field_type)) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700197 LOG(ERROR) << "JNI ERROR: attempt to set field " << PrettyField(f) << " with value of wrong type: " << PrettyTypeOf(obj);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700198 JniAbort();
199 return;
200 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700201 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700202 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700203 } else if (field_type != Runtime::Current()->GetClassLinker()->FindPrimitiveClass(prim)) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700204 LOG(ERROR) << "JNI ERROR: attempt to set field " << PrettyField(f) << " with value of wrong type: " << prim;
205 JniAbort();
206 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700207 }
208
209 if (isStatic && !f->IsStatic()) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700210 if (isStatic) {
211 LOG(ERROR) << "JNI ERROR: accessing non-static field " << PrettyField(f) << " as static";
212 } else {
213 LOG(ERROR) << "JNI ERROR: accessing static field " << PrettyField(f) << " as non-static";
214 }
215 JniAbort();
216 return;
217 }
218 }
219
220 /*
221 * Verify that this instance field ID is valid for this object.
222 *
223 * Assumes "jobj" has already been validated.
224 */
225 void checkInstanceFieldID(jobject java_object, jfieldID fid) {
226 ScopedJniThreadState ts(mEnv);
227
228 Object* o = Decode<Object*>(ts, java_object);
229 if (!Heap::IsHeapAddress(o)) {
230 LOG(ERROR) << "JNI ERROR: field operation on invalid " << GetIndirectRefKind(java_object) << ": " << java_object;
231 JniAbort();
232 return;
233 }
234
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700235 Field* f = DecodeField(fid);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700236 Class* f_type = f->GetType();
237 // check invariant that all jfieldIDs have resovled types
238 DCHECK(f_type != NULL);
Elliott Hughesa2501992011-08-26 19:39:54 -0700239 Class* c = o->GetClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700240 if (c->FindInstanceField(f->GetName()->ToModifiedUtf8(), f_type) == NULL) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700241 LOG(ERROR) << "JNI ERROR: jfieldID " << PrettyField(f) << " not valid for an object of class " << PrettyTypeOf(o);
Elliott Hughesa2501992011-08-26 19:39:54 -0700242 JniAbort();
243 }
244 }
245
246 /*
247 * Verify that the pointer value is non-NULL.
248 */
249 void checkNonNull(const void* ptr) {
250 if (ptr == NULL) {
251 LOG(ERROR) << "JNI ERROR: invalid null pointer";
252 JniAbort();
253 }
254 }
255
256 /*
257 * Verify that the method's return type matches the type of call.
258 * 'expectedType' will be "L" for all objects, including arrays.
259 */
260 void checkSig(jmethodID mid, const char* expectedType, bool isStatic) {
261 ScopedJniThreadState ts(mEnv);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700262 const Method* m = DecodeMethod(mid);
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700263 if (*expectedType != m->GetShorty()->CharAt(0)) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700264 LOG(ERROR) << "JNI ERROR: expected return type '" << *expectedType << "' calling " << PrettyMethod(m);
Elliott Hughesa2501992011-08-26 19:39:54 -0700265 JniAbort();
266 } else if (isStatic && !m->IsStatic()) {
267 if (isStatic) {
268 LOG(ERROR) << "JNI ERROR: calling non-static method " << PrettyMethod(m) << " with static call";
269 } else {
270 LOG(ERROR) << "JNI ERROR: calling static method " << PrettyMethod(m) << " with non-static call";
271 }
272 JniAbort();
273 }
274 }
275
276 /*
277 * Verify that this static field ID is valid for this class.
278 *
279 * Assumes "java_class" has already been validated.
280 */
281 void checkStaticFieldID(jclass java_class, jfieldID fid) {
282 ScopedJniThreadState ts(mEnv);
283 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700284 const Field* f = DecodeField(fid);
Elliott Hughesa2501992011-08-26 19:39:54 -0700285 if (f->GetDeclaringClass() != c) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700286 LOG(ERROR) << "JNI ERROR: static jfieldID " << fid << " not valid for class " << PrettyClass(c);
Elliott Hughesa2501992011-08-26 19:39:54 -0700287 JniAbort();
288 }
289 }
290
291 /*
292 * Verify that "mid" is appropriate for "clazz".
293 *
294 * A mismatch isn't dangerous, because the jmethodID defines the class. In
295 * fact, jclazz is unused in the implementation. It's best if we don't
296 * allow bad code in the system though.
297 *
298 * Instances of "jclazz" must be instances of the method's declaring class.
299 */
300 void checkStaticMethod(jclass java_class, jmethodID mid) {
301 ScopedJniThreadState ts(mEnv);
302 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700303 const Method* m = DecodeMethod(mid);
Elliott Hughesa2501992011-08-26 19:39:54 -0700304 if (!c->IsAssignableFrom(m->GetDeclaringClass())) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700305 LOG(ERROR) << "JNI ERROR: can't call static " << PrettyMethod(m) << " on class " << PrettyClass(c);
Elliott Hughesa2501992011-08-26 19:39:54 -0700306 JniAbort();
307 }
308 }
309
310 /*
311 * Verify that "mid" is appropriate for "jobj".
312 *
313 * Make sure the object is an instance of the method's declaring class.
314 * (Note the mid might point to a declaration in an interface; this
315 * will be handled automatically by the instanceof check.)
316 */
317 void checkVirtualMethod(jobject java_object, jmethodID mid) {
318 ScopedJniThreadState ts(mEnv);
319 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700320 const Method* m = DecodeMethod(mid);
Elliott Hughesa2501992011-08-26 19:39:54 -0700321 if (!o->InstanceOf(m->GetDeclaringClass())) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700322 LOG(ERROR) << "JNI ERROR: can't call " << PrettyMethod(m) << " on instance of " << PrettyTypeOf(o);
Elliott Hughesa2501992011-08-26 19:39:54 -0700323 JniAbort();
324 }
325 }
326
327 /**
328 * The format string is a sequence of the following characters,
329 * and must be followed by arguments of the corresponding types
330 * in the same order.
331 *
332 * Java primitive types:
333 * B - jbyte
334 * C - jchar
335 * D - jdouble
336 * F - jfloat
337 * I - jint
338 * J - jlong
339 * S - jshort
340 * Z - jboolean (shown as true and false)
341 * V - void
342 *
343 * Java reference types:
344 * L - jobject
345 * a - jarray
346 * c - jclass
347 * s - jstring
348 *
349 * JNI types:
350 * b - jboolean (shown as JNI_TRUE and JNI_FALSE)
351 * f - jfieldID
352 * m - jmethodID
353 * p - void*
354 * r - jint (for release mode arguments)
355 * u - const char* (modified UTF-8)
356 * z - jsize (for lengths; use i if negative values are okay)
357 * v - JavaVM*
358 * E - JNIEnv*
359 * . - no argument; just print "..." (used for varargs JNI calls)
360 *
361 * Use the kFlag_NullableUtf flag where 'u' field(s) are nullable.
362 */
363 void check(bool entry, const char* fmt0, ...) {
364 va_list ap;
365
Elliott Hughesa0957642011-09-02 14:27:33 -0700366 const Method* traceMethod = NULL;
367 if ((!mVm->trace.empty() || mVm->log_third_party_jni) && mHasMethod) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700368 // We need to guard some of the invocation interface's calls: a bad caller might
369 // use DetachCurrentThread or GetEnv on a thread that's not yet attached.
Elliott Hughesa0957642011-09-02 14:27:33 -0700370 Thread* self = Thread::Current();
371 if ((mFlags & kFlag_Invocation) == 0 || self != NULL) {
372 traceMethod = self->GetCurrentMethod();
Elliott Hughesa2501992011-08-26 19:39:54 -0700373 }
374 }
Elliott Hughesa0957642011-09-02 14:27:33 -0700375
376 if (traceMethod != NULL && ShouldTrace(mVm, traceMethod)) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700377 va_start(ap, fmt0);
378 std::string msg;
379 for (const char* fmt = fmt0; *fmt;) {
380 char ch = *fmt++;
381 if (ch == 'B') { // jbyte
382 jbyte b = va_arg(ap, int);
383 if (b >= 0 && b < 10) {
384 StringAppendF(&msg, "%d", b);
385 } else {
386 StringAppendF(&msg, "%#x (%d)", b, b);
387 }
388 } else if (ch == 'C') { // jchar
389 jchar c = va_arg(ap, int);
390 if (c < 0x7f && c >= ' ') {
391 StringAppendF(&msg, "U+%x ('%c')", c, c);
392 } else {
393 StringAppendF(&msg, "U+%x", c);
394 }
395 } else if (ch == 'F' || ch == 'D') { // jfloat, jdouble
396 StringAppendF(&msg, "%g", va_arg(ap, double));
397 } else if (ch == 'I' || ch == 'S') { // jint, jshort
398 StringAppendF(&msg, "%d", va_arg(ap, int));
399 } else if (ch == 'J') { // jlong
400 StringAppendF(&msg, "%lld", va_arg(ap, jlong));
401 } else if (ch == 'Z') { // jboolean
402 StringAppendF(&msg, "%s", va_arg(ap, int) ? "true" : "false");
403 } else if (ch == 'V') { // void
404 msg += "void";
405 } else if (ch == 'v') { // JavaVM*
406 JavaVM* vm = va_arg(ap, JavaVM*);
407 StringAppendF(&msg, "(JavaVM*)%p", vm);
408 } else if (ch == 'E') { // JNIEnv*
409 JNIEnv* env = va_arg(ap, JNIEnv*);
410 StringAppendF(&msg, "(JNIEnv*)%p", env);
411 } else if (ch == 'L' || ch == 'a' || ch == 's') { // jobject, jarray, jstring
412 // For logging purposes, these are identical.
413 jobject o = va_arg(ap, jobject);
414 if (o == NULL) {
415 msg += "NULL";
416 } else {
417 StringAppendF(&msg, "%p", o);
418 }
419 } else if (ch == 'b') { // jboolean (JNI-style)
420 jboolean b = va_arg(ap, int);
421 msg += (b ? "JNI_TRUE" : "JNI_FALSE");
422 } else if (ch == 'c') { // jclass
423 jclass jc = va_arg(ap, jclass);
424 Class* c = reinterpret_cast<Class*>(Thread::Current()->DecodeJObject(jc));
425 if (c == NULL) {
426 msg += "NULL";
427 } else if (c == kInvalidIndirectRefObject || !Heap::IsHeapAddress(c)) {
428 StringAppendF(&msg, "%p(INVALID)", jc);
429 } else {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700430 msg += PrettyClass(c);
Elliott Hughesa2501992011-08-26 19:39:54 -0700431 if (!entry) {
432 StringAppendF(&msg, " (%p)", jc);
433 }
434 }
435 } else if (ch == 'f') { // jfieldID
436 jfieldID fid = va_arg(ap, jfieldID);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700437 Field* f = reinterpret_cast<Field*>(fid);
Elliott Hughesa2501992011-08-26 19:39:54 -0700438 msg += PrettyField(f);
439 if (!entry) {
440 StringAppendF(&msg, " (%p)", fid);
441 }
442 } else if (ch == 'z') { // non-negative jsize
443 // You might expect jsize to be size_t, but it's not; it's the same as jint.
444 // We only treat this specially so we can do the non-negative check.
445 // TODO: maybe this wasn't worth it?
446 jint i = va_arg(ap, jint);
447 StringAppendF(&msg, "%d", i);
448 } else if (ch == 'm') { // jmethodID
449 jmethodID mid = va_arg(ap, jmethodID);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700450 Method* m = reinterpret_cast<Method*>(mid);
Elliott Hughesa2501992011-08-26 19:39:54 -0700451 msg += PrettyMethod(m);
452 if (!entry) {
453 StringAppendF(&msg, " (%p)", mid);
454 }
455 } else if (ch == 'p') { // void* ("pointer")
456 void* p = va_arg(ap, void*);
457 if (p == NULL) {
458 msg += "NULL";
459 } else {
460 StringAppendF(&msg, "(void*) %p", p);
461 }
462 } else if (ch == 'r') { // jint (release mode)
463 jint releaseMode = va_arg(ap, jint);
464 if (releaseMode == 0) {
465 msg += "0";
466 } else if (releaseMode == JNI_ABORT) {
467 msg += "JNI_ABORT";
468 } else if (releaseMode == JNI_COMMIT) {
469 msg += "JNI_COMMIT";
470 } else {
471 StringAppendF(&msg, "invalid release mode %d", releaseMode);
472 }
473 } else if (ch == 'u') { // const char* (modified UTF-8)
474 const char* utf = va_arg(ap, const char*);
475 if (utf == NULL) {
476 msg += "NULL";
477 } else {
478 StringAppendF(&msg, "\"%s\"", utf);
479 }
480 } else if (ch == '.') {
481 msg += "...";
482 } else {
483 LOG(ERROR) << "unknown trace format specifier: " << ch;
484 JniAbort();
485 return;
486 }
487 if (*fmt) {
488 StringAppendF(&msg, ", ");
489 }
490 }
491 va_end(ap);
492
493 if (entry) {
494 if (mHasMethod) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700495 std::string methodName(PrettyMethod(traceMethod, false));
Elliott Hughesa2501992011-08-26 19:39:54 -0700496 LOG(INFO) << "JNI: " << methodName << " -> " << mFunctionName << "(" << msg << ")";
497 mIndent = methodName.size() + 1;
498 } else {
499 LOG(INFO) << "JNI: -> " << mFunctionName << "(" << msg << ")";
500 mIndent = 0;
501 }
502 } else {
503 LOG(INFO) << StringPrintf("JNI: %*s<- %s returned %s", mIndent, "", mFunctionName, msg.c_str());
504 }
505 }
506
507 // We always do the thorough checks on entry, and never on exit...
508 if (entry) {
509 va_start(ap, fmt0);
510 for (const char* fmt = fmt0; *fmt; ++fmt) {
511 char ch = *fmt;
512 if (ch == 'a') {
513 checkArray(va_arg(ap, jarray));
514 } else if (ch == 'c') {
515 checkInstance(kClass, va_arg(ap, jclass));
516 } else if (ch == 'L') {
517 checkObject(va_arg(ap, jobject));
518 } else if (ch == 'r') {
519 checkReleaseMode(va_arg(ap, jint));
520 } else if (ch == 's') {
521 checkInstance(kString, va_arg(ap, jstring));
522 } else if (ch == 'u') {
523 if ((mFlags & kFlag_Release) != 0) {
524 checkNonNull(va_arg(ap, const char*));
525 } else {
526 bool nullable = ((mFlags & kFlag_NullableUtf) != 0);
527 checkUtfString(va_arg(ap, const char*), nullable);
528 }
529 } else if (ch == 'z') {
530 checkLengthPositive(va_arg(ap, jsize));
531 } else if (strchr("BCISZbfmpEv", ch) != NULL) {
532 va_arg(ap, int); // Skip this argument.
533 } else if (ch == 'D' || ch == 'F') {
534 va_arg(ap, double); // Skip this argument.
535 } else if (ch == 'J') {
536 va_arg(ap, long); // Skip this argument.
537 } else if (ch == '.') {
538 } else {
539 LOG(FATAL) << "unknown check format specifier: " << ch;
540 }
541 }
542 va_end(ap);
543 }
544 }
545
546private:
Elliott Hughesa0957642011-09-02 14:27:33 -0700547 void init(JNIEnv* env, JavaVM* vm, int flags, const char* functionName, bool hasMethod) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700548 mEnv = reinterpret_cast<JNIEnvExt*>(env);
Elliott Hughesa0957642011-09-02 14:27:33 -0700549 mVm = reinterpret_cast<JavaVMExt*>(vm);
Elliott Hughesa2501992011-08-26 19:39:54 -0700550 mFlags = flags;
551 mFunctionName = functionName;
552
553 // Set "hasMethod" to true if we have a valid thread with a method pointer.
554 // We won't have one before attaching a thread, after detaching a thread, or
555 // after destroying the VM.
556 mHasMethod = hasMethod;
557 }
558
559 /*
560 * Verify that "array" is non-NULL and points to an Array object.
561 *
562 * Since we're dealing with objects, switch to "running" mode.
563 */
564 void checkArray(jarray java_array) {
565 if (java_array == NULL) {
566 LOG(ERROR) << "JNI ERROR: received null array";
567 JniAbort();
568 return;
569 }
570
571 ScopedJniThreadState ts(mEnv);
572 Array* a = Decode<Array*>(ts, java_array);
573 if (!Heap::IsHeapAddress(a)) {
574 LOG(ERROR) << "JNI ERROR: jarray is an invalid " << GetIndirectRefKind(java_array) << ": " << reinterpret_cast<void*>(java_array);
575 JniAbort();
576 } else if (!a->IsArrayInstance()) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700577 LOG(ERROR) << "JNI ERROR: jarray argument has non-array type: " << PrettyTypeOf(a);
Elliott Hughesa2501992011-08-26 19:39:54 -0700578 JniAbort();
579 }
580 }
581
582 void checkLengthPositive(jsize length) {
583 if (length < 0) {
584 LOG(ERROR) << "JNI ERROR: negative jsize: " << length;
585 JniAbort();
586 }
587 }
588
589 /*
590 * Verify that "jobj" is a valid object, and that it's an object that JNI
591 * is allowed to know about. We allow NULL references.
592 *
593 * Switches to "running" mode before performing checks.
594 */
595 void checkObject(jobject java_object) {
596 if (java_object == NULL) {
597 return;
598 }
599
600 ScopedJniThreadState ts(mEnv);
601
602 Object* o = Decode<Object*>(ts, java_object);
603 if (o != NULL && !Heap::IsHeapAddress(o)) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700604 // TODO: when we remove work_around_app_jni_bugs, this should be impossible.
Elliott Hughesa2501992011-08-26 19:39:54 -0700605 LOG(ERROR) << "JNI ERROR: native code passing in reference to invalid " << GetIndirectRefKind(java_object) << ": " << java_object;
606 JniAbort();
607 }
608 }
609
610 /*
611 * Verify that the "mode" argument passed to a primitive array Release
612 * function is one of the valid values.
613 */
614 void checkReleaseMode(jint mode) {
615 if (mode != 0 && mode != JNI_COMMIT && mode != JNI_ABORT) {
616 LOG(ERROR) << "JNI ERROR: bad value for release mode: " << mode;
617 JniAbort();
618 }
619 }
620
621 void checkThread(int flags) {
622 Thread* self = Thread::Current();
623 if (self == NULL) {
624 LOG(ERROR) << "JNI ERROR: non-VM thread making JNI calls";
625 JniAbort();
626 return;
627 }
628
629 // Get the *correct* JNIEnv by going through our TLS pointer.
630 JNIEnvExt* threadEnv = self->GetJniEnv();
631
632 /*
633 * Verify that the current thread is (a) attached and (b) associated with
634 * this particular instance of JNIEnv.
635 */
636 if (mEnv != threadEnv) {
637 LOG(ERROR) << "JNI ERROR: thread " << *self << " using JNIEnv* from thread " << *mEnv->self;
638 // If we're keeping broken code limping along, we need to suppress the abort...
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700639 if (!mEnv->work_around_app_jni_bugs) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700640 JniAbort();
641 return;
642 }
643 }
644
645 /*
646 * Verify that, if this thread previously made a critical "get" call, we
647 * do the corresponding "release" call before we try anything else.
648 */
649 switch (flags & kFlag_CritMask) {
650 case kFlag_CritOkay: // okay to call this method
651 break;
652 case kFlag_CritBad: // not okay to call
653 if (threadEnv->critical) {
654 LOG(ERROR) << "JNI ERROR: thread " << *self << " using JNI after critical get";
655 JniAbort();
656 return;
657 }
658 break;
659 case kFlag_CritGet: // this is a "get" call
660 /* don't check here; we allow nested gets */
661 threadEnv->critical++;
662 break;
663 case kFlag_CritRelease: // this is a "release" call
664 threadEnv->critical--;
665 if (threadEnv->critical < 0) {
666 LOG(ERROR) << "JNI ERROR: thread " << *self << " called too many critical releases";
667 JniAbort();
668 return;
669 }
670 break;
671 default:
672 LOG(FATAL) << "bad flags (internal error): " << flags;
673 }
674
675 /*
676 * Verify that, if an exception has been raised, the native code doesn't
677 * make any JNI calls other than the Exception* methods.
678 */
679 if ((flags & kFlag_ExcepOkay) == 0 && self->IsExceptionPending()) {
680 LOG(ERROR) << "JNI ERROR: JNI method called with exception pending";
681 LOG(ERROR) << "Pending exception is: TODO"; // TODO
682 // TODO: dvmLogExceptionStackTrace();
683 JniAbort();
684 return;
685 }
686 }
687
688 /*
689 * Verify that "bytes" points to valid "modified UTF-8" data.
690 */
691 void checkUtfString(const char* bytes, bool nullable) {
692 if (bytes == NULL) {
693 if (!nullable) {
694 LOG(ERROR) << "JNI ERROR: non-nullable const char* was NULL";
695 JniAbort();
696 return;
697 }
698 return;
699 }
700
701 const char* errorKind = NULL;
702 uint8_t utf8 = checkUtfBytes(bytes, &errorKind);
703 if (errorKind != NULL) {
704 LOG(ERROR) << "JNI ERROR: input is not valid UTF-8: illegal " << errorKind << " byte " << StringPrintf("%#x", utf8);
705 LOG(ERROR) << " string: '" << bytes << "'";
706 JniAbort();
707 return;
708 }
709 }
710
711 enum InstanceKind {
712 kClass,
713 kDirectByteBuffer,
714 kString,
715 kThrowable,
716 };
717
718 /*
719 * Verify that "jobj" is a valid non-NULL object reference, and points to
720 * an instance of expectedClass.
721 *
722 * Because we're looking at an object on the GC heap, we have to switch
723 * to "running" mode before doing the checks.
724 */
725 void checkInstance(InstanceKind kind, jobject java_object) {
Elliott Hughesd92bec42011-09-02 17:04:36 -0700726 const char* what = NULL;
Elliott Hughesa2501992011-08-26 19:39:54 -0700727 switch (kind) {
728 case kClass:
729 what = "jclass";
730 break;
731 case kDirectByteBuffer:
732 what = "direct ByteBuffer";
733 break;
734 case kString:
735 what = "jstring";
736 break;
737 case kThrowable:
738 what = "jthrowable";
739 break;
740 default:
741 CHECK(false) << static_cast<int>(kind);
742 }
743
744 if (java_object == NULL) {
745 LOG(ERROR) << "JNI ERROR: received null " << what;
746 JniAbort();
747 return;
748 }
749
750 ScopedJniThreadState ts(mEnv);
751 Object* obj = Decode<Object*>(ts, java_object);
752 if (!Heap::IsHeapAddress(obj)) {
753 LOG(ERROR) << "JNI ERROR: " << what << " is an invalid " << GetIndirectRefKind(java_object) << ": " << java_object;
754 JniAbort();
755 return;
756 }
757
758 bool okay = true;
759 switch (kind) {
760 case kClass:
761 okay = obj->IsClass();
762 break;
763 case kDirectByteBuffer:
764 // TODO
765 break;
766 case kString:
767 okay = obj->IsString();
768 break;
769 case kThrowable:
770 // TODO
771 break;
772 }
773 if (!okay) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700774 LOG(ERROR) << "JNI ERROR: " << what << " has wrong type: " << PrettyTypeOf(obj);
Elliott Hughesa2501992011-08-26 19:39:54 -0700775 JniAbort();
776 }
777 }
778
779 static uint8_t checkUtfBytes(const char* bytes, const char** errorKind) {
780 while (*bytes != '\0') {
781 uint8_t utf8 = *(bytes++);
782 // Switch on the high four bits.
783 switch (utf8 >> 4) {
784 case 0x00:
785 case 0x01:
786 case 0x02:
787 case 0x03:
788 case 0x04:
789 case 0x05:
790 case 0x06:
791 case 0x07:
792 // Bit pattern 0xxx. No need for any extra bytes.
793 break;
794 case 0x08:
795 case 0x09:
796 case 0x0a:
797 case 0x0b:
798 case 0x0f:
799 /*
800 * Bit pattern 10xx or 1111, which are illegal start bytes.
801 * Note: 1111 is valid for normal UTF-8, but not the
802 * modified UTF-8 used here.
803 */
804 *errorKind = "start";
805 return utf8;
806 case 0x0e:
807 // Bit pattern 1110, so there are two additional bytes.
808 utf8 = *(bytes++);
809 if ((utf8 & 0xc0) != 0x80) {
810 *errorKind = "continuation";
811 return utf8;
812 }
813 // Fall through to take care of the final byte.
814 case 0x0c:
815 case 0x0d:
816 // Bit pattern 110x, so there is one additional byte.
817 utf8 = *(bytes++);
818 if ((utf8 & 0xc0) != 0x80) {
819 *errorKind = "continuation";
820 return utf8;
821 }
822 break;
823 }
824 }
825 return 0;
826 }
827
828 void JniAbort() {
829 ::art::JniAbort(mFunctionName);
830 }
831
832 JNIEnvExt* mEnv;
Elliott Hughesa0957642011-09-02 14:27:33 -0700833 JavaVMExt* mVm;
Elliott Hughesa2501992011-08-26 19:39:54 -0700834 const char* mFunctionName;
835 int mFlags;
836 bool mHasMethod;
837 size_t mIndent;
838
839 DISALLOW_COPY_AND_ASSIGN(ScopedCheck);
840};
841
842#define CHECK_JNI_ENTRY(flags, types, args...) \
843 ScopedCheck sc(env, flags, __FUNCTION__); \
844 sc.check(true, types, ##args)
845
846#define CHECK_JNI_EXIT(type, exp) ({ \
847 typeof (exp) _rc = (exp); \
848 sc.check(false, type, _rc); \
849 _rc; })
850#define CHECK_JNI_EXIT_VOID() \
851 sc.check(false, "V")
852
853/*
854 * ===========================================================================
855 * Guarded arrays
856 * ===========================================================================
857 */
858
859#define kGuardLen 512 /* must be multiple of 2 */
860#define kGuardPattern 0xd5e3 /* uncommon values; d5e3d5e3 invalid addr */
861#define kGuardMagic 0xffd5aa96
862
863/* this gets tucked in at the start of the buffer; struct size must be even */
864struct GuardedCopy {
865 uint32_t magic;
866 uLong adler;
867 size_t originalLen;
868 const void* originalPtr;
869
870 /* find the GuardedCopy given the pointer into the "live" data */
871 static inline const GuardedCopy* fromData(const void* dataBuf) {
872 return reinterpret_cast<const GuardedCopy*>(actualBuffer(dataBuf));
873 }
874
875 /*
876 * Create an over-sized buffer to hold the contents of "buf". Copy it in,
877 * filling in the area around it with guard data.
878 *
879 * We use a 16-bit pattern to make a rogue memset less likely to elude us.
880 */
881 static void* create(const void* buf, size_t len, bool modOkay) {
882 size_t newLen = actualLength(len);
883 uint8_t* newBuf = debugAlloc(newLen);
884
885 /* fill it in with a pattern */
886 uint16_t* pat = (uint16_t*) newBuf;
887 for (size_t i = 0; i < newLen / 2; i++) {
888 *pat++ = kGuardPattern;
889 }
890
891 /* copy the data in; note "len" could be zero */
892 memcpy(newBuf + kGuardLen / 2, buf, len);
893
894 /* if modification is not expected, grab a checksum */
895 uLong adler = 0;
896 if (!modOkay) {
897 adler = adler32(0L, Z_NULL, 0);
898 adler = adler32(adler, (const Bytef*)buf, len);
899 *(uLong*)newBuf = adler;
900 }
901
902 GuardedCopy* pExtra = reinterpret_cast<GuardedCopy*>(newBuf);
903 pExtra->magic = kGuardMagic;
904 pExtra->adler = adler;
905 pExtra->originalPtr = buf;
906 pExtra->originalLen = len;
907
908 return newBuf + kGuardLen / 2;
909 }
910
911 /*
912 * Free up the guard buffer, scrub it, and return the original pointer.
913 */
914 static void* destroy(void* dataBuf) {
915 const GuardedCopy* pExtra = GuardedCopy::fromData(dataBuf);
916 void* originalPtr = (void*) pExtra->originalPtr;
917 size_t len = pExtra->originalLen;
918 debugFree(dataBuf, len);
919 return originalPtr;
920 }
921
922 /*
923 * Verify the guard area and, if "modOkay" is false, that the data itself
924 * has not been altered.
925 *
926 * The caller has already checked that "dataBuf" is non-NULL.
927 */
928 static void check(const char* functionName, const void* dataBuf, bool modOkay) {
929 static const uint32_t kMagicCmp = kGuardMagic;
930 const uint8_t* fullBuf = actualBuffer(dataBuf);
931 const GuardedCopy* pExtra = GuardedCopy::fromData(dataBuf);
932
933 /*
934 * Before we do anything with "pExtra", check the magic number. We
935 * do the check with memcmp rather than "==" in case the pointer is
936 * unaligned. If it points to completely bogus memory we're going
937 * to crash, but there's no easy way around that.
938 */
939 if (memcmp(&pExtra->magic, &kMagicCmp, 4) != 0) {
940 uint8_t buf[4];
941 memcpy(buf, &pExtra->magic, 4);
942 LOG(ERROR) << StringPrintf("JNI: guard magic does not match "
943 "(found 0x%02x%02x%02x%02x) -- incorrect data pointer %p?",
944 buf[3], buf[2], buf[1], buf[0], dataBuf); /* assume little endian */
945 JniAbort(functionName);
946 }
947
948 size_t len = pExtra->originalLen;
949
950 /* check bottom half of guard; skip over optional checksum storage */
951 const uint16_t* pat = (uint16_t*) fullBuf;
952 for (size_t i = sizeof(GuardedCopy) / 2; i < (kGuardLen / 2 - sizeof(GuardedCopy)) / 2; i++) {
953 if (pat[i] != kGuardPattern) {
954 LOG(ERROR) << "JNI: guard pattern(1) disturbed at " << (void*) fullBuf << " + " << (i*2);
955 JniAbort(functionName);
956 }
957 }
958
959 int offset = kGuardLen / 2 + len;
960 if (offset & 0x01) {
961 /* odd byte; expected value depends on endian-ness of host */
962 const uint16_t patSample = kGuardPattern;
963 if (fullBuf[offset] != ((const uint8_t*) &patSample)[1]) {
964 LOG(ERROR) << "JNI: guard pattern disturbed in odd byte after "
965 << (void*) fullBuf << " (+" << offset << ") "
966 << StringPrintf("0x%02x 0x%02x", fullBuf[offset], ((const uint8_t*) &patSample)[1]);
967 JniAbort(functionName);
968 }
969 offset++;
970 }
971
972 /* check top half of guard */
973 pat = (uint16_t*) (fullBuf + offset);
974 for (size_t i = 0; i < kGuardLen / 4; i++) {
975 if (pat[i] != kGuardPattern) {
976 LOG(ERROR) << "JNI: guard pattern(2) disturbed at " << (void*) fullBuf << " + " << (offset + i*2);
977 JniAbort(functionName);
978 }
979 }
980
981 /*
982 * If modification is not expected, verify checksum. Strictly speaking
983 * this is wrong: if we told the client that we made a copy, there's no
984 * reason they can't alter the buffer.
985 */
986 if (!modOkay) {
987 uLong adler = adler32(0L, Z_NULL, 0);
988 adler = adler32(adler, (const Bytef*)dataBuf, len);
989 if (pExtra->adler != adler) {
990 LOG(ERROR) << StringPrintf("JNI: buffer modified (0x%08lx vs 0x%08lx) at addr %p", pExtra->adler, adler, dataBuf);
991 JniAbort(functionName);
992 }
993 }
994 }
995
996 private:
997 static uint8_t* debugAlloc(size_t len) {
998 void* result = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
999 if (result == MAP_FAILED) {
1000 PLOG(FATAL) << "GuardedCopy::create mmap(" << len << ") failed";
1001 }
1002 return reinterpret_cast<uint8_t*>(result);
1003 }
1004
1005 static void debugFree(void* dataBuf, size_t len) {
1006 uint8_t* fullBuf = actualBuffer(dataBuf);
1007 size_t totalByteCount = actualLength(len);
1008 // TODO: we could mprotect instead, and keep the allocation around for a while.
1009 // This would be even more expensive, but it might catch more errors.
1010 // if (mprotect(fullBuf, totalByteCount, PROT_NONE) != 0) {
1011 // LOGW("mprotect(PROT_NONE) failed: %s", strerror(errno));
1012 // }
1013 if (munmap(fullBuf, totalByteCount) != 0) {
1014 PLOG(FATAL) << "munmap(" << (void*) fullBuf << ", " << totalByteCount << ") failed";
1015 }
1016 }
1017
1018 static const uint8_t* actualBuffer(const void* dataBuf) {
1019 return reinterpret_cast<const uint8_t*>(dataBuf) - kGuardLen / 2;
1020 }
1021
1022 static uint8_t* actualBuffer(void* dataBuf) {
1023 return reinterpret_cast<uint8_t*>(dataBuf) - kGuardLen / 2;
1024 }
1025
1026 // Underlying length of a user allocation of 'length' bytes.
1027 static size_t actualLength(size_t length) {
1028 return (length + kGuardLen + 1) & ~0x01;
1029 }
1030};
1031
1032/*
1033 * Create a guarded copy of a primitive array. Modifications to the copied
1034 * data are allowed. Returns a pointer to the copied data.
1035 */
1036void* CreateGuardedPACopy(JNIEnv* env, const jarray java_array, jboolean* isCopy) {
1037 ScopedJniThreadState ts(env);
1038
1039 Array* a = Decode<Array*>(ts, java_array);
1040 size_t byte_count = a->GetLength() * a->GetClass()->GetComponentSize();
Elliott Hughesbf86d042011-08-31 17:53:14 -07001041 void* result = GuardedCopy::create(a->GetRawData(), byte_count, true);
Elliott Hughesa2501992011-08-26 19:39:54 -07001042 if (isCopy != NULL) {
1043 *isCopy = JNI_TRUE;
1044 }
1045 return result;
1046}
1047
1048/*
1049 * Perform the array "release" operation, which may or may not copy data
1050 * back into the VM, and may or may not release the underlying storage.
1051 */
1052void ReleaseGuardedPACopy(JNIEnv* env, jarray java_array, void* dataBuf, int mode) {
1053 if (reinterpret_cast<uintptr_t>(dataBuf) == kNoCopyMagic) {
1054 return;
1055 }
1056
1057 ScopedJniThreadState ts(env);
1058 Array* a = Decode<Array*>(ts, java_array);
1059
1060 GuardedCopy::check(__FUNCTION__, dataBuf, true);
1061
1062 if (mode != JNI_ABORT) {
1063 size_t len = GuardedCopy::fromData(dataBuf)->originalLen;
Elliott Hughesbf86d042011-08-31 17:53:14 -07001064 memcpy(a->GetRawData(), dataBuf, len);
Elliott Hughesa2501992011-08-26 19:39:54 -07001065 }
1066 if (mode != JNI_COMMIT) {
1067 GuardedCopy::destroy(dataBuf);
1068 }
1069}
1070
1071/*
1072 * ===========================================================================
1073 * JNI functions
1074 * ===========================================================================
1075 */
1076
1077class CheckJNI {
1078 public:
1079 static jint GetVersion(JNIEnv* env) {
1080 CHECK_JNI_ENTRY(kFlag_Default, "E", env);
1081 return CHECK_JNI_EXIT("I", baseEnv(env)->GetVersion(env));
1082 }
1083
1084 static jclass DefineClass(JNIEnv* env, const char* name, jobject loader, const jbyte* buf, jsize bufLen) {
1085 CHECK_JNI_ENTRY(kFlag_Default, "EuLpz", env, name, loader, buf, bufLen);
1086 sc.checkClassName(name);
1087 return CHECK_JNI_EXIT("c", baseEnv(env)->DefineClass(env, name, loader, buf, bufLen));
1088 }
1089
1090 static jclass FindClass(JNIEnv* env, const char* name) {
1091 CHECK_JNI_ENTRY(kFlag_Default, "Eu", env, name);
1092 sc.checkClassName(name);
1093 return CHECK_JNI_EXIT("c", baseEnv(env)->FindClass(env, name));
1094 }
1095
1096 static jclass GetSuperclass(JNIEnv* env, jclass clazz) {
1097 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, clazz);
1098 return CHECK_JNI_EXIT("c", baseEnv(env)->GetSuperclass(env, clazz));
1099 }
1100
1101 static jboolean IsAssignableFrom(JNIEnv* env, jclass clazz1, jclass clazz2) {
1102 CHECK_JNI_ENTRY(kFlag_Default, "Ecc", env, clazz1, clazz2);
1103 return CHECK_JNI_EXIT("b", baseEnv(env)->IsAssignableFrom(env, clazz1, clazz2));
1104 }
1105
1106 static jmethodID FromReflectedMethod(JNIEnv* env, jobject method) {
1107 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, method);
1108 // TODO: check that 'field' is a java.lang.reflect.Method.
1109 return CHECK_JNI_EXIT("m", baseEnv(env)->FromReflectedMethod(env, method));
1110 }
1111
1112 static jfieldID FromReflectedField(JNIEnv* env, jobject field) {
1113 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, field);
1114 // TODO: check that 'field' is a java.lang.reflect.Field.
1115 return CHECK_JNI_EXIT("f", baseEnv(env)->FromReflectedField(env, field));
1116 }
1117
1118 static jobject ToReflectedMethod(JNIEnv* env, jclass cls, jmethodID mid, jboolean isStatic) {
1119 CHECK_JNI_ENTRY(kFlag_Default, "Ecmb", env, cls, mid, isStatic);
1120 return CHECK_JNI_EXIT("L", baseEnv(env)->ToReflectedMethod(env, cls, mid, isStatic));
1121 }
1122
1123 static jobject ToReflectedField(JNIEnv* env, jclass cls, jfieldID fid, jboolean isStatic) {
1124 CHECK_JNI_ENTRY(kFlag_Default, "Ecfb", env, cls, fid, isStatic);
1125 return CHECK_JNI_EXIT("L", baseEnv(env)->ToReflectedField(env, cls, fid, isStatic));
1126 }
1127
1128 static jint Throw(JNIEnv* env, jthrowable obj) {
1129 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1130 // TODO: check that 'obj' is a java.lang.Throwable.
1131 return CHECK_JNI_EXIT("I", baseEnv(env)->Throw(env, obj));
1132 }
1133
1134 static jint ThrowNew(JNIEnv* env, jclass clazz, const char* message) {
1135 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Ecu", env, clazz, message);
1136 return CHECK_JNI_EXIT("I", baseEnv(env)->ThrowNew(env, clazz, message));
1137 }
1138
1139 static jthrowable ExceptionOccurred(JNIEnv* env) {
1140 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1141 return CHECK_JNI_EXIT("L", baseEnv(env)->ExceptionOccurred(env));
1142 }
1143
1144 static void ExceptionDescribe(JNIEnv* env) {
1145 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1146 baseEnv(env)->ExceptionDescribe(env);
1147 CHECK_JNI_EXIT_VOID();
1148 }
1149
1150 static void ExceptionClear(JNIEnv* env) {
1151 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1152 baseEnv(env)->ExceptionClear(env);
1153 CHECK_JNI_EXIT_VOID();
1154 }
1155
1156 static void FatalError(JNIEnv* env, const char* msg) {
1157 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Eu", env, msg);
1158 baseEnv(env)->FatalError(env, msg);
1159 CHECK_JNI_EXIT_VOID();
1160 }
1161
1162 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
1163 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EI", env, capacity);
1164 return CHECK_JNI_EXIT("I", baseEnv(env)->PushLocalFrame(env, capacity));
1165 }
1166
1167 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
1168 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, res);
1169 return CHECK_JNI_EXIT("L", baseEnv(env)->PopLocalFrame(env, res));
1170 }
1171
1172 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
1173 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1174 return CHECK_JNI_EXIT("L", baseEnv(env)->NewGlobalRef(env, obj));
1175 }
1176
1177 static jobject NewLocalRef(JNIEnv* env, jobject ref) {
1178 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, ref);
1179 return CHECK_JNI_EXIT("L", baseEnv(env)->NewLocalRef(env, ref));
1180 }
1181
1182 static void DeleteGlobalRef(JNIEnv* env, jobject globalRef) {
1183 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, globalRef);
1184 if (globalRef != NULL && GetIndirectRefKind(globalRef) != kGlobal) {
1185 LOG(ERROR) << "JNI ERROR: DeleteGlobalRef on " << GetIndirectRefKind(globalRef) << ": " << globalRef;
1186 JniAbort(__FUNCTION__);
1187 } else {
1188 baseEnv(env)->DeleteGlobalRef(env, globalRef);
1189 CHECK_JNI_EXIT_VOID();
1190 }
1191 }
1192
1193 static void DeleteWeakGlobalRef(JNIEnv* env, jweak weakGlobalRef) {
1194 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, weakGlobalRef);
1195 if (weakGlobalRef != NULL && GetIndirectRefKind(weakGlobalRef) != kWeakGlobal) {
1196 LOG(ERROR) << "JNI ERROR: DeleteWeakGlobalRef on " << GetIndirectRefKind(weakGlobalRef) << ": " << weakGlobalRef;
1197 JniAbort(__FUNCTION__);
1198 } else {
1199 baseEnv(env)->DeleteWeakGlobalRef(env, weakGlobalRef);
1200 CHECK_JNI_EXIT_VOID();
1201 }
1202 }
1203
1204 static void DeleteLocalRef(JNIEnv* env, jobject localRef) {
1205 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, localRef);
1206 if (localRef != NULL && GetIndirectRefKind(localRef) != kLocal) {
1207 LOG(ERROR) << "JNI ERROR: DeleteLocalRef on " << GetIndirectRefKind(localRef) << ": " << localRef;
1208 JniAbort(__FUNCTION__);
1209 } else {
1210 baseEnv(env)->DeleteLocalRef(env, localRef);
1211 CHECK_JNI_EXIT_VOID();
1212 }
1213 }
1214
1215 static jint EnsureLocalCapacity(JNIEnv *env, jint capacity) {
1216 CHECK_JNI_ENTRY(kFlag_Default, "EI", env, capacity);
1217 return CHECK_JNI_EXIT("I", baseEnv(env)->EnsureLocalCapacity(env, capacity));
1218 }
1219
1220 static jboolean IsSameObject(JNIEnv* env, jobject ref1, jobject ref2) {
1221 CHECK_JNI_ENTRY(kFlag_Default, "ELL", env, ref1, ref2);
1222 return CHECK_JNI_EXIT("b", baseEnv(env)->IsSameObject(env, ref1, ref2));
1223 }
1224
1225 static jobject AllocObject(JNIEnv* env, jclass clazz) {
1226 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, clazz);
1227 return CHECK_JNI_EXIT("L", baseEnv(env)->AllocObject(env, clazz));
1228 }
1229
1230 static jobject NewObject(JNIEnv* env, jclass clazz, jmethodID mid, ...) {
1231 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, clazz, mid);
1232 va_list args;
1233 va_start(args, mid);
1234 jobject result = baseEnv(env)->NewObjectV(env, clazz, mid, args);
1235 va_end(args);
1236 return CHECK_JNI_EXIT("L", result);
1237 }
1238
1239 static jobject NewObjectV(JNIEnv* env, jclass clazz, jmethodID mid, va_list args) {
1240 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, clazz, mid);
1241 return CHECK_JNI_EXIT("L", baseEnv(env)->NewObjectV(env, clazz, mid, args));
1242 }
1243
1244 static jobject NewObjectA(JNIEnv* env, jclass clazz, jmethodID mid, jvalue* args) {
1245 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, clazz, mid);
1246 return CHECK_JNI_EXIT("L", baseEnv(env)->NewObjectA(env, clazz, mid, args));
1247 }
1248
1249 static jclass GetObjectClass(JNIEnv* env, jobject obj) {
1250 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1251 return CHECK_JNI_EXIT("c", baseEnv(env)->GetObjectClass(env, obj));
1252 }
1253
1254 static jboolean IsInstanceOf(JNIEnv* env, jobject obj, jclass clazz) {
1255 CHECK_JNI_ENTRY(kFlag_Default, "ELc", env, obj, clazz);
1256 return CHECK_JNI_EXIT("b", baseEnv(env)->IsInstanceOf(env, obj, clazz));
1257 }
1258
1259 static jmethodID GetMethodID(JNIEnv* env, jclass clazz, const char* name, const char* sig) {
1260 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, clazz, name, sig);
1261 return CHECK_JNI_EXIT("m", baseEnv(env)->GetMethodID(env, clazz, name, sig));
1262 }
1263
1264 static jfieldID GetFieldID(JNIEnv* env, jclass clazz, const char* name, const char* sig) {
1265 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, clazz, name, sig);
1266 return CHECK_JNI_EXIT("f", baseEnv(env)->GetFieldID(env, clazz, name, sig));
1267 }
1268
1269 static jmethodID GetStaticMethodID(JNIEnv* env, jclass clazz, const char* name, const char* sig) {
1270 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, clazz, name, sig);
1271 return CHECK_JNI_EXIT("m", baseEnv(env)->GetStaticMethodID(env, clazz, name, sig));
1272 }
1273
1274 static jfieldID GetStaticFieldID(JNIEnv* env, jclass clazz, const char* name, const char* sig) {
1275 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, clazz, name, sig);
1276 return CHECK_JNI_EXIT("f", baseEnv(env)->GetStaticFieldID(env, clazz, name, sig));
1277 }
1278
1279#define FIELD_ACCESSORS(_ctype, _jname, _type) \
1280 static _ctype GetStatic##_jname##Field(JNIEnv* env, jclass clazz, jfieldID fid) { \
1281 CHECK_JNI_ENTRY(kFlag_Default, "Ecf", env, clazz, fid); \
1282 sc.checkStaticFieldID(clazz, fid); \
1283 return CHECK_JNI_EXIT(_type, baseEnv(env)->GetStatic##_jname##Field(env, clazz, fid)); \
1284 } \
1285 static _ctype Get##_jname##Field(JNIEnv* env, jobject obj, jfieldID fid) { \
1286 CHECK_JNI_ENTRY(kFlag_Default, "ELf", env, obj, fid); \
1287 sc.checkInstanceFieldID(obj, fid); \
1288 return CHECK_JNI_EXIT(_type, baseEnv(env)->Get##_jname##Field(env, obj, fid)); \
1289 } \
1290 static void SetStatic##_jname##Field(JNIEnv* env, jclass clazz, jfieldID fid, _ctype value) { \
1291 CHECK_JNI_ENTRY(kFlag_Default, "Ecf" _type, env, clazz, fid, value); \
1292 sc.checkStaticFieldID(clazz, fid); \
1293 /* "value" arg only used when type == ref */ \
1294 sc.checkFieldType((jobject)(uint32_t)value, fid, _type[0], true); \
1295 baseEnv(env)->SetStatic##_jname##Field(env, clazz, fid, value); \
1296 CHECK_JNI_EXIT_VOID(); \
1297 } \
1298 static void Set##_jname##Field(JNIEnv* env, jobject obj, jfieldID fid, _ctype value) { \
1299 CHECK_JNI_ENTRY(kFlag_Default, "ELf" _type, env, obj, fid, value); \
1300 sc.checkInstanceFieldID(obj, fid); \
1301 /* "value" arg only used when type == ref */ \
1302 sc.checkFieldType((jobject)(uint32_t) value, fid, _type[0], false); \
1303 baseEnv(env)->Set##_jname##Field(env, obj, fid, value); \
1304 CHECK_JNI_EXIT_VOID(); \
1305 }
1306
1307FIELD_ACCESSORS(jobject, Object, "L");
1308FIELD_ACCESSORS(jboolean, Boolean, "Z");
1309FIELD_ACCESSORS(jbyte, Byte, "B");
1310FIELD_ACCESSORS(jchar, Char, "C");
1311FIELD_ACCESSORS(jshort, Short, "S");
1312FIELD_ACCESSORS(jint, Int, "I");
1313FIELD_ACCESSORS(jlong, Long, "J");
1314FIELD_ACCESSORS(jfloat, Float, "F");
1315FIELD_ACCESSORS(jdouble, Double, "D");
1316
1317#define CALL(_ctype, _jname, _retdecl, _retasgn, _retok, _retsig) \
1318 /* Virtual... */ \
1319 static _ctype Call##_jname##Method(JNIEnv* env, jobject obj, \
1320 jmethodID mid, ...) \
1321 { \
1322 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
1323 sc.checkSig(mid, _retsig, false); \
1324 sc.checkVirtualMethod(obj, mid); \
1325 _retdecl; \
1326 va_list args; \
1327 va_start(args, mid); \
1328 _retasgn baseEnv(env)->Call##_jname##MethodV(env, obj, mid, args); \
1329 va_end(args); \
1330 _retok; \
1331 } \
1332 static _ctype Call##_jname##MethodV(JNIEnv* env, jobject obj, \
1333 jmethodID mid, va_list args) \
1334 { \
1335 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
1336 sc.checkSig(mid, _retsig, false); \
1337 sc.checkVirtualMethod(obj, mid); \
1338 _retdecl; \
1339 _retasgn baseEnv(env)->Call##_jname##MethodV(env, obj, mid, args); \
1340 _retok; \
1341 } \
1342 static _ctype Call##_jname##MethodA(JNIEnv* env, jobject obj, \
1343 jmethodID mid, jvalue* args) \
1344 { \
1345 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
1346 sc.checkSig(mid, _retsig, false); \
1347 sc.checkVirtualMethod(obj, mid); \
1348 _retdecl; \
1349 _retasgn baseEnv(env)->Call##_jname##MethodA(env, obj, mid, args); \
1350 _retok; \
1351 } \
1352 /* Non-virtual... */ \
1353 static _ctype CallNonvirtual##_jname##Method(JNIEnv* env, \
1354 jobject obj, jclass clazz, jmethodID mid, ...) \
1355 { \
1356 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, clazz, mid); /* TODO: args! */ \
1357 sc.checkSig(mid, _retsig, false); \
1358 sc.checkVirtualMethod(obj, mid); \
1359 _retdecl; \
1360 va_list args; \
1361 va_start(args, mid); \
1362 _retasgn baseEnv(env)->CallNonvirtual##_jname##MethodV(env, obj, clazz, mid, args); \
1363 va_end(args); \
1364 _retok; \
1365 } \
1366 static _ctype CallNonvirtual##_jname##MethodV(JNIEnv* env, \
1367 jobject obj, jclass clazz, jmethodID mid, va_list args) \
1368 { \
1369 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, clazz, mid); /* TODO: args! */ \
1370 sc.checkSig(mid, _retsig, false); \
1371 sc.checkVirtualMethod(obj, mid); \
1372 _retdecl; \
1373 _retasgn baseEnv(env)->CallNonvirtual##_jname##MethodV(env, obj, clazz, mid, args); \
1374 _retok; \
1375 } \
1376 static _ctype CallNonvirtual##_jname##MethodA(JNIEnv* env, \
1377 jobject obj, jclass clazz, jmethodID mid, jvalue* args) \
1378 { \
1379 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, clazz, mid); /* TODO: args! */ \
1380 sc.checkSig(mid, _retsig, false); \
1381 sc.checkVirtualMethod(obj, mid); \
1382 _retdecl; \
1383 _retasgn baseEnv(env)->CallNonvirtual##_jname##MethodA(env, obj, clazz, mid, args); \
1384 _retok; \
1385 } \
1386 /* Static... */ \
1387 static _ctype CallStatic##_jname##Method(JNIEnv* env, \
1388 jclass clazz, jmethodID mid, ...) \
1389 { \
1390 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, clazz, mid); /* TODO: args! */ \
1391 sc.checkSig(mid, _retsig, true); \
1392 sc.checkStaticMethod(clazz, mid); \
1393 _retdecl; \
1394 va_list args; \
1395 va_start(args, mid); \
1396 _retasgn baseEnv(env)->CallStatic##_jname##MethodV(env, clazz, mid, args); \
1397 va_end(args); \
1398 _retok; \
1399 } \
1400 static _ctype CallStatic##_jname##MethodV(JNIEnv* env, \
1401 jclass clazz, jmethodID mid, va_list args) \
1402 { \
1403 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, clazz, mid); /* TODO: args! */ \
1404 sc.checkSig(mid, _retsig, true); \
1405 sc.checkStaticMethod(clazz, mid); \
1406 _retdecl; \
1407 _retasgn baseEnv(env)->CallStatic##_jname##MethodV(env, clazz, mid, args); \
1408 _retok; \
1409 } \
1410 static _ctype CallStatic##_jname##MethodA(JNIEnv* env, \
1411 jclass clazz, jmethodID mid, jvalue* args) \
1412 { \
1413 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, clazz, mid); /* TODO: args! */ \
1414 sc.checkSig(mid, _retsig, true); \
1415 sc.checkStaticMethod(clazz, mid); \
1416 _retdecl; \
1417 _retasgn baseEnv(env)->CallStatic##_jname##MethodA(env, clazz, mid, args); \
1418 _retok; \
1419 }
1420
1421#define NON_VOID_RETURN(_retsig, _ctype) return CHECK_JNI_EXIT(_retsig, (_ctype) result)
1422#define VOID_RETURN CHECK_JNI_EXIT_VOID()
1423
1424CALL(jobject, Object, Object* result, result=(Object*), NON_VOID_RETURN("L", jobject), "L");
1425CALL(jboolean, Boolean, jboolean result, result=, NON_VOID_RETURN("Z", jboolean), "Z");
1426CALL(jbyte, Byte, jbyte result, result=, NON_VOID_RETURN("B", jbyte), "B");
1427CALL(jchar, Char, jchar result, result=, NON_VOID_RETURN("C", jchar), "C");
1428CALL(jshort, Short, jshort result, result=, NON_VOID_RETURN("S", jshort), "S");
1429CALL(jint, Int, jint result, result=, NON_VOID_RETURN("I", jint), "I");
1430CALL(jlong, Long, jlong result, result=, NON_VOID_RETURN("J", jlong), "J");
1431CALL(jfloat, Float, jfloat result, result=, NON_VOID_RETURN("F", jfloat), "F");
1432CALL(jdouble, Double, jdouble result, result=, NON_VOID_RETURN("D", jdouble), "D");
1433CALL(void, Void, , , VOID_RETURN, "V");
1434
1435 static jstring NewString(JNIEnv* env, const jchar* unicodeChars, jsize len) {
1436 CHECK_JNI_ENTRY(kFlag_Default, "Epz", env, unicodeChars, len);
1437 return CHECK_JNI_EXIT("s", baseEnv(env)->NewString(env, unicodeChars, len));
1438 }
1439
1440 static jsize GetStringLength(JNIEnv* env, jstring string) {
1441 CHECK_JNI_ENTRY(kFlag_CritOkay, "Es", env, string);
1442 return CHECK_JNI_EXIT("I", baseEnv(env)->GetStringLength(env, string));
1443 }
1444
1445 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* isCopy) {
1446 CHECK_JNI_ENTRY(kFlag_CritOkay, "Esp", env, java_string, isCopy);
1447 const jchar* result = baseEnv(env)->GetStringChars(env, java_string, isCopy);
1448 if (sc.forceCopy() && result != NULL) {
1449 ScopedJniThreadState ts(env);
1450 String* s = Decode<String*>(ts, java_string);
1451 int byteCount = s->GetLength() * 2;
1452 result = (const jchar*) GuardedCopy::create(result, byteCount, false);
1453 if (isCopy != NULL) {
1454 *isCopy = JNI_TRUE;
1455 }
1456 }
1457 return CHECK_JNI_EXIT("p", result);
1458 }
1459
1460 static void ReleaseStringChars(JNIEnv* env, jstring string, const jchar* chars) {
1461 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "Esp", env, string, chars);
1462 sc.checkNonNull(chars);
1463 if (sc.forceCopy()) {
1464 GuardedCopy::check(__FUNCTION__, chars, false);
1465 chars = (const jchar*) GuardedCopy::destroy((jchar*)chars);
1466 }
1467 baseEnv(env)->ReleaseStringChars(env, string, chars);
1468 CHECK_JNI_EXIT_VOID();
1469 }
1470
1471 static jstring NewStringUTF(JNIEnv* env, const char* bytes) {
1472 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Eu", env, bytes); // TODO: show pointer and truncate string.
1473 return CHECK_JNI_EXIT("s", baseEnv(env)->NewStringUTF(env, bytes));
1474 }
1475
1476 static jsize GetStringUTFLength(JNIEnv* env, jstring string) {
1477 CHECK_JNI_ENTRY(kFlag_CritOkay, "Es", env, string);
1478 return CHECK_JNI_EXIT("I", baseEnv(env)->GetStringUTFLength(env, string));
1479 }
1480
1481 static const char* GetStringUTFChars(JNIEnv* env, jstring string, jboolean* isCopy) {
1482 CHECK_JNI_ENTRY(kFlag_CritOkay, "Esp", env, string, isCopy);
1483 const char* result = baseEnv(env)->GetStringUTFChars(env, string, isCopy);
1484 if (sc.forceCopy() && result != NULL) {
1485 result = (const char*) GuardedCopy::create(result, strlen(result) + 1, false);
1486 if (isCopy != NULL) {
1487 *isCopy = JNI_TRUE;
1488 }
1489 }
1490 return CHECK_JNI_EXIT("u", result); // TODO: show pointer and truncate string.
1491 }
1492
1493 static void ReleaseStringUTFChars(JNIEnv* env, jstring string, const char* utf) {
1494 CHECK_JNI_ENTRY(kFlag_ExcepOkay | kFlag_Release, "Esu", env, string, utf); // TODO: show pointer and truncate string.
1495 if (sc.forceCopy()) {
1496 GuardedCopy::check(__FUNCTION__, utf, false);
1497 utf = (const char*) GuardedCopy::destroy((char*)utf);
1498 }
1499 baseEnv(env)->ReleaseStringUTFChars(env, string, utf);
1500 CHECK_JNI_EXIT_VOID();
1501 }
1502
1503 static jsize GetArrayLength(JNIEnv* env, jarray array) {
1504 CHECK_JNI_ENTRY(kFlag_CritOkay, "Ea", env, array);
1505 return CHECK_JNI_EXIT("I", baseEnv(env)->GetArrayLength(env, array));
1506 }
1507
1508 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass elementClass, jobject initialElement) {
1509 CHECK_JNI_ENTRY(kFlag_Default, "EzcL", env, length, elementClass, initialElement);
1510 return CHECK_JNI_EXIT("a", baseEnv(env)->NewObjectArray(env, length, elementClass, initialElement));
1511 }
1512
1513 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index) {
1514 CHECK_JNI_ENTRY(kFlag_Default, "EaI", env, array, index);
1515 return CHECK_JNI_EXIT("L", baseEnv(env)->GetObjectArrayElement(env, array, index));
1516 }
1517
1518 static void SetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index, jobject value) {
1519 CHECK_JNI_ENTRY(kFlag_Default, "EaIL", env, array, index, value);
1520 baseEnv(env)->SetObjectArrayElement(env, array, index, value);
1521 CHECK_JNI_EXIT_VOID();
1522 }
1523
1524#define NEW_PRIMITIVE_ARRAY(_artype, _jname) \
1525 static _artype New##_jname##Array(JNIEnv* env, jsize length) { \
1526 CHECK_JNI_ENTRY(kFlag_Default, "Ez", env, length); \
1527 return CHECK_JNI_EXIT("a", baseEnv(env)->New##_jname##Array(env, length)); \
1528 }
1529NEW_PRIMITIVE_ARRAY(jbooleanArray, Boolean);
1530NEW_PRIMITIVE_ARRAY(jbyteArray, Byte);
1531NEW_PRIMITIVE_ARRAY(jcharArray, Char);
1532NEW_PRIMITIVE_ARRAY(jshortArray, Short);
1533NEW_PRIMITIVE_ARRAY(jintArray, Int);
1534NEW_PRIMITIVE_ARRAY(jlongArray, Long);
1535NEW_PRIMITIVE_ARRAY(jfloatArray, Float);
1536NEW_PRIMITIVE_ARRAY(jdoubleArray, Double);
1537
1538class ForceCopyGetChecker {
1539public:
1540 ForceCopyGetChecker(ScopedCheck& sc, jboolean* isCopy) {
1541 forceCopy = sc.forceCopy();
1542 noCopy = 0;
1543 if (forceCopy && isCopy != NULL) {
1544 /* capture this before the base call tramples on it */
1545 noCopy = *(uint32_t*) isCopy;
1546 }
1547 }
1548
1549 template<typename ResultT>
1550 ResultT check(JNIEnv* env, jarray array, jboolean* isCopy, ResultT result) {
1551 if (forceCopy && result != NULL) {
1552 if (noCopy != kNoCopyMagic) {
1553 result = reinterpret_cast<ResultT>(CreateGuardedPACopy(env, array, isCopy));
1554 }
1555 }
1556 return result;
1557 }
1558
1559 uint32_t noCopy;
1560 bool forceCopy;
1561};
1562
1563#define GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \
1564 static _ctype* Get##_jname##ArrayElements(JNIEnv* env, _ctype##Array array, jboolean* isCopy) { \
1565 CHECK_JNI_ENTRY(kFlag_Default, "Eap", env, array, isCopy); \
1566 _ctype* result = ForceCopyGetChecker(sc, isCopy).check(env, array, isCopy, baseEnv(env)->Get##_jname##ArrayElements(env, array, isCopy)); \
1567 return CHECK_JNI_EXIT("p", result); \
1568 }
1569
1570#define RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \
1571 static void Release##_jname##ArrayElements(JNIEnv* env, _ctype##Array array, _ctype* elems, jint mode) { \
1572 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "Eapr", env, array, elems, mode); \
1573 sc.checkNonNull(elems); \
1574 if (sc.forceCopy()) { \
1575 ReleaseGuardedPACopy(env, array, elems, mode); \
1576 } \
1577 baseEnv(env)->Release##_jname##ArrayElements(env, array, elems, mode); \
1578 CHECK_JNI_EXIT_VOID(); \
1579 }
1580
1581#define GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \
1582 static void Get##_jname##ArrayRegion(JNIEnv* env, _ctype##Array array, jsize start, jsize len, _ctype* buf) { \
1583 CHECK_JNI_ENTRY(kFlag_Default, "EaIIp", env, array, start, len, buf); \
1584 baseEnv(env)->Get##_jname##ArrayRegion(env, array, start, len, buf); \
1585 CHECK_JNI_EXIT_VOID(); \
1586 }
1587
1588#define SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \
1589 static void Set##_jname##ArrayRegion(JNIEnv* env, _ctype##Array array, jsize start, jsize len, const _ctype* buf) { \
1590 CHECK_JNI_ENTRY(kFlag_Default, "EaIIp", env, array, start, len, buf); \
1591 baseEnv(env)->Set##_jname##ArrayRegion(env, array, start, len, buf); \
1592 CHECK_JNI_EXIT_VOID(); \
1593 }
1594
1595#define PRIMITIVE_ARRAY_FUNCTIONS(_ctype, _jname, _typechar) \
1596 GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \
1597 RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \
1598 GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname); \
1599 SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname);
1600
1601/* TODO: verify primitive array type matches call type */
1602PRIMITIVE_ARRAY_FUNCTIONS(jboolean, Boolean, 'Z');
1603PRIMITIVE_ARRAY_FUNCTIONS(jbyte, Byte, 'B');
1604PRIMITIVE_ARRAY_FUNCTIONS(jchar, Char, 'C');
1605PRIMITIVE_ARRAY_FUNCTIONS(jshort, Short, 'S');
1606PRIMITIVE_ARRAY_FUNCTIONS(jint, Int, 'I');
1607PRIMITIVE_ARRAY_FUNCTIONS(jlong, Long, 'J');
1608PRIMITIVE_ARRAY_FUNCTIONS(jfloat, Float, 'F');
1609PRIMITIVE_ARRAY_FUNCTIONS(jdouble, Double, 'D');
1610
1611 static jint RegisterNatives(JNIEnv* env, jclass clazz, const JNINativeMethod* methods, jint nMethods) {
1612 CHECK_JNI_ENTRY(kFlag_Default, "EcpI", env, clazz, methods, nMethods);
1613 return CHECK_JNI_EXIT("I", baseEnv(env)->RegisterNatives(env, clazz, methods, nMethods));
1614 }
1615
1616 static jint UnregisterNatives(JNIEnv* env, jclass clazz) {
1617 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, clazz);
1618 return CHECK_JNI_EXIT("I", baseEnv(env)->UnregisterNatives(env, clazz));
1619 }
1620
1621 static jint MonitorEnter(JNIEnv* env, jobject obj) {
1622 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1623 return CHECK_JNI_EXIT("I", baseEnv(env)->MonitorEnter(env, obj));
1624 }
1625
1626 static jint MonitorExit(JNIEnv* env, jobject obj) {
1627 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, obj);
1628 return CHECK_JNI_EXIT("I", baseEnv(env)->MonitorExit(env, obj));
1629 }
1630
1631 static jint GetJavaVM(JNIEnv *env, JavaVM **vm) {
1632 CHECK_JNI_ENTRY(kFlag_Default, "Ep", env, vm);
1633 return CHECK_JNI_EXIT("I", baseEnv(env)->GetJavaVM(env, vm));
1634 }
1635
1636 static void GetStringRegion(JNIEnv* env, jstring str, jsize start, jsize len, jchar* buf) {
1637 CHECK_JNI_ENTRY(kFlag_CritOkay, "EsIIp", env, str, start, len, buf);
1638 baseEnv(env)->GetStringRegion(env, str, start, len, buf);
1639 CHECK_JNI_EXIT_VOID();
1640 }
1641
1642 static void GetStringUTFRegion(JNIEnv* env, jstring str, jsize start, jsize len, char* buf) {
1643 CHECK_JNI_ENTRY(kFlag_CritOkay, "EsIIp", env, str, start, len, buf);
1644 baseEnv(env)->GetStringUTFRegion(env, str, start, len, buf);
1645 CHECK_JNI_EXIT_VOID();
1646 }
1647
1648 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* isCopy) {
1649 CHECK_JNI_ENTRY(kFlag_CritGet, "Eap", env, array, isCopy);
1650 void* result = baseEnv(env)->GetPrimitiveArrayCritical(env, array, isCopy);
1651 if (sc.forceCopy() && result != NULL) {
1652 result = CreateGuardedPACopy(env, array, isCopy);
1653 }
1654 return CHECK_JNI_EXIT("p", result);
1655 }
1656
1657 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* carray, jint mode) {
1658 CHECK_JNI_ENTRY(kFlag_CritRelease | kFlag_ExcepOkay, "Eapr", env, array, carray, mode);
1659 sc.checkNonNull(carray);
1660 if (sc.forceCopy()) {
1661 ReleaseGuardedPACopy(env, array, carray, mode);
1662 }
1663 baseEnv(env)->ReleasePrimitiveArrayCritical(env, array, carray, mode);
1664 CHECK_JNI_EXIT_VOID();
1665 }
1666
1667 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* isCopy) {
1668 CHECK_JNI_ENTRY(kFlag_CritGet, "Esp", env, java_string, isCopy);
1669 const jchar* result = baseEnv(env)->GetStringCritical(env, java_string, isCopy);
1670 if (sc.forceCopy() && result != NULL) {
1671 ScopedJniThreadState ts(env);
1672 String* s = Decode<String*>(ts, java_string);
1673 int byteCount = s->GetLength() * 2;
1674 result = (const jchar*) GuardedCopy::create(result, byteCount, false);
1675 if (isCopy != NULL) {
1676 *isCopy = JNI_TRUE;
1677 }
1678 }
1679 return CHECK_JNI_EXIT("p", result);
1680 }
1681
1682 static void ReleaseStringCritical(JNIEnv* env, jstring string, const jchar* carray) {
1683 CHECK_JNI_ENTRY(kFlag_CritRelease | kFlag_ExcepOkay, "Esp", env, string, carray);
1684 sc.checkNonNull(carray);
1685 if (sc.forceCopy()) {
1686 GuardedCopy::check(__FUNCTION__, carray, false);
1687 carray = (const jchar*) GuardedCopy::destroy((jchar*)carray);
1688 }
1689 baseEnv(env)->ReleaseStringCritical(env, string, carray);
1690 CHECK_JNI_EXIT_VOID();
1691 }
1692
1693 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
1694 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1695 return CHECK_JNI_EXIT("L", baseEnv(env)->NewWeakGlobalRef(env, obj));
1696 }
1697
1698 static jboolean ExceptionCheck(JNIEnv* env) {
1699 CHECK_JNI_ENTRY(kFlag_CritOkay | kFlag_ExcepOkay, "E", env);
1700 return CHECK_JNI_EXIT("b", baseEnv(env)->ExceptionCheck(env));
1701 }
1702
1703 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject obj) {
1704 // Note: we use "Ep" rather than "EL" because this is the one JNI function
1705 // that it's okay to pass an invalid reference to.
1706 CHECK_JNI_ENTRY(kFlag_Default, "Ep", env, obj);
1707 // TODO: proper decoding of jobjectRefType!
1708 return CHECK_JNI_EXIT("I", baseEnv(env)->GetObjectRefType(env, obj));
1709 }
1710
1711 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
1712 CHECK_JNI_ENTRY(kFlag_Default, "EpJ", env, address, capacity);
1713 if (address == NULL) {
1714 LOG(ERROR) << "JNI ERROR: non-nullable address is NULL";
1715 JniAbort(__FUNCTION__);
1716 }
1717 if (capacity <= 0) {
1718 LOG(ERROR) << "JNI ERROR: capacity must be greater than 0: " << capacity;
1719 JniAbort(__FUNCTION__);
1720 }
1721 return CHECK_JNI_EXIT("L", baseEnv(env)->NewDirectByteBuffer(env, address, capacity));
1722 }
1723
1724 static void* GetDirectBufferAddress(JNIEnv* env, jobject buf) {
1725 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, buf);
1726 // TODO: check that 'buf' is a java.nio.Buffer.
1727 return CHECK_JNI_EXIT("p", baseEnv(env)->GetDirectBufferAddress(env, buf));
1728 }
1729
1730 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject buf) {
1731 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, buf);
1732 // TODO: check that 'buf' is a java.nio.Buffer.
1733 return CHECK_JNI_EXIT("J", baseEnv(env)->GetDirectBufferCapacity(env, buf));
1734 }
1735
1736 private:
1737 static inline const JNINativeInterface* baseEnv(JNIEnv* env) {
1738 return reinterpret_cast<JNIEnvExt*>(env)->unchecked_functions;
1739 }
1740};
1741
1742const JNINativeInterface gCheckNativeInterface = {
1743 NULL, // reserved0.
1744 NULL, // reserved1.
1745 NULL, // reserved2.
1746 NULL, // reserved3.
1747 CheckJNI::GetVersion,
1748 CheckJNI::DefineClass,
1749 CheckJNI::FindClass,
1750 CheckJNI::FromReflectedMethod,
1751 CheckJNI::FromReflectedField,
1752 CheckJNI::ToReflectedMethod,
1753 CheckJNI::GetSuperclass,
1754 CheckJNI::IsAssignableFrom,
1755 CheckJNI::ToReflectedField,
1756 CheckJNI::Throw,
1757 CheckJNI::ThrowNew,
1758 CheckJNI::ExceptionOccurred,
1759 CheckJNI::ExceptionDescribe,
1760 CheckJNI::ExceptionClear,
1761 CheckJNI::FatalError,
1762 CheckJNI::PushLocalFrame,
1763 CheckJNI::PopLocalFrame,
1764 CheckJNI::NewGlobalRef,
1765 CheckJNI::DeleteGlobalRef,
1766 CheckJNI::DeleteLocalRef,
1767 CheckJNI::IsSameObject,
1768 CheckJNI::NewLocalRef,
1769 CheckJNI::EnsureLocalCapacity,
1770 CheckJNI::AllocObject,
1771 CheckJNI::NewObject,
1772 CheckJNI::NewObjectV,
1773 CheckJNI::NewObjectA,
1774 CheckJNI::GetObjectClass,
1775 CheckJNI::IsInstanceOf,
1776 CheckJNI::GetMethodID,
1777 CheckJNI::CallObjectMethod,
1778 CheckJNI::CallObjectMethodV,
1779 CheckJNI::CallObjectMethodA,
1780 CheckJNI::CallBooleanMethod,
1781 CheckJNI::CallBooleanMethodV,
1782 CheckJNI::CallBooleanMethodA,
1783 CheckJNI::CallByteMethod,
1784 CheckJNI::CallByteMethodV,
1785 CheckJNI::CallByteMethodA,
1786 CheckJNI::CallCharMethod,
1787 CheckJNI::CallCharMethodV,
1788 CheckJNI::CallCharMethodA,
1789 CheckJNI::CallShortMethod,
1790 CheckJNI::CallShortMethodV,
1791 CheckJNI::CallShortMethodA,
1792 CheckJNI::CallIntMethod,
1793 CheckJNI::CallIntMethodV,
1794 CheckJNI::CallIntMethodA,
1795 CheckJNI::CallLongMethod,
1796 CheckJNI::CallLongMethodV,
1797 CheckJNI::CallLongMethodA,
1798 CheckJNI::CallFloatMethod,
1799 CheckJNI::CallFloatMethodV,
1800 CheckJNI::CallFloatMethodA,
1801 CheckJNI::CallDoubleMethod,
1802 CheckJNI::CallDoubleMethodV,
1803 CheckJNI::CallDoubleMethodA,
1804 CheckJNI::CallVoidMethod,
1805 CheckJNI::CallVoidMethodV,
1806 CheckJNI::CallVoidMethodA,
1807 CheckJNI::CallNonvirtualObjectMethod,
1808 CheckJNI::CallNonvirtualObjectMethodV,
1809 CheckJNI::CallNonvirtualObjectMethodA,
1810 CheckJNI::CallNonvirtualBooleanMethod,
1811 CheckJNI::CallNonvirtualBooleanMethodV,
1812 CheckJNI::CallNonvirtualBooleanMethodA,
1813 CheckJNI::CallNonvirtualByteMethod,
1814 CheckJNI::CallNonvirtualByteMethodV,
1815 CheckJNI::CallNonvirtualByteMethodA,
1816 CheckJNI::CallNonvirtualCharMethod,
1817 CheckJNI::CallNonvirtualCharMethodV,
1818 CheckJNI::CallNonvirtualCharMethodA,
1819 CheckJNI::CallNonvirtualShortMethod,
1820 CheckJNI::CallNonvirtualShortMethodV,
1821 CheckJNI::CallNonvirtualShortMethodA,
1822 CheckJNI::CallNonvirtualIntMethod,
1823 CheckJNI::CallNonvirtualIntMethodV,
1824 CheckJNI::CallNonvirtualIntMethodA,
1825 CheckJNI::CallNonvirtualLongMethod,
1826 CheckJNI::CallNonvirtualLongMethodV,
1827 CheckJNI::CallNonvirtualLongMethodA,
1828 CheckJNI::CallNonvirtualFloatMethod,
1829 CheckJNI::CallNonvirtualFloatMethodV,
1830 CheckJNI::CallNonvirtualFloatMethodA,
1831 CheckJNI::CallNonvirtualDoubleMethod,
1832 CheckJNI::CallNonvirtualDoubleMethodV,
1833 CheckJNI::CallNonvirtualDoubleMethodA,
1834 CheckJNI::CallNonvirtualVoidMethod,
1835 CheckJNI::CallNonvirtualVoidMethodV,
1836 CheckJNI::CallNonvirtualVoidMethodA,
1837 CheckJNI::GetFieldID,
1838 CheckJNI::GetObjectField,
1839 CheckJNI::GetBooleanField,
1840 CheckJNI::GetByteField,
1841 CheckJNI::GetCharField,
1842 CheckJNI::GetShortField,
1843 CheckJNI::GetIntField,
1844 CheckJNI::GetLongField,
1845 CheckJNI::GetFloatField,
1846 CheckJNI::GetDoubleField,
1847 CheckJNI::SetObjectField,
1848 CheckJNI::SetBooleanField,
1849 CheckJNI::SetByteField,
1850 CheckJNI::SetCharField,
1851 CheckJNI::SetShortField,
1852 CheckJNI::SetIntField,
1853 CheckJNI::SetLongField,
1854 CheckJNI::SetFloatField,
1855 CheckJNI::SetDoubleField,
1856 CheckJNI::GetStaticMethodID,
1857 CheckJNI::CallStaticObjectMethod,
1858 CheckJNI::CallStaticObjectMethodV,
1859 CheckJNI::CallStaticObjectMethodA,
1860 CheckJNI::CallStaticBooleanMethod,
1861 CheckJNI::CallStaticBooleanMethodV,
1862 CheckJNI::CallStaticBooleanMethodA,
1863 CheckJNI::CallStaticByteMethod,
1864 CheckJNI::CallStaticByteMethodV,
1865 CheckJNI::CallStaticByteMethodA,
1866 CheckJNI::CallStaticCharMethod,
1867 CheckJNI::CallStaticCharMethodV,
1868 CheckJNI::CallStaticCharMethodA,
1869 CheckJNI::CallStaticShortMethod,
1870 CheckJNI::CallStaticShortMethodV,
1871 CheckJNI::CallStaticShortMethodA,
1872 CheckJNI::CallStaticIntMethod,
1873 CheckJNI::CallStaticIntMethodV,
1874 CheckJNI::CallStaticIntMethodA,
1875 CheckJNI::CallStaticLongMethod,
1876 CheckJNI::CallStaticLongMethodV,
1877 CheckJNI::CallStaticLongMethodA,
1878 CheckJNI::CallStaticFloatMethod,
1879 CheckJNI::CallStaticFloatMethodV,
1880 CheckJNI::CallStaticFloatMethodA,
1881 CheckJNI::CallStaticDoubleMethod,
1882 CheckJNI::CallStaticDoubleMethodV,
1883 CheckJNI::CallStaticDoubleMethodA,
1884 CheckJNI::CallStaticVoidMethod,
1885 CheckJNI::CallStaticVoidMethodV,
1886 CheckJNI::CallStaticVoidMethodA,
1887 CheckJNI::GetStaticFieldID,
1888 CheckJNI::GetStaticObjectField,
1889 CheckJNI::GetStaticBooleanField,
1890 CheckJNI::GetStaticByteField,
1891 CheckJNI::GetStaticCharField,
1892 CheckJNI::GetStaticShortField,
1893 CheckJNI::GetStaticIntField,
1894 CheckJNI::GetStaticLongField,
1895 CheckJNI::GetStaticFloatField,
1896 CheckJNI::GetStaticDoubleField,
1897 CheckJNI::SetStaticObjectField,
1898 CheckJNI::SetStaticBooleanField,
1899 CheckJNI::SetStaticByteField,
1900 CheckJNI::SetStaticCharField,
1901 CheckJNI::SetStaticShortField,
1902 CheckJNI::SetStaticIntField,
1903 CheckJNI::SetStaticLongField,
1904 CheckJNI::SetStaticFloatField,
1905 CheckJNI::SetStaticDoubleField,
1906 CheckJNI::NewString,
1907 CheckJNI::GetStringLength,
1908 CheckJNI::GetStringChars,
1909 CheckJNI::ReleaseStringChars,
1910 CheckJNI::NewStringUTF,
1911 CheckJNI::GetStringUTFLength,
1912 CheckJNI::GetStringUTFChars,
1913 CheckJNI::ReleaseStringUTFChars,
1914 CheckJNI::GetArrayLength,
1915 CheckJNI::NewObjectArray,
1916 CheckJNI::GetObjectArrayElement,
1917 CheckJNI::SetObjectArrayElement,
1918 CheckJNI::NewBooleanArray,
1919 CheckJNI::NewByteArray,
1920 CheckJNI::NewCharArray,
1921 CheckJNI::NewShortArray,
1922 CheckJNI::NewIntArray,
1923 CheckJNI::NewLongArray,
1924 CheckJNI::NewFloatArray,
1925 CheckJNI::NewDoubleArray,
1926 CheckJNI::GetBooleanArrayElements,
1927 CheckJNI::GetByteArrayElements,
1928 CheckJNI::GetCharArrayElements,
1929 CheckJNI::GetShortArrayElements,
1930 CheckJNI::GetIntArrayElements,
1931 CheckJNI::GetLongArrayElements,
1932 CheckJNI::GetFloatArrayElements,
1933 CheckJNI::GetDoubleArrayElements,
1934 CheckJNI::ReleaseBooleanArrayElements,
1935 CheckJNI::ReleaseByteArrayElements,
1936 CheckJNI::ReleaseCharArrayElements,
1937 CheckJNI::ReleaseShortArrayElements,
1938 CheckJNI::ReleaseIntArrayElements,
1939 CheckJNI::ReleaseLongArrayElements,
1940 CheckJNI::ReleaseFloatArrayElements,
1941 CheckJNI::ReleaseDoubleArrayElements,
1942 CheckJNI::GetBooleanArrayRegion,
1943 CheckJNI::GetByteArrayRegion,
1944 CheckJNI::GetCharArrayRegion,
1945 CheckJNI::GetShortArrayRegion,
1946 CheckJNI::GetIntArrayRegion,
1947 CheckJNI::GetLongArrayRegion,
1948 CheckJNI::GetFloatArrayRegion,
1949 CheckJNI::GetDoubleArrayRegion,
1950 CheckJNI::SetBooleanArrayRegion,
1951 CheckJNI::SetByteArrayRegion,
1952 CheckJNI::SetCharArrayRegion,
1953 CheckJNI::SetShortArrayRegion,
1954 CheckJNI::SetIntArrayRegion,
1955 CheckJNI::SetLongArrayRegion,
1956 CheckJNI::SetFloatArrayRegion,
1957 CheckJNI::SetDoubleArrayRegion,
1958 CheckJNI::RegisterNatives,
1959 CheckJNI::UnregisterNatives,
1960 CheckJNI::MonitorEnter,
1961 CheckJNI::MonitorExit,
1962 CheckJNI::GetJavaVM,
1963 CheckJNI::GetStringRegion,
1964 CheckJNI::GetStringUTFRegion,
1965 CheckJNI::GetPrimitiveArrayCritical,
1966 CheckJNI::ReleasePrimitiveArrayCritical,
1967 CheckJNI::GetStringCritical,
1968 CheckJNI::ReleaseStringCritical,
1969 CheckJNI::NewWeakGlobalRef,
1970 CheckJNI::DeleteWeakGlobalRef,
1971 CheckJNI::ExceptionCheck,
1972 CheckJNI::NewDirectByteBuffer,
1973 CheckJNI::GetDirectBufferAddress,
1974 CheckJNI::GetDirectBufferCapacity,
1975 CheckJNI::GetObjectRefType,
1976};
1977
1978const JNINativeInterface* GetCheckJniNativeInterface() {
1979 return &gCheckNativeInterface;
1980}
1981
1982class CheckJII {
1983public:
1984 static jint DestroyJavaVM(JavaVM* vm) {
Elliott Hughesa0957642011-09-02 14:27:33 -07001985 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughesa2501992011-08-26 19:39:54 -07001986 sc.check(true, "v", vm);
1987 return CHECK_JNI_EXIT("I", baseVm(vm)->DestroyJavaVM(vm));
1988 }
1989
1990 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughesa0957642011-09-02 14:27:33 -07001991 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughesa2501992011-08-26 19:39:54 -07001992 sc.check(true, "vpp", vm, p_env, thr_args);
1993 return CHECK_JNI_EXIT("I", baseVm(vm)->AttachCurrentThread(vm, p_env, thr_args));
1994 }
1995
1996 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughesa0957642011-09-02 14:27:33 -07001997 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughesa2501992011-08-26 19:39:54 -07001998 sc.check(true, "vpp", vm, p_env, thr_args);
1999 return CHECK_JNI_EXIT("I", baseVm(vm)->AttachCurrentThreadAsDaemon(vm, p_env, thr_args));
2000 }
2001
2002 static jint DetachCurrentThread(JavaVM* vm) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002003 ScopedCheck sc(vm, true, __FUNCTION__);
Elliott Hughesa2501992011-08-26 19:39:54 -07002004 sc.check(true, "v", vm);
2005 return CHECK_JNI_EXIT("I", baseVm(vm)->DetachCurrentThread(vm));
2006 }
2007
2008 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002009 ScopedCheck sc(vm, true, __FUNCTION__);
Elliott Hughesa2501992011-08-26 19:39:54 -07002010 sc.check(true, "v", vm);
2011 return CHECK_JNI_EXIT("I", baseVm(vm)->GetEnv(vm, env, version));
2012 }
2013
2014 private:
2015 static inline const JNIInvokeInterface* baseVm(JavaVM* vm) {
2016 return reinterpret_cast<JavaVMExt*>(vm)->unchecked_functions;
2017 }
2018};
2019
2020const JNIInvokeInterface gCheckInvokeInterface = {
2021 NULL, // reserved0
2022 NULL, // reserved1
2023 NULL, // reserved2
2024 CheckJII::DestroyJavaVM,
2025 CheckJII::AttachCurrentThread,
2026 CheckJII::DetachCurrentThread,
2027 CheckJII::GetEnv,
2028 CheckJII::AttachCurrentThreadAsDaemon
2029};
2030
2031const JNIInvokeInterface* GetCheckJniInvokeInterface() {
2032 return &gCheckInvokeInterface;
2033}
2034
2035} // namespace art