blob: 4da4b37700dd03314364789dc05014623dcbdca8 [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
Elliott Hughes3f6635a2012-06-19 13:37:49 -070036static void JniAbort(const char* jni_function_name, const char* msg) {
Elliott Hughesa0957642011-09-02 14:27:33 -070037 Thread* self = Thread::Current();
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 Hughes3f6635a2012-06-19 13:37:49 -070041 os << "JNI DETECTED ERROR IN APPLICATION: " << msg;
Elliott Hughesa2501992011-08-26 19:39:54 -070042
43 if (jni_function_name != NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -070044 os << "\n in call to " << jni_function_name;
Elliott Hughesa2501992011-08-26 19:39:54 -070045 }
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) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -070048 os << "\n from " << PrettyMethod(current_method);
Elliott Hughesa0957642011-09-02 14:27:33 -070049 }
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) {
Elliott Hughesb264f082012-04-06 17:10:10 -070055 vm->check_jni_abort_hook(vm->check_jni_abort_hook_data, os.str());
Elliott Hughesa2501992011-08-26 19:39:54 -070056 } else {
Elliott Hughes34e06962012-04-09 13:55:55 -070057 self->SetState(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
Elliott Hughes3f6635a2012-06-19 13:37:49 -070062static void JniAbortV(const char* jni_function_name, const char* fmt, va_list ap) {
63 std::string msg;
64 StringAppendV(&msg, fmt, ap);
65 JniAbort(jni_function_name, msg.c_str());
66}
67
68void JniAbortF(const char* jni_function_name, const char* fmt, ...) {
69 va_list args;
70 va_start(args, fmt);
71 JniAbortV(jni_function_name, fmt, args);
72 va_end(args);
73}
74
Elliott Hughesa2501992011-08-26 19:39:54 -070075/*
76 * ===========================================================================
77 * JNI function helpers
78 * ===========================================================================
79 */
80
Ian Rogers959f8ed2012-02-07 16:33:37 -080081static bool IsSirtLocalRef(JNIEnv* env, jobject localRef) {
82 return GetIndirectRefKind(localRef) == kSirtOrInvalid &&
TDYa12728f1a142012-03-15 21:51:52 -070083 reinterpret_cast<JNIEnvExt*>(env)->self->StackReferencesContain(localRef);
Ian Rogers959f8ed2012-02-07 16:33:37 -080084}
85
Elliott Hughesa2501992011-08-26 19:39:54 -070086template<typename T>
87T Decode(ScopedJniThreadState& ts, jobject obj) {
88 return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj));
89}
90
Elliott Hughes3f6635a2012-06-19 13:37:49 -070091// Hack to allow forcecopy to work with jniGetNonMovableArrayElements.
92// The code deliberately uses an invalid sequence of operations, so we
93// need to pass it through unmodified. Review that code before making
94// any changes here.
Elliott Hughesa2501992011-08-26 19:39:54 -070095#define kNoCopyMagic 0xd5aab57f
96
Elliott Hughes3f6635a2012-06-19 13:37:49 -070097// Flags passed into ScopedCheck.
Elliott Hughesa2501992011-08-26 19:39:54 -070098#define kFlag_Default 0x0000
99
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700100#define kFlag_CritBad 0x0000 // Calling while in critical is not allowed.
101#define kFlag_CritOkay 0x0001 // Calling while in critical is allowed.
102#define kFlag_CritGet 0x0002 // This is a critical "get".
103#define kFlag_CritRelease 0x0003 // This is a critical "release".
104#define kFlag_CritMask 0x0003 // Bit mask to get "crit" value.
Elliott Hughesa2501992011-08-26 19:39:54 -0700105
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700106#define kFlag_ExcepBad 0x0000 // Raised exceptions are not allowed.
107#define kFlag_ExcepOkay 0x0004 // Raised exceptions are allowed.
Elliott Hughesa2501992011-08-26 19:39:54 -0700108
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700109#define kFlag_Release 0x0010 // Are we in a non-critical release function?
110#define kFlag_NullableUtf 0x0020 // Are our UTF parameters nullable?
Elliott Hughesa2501992011-08-26 19:39:54 -0700111
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700112#define kFlag_Invocation 0x8000 // Part of the invocation interface (JavaVM*).
Elliott Hughesa2501992011-08-26 19:39:54 -0700113
Elliott Hughes485cac42011-12-09 17:49:35 -0800114#define kFlag_ForceTrace 0x80000000 // Add this to a JNI function's flags if you want to trace every call.
115
Elliott Hughesa0957642011-09-02 14:27:33 -0700116static const char* gBuiltInPrefixes[] = {
117 "Landroid/",
118 "Lcom/android/",
119 "Lcom/google/android/",
120 "Ldalvik/",
121 "Ljava/",
122 "Ljavax/",
123 "Llibcore/",
124 "Lorg/apache/harmony/",
125 NULL
126};
127
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700128static bool ShouldTrace(JavaVMExt* vm, const Method* method) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700129 // If both "-Xcheck:jni" and "-Xjnitrace:" are enabled, we print trace messages
130 // when a native method that matches the -Xjnitrace argument calls a JNI function
131 // such as NewByteArray.
132 // If -verbose:third-party-jni is on, we want to log any JNI function calls
133 // made by a third-party native method.
Elliott Hughes81ff3182012-03-23 20:35:56 -0700134 std::string class_name(MethodHelper(method).GetDeclaringClassDescriptor());
135 if (!vm->trace.empty() && class_name.find(vm->trace) != std::string::npos) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700136 return true;
137 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800138 if (VLOG_IS_ON(third_party_jni)) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700139 // Return true if we're trying to log all third-party JNI activity and 'method' doesn't look
140 // like part of Android.
Elliott Hughesa0957642011-09-02 14:27:33 -0700141 for (size_t i = 0; gBuiltInPrefixes[i] != NULL; ++i) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700142 if (StartsWith(class_name, gBuiltInPrefixes[i])) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700143 return false;
144 }
145 }
146 return true;
147 }
148 return false;
149}
150
Elliott Hughesa2501992011-08-26 19:39:54 -0700151class ScopedCheck {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800152 public:
Elliott Hughesa2501992011-08-26 19:39:54 -0700153 // For JNIEnv* functions.
154 explicit ScopedCheck(JNIEnv* env, int flags, const char* functionName) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700155 Init(env, reinterpret_cast<JNIEnvExt*>(env)->vm, flags, functionName, true);
156 CheckThread(flags);
Elliott Hughesa2501992011-08-26 19:39:54 -0700157 }
158
159 // For JavaVM* functions.
Elliott Hughes81ff3182012-03-23 20:35:56 -0700160 explicit ScopedCheck(JavaVM* vm, bool has_method, const char* functionName) {
161 Init(NULL, vm, kFlag_Invocation, functionName, has_method);
Elliott Hughesa2501992011-08-26 19:39:54 -0700162 }
163
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700164 bool ForceCopy() {
Elliott Hughesa2501992011-08-26 19:39:54 -0700165 return Runtime::Current()->GetJavaVM()->force_copy;
166 }
167
Elliott Hughes81ff3182012-03-23 20:35:56 -0700168 // Checks that 'class_name' is a valid "fully-qualified" JNI class name, like "java/lang/Thread"
169 // or "[Ljava/lang/Object;". A ClassLoader can actually normalize class names a couple of
170 // times, so using "java.lang.Thread" instead of "java/lang/Thread" might work in some
171 // circumstances, but this is incorrect.
172 void CheckClassName(const char* class_name) {
173 if (!IsValidJniClassName(class_name)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700174 JniAbortF(function_name_,
175 "illegal class name '%s'\n"
176 " (should be of the form 'package/Class', [Lpackage/Class;' or '[[B')",
177 class_name);
Elliott Hughesa2501992011-08-26 19:39:54 -0700178 }
179 }
180
181 /*
182 * Verify that the field is of the appropriate type. If the field has an
183 * object type, "java_object" is the object we're trying to assign into it.
184 *
185 * Works for both static and instance fields.
186 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700187 void CheckFieldType(jobject java_object, jfieldID fid, char prim, bool isStatic) {
188 ScopedJniThreadState ts(env_);
189 Field* f = CheckFieldID(fid);
190 if (f == NULL) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700191 return;
192 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800193 Class* field_type = FieldHelper(f).GetType();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700194 if (!field_type->IsPrimitive()) {
195 if (java_object != NULL) {
196 Object* obj = Decode<Object*>(ts, java_object);
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700197 // 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.
Elliott Hughes88c5c352012-03-15 18:49:48 -0700200 if (!Runtime::Current()->GetHeap()->IsHeapAddress(obj)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700201 JniAbortF(function_name_, "field operation on invalid %s: %p",
202 ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700203 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700204 } else {
Brian Carlstrom16192862011-09-12 17:50:06 -0700205 if (!obj->InstanceOf(field_type)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700206 JniAbortF(function_name_, "attempt to set field %s with value of wrong type: %s",
207 PrettyField(f).c_str(), PrettyTypeOf(obj).c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700208 return;
209 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700210 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700211 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700212 } else if (field_type != Runtime::Current()->GetClassLinker()->FindPrimitiveClass(prim)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700213 JniAbortF(function_name_, "attempt to set field %s with value of wrong type: %c",
214 PrettyField(f).c_str(), prim);
Elliott Hughesa2501992011-08-26 19:39:54 -0700215 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700216 }
217
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700218 if (isStatic != f->IsStatic()) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700219 if (isStatic) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700220 JniAbortF(function_name_, "accessing non-static field %s as static", PrettyField(f).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700221 } else {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700222 JniAbortF(function_name_, "accessing static field %s as non-static", PrettyField(f).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700223 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700224 return;
225 }
226 }
227
228 /*
229 * Verify that this instance field ID is valid for this object.
230 *
231 * Assumes "jobj" has already been validated.
232 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700233 void CheckInstanceFieldID(jobject java_object, jfieldID fid) {
234 ScopedJniThreadState ts(env_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700235
236 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800237 if (o == NULL || !Runtime::Current()->GetHeap()->IsHeapAddress(o)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700238 JniAbortF(function_name_, "field operation on invalid %s: %p",
239 ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700240 return;
241 }
242
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700243 Field* f = CheckFieldID(fid);
244 if (f == NULL) {
245 return;
246 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700247 Class* c = o->GetClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800248 FieldHelper fh(f);
249 if (c->FindInstanceField(fh.GetName(), fh.GetTypeDescriptor()) == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700250 JniAbortF(function_name_, "jfieldID %s not valid for an object of class %s",
251 PrettyField(f).c_str(), PrettyTypeOf(o).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700252 }
253 }
254
255 /*
256 * Verify that the pointer value is non-NULL.
257 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700258 void CheckNonNull(const void* ptr) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700259 if (ptr == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700260 JniAbortF(function_name_, "non-nullable argument was NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -0700261 }
262 }
263
264 /*
265 * Verify that the method's return type matches the type of call.
266 * 'expectedType' will be "L" for all objects, including arrays.
267 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700268 void CheckSig(jmethodID mid, const char* expectedType, bool isStatic) {
269 ScopedJniThreadState ts(env_);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800270 Method* m = CheckMethodID(mid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700271 if (m == NULL) {
272 return;
273 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800274 if (*expectedType != MethodHelper(m).GetShorty()[0]) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700275 JniAbortF(function_name_, "the return type of %s does not match %s",
276 function_name_, PrettyMethod(m).c_str());
277 }
278 if (isStatic != m->IsStatic()) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700279 if (isStatic) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700280 JniAbortF(function_name_, "calling non-static method %s with %s",
281 PrettyMethod(m).c_str(), function_name_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700282 } else {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700283 JniAbortF(function_name_, "calling static method %s with %s",
284 PrettyMethod(m).c_str(), function_name_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700285 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700286 }
287 }
288
289 /*
290 * Verify that this static field ID is valid for this class.
291 *
292 * Assumes "java_class" has already been validated.
293 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700294 void CheckStaticFieldID(jclass java_class, jfieldID fid) {
295 ScopedJniThreadState ts(env_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700296 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700297 const Field* f = CheckFieldID(fid);
298 if (f == NULL) {
299 return;
300 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700301 if (f->GetDeclaringClass() != c) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700302 JniAbortF(function_name_, "static jfieldID %p not valid for class %s",
303 fid, PrettyClass(c).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700304 }
305 }
306
307 /*
Elliott Hughese84278b2012-03-22 10:06:53 -0700308 * Verify that "mid" is appropriate for "java_class".
Elliott Hughesa2501992011-08-26 19:39:54 -0700309 *
310 * A mismatch isn't dangerous, because the jmethodID defines the class. In
Elliott Hughese84278b2012-03-22 10:06:53 -0700311 * fact, java_class is unused in the implementation. It's best if we don't
Elliott Hughesa2501992011-08-26 19:39:54 -0700312 * allow bad code in the system though.
313 *
Elliott Hughese84278b2012-03-22 10:06:53 -0700314 * Instances of "java_class" must be instances of the method's declaring class.
Elliott Hughesa2501992011-08-26 19:39:54 -0700315 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700316 void CheckStaticMethod(jclass java_class, jmethodID mid) {
317 ScopedJniThreadState ts(env_);
318 const Method* m = CheckMethodID(mid);
319 if (m == NULL) {
320 return;
321 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700322 Class* c = Decode<Class*>(ts, java_class);
Elliott Hughesa2501992011-08-26 19:39:54 -0700323 if (!c->IsAssignableFrom(m->GetDeclaringClass())) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700324 JniAbortF(function_name_, "can't call static %s on class %s",
325 PrettyMethod(m).c_str(), PrettyClass(c).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700326 }
327 }
328
329 /*
330 * Verify that "mid" is appropriate for "jobj".
331 *
332 * Make sure the object is an instance of the method's declaring class.
333 * (Note the mid might point to a declaration in an interface; this
334 * will be handled automatically by the instanceof check.)
335 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700336 void CheckVirtualMethod(jobject java_object, jmethodID mid) {
337 ScopedJniThreadState ts(env_);
338 const Method* m = CheckMethodID(mid);
339 if (m == NULL) {
340 return;
341 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700342 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700343 if (!o->InstanceOf(m->GetDeclaringClass())) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700344 JniAbortF(function_name_, "can't call %s on instance of %s",
345 PrettyMethod(m).c_str(), PrettyTypeOf(o).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700346 }
347 }
348
349 /**
350 * The format string is a sequence of the following characters,
351 * and must be followed by arguments of the corresponding types
352 * in the same order.
353 *
354 * Java primitive types:
355 * B - jbyte
356 * C - jchar
357 * D - jdouble
358 * F - jfloat
359 * I - jint
360 * J - jlong
361 * S - jshort
362 * Z - jboolean (shown as true and false)
363 * V - void
364 *
365 * Java reference types:
366 * L - jobject
367 * a - jarray
368 * c - jclass
369 * s - jstring
370 *
371 * JNI types:
372 * b - jboolean (shown as JNI_TRUE and JNI_FALSE)
373 * f - jfieldID
374 * m - jmethodID
375 * p - void*
376 * r - jint (for release mode arguments)
Elliott Hughes78090d12011-10-07 14:31:47 -0700377 * u - const char* (Modified UTF-8)
Elliott Hughesa2501992011-08-26 19:39:54 -0700378 * z - jsize (for lengths; use i if negative values are okay)
379 * v - JavaVM*
380 * E - JNIEnv*
381 * . - no argument; just print "..." (used for varargs JNI calls)
382 *
383 * Use the kFlag_NullableUtf flag where 'u' field(s) are nullable.
384 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700385 void Check(bool entry, const char* fmt0, ...) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700386 va_list ap;
387
Elliott Hughesa0957642011-09-02 14:27:33 -0700388 const Method* traceMethod = NULL;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800389 if ((!vm_->trace.empty() || VLOG_IS_ON(third_party_jni)) && has_method_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700390 // We need to guard some of the invocation interface's calls: a bad caller might
391 // use DetachCurrentThread or GetEnv on a thread that's not yet attached.
Elliott Hughesa0957642011-09-02 14:27:33 -0700392 Thread* self = Thread::Current();
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700393 if ((flags_ & kFlag_Invocation) == 0 || self != NULL) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700394 traceMethod = self->GetCurrentMethod();
Elliott Hughesa2501992011-08-26 19:39:54 -0700395 }
396 }
Elliott Hughesa0957642011-09-02 14:27:33 -0700397
Elliott Hughes485cac42011-12-09 17:49:35 -0800398 if (((flags_ & kFlag_ForceTrace) != 0) || (traceMethod != NULL && ShouldTrace(vm_, traceMethod))) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700399 va_start(ap, fmt0);
400 std::string msg;
401 for (const char* fmt = fmt0; *fmt;) {
402 char ch = *fmt++;
403 if (ch == 'B') { // jbyte
404 jbyte b = va_arg(ap, int);
405 if (b >= 0 && b < 10) {
406 StringAppendF(&msg, "%d", b);
407 } else {
408 StringAppendF(&msg, "%#x (%d)", b, b);
409 }
410 } else if (ch == 'C') { // jchar
411 jchar c = va_arg(ap, int);
412 if (c < 0x7f && c >= ' ') {
413 StringAppendF(&msg, "U+%x ('%c')", c, c);
414 } else {
415 StringAppendF(&msg, "U+%x", c);
416 }
417 } else if (ch == 'F' || ch == 'D') { // jfloat, jdouble
418 StringAppendF(&msg, "%g", va_arg(ap, double));
419 } else if (ch == 'I' || ch == 'S') { // jint, jshort
420 StringAppendF(&msg, "%d", va_arg(ap, int));
421 } else if (ch == 'J') { // jlong
422 StringAppendF(&msg, "%lld", va_arg(ap, jlong));
423 } else if (ch == 'Z') { // jboolean
424 StringAppendF(&msg, "%s", va_arg(ap, int) ? "true" : "false");
425 } else if (ch == 'V') { // void
426 msg += "void";
427 } else if (ch == 'v') { // JavaVM*
428 JavaVM* vm = va_arg(ap, JavaVM*);
429 StringAppendF(&msg, "(JavaVM*)%p", vm);
430 } else if (ch == 'E') { // JNIEnv*
431 JNIEnv* env = va_arg(ap, JNIEnv*);
432 StringAppendF(&msg, "(JNIEnv*)%p", env);
433 } else if (ch == 'L' || ch == 'a' || ch == 's') { // jobject, jarray, jstring
434 // For logging purposes, these are identical.
435 jobject o = va_arg(ap, jobject);
436 if (o == NULL) {
437 msg += "NULL";
438 } else {
439 StringAppendF(&msg, "%p", o);
440 }
441 } else if (ch == 'b') { // jboolean (JNI-style)
442 jboolean b = va_arg(ap, int);
443 msg += (b ? "JNI_TRUE" : "JNI_FALSE");
444 } else if (ch == 'c') { // jclass
445 jclass jc = va_arg(ap, jclass);
446 Class* c = reinterpret_cast<Class*>(Thread::Current()->DecodeJObject(jc));
447 if (c == NULL) {
448 msg += "NULL";
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800449 } else if (c == kInvalidIndirectRefObject || !Runtime::Current()->GetHeap()->IsHeapAddress(c)) {
Elliott Hughes485cac42011-12-09 17:49:35 -0800450 StringAppendF(&msg, "INVALID POINTER:%p", jc);
451 } else if (!c->IsClass()) {
452 msg += "INVALID NON-CLASS OBJECT OF TYPE:" + PrettyTypeOf(c);
Elliott Hughesa2501992011-08-26 19:39:54 -0700453 } else {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700454 msg += PrettyClass(c);
Elliott Hughesa2501992011-08-26 19:39:54 -0700455 if (!entry) {
456 StringAppendF(&msg, " (%p)", jc);
457 }
458 }
459 } else if (ch == 'f') { // jfieldID
460 jfieldID fid = va_arg(ap, jfieldID);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700461 Field* f = reinterpret_cast<Field*>(fid);
Elliott Hughesa2501992011-08-26 19:39:54 -0700462 msg += PrettyField(f);
463 if (!entry) {
464 StringAppendF(&msg, " (%p)", fid);
465 }
466 } else if (ch == 'z') { // non-negative jsize
467 // You might expect jsize to be size_t, but it's not; it's the same as jint.
468 // We only treat this specially so we can do the non-negative check.
469 // TODO: maybe this wasn't worth it?
470 jint i = va_arg(ap, jint);
471 StringAppendF(&msg, "%d", i);
472 } else if (ch == 'm') { // jmethodID
473 jmethodID mid = va_arg(ap, jmethodID);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700474 Method* m = reinterpret_cast<Method*>(mid);
Elliott Hughesa2501992011-08-26 19:39:54 -0700475 msg += PrettyMethod(m);
476 if (!entry) {
477 StringAppendF(&msg, " (%p)", mid);
478 }
479 } else if (ch == 'p') { // void* ("pointer")
480 void* p = va_arg(ap, void*);
481 if (p == NULL) {
482 msg += "NULL";
483 } else {
484 StringAppendF(&msg, "(void*) %p", p);
485 }
486 } else if (ch == 'r') { // jint (release mode)
487 jint releaseMode = va_arg(ap, jint);
488 if (releaseMode == 0) {
489 msg += "0";
490 } else if (releaseMode == JNI_ABORT) {
491 msg += "JNI_ABORT";
492 } else if (releaseMode == JNI_COMMIT) {
493 msg += "JNI_COMMIT";
494 } else {
495 StringAppendF(&msg, "invalid release mode %d", releaseMode);
496 }
Elliott Hughes78090d12011-10-07 14:31:47 -0700497 } else if (ch == 'u') { // const char* (Modified UTF-8)
Elliott Hughesa2501992011-08-26 19:39:54 -0700498 const char* utf = va_arg(ap, const char*);
499 if (utf == NULL) {
500 msg += "NULL";
501 } else {
502 StringAppendF(&msg, "\"%s\"", utf);
503 }
504 } else if (ch == '.') {
505 msg += "...";
506 } else {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700507 JniAbortF(function_name_, "unknown trace format specifier: %c", ch);
Elliott Hughesa2501992011-08-26 19:39:54 -0700508 return;
509 }
510 if (*fmt) {
511 StringAppendF(&msg, ", ");
512 }
513 }
514 va_end(ap);
515
Elliott Hughes485cac42011-12-09 17:49:35 -0800516 if ((flags_ & kFlag_ForceTrace) != 0) {
517 LOG(INFO) << "JNI: call to " << function_name_ << "(" << msg << ")";
518 } else if (entry) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700519 if (has_method_) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700520 std::string methodName(PrettyMethod(traceMethod, false));
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700521 LOG(INFO) << "JNI: " << methodName << " -> " << function_name_ << "(" << msg << ")";
522 indent_ = methodName.size() + 1;
Elliott Hughesa2501992011-08-26 19:39:54 -0700523 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700524 LOG(INFO) << "JNI: -> " << function_name_ << "(" << msg << ")";
525 indent_ = 0;
Elliott Hughesa2501992011-08-26 19:39:54 -0700526 }
527 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700528 LOG(INFO) << StringPrintf("JNI: %*s<- %s returned %s", indent_, "", function_name_, msg.c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700529 }
530 }
531
532 // We always do the thorough checks on entry, and never on exit...
533 if (entry) {
534 va_start(ap, fmt0);
535 for (const char* fmt = fmt0; *fmt; ++fmt) {
536 char ch = *fmt;
537 if (ch == 'a') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700538 CheckArray(va_arg(ap, jarray));
Elliott Hughesa2501992011-08-26 19:39:54 -0700539 } else if (ch == 'c') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700540 CheckInstance(kClass, va_arg(ap, jclass));
Elliott Hughesa2501992011-08-26 19:39:54 -0700541 } else if (ch == 'L') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700542 CheckObject(va_arg(ap, jobject));
Elliott Hughesa2501992011-08-26 19:39:54 -0700543 } else if (ch == 'r') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700544 CheckReleaseMode(va_arg(ap, jint));
Elliott Hughesa2501992011-08-26 19:39:54 -0700545 } else if (ch == 's') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700546 CheckInstance(kString, va_arg(ap, jstring));
Elliott Hughesa2501992011-08-26 19:39:54 -0700547 } else if (ch == 'u') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700548 if ((flags_ & kFlag_Release) != 0) {
549 CheckNonNull(va_arg(ap, const char*));
Elliott Hughesa2501992011-08-26 19:39:54 -0700550 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700551 bool nullable = ((flags_ & kFlag_NullableUtf) != 0);
552 CheckUtfString(va_arg(ap, const char*), nullable);
Elliott Hughesa2501992011-08-26 19:39:54 -0700553 }
554 } else if (ch == 'z') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700555 CheckLengthPositive(va_arg(ap, jsize));
Elliott Hughesa2501992011-08-26 19:39:54 -0700556 } else if (strchr("BCISZbfmpEv", ch) != NULL) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800557 va_arg(ap, uint32_t); // Skip this argument.
Elliott Hughesa2501992011-08-26 19:39:54 -0700558 } else if (ch == 'D' || ch == 'F') {
559 va_arg(ap, double); // Skip this argument.
560 } else if (ch == 'J') {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800561 va_arg(ap, uint64_t); // Skip this argument.
Elliott Hughesa2501992011-08-26 19:39:54 -0700562 } else if (ch == '.') {
563 } else {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800564 LOG(FATAL) << "Unknown check format specifier: " << ch;
Elliott Hughesa2501992011-08-26 19:39:54 -0700565 }
566 }
567 va_end(ap);
568 }
569 }
570
Elliott Hughesa92853e2012-02-07 16:09:27 -0800571 enum InstanceKind {
572 kClass,
Elliott Hughes0f3c5532012-03-30 14:51:51 -0700573 kDirectByteBuffer,
574 kObject,
575 kString,
576 kThrowable,
Elliott Hughesa92853e2012-02-07 16:09:27 -0800577 };
578
579 /*
580 * Verify that "jobj" is a valid non-NULL object reference, and points to
581 * an instance of expectedClass.
582 *
583 * Because we're looking at an object on the GC heap, we have to switch
584 * to "running" mode before doing the checks.
585 */
586 bool CheckInstance(InstanceKind kind, jobject java_object) {
587 const char* what = NULL;
588 switch (kind) {
589 case kClass:
590 what = "jclass";
591 break;
592 case kDirectByteBuffer:
593 what = "direct ByteBuffer";
594 break;
595 case kObject:
596 what = "jobject";
597 break;
598 case kString:
599 what = "jstring";
600 break;
601 case kThrowable:
602 what = "jthrowable";
603 break;
604 default:
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700605 LOG(FATAL) << "Unknown kind " << static_cast<int>(kind);
Elliott Hughesa92853e2012-02-07 16:09:27 -0800606 }
607
608 if (java_object == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700609 JniAbortF(function_name_, "%s received null %s", function_name_, what);
Elliott Hughesa92853e2012-02-07 16:09:27 -0800610 return false;
611 }
612
613 ScopedJniThreadState ts(env_);
614 Object* obj = Decode<Object*>(ts, java_object);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800615 if (!Runtime::Current()->GetHeap()->IsHeapAddress(obj)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700616 JniAbortF(function_name_, "%s is an invalid %s: %p (%p)",
617 what, ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object, obj);
Elliott Hughesa92853e2012-02-07 16:09:27 -0800618 return false;
619 }
620
621 bool okay = true;
622 switch (kind) {
623 case kClass:
624 okay = obj->IsClass();
625 break;
626 case kDirectByteBuffer:
627 UNIMPLEMENTED(FATAL);
628 break;
629 case kString:
630 okay = obj->GetClass()->IsStringClass();
631 break;
632 case kThrowable:
633 okay = obj->GetClass()->IsThrowableClass();
634 break;
635 case kObject:
636 break;
637 }
638 if (!okay) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700639 JniAbortF(function_name_, "%s has wrong type: %s", what, PrettyTypeOf(obj).c_str());
Elliott Hughesa92853e2012-02-07 16:09:27 -0800640 return false;
641 }
642
643 return true;
644 }
645
Elliott Hughesba8eee12012-01-24 20:25:24 -0800646 private:
Elliott Hughes81ff3182012-03-23 20:35:56 -0700647 // Set "has_method" to true if we have a valid thread with a method pointer.
648 // We won't have one before attaching a thread, after detaching a thread, or
649 // when shutting down the runtime.
650 void Init(JNIEnv* env, JavaVM* vm, int flags, const char* functionName, bool has_method) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700651 env_ = reinterpret_cast<JNIEnvExt*>(env);
652 vm_ = reinterpret_cast<JavaVMExt*>(vm);
653 flags_ = flags;
654 function_name_ = functionName;
Elliott Hughes81ff3182012-03-23 20:35:56 -0700655 has_method_ = has_method;
Elliott Hughesa2501992011-08-26 19:39:54 -0700656 }
657
658 /*
659 * Verify that "array" is non-NULL and points to an Array object.
660 *
661 * Since we're dealing with objects, switch to "running" mode.
662 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700663 void CheckArray(jarray java_array) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700664 if (java_array == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700665 JniAbortF(function_name_, "jarray was NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -0700666 return;
667 }
668
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700669 ScopedJniThreadState ts(env_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700670 Array* a = Decode<Array*>(ts, java_array);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800671 if (!Runtime::Current()->GetHeap()->IsHeapAddress(a)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700672 JniAbortF(function_name_, "jarray is an invalid %s: %p (%p)",
673 ToStr<IndirectRefKind>(GetIndirectRefKind(java_array)).c_str(), java_array, a);
Elliott Hughesa2501992011-08-26 19:39:54 -0700674 } else if (!a->IsArrayInstance()) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700675 JniAbortF(function_name_, "jarray argument has non-array type: %s", PrettyTypeOf(a).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700676 }
677 }
678
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700679 void CheckLengthPositive(jsize length) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700680 if (length < 0) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700681 JniAbortF(function_name_, "negative jsize: %d", length);
Elliott Hughesa2501992011-08-26 19:39:54 -0700682 }
683 }
684
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700685 Field* CheckFieldID(jfieldID fid) {
686 if (fid == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700687 JniAbortF(function_name_, "jfieldID was NULL");
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700688 return NULL;
689 }
690 Field* f = DecodeField(fid);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800691 if (!Runtime::Current()->GetHeap()->IsHeapAddress(f)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700692 JniAbortF(function_name_, "invalid jfieldID: %p", fid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700693 return NULL;
694 }
695 return f;
696 }
697
698 Method* CheckMethodID(jmethodID mid) {
699 if (mid == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700700 JniAbortF(function_name_, "jmethodID was NULL");
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700701 return NULL;
702 }
703 Method* m = DecodeMethod(mid);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800704 if (!Runtime::Current()->GetHeap()->IsHeapAddress(m)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700705 JniAbortF(function_name_, "invalid jmethodID: %p", mid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700706 return NULL;
707 }
708 return m;
709 }
710
Elliott Hughesa2501992011-08-26 19:39:54 -0700711 /*
712 * Verify that "jobj" is a valid object, and that it's an object that JNI
713 * is allowed to know about. We allow NULL references.
714 *
715 * Switches to "running" mode before performing checks.
716 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700717 void CheckObject(jobject java_object) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700718 if (java_object == NULL) {
719 return;
720 }
721
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700722 ScopedJniThreadState ts(env_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700723
724 Object* o = Decode<Object*>(ts, java_object);
Elliott Hughes88c5c352012-03-15 18:49:48 -0700725 if (!Runtime::Current()->GetHeap()->IsHeapAddress(o)) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700726 // TODO: when we remove work_around_app_jni_bugs, this should be impossible.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700727 JniAbortF(function_name_, "native code passing in reference to invalid %s: %p",
728 ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700729 }
730 }
731
732 /*
733 * Verify that the "mode" argument passed to a primitive array Release
734 * function is one of the valid values.
735 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700736 void CheckReleaseMode(jint mode) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700737 if (mode != 0 && mode != JNI_COMMIT && mode != JNI_ABORT) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700738 JniAbortF(function_name_, "bad value for release mode: %d", mode);
Elliott Hughesa2501992011-08-26 19:39:54 -0700739 }
740 }
741
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700742 void CheckThread(int flags) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700743 Thread* self = Thread::Current();
744 if (self == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700745 JniAbortF(function_name_, "a thread (tid %d) is making JNI calls without being attached", GetTid());
Elliott Hughesa2501992011-08-26 19:39:54 -0700746 return;
747 }
748
749 // Get the *correct* JNIEnv by going through our TLS pointer.
750 JNIEnvExt* threadEnv = self->GetJniEnv();
751
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700752 // Verify that the current thread is (a) attached and (b) associated with
753 // this particular instance of JNIEnv.
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700754 if (env_ != threadEnv) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700755 if (vm_->work_around_app_jni_bugs) {
756 // If we're keeping broken code limping along, we need to suppress the abort...
757 LOG(ERROR) << "APP BUG DETECTED: thread " << *self << " using JNIEnv* from thread " << *env_->self;
758 } else {
759 JniAbortF(function_name_, "thread %s using JNIEnv* from thread %s",
760 ToStr<Thread>(*self).c_str(), ToStr<Thread>(*env_->self).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700761 return;
762 }
763 }
764
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700765 // Verify that, if this thread previously made a critical "get" call, we
766 // do the corresponding "release" call before we try anything else.
Elliott Hughesa2501992011-08-26 19:39:54 -0700767 switch (flags & kFlag_CritMask) {
768 case kFlag_CritOkay: // okay to call this method
769 break;
770 case kFlag_CritBad: // not okay to call
771 if (threadEnv->critical) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700772 JniAbortF(function_name_, "thread %s using JNI after critical get", ToStr<Thread>(*self).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700773 return;
774 }
775 break;
776 case kFlag_CritGet: // this is a "get" call
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700777 // Don't check here; we allow nested gets.
Elliott Hughesa2501992011-08-26 19:39:54 -0700778 threadEnv->critical++;
779 break;
780 case kFlag_CritRelease: // this is a "release" call
781 threadEnv->critical--;
782 if (threadEnv->critical < 0) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700783 JniAbortF(function_name_, "thread %s called too many critical releases", ToStr<Thread>(*self).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700784 return;
785 }
786 break;
787 default:
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800788 LOG(FATAL) << "Bad flags (internal error): " << flags;
Elliott Hughesa2501992011-08-26 19:39:54 -0700789 }
790
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700791 // Verify that, if an exception has been raised, the native code doesn't
792 // make any JNI calls other than the Exception* methods.
Elliott Hughesa2501992011-08-26 19:39:54 -0700793 if ((flags & kFlag_ExcepOkay) == 0 && self->IsExceptionPending()) {
Elliott Hughes30646832011-10-13 16:59:46 -0700794 std::string type(PrettyTypeOf(self->GetException()));
Elliott Hughes30646832011-10-13 16:59:46 -0700795 // TODO: write native code that doesn't require allocation for dumping an exception.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700796 // TODO: do we care any more? art always dumps pending exceptions on aborting threads.
Elliott Hughes30646832011-10-13 16:59:46 -0700797 if (type != "java.lang.OutOfMemoryError") {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700798 JniAbortF(function_name_, "JNI %s called with pending exception: %s",
799 function_name_, type.c_str(), jniGetStackTrace(env_).c_str());
800 } else {
801 JniAbortF(function_name_, "JNI %s called with %s pending", function_name_, type.c_str());
Elliott Hughes30646832011-10-13 16:59:46 -0700802 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700803 return;
804 }
805 }
806
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700807 // Verifies that "bytes" points to valid Modified UTF-8 data.
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700808 void CheckUtfString(const char* bytes, bool nullable) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700809 if (bytes == NULL) {
810 if (!nullable) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700811 JniAbortF(function_name_, "non-nullable const char* was NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -0700812 return;
813 }
814 return;
815 }
816
817 const char* errorKind = NULL;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700818 uint8_t utf8 = CheckUtfBytes(bytes, &errorKind);
Elliott Hughesa2501992011-08-26 19:39:54 -0700819 if (errorKind != NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700820 JniAbortF(function_name_,
821 "input is not valid Modified UTF-8: illegal %s byte %#x\n"
822 " string: '%s'", errorKind, utf8, bytes);
Elliott Hughesa2501992011-08-26 19:39:54 -0700823 return;
824 }
825 }
826
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700827 static uint8_t CheckUtfBytes(const char* bytes, const char** errorKind) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700828 while (*bytes != '\0') {
829 uint8_t utf8 = *(bytes++);
830 // Switch on the high four bits.
831 switch (utf8 >> 4) {
832 case 0x00:
833 case 0x01:
834 case 0x02:
835 case 0x03:
836 case 0x04:
837 case 0x05:
838 case 0x06:
839 case 0x07:
840 // Bit pattern 0xxx. No need for any extra bytes.
841 break;
842 case 0x08:
843 case 0x09:
844 case 0x0a:
845 case 0x0b:
846 case 0x0f:
847 /*
848 * Bit pattern 10xx or 1111, which are illegal start bytes.
849 * Note: 1111 is valid for normal UTF-8, but not the
Elliott Hughes78090d12011-10-07 14:31:47 -0700850 * Modified UTF-8 used here.
Elliott Hughesa2501992011-08-26 19:39:54 -0700851 */
852 *errorKind = "start";
853 return utf8;
854 case 0x0e:
855 // Bit pattern 1110, so there are two additional bytes.
856 utf8 = *(bytes++);
857 if ((utf8 & 0xc0) != 0x80) {
858 *errorKind = "continuation";
859 return utf8;
860 }
861 // Fall through to take care of the final byte.
862 case 0x0c:
863 case 0x0d:
864 // Bit pattern 110x, so there is one additional byte.
865 utf8 = *(bytes++);
866 if ((utf8 & 0xc0) != 0x80) {
867 *errorKind = "continuation";
868 return utf8;
869 }
870 break;
871 }
872 }
873 return 0;
874 }
875
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700876 JNIEnvExt* env_;
877 JavaVMExt* vm_;
878 const char* function_name_;
879 int flags_;
880 bool has_method_;
Elliott Hughes92cb4982011-12-16 16:57:28 -0800881 int indent_;
Elliott Hughesa2501992011-08-26 19:39:54 -0700882
883 DISALLOW_COPY_AND_ASSIGN(ScopedCheck);
884};
885
886#define CHECK_JNI_ENTRY(flags, types, args...) \
887 ScopedCheck sc(env, flags, __FUNCTION__); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700888 sc.Check(true, types, ##args)
Elliott Hughesa2501992011-08-26 19:39:54 -0700889
890#define CHECK_JNI_EXIT(type, exp) ({ \
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700891 typeof(exp) _rc = (exp); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700892 sc.Check(false, type, _rc); \
Elliott Hughesa2501992011-08-26 19:39:54 -0700893 _rc; })
894#define CHECK_JNI_EXIT_VOID() \
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700895 sc.Check(false, "V")
Elliott Hughesa2501992011-08-26 19:39:54 -0700896
897/*
898 * ===========================================================================
899 * Guarded arrays
900 * ===========================================================================
901 */
902
903#define kGuardLen 512 /* must be multiple of 2 */
904#define kGuardPattern 0xd5e3 /* uncommon values; d5e3d5e3 invalid addr */
905#define kGuardMagic 0xffd5aa96
906
907/* this gets tucked in at the start of the buffer; struct size must be even */
908struct GuardedCopy {
909 uint32_t magic;
910 uLong adler;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700911 size_t original_length;
912 const void* original_ptr;
Elliott Hughesa2501992011-08-26 19:39:54 -0700913
914 /* find the GuardedCopy given the pointer into the "live" data */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700915 static inline const GuardedCopy* FromData(const void* dataBuf) {
916 return reinterpret_cast<const GuardedCopy*>(ActualBuffer(dataBuf));
Elliott Hughesa2501992011-08-26 19:39:54 -0700917 }
918
919 /*
920 * Create an over-sized buffer to hold the contents of "buf". Copy it in,
921 * filling in the area around it with guard data.
922 *
923 * We use a 16-bit pattern to make a rogue memset less likely to elude us.
924 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700925 static void* Create(const void* buf, size_t len, bool modOkay) {
926 size_t newLen = ActualLength(len);
927 uint8_t* newBuf = DebugAlloc(newLen);
Elliott Hughesa2501992011-08-26 19:39:54 -0700928
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700929 // Fill it in with a pattern.
Elliott Hughesba8eee12012-01-24 20:25:24 -0800930 uint16_t* pat = reinterpret_cast<uint16_t*>(newBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -0700931 for (size_t i = 0; i < newLen / 2; i++) {
932 *pat++ = kGuardPattern;
933 }
934
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700935 // Copy the data in; note "len" could be zero.
Elliott Hughesa2501992011-08-26 19:39:54 -0700936 memcpy(newBuf + kGuardLen / 2, buf, len);
937
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700938 // If modification is not expected, grab a checksum.
Elliott Hughesa2501992011-08-26 19:39:54 -0700939 uLong adler = 0;
940 if (!modOkay) {
941 adler = adler32(0L, Z_NULL, 0);
Elliott Hughesba8eee12012-01-24 20:25:24 -0800942 adler = adler32(adler, reinterpret_cast<const Bytef*>(buf), len);
943 *reinterpret_cast<uLong*>(newBuf) = adler;
Elliott Hughesa2501992011-08-26 19:39:54 -0700944 }
945
946 GuardedCopy* pExtra = reinterpret_cast<GuardedCopy*>(newBuf);
947 pExtra->magic = kGuardMagic;
948 pExtra->adler = adler;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700949 pExtra->original_ptr = buf;
950 pExtra->original_length = len;
Elliott Hughesa2501992011-08-26 19:39:54 -0700951
952 return newBuf + kGuardLen / 2;
953 }
954
955 /*
956 * Free up the guard buffer, scrub it, and return the original pointer.
957 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700958 static void* Destroy(void* dataBuf) {
959 const GuardedCopy* pExtra = GuardedCopy::FromData(dataBuf);
Elliott Hughesba8eee12012-01-24 20:25:24 -0800960 void* original_ptr = const_cast<void*>(pExtra->original_ptr);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700961 size_t len = pExtra->original_length;
962 DebugFree(dataBuf, len);
963 return original_ptr;
Elliott Hughesa2501992011-08-26 19:39:54 -0700964 }
965
966 /*
967 * Verify the guard area and, if "modOkay" is false, that the data itself
968 * has not been altered.
969 *
970 * The caller has already checked that "dataBuf" is non-NULL.
971 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700972 static void Check(const char* functionName, const void* dataBuf, bool modOkay) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700973 static const uint32_t kMagicCmp = kGuardMagic;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700974 const uint8_t* fullBuf = ActualBuffer(dataBuf);
975 const GuardedCopy* pExtra = GuardedCopy::FromData(dataBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -0700976
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700977 // Before we do anything with "pExtra", check the magic number. We
978 // do the check with memcmp rather than "==" in case the pointer is
979 // unaligned. If it points to completely bogus memory we're going
980 // to crash, but there's no easy way around that.
Elliott Hughesa2501992011-08-26 19:39:54 -0700981 if (memcmp(&pExtra->magic, &kMagicCmp, 4) != 0) {
982 uint8_t buf[4];
983 memcpy(buf, &pExtra->magic, 4);
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700984 JniAbortF(functionName,
985 "guard magic does not match (found 0x%02x%02x%02x%02x) -- incorrect data pointer %p?",
986 buf[3], buf[2], buf[1], buf[0], dataBuf); // Assumes little-endian.
Elliott Hughesa2501992011-08-26 19:39:54 -0700987 }
988
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700989 size_t len = pExtra->original_length;
Elliott Hughesa2501992011-08-26 19:39:54 -0700990
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700991 // Check bottom half of guard; skip over optional checksum storage.
Elliott Hughesba8eee12012-01-24 20:25:24 -0800992 const uint16_t* pat = reinterpret_cast<const uint16_t*>(fullBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -0700993 for (size_t i = sizeof(GuardedCopy) / 2; i < (kGuardLen / 2 - sizeof(GuardedCopy)) / 2; i++) {
994 if (pat[i] != kGuardPattern) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700995 JniAbortF(functionName, "guard pattern(1) disturbed at %p +%d", fullBuf, i*2);
Elliott Hughesa2501992011-08-26 19:39:54 -0700996 }
997 }
998
999 int offset = kGuardLen / 2 + len;
1000 if (offset & 0x01) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001001 // Odd byte; expected value depends on endian.
Elliott Hughesa2501992011-08-26 19:39:54 -07001002 const uint16_t patSample = kGuardPattern;
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001003 uint8_t expected_byte = reinterpret_cast<const uint8_t*>(&patSample)[1];
1004 if (fullBuf[offset] != expected_byte) {
1005 JniAbortF(functionName, "guard pattern disturbed in odd byte after %p +%d 0x%02x 0x%02x",
1006 fullBuf, offset, fullBuf[offset], expected_byte);
Elliott Hughesa2501992011-08-26 19:39:54 -07001007 }
1008 offset++;
1009 }
1010
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001011 // Check top half of guard.
Elliott Hughesba8eee12012-01-24 20:25:24 -08001012 pat = reinterpret_cast<const uint16_t*>(fullBuf + offset);
Elliott Hughesa2501992011-08-26 19:39:54 -07001013 for (size_t i = 0; i < kGuardLen / 4; i++) {
1014 if (pat[i] != kGuardPattern) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001015 JniAbortF(functionName, "guard pattern(2) disturbed at %p +%d", fullBuf, offset + i*2);
Elliott Hughesa2501992011-08-26 19:39:54 -07001016 }
1017 }
1018
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001019 // If modification is not expected, verify checksum. Strictly speaking
1020 // this is wrong: if we told the client that we made a copy, there's no
1021 // reason they can't alter the buffer.
Elliott Hughesa2501992011-08-26 19:39:54 -07001022 if (!modOkay) {
1023 uLong adler = adler32(0L, Z_NULL, 0);
1024 adler = adler32(adler, (const Bytef*)dataBuf, len);
1025 if (pExtra->adler != adler) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001026 JniAbortF(functionName, "buffer modified (0x%08lx vs 0x%08lx) at address %p",
1027 pExtra->adler, adler, dataBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -07001028 }
1029 }
1030 }
1031
1032 private:
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001033 static uint8_t* DebugAlloc(size_t len) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001034 void* result = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
1035 if (result == MAP_FAILED) {
1036 PLOG(FATAL) << "GuardedCopy::create mmap(" << len << ") failed";
1037 }
1038 return reinterpret_cast<uint8_t*>(result);
1039 }
1040
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001041 static void DebugFree(void* dataBuf, size_t len) {
1042 uint8_t* fullBuf = ActualBuffer(dataBuf);
1043 size_t totalByteCount = ActualLength(len);
Elliott Hughesa2501992011-08-26 19:39:54 -07001044 // TODO: we could mprotect instead, and keep the allocation around for a while.
1045 // This would be even more expensive, but it might catch more errors.
1046 // if (mprotect(fullBuf, totalByteCount, PROT_NONE) != 0) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -07001047 // PLOG(WARNING) << "mprotect(PROT_NONE) failed";
Elliott Hughesa2501992011-08-26 19:39:54 -07001048 // }
1049 if (munmap(fullBuf, totalByteCount) != 0) {
Elliott Hughesba8eee12012-01-24 20:25:24 -08001050 PLOG(FATAL) << "munmap(" << reinterpret_cast<void*>(fullBuf) << ", " << totalByteCount << ") failed";
Elliott Hughesa2501992011-08-26 19:39:54 -07001051 }
1052 }
1053
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001054 static const uint8_t* ActualBuffer(const void* dataBuf) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001055 return reinterpret_cast<const uint8_t*>(dataBuf) - kGuardLen / 2;
1056 }
1057
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001058 static uint8_t* ActualBuffer(void* dataBuf) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001059 return reinterpret_cast<uint8_t*>(dataBuf) - kGuardLen / 2;
1060 }
1061
1062 // Underlying length of a user allocation of 'length' bytes.
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001063 static size_t ActualLength(size_t length) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001064 return (length + kGuardLen + 1) & ~0x01;
1065 }
1066};
1067
1068/*
1069 * Create a guarded copy of a primitive array. Modifications to the copied
1070 * data are allowed. Returns a pointer to the copied data.
1071 */
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001072static void* CreateGuardedPACopy(JNIEnv* env, const jarray java_array, jboolean* isCopy) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001073 ScopedJniThreadState ts(env);
1074
1075 Array* a = Decode<Array*>(ts, java_array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001076 size_t component_size = a->GetClass()->GetComponentSize();
1077 size_t byte_count = a->GetLength() * component_size;
1078 void* result = GuardedCopy::Create(a->GetRawData(component_size), byte_count, true);
Elliott Hughesa2501992011-08-26 19:39:54 -07001079 if (isCopy != NULL) {
1080 *isCopy = JNI_TRUE;
1081 }
1082 return result;
1083}
1084
1085/*
1086 * Perform the array "release" operation, which may or may not copy data
Elliott Hughes81ff3182012-03-23 20:35:56 -07001087 * back into the managed heap, and may or may not release the underlying storage.
Elliott Hughesa2501992011-08-26 19:39:54 -07001088 */
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001089static void ReleaseGuardedPACopy(JNIEnv* env, jarray java_array, void* dataBuf, int mode) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001090 if (reinterpret_cast<uintptr_t>(dataBuf) == kNoCopyMagic) {
1091 return;
1092 }
1093
1094 ScopedJniThreadState ts(env);
1095 Array* a = Decode<Array*>(ts, java_array);
1096
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001097 GuardedCopy::Check(__FUNCTION__, dataBuf, true);
Elliott Hughesa2501992011-08-26 19:39:54 -07001098
1099 if (mode != JNI_ABORT) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001100 size_t len = GuardedCopy::FromData(dataBuf)->original_length;
Ian Rogersa15e67d2012-02-28 13:51:55 -08001101 memcpy(a->GetRawData(a->GetClass()->GetComponentSize()), dataBuf, len);
Elliott Hughesa2501992011-08-26 19:39:54 -07001102 }
1103 if (mode != JNI_COMMIT) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001104 GuardedCopy::Destroy(dataBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -07001105 }
1106}
1107
1108/*
1109 * ===========================================================================
1110 * JNI functions
1111 * ===========================================================================
1112 */
1113
1114class CheckJNI {
1115 public:
1116 static jint GetVersion(JNIEnv* env) {
1117 CHECK_JNI_ENTRY(kFlag_Default, "E", env);
1118 return CHECK_JNI_EXIT("I", baseEnv(env)->GetVersion(env));
1119 }
1120
1121 static jclass DefineClass(JNIEnv* env, const char* name, jobject loader, const jbyte* buf, jsize bufLen) {
1122 CHECK_JNI_ENTRY(kFlag_Default, "EuLpz", env, name, loader, buf, bufLen);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001123 sc.CheckClassName(name);
Elliott Hughesa2501992011-08-26 19:39:54 -07001124 return CHECK_JNI_EXIT("c", baseEnv(env)->DefineClass(env, name, loader, buf, bufLen));
1125 }
1126
1127 static jclass FindClass(JNIEnv* env, const char* name) {
1128 CHECK_JNI_ENTRY(kFlag_Default, "Eu", env, name);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001129 sc.CheckClassName(name);
Elliott Hughesa2501992011-08-26 19:39:54 -07001130 return CHECK_JNI_EXIT("c", baseEnv(env)->FindClass(env, name));
1131 }
1132
Elliott Hughese84278b2012-03-22 10:06:53 -07001133 static jclass GetSuperclass(JNIEnv* env, jclass c) {
1134 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1135 return CHECK_JNI_EXIT("c", baseEnv(env)->GetSuperclass(env, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001136 }
1137
Elliott Hughese84278b2012-03-22 10:06:53 -07001138 static jboolean IsAssignableFrom(JNIEnv* env, jclass c1, jclass c2) {
1139 CHECK_JNI_ENTRY(kFlag_Default, "Ecc", env, c1, c2);
1140 return CHECK_JNI_EXIT("b", baseEnv(env)->IsAssignableFrom(env, c1, c2));
Elliott Hughesa2501992011-08-26 19:39:54 -07001141 }
1142
1143 static jmethodID FromReflectedMethod(JNIEnv* env, jobject method) {
1144 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, method);
1145 // TODO: check that 'field' is a java.lang.reflect.Method.
1146 return CHECK_JNI_EXIT("m", baseEnv(env)->FromReflectedMethod(env, method));
1147 }
1148
1149 static jfieldID FromReflectedField(JNIEnv* env, jobject field) {
1150 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, field);
1151 // TODO: check that 'field' is a java.lang.reflect.Field.
1152 return CHECK_JNI_EXIT("f", baseEnv(env)->FromReflectedField(env, field));
1153 }
1154
1155 static jobject ToReflectedMethod(JNIEnv* env, jclass cls, jmethodID mid, jboolean isStatic) {
1156 CHECK_JNI_ENTRY(kFlag_Default, "Ecmb", env, cls, mid, isStatic);
1157 return CHECK_JNI_EXIT("L", baseEnv(env)->ToReflectedMethod(env, cls, mid, isStatic));
1158 }
1159
1160 static jobject ToReflectedField(JNIEnv* env, jclass cls, jfieldID fid, jboolean isStatic) {
1161 CHECK_JNI_ENTRY(kFlag_Default, "Ecfb", env, cls, fid, isStatic);
1162 return CHECK_JNI_EXIT("L", baseEnv(env)->ToReflectedField(env, cls, fid, isStatic));
1163 }
1164
1165 static jint Throw(JNIEnv* env, jthrowable obj) {
1166 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1167 // TODO: check that 'obj' is a java.lang.Throwable.
1168 return CHECK_JNI_EXIT("I", baseEnv(env)->Throw(env, obj));
1169 }
1170
Elliott Hughese84278b2012-03-22 10:06:53 -07001171 static jint ThrowNew(JNIEnv* env, jclass c, const char* message) {
1172 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Ecu", env, c, message);
1173 return CHECK_JNI_EXIT("I", baseEnv(env)->ThrowNew(env, c, message));
Elliott Hughesa2501992011-08-26 19:39:54 -07001174 }
1175
1176 static jthrowable ExceptionOccurred(JNIEnv* env) {
1177 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1178 return CHECK_JNI_EXIT("L", baseEnv(env)->ExceptionOccurred(env));
1179 }
1180
1181 static void ExceptionDescribe(JNIEnv* env) {
1182 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1183 baseEnv(env)->ExceptionDescribe(env);
1184 CHECK_JNI_EXIT_VOID();
1185 }
1186
1187 static void ExceptionClear(JNIEnv* env) {
1188 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1189 baseEnv(env)->ExceptionClear(env);
1190 CHECK_JNI_EXIT_VOID();
1191 }
1192
1193 static void FatalError(JNIEnv* env, const char* msg) {
1194 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Eu", env, msg);
1195 baseEnv(env)->FatalError(env, msg);
1196 CHECK_JNI_EXIT_VOID();
1197 }
1198
1199 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
1200 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EI", env, capacity);
1201 return CHECK_JNI_EXIT("I", baseEnv(env)->PushLocalFrame(env, capacity));
1202 }
1203
1204 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
1205 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, res);
1206 return CHECK_JNI_EXIT("L", baseEnv(env)->PopLocalFrame(env, res));
1207 }
1208
1209 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
1210 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1211 return CHECK_JNI_EXIT("L", baseEnv(env)->NewGlobalRef(env, obj));
1212 }
1213
1214 static jobject NewLocalRef(JNIEnv* env, jobject ref) {
1215 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, ref);
1216 return CHECK_JNI_EXIT("L", baseEnv(env)->NewLocalRef(env, ref));
1217 }
1218
1219 static void DeleteGlobalRef(JNIEnv* env, jobject globalRef) {
1220 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, globalRef);
1221 if (globalRef != NULL && GetIndirectRefKind(globalRef) != kGlobal) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001222 JniAbortF(__FUNCTION__, "DeleteGlobalRef on %s: %p",
1223 ToStr<IndirectRefKind>(GetIndirectRefKind(globalRef)).c_str(), globalRef);
Elliott Hughesa2501992011-08-26 19:39:54 -07001224 } else {
1225 baseEnv(env)->DeleteGlobalRef(env, globalRef);
1226 CHECK_JNI_EXIT_VOID();
1227 }
1228 }
1229
1230 static void DeleteWeakGlobalRef(JNIEnv* env, jweak weakGlobalRef) {
1231 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, weakGlobalRef);
1232 if (weakGlobalRef != NULL && GetIndirectRefKind(weakGlobalRef) != kWeakGlobal) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001233 JniAbortF(__FUNCTION__, "DeleteWeakGlobalRef on %s: %p",
1234 ToStr<IndirectRefKind>(GetIndirectRefKind(weakGlobalRef)).c_str(), weakGlobalRef);
Elliott Hughesa2501992011-08-26 19:39:54 -07001235 } else {
1236 baseEnv(env)->DeleteWeakGlobalRef(env, weakGlobalRef);
1237 CHECK_JNI_EXIT_VOID();
1238 }
1239 }
1240
1241 static void DeleteLocalRef(JNIEnv* env, jobject localRef) {
1242 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, localRef);
Ian Rogers959f8ed2012-02-07 16:33:37 -08001243 if (localRef != NULL && GetIndirectRefKind(localRef) != kLocal && !IsSirtLocalRef(env, localRef)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001244 JniAbortF(__FUNCTION__, "DeleteLocalRef on %s: %p",
1245 ToStr<IndirectRefKind>(GetIndirectRefKind(localRef)).c_str(), localRef);
Elliott Hughesa2501992011-08-26 19:39:54 -07001246 } else {
1247 baseEnv(env)->DeleteLocalRef(env, localRef);
1248 CHECK_JNI_EXIT_VOID();
1249 }
1250 }
1251
1252 static jint EnsureLocalCapacity(JNIEnv *env, jint capacity) {
1253 CHECK_JNI_ENTRY(kFlag_Default, "EI", env, capacity);
1254 return CHECK_JNI_EXIT("I", baseEnv(env)->EnsureLocalCapacity(env, capacity));
1255 }
1256
1257 static jboolean IsSameObject(JNIEnv* env, jobject ref1, jobject ref2) {
1258 CHECK_JNI_ENTRY(kFlag_Default, "ELL", env, ref1, ref2);
1259 return CHECK_JNI_EXIT("b", baseEnv(env)->IsSameObject(env, ref1, ref2));
1260 }
1261
Elliott Hughese84278b2012-03-22 10:06:53 -07001262 static jobject AllocObject(JNIEnv* env, jclass c) {
1263 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1264 return CHECK_JNI_EXIT("L", baseEnv(env)->AllocObject(env, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001265 }
1266
Elliott Hughese84278b2012-03-22 10:06:53 -07001267 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
1268 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
Elliott Hughesa2501992011-08-26 19:39:54 -07001269 va_list args;
1270 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -07001271 jobject result = baseEnv(env)->NewObjectV(env, c, mid, args);
Elliott Hughesa2501992011-08-26 19:39:54 -07001272 va_end(args);
1273 return CHECK_JNI_EXIT("L", result);
1274 }
1275
Elliott Hughese84278b2012-03-22 10:06:53 -07001276 static jobject NewObjectV(JNIEnv* env, jclass c, jmethodID mid, va_list args) {
1277 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
1278 return CHECK_JNI_EXIT("L", baseEnv(env)->NewObjectV(env, c, mid, args));
Elliott Hughesa2501992011-08-26 19:39:54 -07001279 }
1280
Elliott Hughese84278b2012-03-22 10:06:53 -07001281 static jobject NewObjectA(JNIEnv* env, jclass c, jmethodID mid, jvalue* args) {
1282 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
1283 return CHECK_JNI_EXIT("L", baseEnv(env)->NewObjectA(env, c, mid, args));
Elliott Hughesa2501992011-08-26 19:39:54 -07001284 }
1285
1286 static jclass GetObjectClass(JNIEnv* env, jobject obj) {
1287 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1288 return CHECK_JNI_EXIT("c", baseEnv(env)->GetObjectClass(env, obj));
1289 }
1290
Elliott Hughese84278b2012-03-22 10:06:53 -07001291 static jboolean IsInstanceOf(JNIEnv* env, jobject obj, jclass c) {
1292 CHECK_JNI_ENTRY(kFlag_Default, "ELc", env, obj, c);
1293 return CHECK_JNI_EXIT("b", baseEnv(env)->IsInstanceOf(env, obj, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001294 }
1295
Elliott Hughese84278b2012-03-22 10:06:53 -07001296 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1297 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1298 return CHECK_JNI_EXIT("m", baseEnv(env)->GetMethodID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001299 }
1300
Elliott Hughese84278b2012-03-22 10:06:53 -07001301 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1302 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1303 return CHECK_JNI_EXIT("f", baseEnv(env)->GetFieldID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001304 }
1305
Elliott Hughese84278b2012-03-22 10:06:53 -07001306 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1307 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1308 return CHECK_JNI_EXIT("m", baseEnv(env)->GetStaticMethodID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001309 }
1310
Elliott Hughese84278b2012-03-22 10:06:53 -07001311 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1312 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1313 return CHECK_JNI_EXIT("f", baseEnv(env)->GetStaticFieldID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001314 }
1315
1316#define FIELD_ACCESSORS(_ctype, _jname, _type) \
Elliott Hughese84278b2012-03-22 10:06:53 -07001317 static _ctype GetStatic##_jname##Field(JNIEnv* env, jclass c, jfieldID fid) { \
1318 CHECK_JNI_ENTRY(kFlag_Default, "Ecf", env, c, fid); \
1319 sc.CheckStaticFieldID(c, fid); \
1320 return CHECK_JNI_EXIT(_type, baseEnv(env)->GetStatic##_jname##Field(env, c, fid)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001321 } \
1322 static _ctype Get##_jname##Field(JNIEnv* env, jobject obj, jfieldID fid) { \
1323 CHECK_JNI_ENTRY(kFlag_Default, "ELf", env, obj, fid); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001324 sc.CheckInstanceFieldID(obj, fid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001325 return CHECK_JNI_EXIT(_type, baseEnv(env)->Get##_jname##Field(env, obj, fid)); \
1326 } \
Elliott Hughese84278b2012-03-22 10:06:53 -07001327 static void SetStatic##_jname##Field(JNIEnv* env, jclass c, jfieldID fid, _ctype value) { \
1328 CHECK_JNI_ENTRY(kFlag_Default, "Ecf" _type, env, c, fid, value); \
1329 sc.CheckStaticFieldID(c, fid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001330 /* "value" arg only used when type == ref */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001331 sc.CheckFieldType((jobject)(uint32_t)value, fid, _type[0], true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001332 baseEnv(env)->SetStatic##_jname##Field(env, c, fid, value); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001333 CHECK_JNI_EXIT_VOID(); \
1334 } \
1335 static void Set##_jname##Field(JNIEnv* env, jobject obj, jfieldID fid, _ctype value) { \
1336 CHECK_JNI_ENTRY(kFlag_Default, "ELf" _type, env, obj, fid, value); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001337 sc.CheckInstanceFieldID(obj, fid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001338 /* "value" arg only used when type == ref */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001339 sc.CheckFieldType((jobject)(uint32_t) value, fid, _type[0], false); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001340 baseEnv(env)->Set##_jname##Field(env, obj, fid, value); \
1341 CHECK_JNI_EXIT_VOID(); \
1342 }
1343
1344FIELD_ACCESSORS(jobject, Object, "L");
1345FIELD_ACCESSORS(jboolean, Boolean, "Z");
1346FIELD_ACCESSORS(jbyte, Byte, "B");
1347FIELD_ACCESSORS(jchar, Char, "C");
1348FIELD_ACCESSORS(jshort, Short, "S");
1349FIELD_ACCESSORS(jint, Int, "I");
1350FIELD_ACCESSORS(jlong, Long, "J");
1351FIELD_ACCESSORS(jfloat, Float, "F");
1352FIELD_ACCESSORS(jdouble, Double, "D");
1353
1354#define CALL(_ctype, _jname, _retdecl, _retasgn, _retok, _retsig) \
1355 /* Virtual... */ \
1356 static _ctype Call##_jname##Method(JNIEnv* env, jobject obj, \
1357 jmethodID mid, ...) \
1358 { \
1359 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001360 sc.CheckSig(mid, _retsig, false); \
1361 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001362 _retdecl; \
1363 va_list args; \
1364 va_start(args, mid); \
Elliott Hughesba8eee12012-01-24 20:25:24 -08001365 _retasgn(baseEnv(env)->Call##_jname##MethodV(env, obj, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001366 va_end(args); \
1367 _retok; \
1368 } \
1369 static _ctype Call##_jname##MethodV(JNIEnv* env, jobject obj, \
1370 jmethodID mid, va_list args) \
1371 { \
1372 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001373 sc.CheckSig(mid, _retsig, false); \
1374 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001375 _retdecl; \
Elliott Hughesba8eee12012-01-24 20:25:24 -08001376 _retasgn(baseEnv(env)->Call##_jname##MethodV(env, obj, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001377 _retok; \
1378 } \
1379 static _ctype Call##_jname##MethodA(JNIEnv* env, jobject obj, \
1380 jmethodID mid, jvalue* args) \
1381 { \
1382 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001383 sc.CheckSig(mid, _retsig, false); \
1384 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001385 _retdecl; \
Elliott Hughesba8eee12012-01-24 20:25:24 -08001386 _retasgn(baseEnv(env)->Call##_jname##MethodA(env, obj, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001387 _retok; \
1388 } \
1389 /* Non-virtual... */ \
1390 static _ctype CallNonvirtual##_jname##Method(JNIEnv* env, \
Elliott Hughese84278b2012-03-22 10:06:53 -07001391 jobject obj, jclass c, jmethodID mid, ...) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001392 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001393 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001394 sc.CheckSig(mid, _retsig, false); \
1395 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001396 _retdecl; \
1397 va_list args; \
1398 va_start(args, mid); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001399 _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodV(env, obj, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001400 va_end(args); \
1401 _retok; \
1402 } \
1403 static _ctype CallNonvirtual##_jname##MethodV(JNIEnv* env, \
Elliott Hughese84278b2012-03-22 10:06:53 -07001404 jobject obj, jclass c, jmethodID mid, va_list args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001405 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001406 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001407 sc.CheckSig(mid, _retsig, false); \
1408 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001409 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001410 _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodV(env, obj, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001411 _retok; \
1412 } \
1413 static _ctype CallNonvirtual##_jname##MethodA(JNIEnv* env, \
Elliott Hughese84278b2012-03-22 10:06:53 -07001414 jobject obj, jclass c, jmethodID mid, jvalue* args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001415 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001416 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001417 sc.CheckSig(mid, _retsig, false); \
1418 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001419 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001420 _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodA(env, obj, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001421 _retok; \
1422 } \
1423 /* Static... */ \
Elliott Hughese84278b2012-03-22 10:06:53 -07001424 static _ctype CallStatic##_jname##Method(JNIEnv* env, jclass c, jmethodID mid, ...) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001425 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001426 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001427 sc.CheckSig(mid, _retsig, true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001428 sc.CheckStaticMethod(c, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001429 _retdecl; \
1430 va_list args; \
1431 va_start(args, mid); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001432 _retasgn(baseEnv(env)->CallStatic##_jname##MethodV(env, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001433 va_end(args); \
1434 _retok; \
1435 } \
Elliott Hughese84278b2012-03-22 10:06:53 -07001436 static _ctype CallStatic##_jname##MethodV(JNIEnv* env, jclass c, jmethodID mid, va_list args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001437 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001438 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001439 sc.CheckSig(mid, _retsig, true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001440 sc.CheckStaticMethod(c, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001441 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001442 _retasgn(baseEnv(env)->CallStatic##_jname##MethodV(env, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001443 _retok; \
1444 } \
Elliott Hughese84278b2012-03-22 10:06:53 -07001445 static _ctype CallStatic##_jname##MethodA(JNIEnv* env, jclass c, jmethodID mid, jvalue* args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001446 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001447 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001448 sc.CheckSig(mid, _retsig, true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001449 sc.CheckStaticMethod(c, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001450 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001451 _retasgn(baseEnv(env)->CallStatic##_jname##MethodA(env, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001452 _retok; \
1453 }
1454
1455#define NON_VOID_RETURN(_retsig, _ctype) return CHECK_JNI_EXIT(_retsig, (_ctype) result)
1456#define VOID_RETURN CHECK_JNI_EXIT_VOID()
1457
Elliott Hughesba8eee12012-01-24 20:25:24 -08001458CALL(jobject, Object, Object* result, result = reinterpret_cast<Object*>, NON_VOID_RETURN("L", jobject), "L");
1459CALL(jboolean, Boolean, jboolean result, result =, NON_VOID_RETURN("Z", jboolean), "Z");
1460CALL(jbyte, Byte, jbyte result, result =, NON_VOID_RETURN("B", jbyte), "B");
1461CALL(jchar, Char, jchar result, result =, NON_VOID_RETURN("C", jchar), "C");
1462CALL(jshort, Short, jshort result, result =, NON_VOID_RETURN("S", jshort), "S");
1463CALL(jint, Int, jint result, result =, NON_VOID_RETURN("I", jint), "I");
1464CALL(jlong, Long, jlong result, result =, NON_VOID_RETURN("J", jlong), "J");
1465CALL(jfloat, Float, jfloat result, result =, NON_VOID_RETURN("F", jfloat), "F");
1466CALL(jdouble, Double, jdouble result, result =, NON_VOID_RETURN("D", jdouble), "D");
Elliott Hughesa2501992011-08-26 19:39:54 -07001467CALL(void, Void, , , VOID_RETURN, "V");
1468
1469 static jstring NewString(JNIEnv* env, const jchar* unicodeChars, jsize len) {
1470 CHECK_JNI_ENTRY(kFlag_Default, "Epz", env, unicodeChars, len);
1471 return CHECK_JNI_EXIT("s", baseEnv(env)->NewString(env, unicodeChars, len));
1472 }
1473
1474 static jsize GetStringLength(JNIEnv* env, jstring string) {
1475 CHECK_JNI_ENTRY(kFlag_CritOkay, "Es", env, string);
1476 return CHECK_JNI_EXIT("I", baseEnv(env)->GetStringLength(env, string));
1477 }
1478
1479 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* isCopy) {
1480 CHECK_JNI_ENTRY(kFlag_CritOkay, "Esp", env, java_string, isCopy);
1481 const jchar* result = baseEnv(env)->GetStringChars(env, java_string, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001482 if (sc.ForceCopy() && result != NULL) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001483 ScopedJniThreadState ts(env);
1484 String* s = Decode<String*>(ts, java_string);
1485 int byteCount = s->GetLength() * 2;
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001486 result = (const jchar*) GuardedCopy::Create(result, byteCount, false);
Elliott Hughesa2501992011-08-26 19:39:54 -07001487 if (isCopy != NULL) {
1488 *isCopy = JNI_TRUE;
1489 }
1490 }
1491 return CHECK_JNI_EXIT("p", result);
1492 }
1493
1494 static void ReleaseStringChars(JNIEnv* env, jstring string, const jchar* chars) {
1495 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "Esp", env, string, chars);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001496 sc.CheckNonNull(chars);
1497 if (sc.ForceCopy()) {
1498 GuardedCopy::Check(__FUNCTION__, chars, false);
Elliott Hughesba8eee12012-01-24 20:25:24 -08001499 chars = reinterpret_cast<const jchar*>(GuardedCopy::Destroy(const_cast<jchar*>(chars)));
Elliott Hughesa2501992011-08-26 19:39:54 -07001500 }
1501 baseEnv(env)->ReleaseStringChars(env, string, chars);
1502 CHECK_JNI_EXIT_VOID();
1503 }
1504
1505 static jstring NewStringUTF(JNIEnv* env, const char* bytes) {
1506 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Eu", env, bytes); // TODO: show pointer and truncate string.
1507 return CHECK_JNI_EXIT("s", baseEnv(env)->NewStringUTF(env, bytes));
1508 }
1509
1510 static jsize GetStringUTFLength(JNIEnv* env, jstring string) {
1511 CHECK_JNI_ENTRY(kFlag_CritOkay, "Es", env, string);
1512 return CHECK_JNI_EXIT("I", baseEnv(env)->GetStringUTFLength(env, string));
1513 }
1514
1515 static const char* GetStringUTFChars(JNIEnv* env, jstring string, jboolean* isCopy) {
1516 CHECK_JNI_ENTRY(kFlag_CritOkay, "Esp", env, string, isCopy);
1517 const char* result = baseEnv(env)->GetStringUTFChars(env, string, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001518 if (sc.ForceCopy() && result != NULL) {
1519 result = (const char*) GuardedCopy::Create(result, strlen(result) + 1, false);
Elliott Hughesa2501992011-08-26 19:39:54 -07001520 if (isCopy != NULL) {
1521 *isCopy = JNI_TRUE;
1522 }
1523 }
1524 return CHECK_JNI_EXIT("u", result); // TODO: show pointer and truncate string.
1525 }
1526
1527 static void ReleaseStringUTFChars(JNIEnv* env, jstring string, const char* utf) {
1528 CHECK_JNI_ENTRY(kFlag_ExcepOkay | kFlag_Release, "Esu", env, string, utf); // TODO: show pointer and truncate string.
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001529 if (sc.ForceCopy()) {
1530 GuardedCopy::Check(__FUNCTION__, utf, false);
Elliott Hughesba8eee12012-01-24 20:25:24 -08001531 utf = reinterpret_cast<const char*>(GuardedCopy::Destroy(const_cast<char*>(utf)));
Elliott Hughesa2501992011-08-26 19:39:54 -07001532 }
1533 baseEnv(env)->ReleaseStringUTFChars(env, string, utf);
1534 CHECK_JNI_EXIT_VOID();
1535 }
1536
1537 static jsize GetArrayLength(JNIEnv* env, jarray array) {
1538 CHECK_JNI_ENTRY(kFlag_CritOkay, "Ea", env, array);
1539 return CHECK_JNI_EXIT("I", baseEnv(env)->GetArrayLength(env, array));
1540 }
1541
1542 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass elementClass, jobject initialElement) {
1543 CHECK_JNI_ENTRY(kFlag_Default, "EzcL", env, length, elementClass, initialElement);
1544 return CHECK_JNI_EXIT("a", baseEnv(env)->NewObjectArray(env, length, elementClass, initialElement));
1545 }
1546
1547 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index) {
1548 CHECK_JNI_ENTRY(kFlag_Default, "EaI", env, array, index);
1549 return CHECK_JNI_EXIT("L", baseEnv(env)->GetObjectArrayElement(env, array, index));
1550 }
1551
1552 static void SetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index, jobject value) {
1553 CHECK_JNI_ENTRY(kFlag_Default, "EaIL", env, array, index, value);
1554 baseEnv(env)->SetObjectArrayElement(env, array, index, value);
1555 CHECK_JNI_EXIT_VOID();
1556 }
1557
1558#define NEW_PRIMITIVE_ARRAY(_artype, _jname) \
1559 static _artype New##_jname##Array(JNIEnv* env, jsize length) { \
1560 CHECK_JNI_ENTRY(kFlag_Default, "Ez", env, length); \
1561 return CHECK_JNI_EXIT("a", baseEnv(env)->New##_jname##Array(env, length)); \
1562 }
1563NEW_PRIMITIVE_ARRAY(jbooleanArray, Boolean);
1564NEW_PRIMITIVE_ARRAY(jbyteArray, Byte);
1565NEW_PRIMITIVE_ARRAY(jcharArray, Char);
1566NEW_PRIMITIVE_ARRAY(jshortArray, Short);
1567NEW_PRIMITIVE_ARRAY(jintArray, Int);
1568NEW_PRIMITIVE_ARRAY(jlongArray, Long);
1569NEW_PRIMITIVE_ARRAY(jfloatArray, Float);
1570NEW_PRIMITIVE_ARRAY(jdoubleArray, Double);
1571
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001572struct ForceCopyGetChecker {
Elliott Hughesba8eee12012-01-24 20:25:24 -08001573 public:
Elliott Hughesa2501992011-08-26 19:39:54 -07001574 ForceCopyGetChecker(ScopedCheck& sc, jboolean* isCopy) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001575 force_copy = sc.ForceCopy();
1576 no_copy = 0;
1577 if (force_copy && isCopy != NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001578 // Capture this before the base call tramples on it.
Elliott Hughesba8eee12012-01-24 20:25:24 -08001579 no_copy = *reinterpret_cast<uint32_t*>(isCopy);
Elliott Hughesa2501992011-08-26 19:39:54 -07001580 }
1581 }
1582
1583 template<typename ResultT>
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001584 ResultT Check(JNIEnv* env, jarray array, jboolean* isCopy, ResultT result) {
1585 if (force_copy && result != NULL) {
1586 if (no_copy != kNoCopyMagic) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001587 result = reinterpret_cast<ResultT>(CreateGuardedPACopy(env, array, isCopy));
1588 }
1589 }
1590 return result;
1591 }
1592
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001593 uint32_t no_copy;
1594 bool force_copy;
Elliott Hughesa2501992011-08-26 19:39:54 -07001595};
1596
1597#define GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \
1598 static _ctype* Get##_jname##ArrayElements(JNIEnv* env, _ctype##Array array, jboolean* isCopy) { \
1599 CHECK_JNI_ENTRY(kFlag_Default, "Eap", env, array, isCopy); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001600 _ctype* result = ForceCopyGetChecker(sc, isCopy).Check(env, array, isCopy, baseEnv(env)->Get##_jname##ArrayElements(env, array, isCopy)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001601 return CHECK_JNI_EXIT("p", result); \
1602 }
1603
1604#define RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \
1605 static void Release##_jname##ArrayElements(JNIEnv* env, _ctype##Array array, _ctype* elems, jint mode) { \
1606 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "Eapr", env, array, elems, mode); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001607 sc.CheckNonNull(elems); \
1608 if (sc.ForceCopy()) { \
Elliott Hughesa2501992011-08-26 19:39:54 -07001609 ReleaseGuardedPACopy(env, array, elems, mode); \
1610 } \
1611 baseEnv(env)->Release##_jname##ArrayElements(env, array, elems, mode); \
1612 CHECK_JNI_EXIT_VOID(); \
1613 }
1614
1615#define GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \
1616 static void Get##_jname##ArrayRegion(JNIEnv* env, _ctype##Array array, jsize start, jsize len, _ctype* buf) { \
1617 CHECK_JNI_ENTRY(kFlag_Default, "EaIIp", env, array, start, len, buf); \
1618 baseEnv(env)->Get##_jname##ArrayRegion(env, array, start, len, buf); \
1619 CHECK_JNI_EXIT_VOID(); \
1620 }
1621
1622#define SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \
1623 static void Set##_jname##ArrayRegion(JNIEnv* env, _ctype##Array array, jsize start, jsize len, const _ctype* buf) { \
1624 CHECK_JNI_ENTRY(kFlag_Default, "EaIIp", env, array, start, len, buf); \
1625 baseEnv(env)->Set##_jname##ArrayRegion(env, array, start, len, buf); \
1626 CHECK_JNI_EXIT_VOID(); \
1627 }
1628
1629#define PRIMITIVE_ARRAY_FUNCTIONS(_ctype, _jname, _typechar) \
1630 GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \
1631 RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \
1632 GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname); \
1633 SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname);
1634
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001635// TODO: verify primitive array type matches call type.
Elliott Hughesa2501992011-08-26 19:39:54 -07001636PRIMITIVE_ARRAY_FUNCTIONS(jboolean, Boolean, 'Z');
1637PRIMITIVE_ARRAY_FUNCTIONS(jbyte, Byte, 'B');
1638PRIMITIVE_ARRAY_FUNCTIONS(jchar, Char, 'C');
1639PRIMITIVE_ARRAY_FUNCTIONS(jshort, Short, 'S');
1640PRIMITIVE_ARRAY_FUNCTIONS(jint, Int, 'I');
1641PRIMITIVE_ARRAY_FUNCTIONS(jlong, Long, 'J');
1642PRIMITIVE_ARRAY_FUNCTIONS(jfloat, Float, 'F');
1643PRIMITIVE_ARRAY_FUNCTIONS(jdouble, Double, 'D');
1644
Elliott Hughese84278b2012-03-22 10:06:53 -07001645 static jint RegisterNatives(JNIEnv* env, jclass c, const JNINativeMethod* methods, jint nMethods) {
1646 CHECK_JNI_ENTRY(kFlag_Default, "EcpI", env, c, methods, nMethods);
1647 return CHECK_JNI_EXIT("I", baseEnv(env)->RegisterNatives(env, c, methods, nMethods));
Elliott Hughesa2501992011-08-26 19:39:54 -07001648 }
1649
Elliott Hughese84278b2012-03-22 10:06:53 -07001650 static jint UnregisterNatives(JNIEnv* env, jclass c) {
1651 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1652 return CHECK_JNI_EXIT("I", baseEnv(env)->UnregisterNatives(env, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001653 }
1654
1655 static jint MonitorEnter(JNIEnv* env, jobject obj) {
1656 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
Elliott Hughesa92853e2012-02-07 16:09:27 -08001657 if (!sc.CheckInstance(ScopedCheck::kObject, obj)) {
1658 return JNI_ERR; // Only for jni_internal_test. Real code will have aborted already.
1659 }
Elliott Hughesa2501992011-08-26 19:39:54 -07001660 return CHECK_JNI_EXIT("I", baseEnv(env)->MonitorEnter(env, obj));
1661 }
1662
1663 static jint MonitorExit(JNIEnv* env, jobject obj) {
1664 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, obj);
Elliott Hughesa92853e2012-02-07 16:09:27 -08001665 if (!sc.CheckInstance(ScopedCheck::kObject, obj)) {
1666 return JNI_ERR; // Only for jni_internal_test. Real code will have aborted already.
1667 }
Elliott Hughesa2501992011-08-26 19:39:54 -07001668 return CHECK_JNI_EXIT("I", baseEnv(env)->MonitorExit(env, obj));
1669 }
1670
1671 static jint GetJavaVM(JNIEnv *env, JavaVM **vm) {
1672 CHECK_JNI_ENTRY(kFlag_Default, "Ep", env, vm);
1673 return CHECK_JNI_EXIT("I", baseEnv(env)->GetJavaVM(env, vm));
1674 }
1675
1676 static void GetStringRegion(JNIEnv* env, jstring str, jsize start, jsize len, jchar* buf) {
1677 CHECK_JNI_ENTRY(kFlag_CritOkay, "EsIIp", env, str, start, len, buf);
1678 baseEnv(env)->GetStringRegion(env, str, start, len, buf);
1679 CHECK_JNI_EXIT_VOID();
1680 }
1681
1682 static void GetStringUTFRegion(JNIEnv* env, jstring str, jsize start, jsize len, char* buf) {
1683 CHECK_JNI_ENTRY(kFlag_CritOkay, "EsIIp", env, str, start, len, buf);
1684 baseEnv(env)->GetStringUTFRegion(env, str, start, len, buf);
1685 CHECK_JNI_EXIT_VOID();
1686 }
1687
1688 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* isCopy) {
1689 CHECK_JNI_ENTRY(kFlag_CritGet, "Eap", env, array, isCopy);
1690 void* result = baseEnv(env)->GetPrimitiveArrayCritical(env, array, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001691 if (sc.ForceCopy() && result != NULL) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001692 result = CreateGuardedPACopy(env, array, isCopy);
1693 }
1694 return CHECK_JNI_EXIT("p", result);
1695 }
1696
1697 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* carray, jint mode) {
1698 CHECK_JNI_ENTRY(kFlag_CritRelease | kFlag_ExcepOkay, "Eapr", env, array, carray, mode);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001699 sc.CheckNonNull(carray);
1700 if (sc.ForceCopy()) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001701 ReleaseGuardedPACopy(env, array, carray, mode);
1702 }
1703 baseEnv(env)->ReleasePrimitiveArrayCritical(env, array, carray, mode);
1704 CHECK_JNI_EXIT_VOID();
1705 }
1706
1707 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* isCopy) {
1708 CHECK_JNI_ENTRY(kFlag_CritGet, "Esp", env, java_string, isCopy);
1709 const jchar* result = baseEnv(env)->GetStringCritical(env, java_string, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001710 if (sc.ForceCopy() && result != NULL) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001711 ScopedJniThreadState ts(env);
1712 String* s = Decode<String*>(ts, java_string);
1713 int byteCount = s->GetLength() * 2;
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001714 result = (const jchar*) GuardedCopy::Create(result, byteCount, false);
Elliott Hughesa2501992011-08-26 19:39:54 -07001715 if (isCopy != NULL) {
1716 *isCopy = JNI_TRUE;
1717 }
1718 }
1719 return CHECK_JNI_EXIT("p", result);
1720 }
1721
1722 static void ReleaseStringCritical(JNIEnv* env, jstring string, const jchar* carray) {
1723 CHECK_JNI_ENTRY(kFlag_CritRelease | kFlag_ExcepOkay, "Esp", env, string, carray);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001724 sc.CheckNonNull(carray);
1725 if (sc.ForceCopy()) {
1726 GuardedCopy::Check(__FUNCTION__, carray, false);
Elliott Hughesba8eee12012-01-24 20:25:24 -08001727 carray = reinterpret_cast<const jchar*>(GuardedCopy::Destroy(const_cast<jchar*>(carray)));
Elliott Hughesa2501992011-08-26 19:39:54 -07001728 }
1729 baseEnv(env)->ReleaseStringCritical(env, string, carray);
1730 CHECK_JNI_EXIT_VOID();
1731 }
1732
1733 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
1734 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1735 return CHECK_JNI_EXIT("L", baseEnv(env)->NewWeakGlobalRef(env, obj));
1736 }
1737
1738 static jboolean ExceptionCheck(JNIEnv* env) {
1739 CHECK_JNI_ENTRY(kFlag_CritOkay | kFlag_ExcepOkay, "E", env);
1740 return CHECK_JNI_EXIT("b", baseEnv(env)->ExceptionCheck(env));
1741 }
1742
1743 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject obj) {
1744 // Note: we use "Ep" rather than "EL" because this is the one JNI function
1745 // that it's okay to pass an invalid reference to.
1746 CHECK_JNI_ENTRY(kFlag_Default, "Ep", env, obj);
1747 // TODO: proper decoding of jobjectRefType!
1748 return CHECK_JNI_EXIT("I", baseEnv(env)->GetObjectRefType(env, obj));
1749 }
1750
1751 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
1752 CHECK_JNI_ENTRY(kFlag_Default, "EpJ", env, address, capacity);
1753 if (address == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001754 JniAbortF(__FUNCTION__, "non-nullable address is NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -07001755 }
1756 if (capacity <= 0) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001757 JniAbortF(__FUNCTION__, "capacity must be greater than 0: %d", capacity);
Elliott Hughesa2501992011-08-26 19:39:54 -07001758 }
1759 return CHECK_JNI_EXIT("L", baseEnv(env)->NewDirectByteBuffer(env, address, capacity));
1760 }
1761
1762 static void* GetDirectBufferAddress(JNIEnv* env, jobject buf) {
1763 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, buf);
1764 // TODO: check that 'buf' is a java.nio.Buffer.
1765 return CHECK_JNI_EXIT("p", baseEnv(env)->GetDirectBufferAddress(env, buf));
1766 }
1767
1768 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject buf) {
1769 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, buf);
1770 // TODO: check that 'buf' is a java.nio.Buffer.
1771 return CHECK_JNI_EXIT("J", baseEnv(env)->GetDirectBufferCapacity(env, buf));
1772 }
1773
1774 private:
1775 static inline const JNINativeInterface* baseEnv(JNIEnv* env) {
1776 return reinterpret_cast<JNIEnvExt*>(env)->unchecked_functions;
1777 }
1778};
1779
1780const JNINativeInterface gCheckNativeInterface = {
1781 NULL, // reserved0.
1782 NULL, // reserved1.
1783 NULL, // reserved2.
1784 NULL, // reserved3.
1785 CheckJNI::GetVersion,
1786 CheckJNI::DefineClass,
1787 CheckJNI::FindClass,
1788 CheckJNI::FromReflectedMethod,
1789 CheckJNI::FromReflectedField,
1790 CheckJNI::ToReflectedMethod,
1791 CheckJNI::GetSuperclass,
1792 CheckJNI::IsAssignableFrom,
1793 CheckJNI::ToReflectedField,
1794 CheckJNI::Throw,
1795 CheckJNI::ThrowNew,
1796 CheckJNI::ExceptionOccurred,
1797 CheckJNI::ExceptionDescribe,
1798 CheckJNI::ExceptionClear,
1799 CheckJNI::FatalError,
1800 CheckJNI::PushLocalFrame,
1801 CheckJNI::PopLocalFrame,
1802 CheckJNI::NewGlobalRef,
1803 CheckJNI::DeleteGlobalRef,
1804 CheckJNI::DeleteLocalRef,
1805 CheckJNI::IsSameObject,
1806 CheckJNI::NewLocalRef,
1807 CheckJNI::EnsureLocalCapacity,
1808 CheckJNI::AllocObject,
1809 CheckJNI::NewObject,
1810 CheckJNI::NewObjectV,
1811 CheckJNI::NewObjectA,
1812 CheckJNI::GetObjectClass,
1813 CheckJNI::IsInstanceOf,
1814 CheckJNI::GetMethodID,
1815 CheckJNI::CallObjectMethod,
1816 CheckJNI::CallObjectMethodV,
1817 CheckJNI::CallObjectMethodA,
1818 CheckJNI::CallBooleanMethod,
1819 CheckJNI::CallBooleanMethodV,
1820 CheckJNI::CallBooleanMethodA,
1821 CheckJNI::CallByteMethod,
1822 CheckJNI::CallByteMethodV,
1823 CheckJNI::CallByteMethodA,
1824 CheckJNI::CallCharMethod,
1825 CheckJNI::CallCharMethodV,
1826 CheckJNI::CallCharMethodA,
1827 CheckJNI::CallShortMethod,
1828 CheckJNI::CallShortMethodV,
1829 CheckJNI::CallShortMethodA,
1830 CheckJNI::CallIntMethod,
1831 CheckJNI::CallIntMethodV,
1832 CheckJNI::CallIntMethodA,
1833 CheckJNI::CallLongMethod,
1834 CheckJNI::CallLongMethodV,
1835 CheckJNI::CallLongMethodA,
1836 CheckJNI::CallFloatMethod,
1837 CheckJNI::CallFloatMethodV,
1838 CheckJNI::CallFloatMethodA,
1839 CheckJNI::CallDoubleMethod,
1840 CheckJNI::CallDoubleMethodV,
1841 CheckJNI::CallDoubleMethodA,
1842 CheckJNI::CallVoidMethod,
1843 CheckJNI::CallVoidMethodV,
1844 CheckJNI::CallVoidMethodA,
1845 CheckJNI::CallNonvirtualObjectMethod,
1846 CheckJNI::CallNonvirtualObjectMethodV,
1847 CheckJNI::CallNonvirtualObjectMethodA,
1848 CheckJNI::CallNonvirtualBooleanMethod,
1849 CheckJNI::CallNonvirtualBooleanMethodV,
1850 CheckJNI::CallNonvirtualBooleanMethodA,
1851 CheckJNI::CallNonvirtualByteMethod,
1852 CheckJNI::CallNonvirtualByteMethodV,
1853 CheckJNI::CallNonvirtualByteMethodA,
1854 CheckJNI::CallNonvirtualCharMethod,
1855 CheckJNI::CallNonvirtualCharMethodV,
1856 CheckJNI::CallNonvirtualCharMethodA,
1857 CheckJNI::CallNonvirtualShortMethod,
1858 CheckJNI::CallNonvirtualShortMethodV,
1859 CheckJNI::CallNonvirtualShortMethodA,
1860 CheckJNI::CallNonvirtualIntMethod,
1861 CheckJNI::CallNonvirtualIntMethodV,
1862 CheckJNI::CallNonvirtualIntMethodA,
1863 CheckJNI::CallNonvirtualLongMethod,
1864 CheckJNI::CallNonvirtualLongMethodV,
1865 CheckJNI::CallNonvirtualLongMethodA,
1866 CheckJNI::CallNonvirtualFloatMethod,
1867 CheckJNI::CallNonvirtualFloatMethodV,
1868 CheckJNI::CallNonvirtualFloatMethodA,
1869 CheckJNI::CallNonvirtualDoubleMethod,
1870 CheckJNI::CallNonvirtualDoubleMethodV,
1871 CheckJNI::CallNonvirtualDoubleMethodA,
1872 CheckJNI::CallNonvirtualVoidMethod,
1873 CheckJNI::CallNonvirtualVoidMethodV,
1874 CheckJNI::CallNonvirtualVoidMethodA,
1875 CheckJNI::GetFieldID,
1876 CheckJNI::GetObjectField,
1877 CheckJNI::GetBooleanField,
1878 CheckJNI::GetByteField,
1879 CheckJNI::GetCharField,
1880 CheckJNI::GetShortField,
1881 CheckJNI::GetIntField,
1882 CheckJNI::GetLongField,
1883 CheckJNI::GetFloatField,
1884 CheckJNI::GetDoubleField,
1885 CheckJNI::SetObjectField,
1886 CheckJNI::SetBooleanField,
1887 CheckJNI::SetByteField,
1888 CheckJNI::SetCharField,
1889 CheckJNI::SetShortField,
1890 CheckJNI::SetIntField,
1891 CheckJNI::SetLongField,
1892 CheckJNI::SetFloatField,
1893 CheckJNI::SetDoubleField,
1894 CheckJNI::GetStaticMethodID,
1895 CheckJNI::CallStaticObjectMethod,
1896 CheckJNI::CallStaticObjectMethodV,
1897 CheckJNI::CallStaticObjectMethodA,
1898 CheckJNI::CallStaticBooleanMethod,
1899 CheckJNI::CallStaticBooleanMethodV,
1900 CheckJNI::CallStaticBooleanMethodA,
1901 CheckJNI::CallStaticByteMethod,
1902 CheckJNI::CallStaticByteMethodV,
1903 CheckJNI::CallStaticByteMethodA,
1904 CheckJNI::CallStaticCharMethod,
1905 CheckJNI::CallStaticCharMethodV,
1906 CheckJNI::CallStaticCharMethodA,
1907 CheckJNI::CallStaticShortMethod,
1908 CheckJNI::CallStaticShortMethodV,
1909 CheckJNI::CallStaticShortMethodA,
1910 CheckJNI::CallStaticIntMethod,
1911 CheckJNI::CallStaticIntMethodV,
1912 CheckJNI::CallStaticIntMethodA,
1913 CheckJNI::CallStaticLongMethod,
1914 CheckJNI::CallStaticLongMethodV,
1915 CheckJNI::CallStaticLongMethodA,
1916 CheckJNI::CallStaticFloatMethod,
1917 CheckJNI::CallStaticFloatMethodV,
1918 CheckJNI::CallStaticFloatMethodA,
1919 CheckJNI::CallStaticDoubleMethod,
1920 CheckJNI::CallStaticDoubleMethodV,
1921 CheckJNI::CallStaticDoubleMethodA,
1922 CheckJNI::CallStaticVoidMethod,
1923 CheckJNI::CallStaticVoidMethodV,
1924 CheckJNI::CallStaticVoidMethodA,
1925 CheckJNI::GetStaticFieldID,
1926 CheckJNI::GetStaticObjectField,
1927 CheckJNI::GetStaticBooleanField,
1928 CheckJNI::GetStaticByteField,
1929 CheckJNI::GetStaticCharField,
1930 CheckJNI::GetStaticShortField,
1931 CheckJNI::GetStaticIntField,
1932 CheckJNI::GetStaticLongField,
1933 CheckJNI::GetStaticFloatField,
1934 CheckJNI::GetStaticDoubleField,
1935 CheckJNI::SetStaticObjectField,
1936 CheckJNI::SetStaticBooleanField,
1937 CheckJNI::SetStaticByteField,
1938 CheckJNI::SetStaticCharField,
1939 CheckJNI::SetStaticShortField,
1940 CheckJNI::SetStaticIntField,
1941 CheckJNI::SetStaticLongField,
1942 CheckJNI::SetStaticFloatField,
1943 CheckJNI::SetStaticDoubleField,
1944 CheckJNI::NewString,
1945 CheckJNI::GetStringLength,
1946 CheckJNI::GetStringChars,
1947 CheckJNI::ReleaseStringChars,
1948 CheckJNI::NewStringUTF,
1949 CheckJNI::GetStringUTFLength,
1950 CheckJNI::GetStringUTFChars,
1951 CheckJNI::ReleaseStringUTFChars,
1952 CheckJNI::GetArrayLength,
1953 CheckJNI::NewObjectArray,
1954 CheckJNI::GetObjectArrayElement,
1955 CheckJNI::SetObjectArrayElement,
1956 CheckJNI::NewBooleanArray,
1957 CheckJNI::NewByteArray,
1958 CheckJNI::NewCharArray,
1959 CheckJNI::NewShortArray,
1960 CheckJNI::NewIntArray,
1961 CheckJNI::NewLongArray,
1962 CheckJNI::NewFloatArray,
1963 CheckJNI::NewDoubleArray,
1964 CheckJNI::GetBooleanArrayElements,
1965 CheckJNI::GetByteArrayElements,
1966 CheckJNI::GetCharArrayElements,
1967 CheckJNI::GetShortArrayElements,
1968 CheckJNI::GetIntArrayElements,
1969 CheckJNI::GetLongArrayElements,
1970 CheckJNI::GetFloatArrayElements,
1971 CheckJNI::GetDoubleArrayElements,
1972 CheckJNI::ReleaseBooleanArrayElements,
1973 CheckJNI::ReleaseByteArrayElements,
1974 CheckJNI::ReleaseCharArrayElements,
1975 CheckJNI::ReleaseShortArrayElements,
1976 CheckJNI::ReleaseIntArrayElements,
1977 CheckJNI::ReleaseLongArrayElements,
1978 CheckJNI::ReleaseFloatArrayElements,
1979 CheckJNI::ReleaseDoubleArrayElements,
1980 CheckJNI::GetBooleanArrayRegion,
1981 CheckJNI::GetByteArrayRegion,
1982 CheckJNI::GetCharArrayRegion,
1983 CheckJNI::GetShortArrayRegion,
1984 CheckJNI::GetIntArrayRegion,
1985 CheckJNI::GetLongArrayRegion,
1986 CheckJNI::GetFloatArrayRegion,
1987 CheckJNI::GetDoubleArrayRegion,
1988 CheckJNI::SetBooleanArrayRegion,
1989 CheckJNI::SetByteArrayRegion,
1990 CheckJNI::SetCharArrayRegion,
1991 CheckJNI::SetShortArrayRegion,
1992 CheckJNI::SetIntArrayRegion,
1993 CheckJNI::SetLongArrayRegion,
1994 CheckJNI::SetFloatArrayRegion,
1995 CheckJNI::SetDoubleArrayRegion,
1996 CheckJNI::RegisterNatives,
1997 CheckJNI::UnregisterNatives,
1998 CheckJNI::MonitorEnter,
1999 CheckJNI::MonitorExit,
2000 CheckJNI::GetJavaVM,
2001 CheckJNI::GetStringRegion,
2002 CheckJNI::GetStringUTFRegion,
2003 CheckJNI::GetPrimitiveArrayCritical,
2004 CheckJNI::ReleasePrimitiveArrayCritical,
2005 CheckJNI::GetStringCritical,
2006 CheckJNI::ReleaseStringCritical,
2007 CheckJNI::NewWeakGlobalRef,
2008 CheckJNI::DeleteWeakGlobalRef,
2009 CheckJNI::ExceptionCheck,
2010 CheckJNI::NewDirectByteBuffer,
2011 CheckJNI::GetDirectBufferAddress,
2012 CheckJNI::GetDirectBufferCapacity,
2013 CheckJNI::GetObjectRefType,
2014};
2015
2016const JNINativeInterface* GetCheckJniNativeInterface() {
2017 return &gCheckNativeInterface;
2018}
2019
2020class CheckJII {
Elliott Hughesba8eee12012-01-24 20:25:24 -08002021 public:
Elliott Hughesa2501992011-08-26 19:39:54 -07002022 static jint DestroyJavaVM(JavaVM* vm) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002023 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002024 sc.Check(true, "v", vm);
2025 return CHECK_JNI_EXIT("I", BaseVm(vm)->DestroyJavaVM(vm));
Elliott Hughesa2501992011-08-26 19:39:54 -07002026 }
2027
2028 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002029 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002030 sc.Check(true, "vpp", vm, p_env, thr_args);
2031 return CHECK_JNI_EXIT("I", BaseVm(vm)->AttachCurrentThread(vm, p_env, thr_args));
Elliott Hughesa2501992011-08-26 19:39:54 -07002032 }
2033
2034 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002035 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002036 sc.Check(true, "vpp", vm, p_env, thr_args);
2037 return CHECK_JNI_EXIT("I", BaseVm(vm)->AttachCurrentThreadAsDaemon(vm, p_env, thr_args));
Elliott Hughesa2501992011-08-26 19:39:54 -07002038 }
2039
2040 static jint DetachCurrentThread(JavaVM* vm) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002041 ScopedCheck sc(vm, true, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002042 sc.Check(true, "v", vm);
2043 return CHECK_JNI_EXIT("I", BaseVm(vm)->DetachCurrentThread(vm));
Elliott Hughesa2501992011-08-26 19:39:54 -07002044 }
2045
2046 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002047 ScopedCheck sc(vm, true, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002048 sc.Check(true, "v", vm);
2049 return CHECK_JNI_EXIT("I", BaseVm(vm)->GetEnv(vm, env, version));
Elliott Hughesa2501992011-08-26 19:39:54 -07002050 }
2051
2052 private:
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002053 static inline const JNIInvokeInterface* BaseVm(JavaVM* vm) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002054 return reinterpret_cast<JavaVMExt*>(vm)->unchecked_functions;
2055 }
2056};
2057
2058const JNIInvokeInterface gCheckInvokeInterface = {
2059 NULL, // reserved0
2060 NULL, // reserved1
2061 NULL, // reserved2
2062 CheckJII::DestroyJavaVM,
2063 CheckJII::AttachCurrentThread,
2064 CheckJII::DetachCurrentThread,
2065 CheckJII::GetEnv,
2066 CheckJII::AttachCurrentThreadAsDaemon
2067};
2068
2069const JNIInvokeInterface* GetCheckJniInvokeInterface() {
2070 return &gCheckInvokeInterface;
2071}
2072
2073} // namespace art