blob: 7c5186adc196339a3c57126902305d6347de4d07 [file] [log] [blame]
Elliott Hughesa2501992011-08-26 19:39:54 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "jni_internal.h"
18
19#include <sys/mman.h>
20#include <zlib.h>
21
22#include "class_linker.h"
23#include "logging.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080024#include "object_utils.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070025#include "scoped_thread_state_change.h"
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070026#include "gc/space.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070027#include "thread.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070028#include "runtime.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070029
Elliott Hughese6087632011-09-26 12:18:25 -070030#define LIBCORE_CPP_JNI_HELPERS
31#include <JNIHelp.h> // from libcore
32#undef LIBCORE_CPP_JNI_HELPERS
33
Elliott Hughesa2501992011-08-26 19:39:54 -070034namespace art {
35
Elliott Hughes3f6635a2012-06-19 13:37:49 -070036static void JniAbort(const char* jni_function_name, const char* msg) {
Elliott Hughesa0957642011-09-02 14:27:33 -070037 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070038 ScopedObjectAccess soa(self);
Mathieu Chartier66f19252012-09-18 08:57:04 -070039 AbstractMethod* current_method = self->GetCurrentMethod();
Elliott Hughesa2501992011-08-26 19:39:54 -070040
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070041 std::ostringstream os;
Elliott Hughes3f6635a2012-06-19 13:37:49 -070042 os << "JNI DETECTED ERROR IN APPLICATION: " << msg;
Elliott Hughesa2501992011-08-26 19:39:54 -070043
44 if (jni_function_name != NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -070045 os << "\n in call to " << jni_function_name;
Elliott Hughesa2501992011-08-26 19:39:54 -070046 }
Elliott Hughesa0957642011-09-02 14:27:33 -070047 // TODO: is this useful given that we're about to dump the calling thread's stack?
48 if (current_method != NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -070049 os << "\n from " << PrettyMethod(current_method);
Elliott Hughesa0957642011-09-02 14:27:33 -070050 }
51 os << "\n";
52 self->Dump(os);
Elliott Hughesa2501992011-08-26 19:39:54 -070053
54 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
55 if (vm->check_jni_abort_hook != NULL) {
Elliott Hughesb264f082012-04-06 17:10:10 -070056 vm->check_jni_abort_hook(vm->check_jni_abort_hook_data, os.str());
Elliott Hughesa2501992011-08-26 19:39:54 -070057 } else {
Ian Rogers9da7f592012-08-20 17:14:28 -070058 // Ensure that we get a native stack trace for this thread.
59 self->TransitionFromRunnableToSuspended(kNative);
Elliott Hughesa2501992011-08-26 19:39:54 -070060 LOG(FATAL) << os.str();
Ian Rogers9da7f592012-08-20 17:14:28 -070061 self->TransitionFromSuspendedToRunnable(); // Unreachable, keep annotalysis happy.
Elliott Hughesa2501992011-08-26 19:39:54 -070062 }
63}
64
Elliott Hughes3f6635a2012-06-19 13:37:49 -070065static void JniAbortV(const char* jni_function_name, const char* fmt, va_list ap) {
66 std::string msg;
67 StringAppendV(&msg, fmt, ap);
68 JniAbort(jni_function_name, msg.c_str());
69}
70
71void JniAbortF(const char* jni_function_name, const char* fmt, ...) {
72 va_list args;
73 va_start(args, fmt);
74 JniAbortV(jni_function_name, fmt, args);
75 va_end(args);
76}
77
Elliott Hughesa2501992011-08-26 19:39:54 -070078/*
79 * ===========================================================================
80 * JNI function helpers
81 * ===========================================================================
82 */
83
Ian Rogers959f8ed2012-02-07 16:33:37 -080084static bool IsSirtLocalRef(JNIEnv* env, jobject localRef) {
85 return GetIndirectRefKind(localRef) == kSirtOrInvalid &&
Ian Rogers0399dde2012-06-06 17:09:28 -070086 reinterpret_cast<JNIEnvExt*>(env)->self->SirtContains(localRef);
Ian Rogers959f8ed2012-02-07 16:33:37 -080087}
88
Elliott Hughes3f6635a2012-06-19 13:37:49 -070089// Hack to allow forcecopy to work with jniGetNonMovableArrayElements.
90// The code deliberately uses an invalid sequence of operations, so we
91// need to pass it through unmodified. Review that code before making
92// any changes here.
Elliott Hughesa2501992011-08-26 19:39:54 -070093#define kNoCopyMagic 0xd5aab57f
94
Elliott Hughes3f6635a2012-06-19 13:37:49 -070095// Flags passed into ScopedCheck.
Elliott Hughesa2501992011-08-26 19:39:54 -070096#define kFlag_Default 0x0000
97
Elliott Hughes3f6635a2012-06-19 13:37:49 -070098#define kFlag_CritBad 0x0000 // Calling while in critical is not allowed.
99#define kFlag_CritOkay 0x0001 // Calling while in critical is allowed.
100#define kFlag_CritGet 0x0002 // This is a critical "get".
101#define kFlag_CritRelease 0x0003 // This is a critical "release".
102#define kFlag_CritMask 0x0003 // Bit mask to get "crit" value.
Elliott Hughesa2501992011-08-26 19:39:54 -0700103
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700104#define kFlag_ExcepBad 0x0000 // Raised exceptions are not allowed.
105#define kFlag_ExcepOkay 0x0004 // Raised exceptions are allowed.
Elliott Hughesa2501992011-08-26 19:39:54 -0700106
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700107#define kFlag_Release 0x0010 // Are we in a non-critical release function?
108#define kFlag_NullableUtf 0x0020 // Are our UTF parameters nullable?
Elliott Hughesa2501992011-08-26 19:39:54 -0700109
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700110#define kFlag_Invocation 0x8000 // Part of the invocation interface (JavaVM*).
Elliott Hughesa2501992011-08-26 19:39:54 -0700111
Elliott Hughes485cac42011-12-09 17:49:35 -0800112#define kFlag_ForceTrace 0x80000000 // Add this to a JNI function's flags if you want to trace every call.
113
Elliott Hughesa0957642011-09-02 14:27:33 -0700114static const char* gBuiltInPrefixes[] = {
115 "Landroid/",
116 "Lcom/android/",
117 "Lcom/google/android/",
118 "Ldalvik/",
119 "Ljava/",
120 "Ljavax/",
121 "Llibcore/",
122 "Lorg/apache/harmony/",
123 NULL
124};
125
Mathieu Chartier66f19252012-09-18 08:57:04 -0700126static bool ShouldTrace(JavaVMExt* vm, const AbstractMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700127 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700128 // If both "-Xcheck:jni" and "-Xjnitrace:" are enabled, we print trace messages
129 // when a native method that matches the -Xjnitrace argument calls a JNI function
130 // such as NewByteArray.
131 // If -verbose:third-party-jni is on, we want to log any JNI function calls
132 // made by a third-party native method.
Elliott Hughes81ff3182012-03-23 20:35:56 -0700133 std::string class_name(MethodHelper(method).GetDeclaringClassDescriptor());
134 if (!vm->trace.empty() && class_name.find(vm->trace) != std::string::npos) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700135 return true;
136 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800137 if (VLOG_IS_ON(third_party_jni)) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700138 // Return true if we're trying to log all third-party JNI activity and 'method' doesn't look
139 // like part of Android.
Elliott Hughesa0957642011-09-02 14:27:33 -0700140 for (size_t i = 0; gBuiltInPrefixes[i] != NULL; ++i) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700141 if (StartsWith(class_name, gBuiltInPrefixes[i])) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700142 return false;
143 }
144 }
145 return true;
146 }
147 return false;
148}
149
Elliott Hughesa2501992011-08-26 19:39:54 -0700150class ScopedCheck {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800151 public:
Elliott Hughesa2501992011-08-26 19:39:54 -0700152 // For JNIEnv* functions.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700153 explicit ScopedCheck(JNIEnv* env, int flags, const char* functionName)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700154 SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700155 : soa_(env) {
Ian Rogers365c1022012-06-22 15:05:28 -0700156 Init(flags, functionName, true);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700157 CheckThread(flags);
Elliott Hughesa2501992011-08-26 19:39:54 -0700158 }
159
160 // For JavaVM* functions.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700161 // TODO: it's not correct that this is a lock function, but making it so aids annotalysis.
162 explicit ScopedCheck(JavaVM* vm, bool has_method, const char* functionName)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700163 SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700164 : soa_(vm) {
Ian Rogers365c1022012-06-22 15:05:28 -0700165 Init(kFlag_Invocation, functionName, has_method);
Elliott Hughesa2501992011-08-26 19:39:54 -0700166 }
167
Ian Rogersb726dcb2012-09-05 08:57:23 -0700168 ~ScopedCheck() UNLOCK_FUNCTION(Locks::mutator_lock_) {}
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700169
170 const ScopedObjectAccess& soa() {
171 return soa_;
172 }
173
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700174 bool ForceCopy() {
Elliott Hughesa2501992011-08-26 19:39:54 -0700175 return Runtime::Current()->GetJavaVM()->force_copy;
176 }
177
Elliott Hughes81ff3182012-03-23 20:35:56 -0700178 // Checks that 'class_name' is a valid "fully-qualified" JNI class name, like "java/lang/Thread"
179 // or "[Ljava/lang/Object;". A ClassLoader can actually normalize class names a couple of
180 // times, so using "java.lang.Thread" instead of "java/lang/Thread" might work in some
181 // circumstances, but this is incorrect.
182 void CheckClassName(const char* class_name) {
183 if (!IsValidJniClassName(class_name)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700184 JniAbortF(function_name_,
185 "illegal class name '%s'\n"
186 " (should be of the form 'package/Class', [Lpackage/Class;' or '[[B')",
187 class_name);
Elliott Hughesa2501992011-08-26 19:39:54 -0700188 }
189 }
190
191 /*
192 * Verify that the field is of the appropriate type. If the field has an
193 * object type, "java_object" is the object we're trying to assign into it.
194 *
195 * Works for both static and instance fields.
196 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700197 void CheckFieldType(jobject java_object, jfieldID fid, char prim, bool isStatic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700198 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700199 Field* f = CheckFieldID(fid);
200 if (f == NULL) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700201 return;
202 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800203 Class* field_type = FieldHelper(f).GetType();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700204 if (!field_type->IsPrimitive()) {
205 if (java_object != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700206 Object* obj = soa_.Decode<Object*>(java_object);
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700207 // If java_object is a weak global ref whose referent has been cleared,
208 // obj will be NULL. Otherwise, obj should always be non-NULL
209 // and valid.
Elliott Hughes88c5c352012-03-15 18:49:48 -0700210 if (!Runtime::Current()->GetHeap()->IsHeapAddress(obj)) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700211 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700212 JniAbortF(function_name_, "field operation on invalid %s: %p",
213 ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700214 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700215 } else {
Brian Carlstrom16192862011-09-12 17:50:06 -0700216 if (!obj->InstanceOf(field_type)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700217 JniAbortF(function_name_, "attempt to set field %s with value of wrong type: %s",
218 PrettyField(f).c_str(), PrettyTypeOf(obj).c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700219 return;
220 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700221 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700222 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700223 } else if (field_type != Runtime::Current()->GetClassLinker()->FindPrimitiveClass(prim)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700224 JniAbortF(function_name_, "attempt to set field %s with value of wrong type: %c",
225 PrettyField(f).c_str(), prim);
Elliott Hughesa2501992011-08-26 19:39:54 -0700226 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700227 }
228
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700229 if (isStatic != f->IsStatic()) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700230 if (isStatic) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700231 JniAbortF(function_name_, "accessing non-static field %s as static", PrettyField(f).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700232 } else {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700233 JniAbortF(function_name_, "accessing static field %s as non-static", PrettyField(f).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700234 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700235 return;
236 }
237 }
238
239 /*
240 * Verify that this instance field ID is valid for this object.
241 *
242 * Assumes "jobj" has already been validated.
243 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700244 void CheckInstanceFieldID(jobject java_object, jfieldID fid)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700245 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700246 Object* o = soa_.Decode<Object*>(java_object);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800247 if (o == NULL || !Runtime::Current()->GetHeap()->IsHeapAddress(o)) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700248 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700249 JniAbortF(function_name_, "field operation on invalid %s: %p",
250 ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700251 return;
252 }
253
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700254 Field* f = CheckFieldID(fid);
255 if (f == NULL) {
256 return;
257 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700258 Class* c = o->GetClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800259 FieldHelper fh(f);
260 if (c->FindInstanceField(fh.GetName(), fh.GetTypeDescriptor()) == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700261 JniAbortF(function_name_, "jfieldID %s not valid for an object of class %s",
262 PrettyField(f).c_str(), PrettyTypeOf(o).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700263 }
264 }
265
266 /*
267 * Verify that the pointer value is non-NULL.
268 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700269 void CheckNonNull(const void* ptr) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700270 if (ptr == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700271 JniAbortF(function_name_, "non-nullable argument was NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -0700272 }
273 }
274
275 /*
276 * Verify that the method's return type matches the type of call.
277 * 'expectedType' will be "L" for all objects, including arrays.
278 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700279 void CheckSig(jmethodID mid, const char* expectedType, bool isStatic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700280 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700281 AbstractMethod* m = CheckMethodID(mid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700282 if (m == NULL) {
283 return;
284 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800285 if (*expectedType != MethodHelper(m).GetShorty()[0]) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700286 JniAbortF(function_name_, "the return type of %s does not match %s",
287 function_name_, PrettyMethod(m).c_str());
288 }
289 if (isStatic != m->IsStatic()) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700290 if (isStatic) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700291 JniAbortF(function_name_, "calling non-static method %s with %s",
292 PrettyMethod(m).c_str(), function_name_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700293 } else {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700294 JniAbortF(function_name_, "calling static method %s with %s",
295 PrettyMethod(m).c_str(), function_name_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700296 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700297 }
298 }
299
300 /*
301 * Verify that this static field ID is valid for this class.
302 *
303 * Assumes "java_class" has already been validated.
304 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700305 void CheckStaticFieldID(jclass java_class, jfieldID fid)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700306 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700307 Class* c = soa_.Decode<Class*>(java_class);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700308 const Field* f = CheckFieldID(fid);
309 if (f == NULL) {
310 return;
311 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700312 if (f->GetDeclaringClass() != c) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700313 JniAbortF(function_name_, "static jfieldID %p not valid for class %s",
314 fid, PrettyClass(c).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700315 }
316 }
317
318 /*
Elliott Hughese84278b2012-03-22 10:06:53 -0700319 * Verify that "mid" is appropriate for "java_class".
Elliott Hughesa2501992011-08-26 19:39:54 -0700320 *
321 * A mismatch isn't dangerous, because the jmethodID defines the class. In
Elliott Hughese84278b2012-03-22 10:06:53 -0700322 * fact, java_class is unused in the implementation. It's best if we don't
Elliott Hughesa2501992011-08-26 19:39:54 -0700323 * allow bad code in the system though.
324 *
Elliott Hughese84278b2012-03-22 10:06:53 -0700325 * Instances of "java_class" must be instances of the method's declaring class.
Elliott Hughesa2501992011-08-26 19:39:54 -0700326 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700327 void CheckStaticMethod(jclass java_class, jmethodID mid)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700328 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700329 const AbstractMethod* m = CheckMethodID(mid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700330 if (m == NULL) {
331 return;
332 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700333 Class* c = soa_.Decode<Class*>(java_class);
Elliott Hughesa2501992011-08-26 19:39:54 -0700334 if (!c->IsAssignableFrom(m->GetDeclaringClass())) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700335 JniAbortF(function_name_, "can't call static %s on class %s",
336 PrettyMethod(m).c_str(), PrettyClass(c).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700337 }
338 }
339
340 /*
341 * Verify that "mid" is appropriate for "jobj".
342 *
343 * Make sure the object is an instance of the method's declaring class.
344 * (Note the mid might point to a declaration in an interface; this
345 * will be handled automatically by the instanceof check.)
346 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700347 void CheckVirtualMethod(jobject java_object, jmethodID mid)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700348 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700349 const AbstractMethod* m = CheckMethodID(mid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700350 if (m == NULL) {
351 return;
352 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700353 Object* o = soa_.Decode<Object*>(java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700354 if (!o->InstanceOf(m->GetDeclaringClass())) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700355 JniAbortF(function_name_, "can't call %s on instance of %s",
356 PrettyMethod(m).c_str(), PrettyTypeOf(o).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700357 }
358 }
359
360 /**
361 * The format string is a sequence of the following characters,
362 * and must be followed by arguments of the corresponding types
363 * in the same order.
364 *
365 * Java primitive types:
366 * B - jbyte
367 * C - jchar
368 * D - jdouble
369 * F - jfloat
370 * I - jint
371 * J - jlong
372 * S - jshort
373 * Z - jboolean (shown as true and false)
374 * V - void
375 *
376 * Java reference types:
377 * L - jobject
378 * a - jarray
379 * c - jclass
380 * s - jstring
381 *
382 * JNI types:
383 * b - jboolean (shown as JNI_TRUE and JNI_FALSE)
384 * f - jfieldID
385 * m - jmethodID
386 * p - void*
387 * r - jint (for release mode arguments)
Elliott Hughes78090d12011-10-07 14:31:47 -0700388 * u - const char* (Modified UTF-8)
Elliott Hughesa2501992011-08-26 19:39:54 -0700389 * z - jsize (for lengths; use i if negative values are okay)
390 * v - JavaVM*
391 * E - JNIEnv*
392 * . - no argument; just print "..." (used for varargs JNI calls)
393 *
394 * Use the kFlag_NullableUtf flag where 'u' field(s) are nullable.
395 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700396 void Check(bool entry, const char* fmt0, ...)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700397 SHARED_LOCKS_REQUIRED (Locks::mutator_lock_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700398 va_list ap;
399
Mathieu Chartier66f19252012-09-18 08:57:04 -0700400 const AbstractMethod* traceMethod = NULL;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700401 if ((!soa_.Vm()->trace.empty() || VLOG_IS_ON(third_party_jni)) && has_method_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700402 // We need to guard some of the invocation interface's calls: a bad caller might
403 // use DetachCurrentThread or GetEnv on a thread that's not yet attached.
Elliott Hughesa0957642011-09-02 14:27:33 -0700404 Thread* self = Thread::Current();
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700405 if ((flags_ & kFlag_Invocation) == 0 || self != NULL) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700406 traceMethod = self->GetCurrentMethod();
Elliott Hughesa2501992011-08-26 19:39:54 -0700407 }
408 }
Elliott Hughesa0957642011-09-02 14:27:33 -0700409
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700410 if (((flags_ & kFlag_ForceTrace) != 0) || (traceMethod != NULL && ShouldTrace(soa_.Vm(), traceMethod))) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700411 va_start(ap, fmt0);
412 std::string msg;
413 for (const char* fmt = fmt0; *fmt;) {
414 char ch = *fmt++;
415 if (ch == 'B') { // jbyte
416 jbyte b = va_arg(ap, int);
417 if (b >= 0 && b < 10) {
418 StringAppendF(&msg, "%d", b);
419 } else {
420 StringAppendF(&msg, "%#x (%d)", b, b);
421 }
422 } else if (ch == 'C') { // jchar
423 jchar c = va_arg(ap, int);
424 if (c < 0x7f && c >= ' ') {
425 StringAppendF(&msg, "U+%x ('%c')", c, c);
426 } else {
427 StringAppendF(&msg, "U+%x", c);
428 }
429 } else if (ch == 'F' || ch == 'D') { // jfloat, jdouble
430 StringAppendF(&msg, "%g", va_arg(ap, double));
431 } else if (ch == 'I' || ch == 'S') { // jint, jshort
432 StringAppendF(&msg, "%d", va_arg(ap, int));
433 } else if (ch == 'J') { // jlong
434 StringAppendF(&msg, "%lld", va_arg(ap, jlong));
435 } else if (ch == 'Z') { // jboolean
436 StringAppendF(&msg, "%s", va_arg(ap, int) ? "true" : "false");
437 } else if (ch == 'V') { // void
438 msg += "void";
439 } else if (ch == 'v') { // JavaVM*
440 JavaVM* vm = va_arg(ap, JavaVM*);
441 StringAppendF(&msg, "(JavaVM*)%p", vm);
442 } else if (ch == 'E') { // JNIEnv*
443 JNIEnv* env = va_arg(ap, JNIEnv*);
444 StringAppendF(&msg, "(JNIEnv*)%p", env);
445 } else if (ch == 'L' || ch == 'a' || ch == 's') { // jobject, jarray, jstring
446 // For logging purposes, these are identical.
447 jobject o = va_arg(ap, jobject);
448 if (o == NULL) {
449 msg += "NULL";
450 } else {
451 StringAppendF(&msg, "%p", o);
452 }
453 } else if (ch == 'b') { // jboolean (JNI-style)
454 jboolean b = va_arg(ap, int);
455 msg += (b ? "JNI_TRUE" : "JNI_FALSE");
456 } else if (ch == 'c') { // jclass
457 jclass jc = va_arg(ap, jclass);
458 Class* c = reinterpret_cast<Class*>(Thread::Current()->DecodeJObject(jc));
459 if (c == NULL) {
460 msg += "NULL";
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800461 } else if (c == kInvalidIndirectRefObject || !Runtime::Current()->GetHeap()->IsHeapAddress(c)) {
Elliott Hughes485cac42011-12-09 17:49:35 -0800462 StringAppendF(&msg, "INVALID POINTER:%p", jc);
463 } else if (!c->IsClass()) {
464 msg += "INVALID NON-CLASS OBJECT OF TYPE:" + PrettyTypeOf(c);
Elliott Hughesa2501992011-08-26 19:39:54 -0700465 } else {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700466 msg += PrettyClass(c);
Elliott Hughesa2501992011-08-26 19:39:54 -0700467 if (!entry) {
468 StringAppendF(&msg, " (%p)", jc);
469 }
470 }
471 } else if (ch == 'f') { // jfieldID
472 jfieldID fid = va_arg(ap, jfieldID);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700473 Field* f = reinterpret_cast<Field*>(fid);
Elliott Hughesa2501992011-08-26 19:39:54 -0700474 msg += PrettyField(f);
475 if (!entry) {
476 StringAppendF(&msg, " (%p)", fid);
477 }
478 } else if (ch == 'z') { // non-negative jsize
479 // You might expect jsize to be size_t, but it's not; it's the same as jint.
480 // We only treat this specially so we can do the non-negative check.
481 // TODO: maybe this wasn't worth it?
482 jint i = va_arg(ap, jint);
483 StringAppendF(&msg, "%d", i);
484 } else if (ch == 'm') { // jmethodID
485 jmethodID mid = va_arg(ap, jmethodID);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700486 AbstractMethod* m = reinterpret_cast<AbstractMethod*>(mid);
Elliott Hughesa2501992011-08-26 19:39:54 -0700487 msg += PrettyMethod(m);
488 if (!entry) {
489 StringAppendF(&msg, " (%p)", mid);
490 }
491 } else if (ch == 'p') { // void* ("pointer")
492 void* p = va_arg(ap, void*);
493 if (p == NULL) {
494 msg += "NULL";
495 } else {
496 StringAppendF(&msg, "(void*) %p", p);
497 }
498 } else if (ch == 'r') { // jint (release mode)
499 jint releaseMode = va_arg(ap, jint);
500 if (releaseMode == 0) {
501 msg += "0";
502 } else if (releaseMode == JNI_ABORT) {
503 msg += "JNI_ABORT";
504 } else if (releaseMode == JNI_COMMIT) {
505 msg += "JNI_COMMIT";
506 } else {
507 StringAppendF(&msg, "invalid release mode %d", releaseMode);
508 }
Elliott Hughes78090d12011-10-07 14:31:47 -0700509 } else if (ch == 'u') { // const char* (Modified UTF-8)
Elliott Hughesa2501992011-08-26 19:39:54 -0700510 const char* utf = va_arg(ap, const char*);
511 if (utf == NULL) {
512 msg += "NULL";
513 } else {
514 StringAppendF(&msg, "\"%s\"", utf);
515 }
516 } else if (ch == '.') {
517 msg += "...";
518 } else {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700519 JniAbortF(function_name_, "unknown trace format specifier: %c", ch);
Elliott Hughesa2501992011-08-26 19:39:54 -0700520 return;
521 }
522 if (*fmt) {
523 StringAppendF(&msg, ", ");
524 }
525 }
526 va_end(ap);
527
Elliott Hughes485cac42011-12-09 17:49:35 -0800528 if ((flags_ & kFlag_ForceTrace) != 0) {
529 LOG(INFO) << "JNI: call to " << function_name_ << "(" << msg << ")";
530 } else if (entry) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700531 if (has_method_) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700532 std::string methodName(PrettyMethod(traceMethod, false));
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700533 LOG(INFO) << "JNI: " << methodName << " -> " << function_name_ << "(" << msg << ")";
534 indent_ = methodName.size() + 1;
Elliott Hughesa2501992011-08-26 19:39:54 -0700535 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700536 LOG(INFO) << "JNI: -> " << function_name_ << "(" << msg << ")";
537 indent_ = 0;
Elliott Hughesa2501992011-08-26 19:39:54 -0700538 }
539 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700540 LOG(INFO) << StringPrintf("JNI: %*s<- %s returned %s", indent_, "", function_name_, msg.c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700541 }
542 }
543
544 // We always do the thorough checks on entry, and never on exit...
545 if (entry) {
546 va_start(ap, fmt0);
547 for (const char* fmt = fmt0; *fmt; ++fmt) {
548 char ch = *fmt;
549 if (ch == 'a') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700550 CheckArray(va_arg(ap, jarray));
Elliott Hughesa2501992011-08-26 19:39:54 -0700551 } else if (ch == 'c') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700552 CheckInstance(kClass, va_arg(ap, jclass));
Elliott Hughesa2501992011-08-26 19:39:54 -0700553 } else if (ch == 'L') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700554 CheckObject(va_arg(ap, jobject));
Elliott Hughesa2501992011-08-26 19:39:54 -0700555 } else if (ch == 'r') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700556 CheckReleaseMode(va_arg(ap, jint));
Elliott Hughesa2501992011-08-26 19:39:54 -0700557 } else if (ch == 's') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700558 CheckInstance(kString, va_arg(ap, jstring));
Elliott Hughesa2501992011-08-26 19:39:54 -0700559 } else if (ch == 'u') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700560 if ((flags_ & kFlag_Release) != 0) {
561 CheckNonNull(va_arg(ap, const char*));
Elliott Hughesa2501992011-08-26 19:39:54 -0700562 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700563 bool nullable = ((flags_ & kFlag_NullableUtf) != 0);
564 CheckUtfString(va_arg(ap, const char*), nullable);
Elliott Hughesa2501992011-08-26 19:39:54 -0700565 }
566 } else if (ch == 'z') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700567 CheckLengthPositive(va_arg(ap, jsize));
Elliott Hughesa2501992011-08-26 19:39:54 -0700568 } else if (strchr("BCISZbfmpEv", ch) != NULL) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800569 va_arg(ap, uint32_t); // Skip this argument.
Elliott Hughesa2501992011-08-26 19:39:54 -0700570 } else if (ch == 'D' || ch == 'F') {
571 va_arg(ap, double); // Skip this argument.
572 } else if (ch == 'J') {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800573 va_arg(ap, uint64_t); // Skip this argument.
Elliott Hughesa2501992011-08-26 19:39:54 -0700574 } else if (ch == '.') {
575 } else {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800576 LOG(FATAL) << "Unknown check format specifier: " << ch;
Elliott Hughesa2501992011-08-26 19:39:54 -0700577 }
578 }
579 va_end(ap);
580 }
581 }
582
Elliott Hughesa92853e2012-02-07 16:09:27 -0800583 enum InstanceKind {
584 kClass,
Elliott Hughes0f3c5532012-03-30 14:51:51 -0700585 kDirectByteBuffer,
586 kObject,
587 kString,
588 kThrowable,
Elliott Hughesa92853e2012-02-07 16:09:27 -0800589 };
590
591 /*
592 * Verify that "jobj" is a valid non-NULL object reference, and points to
593 * an instance of expectedClass.
594 *
595 * Because we're looking at an object on the GC heap, we have to switch
596 * to "running" mode before doing the checks.
597 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700598 bool CheckInstance(InstanceKind kind, jobject java_object)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700599 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa92853e2012-02-07 16:09:27 -0800600 const char* what = NULL;
601 switch (kind) {
602 case kClass:
603 what = "jclass";
604 break;
605 case kDirectByteBuffer:
606 what = "direct ByteBuffer";
607 break;
608 case kObject:
609 what = "jobject";
610 break;
611 case kString:
612 what = "jstring";
613 break;
614 case kThrowable:
615 what = "jthrowable";
616 break;
617 default:
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700618 LOG(FATAL) << "Unknown kind " << static_cast<int>(kind);
Elliott Hughesa92853e2012-02-07 16:09:27 -0800619 }
620
621 if (java_object == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700622 JniAbortF(function_name_, "%s received null %s", function_name_, what);
Elliott Hughesa92853e2012-02-07 16:09:27 -0800623 return false;
624 }
625
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700626 Object* obj = soa_.Decode<Object*>(java_object);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800627 if (!Runtime::Current()->GetHeap()->IsHeapAddress(obj)) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700628 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700629 JniAbortF(function_name_, "%s is an invalid %s: %p (%p)",
630 what, ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object, obj);
Elliott Hughesa92853e2012-02-07 16:09:27 -0800631 return false;
632 }
633
634 bool okay = true;
635 switch (kind) {
636 case kClass:
637 okay = obj->IsClass();
638 break;
639 case kDirectByteBuffer:
640 UNIMPLEMENTED(FATAL);
641 break;
642 case kString:
643 okay = obj->GetClass()->IsStringClass();
644 break;
645 case kThrowable:
646 okay = obj->GetClass()->IsThrowableClass();
647 break;
648 case kObject:
649 break;
650 }
651 if (!okay) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700652 JniAbortF(function_name_, "%s has wrong type: %s", what, PrettyTypeOf(obj).c_str());
Elliott Hughesa92853e2012-02-07 16:09:27 -0800653 return false;
654 }
655
656 return true;
657 }
658
Elliott Hughesba8eee12012-01-24 20:25:24 -0800659 private:
Elliott Hughes81ff3182012-03-23 20:35:56 -0700660 // Set "has_method" to true if we have a valid thread with a method pointer.
661 // We won't have one before attaching a thread, after detaching a thread, or
662 // when shutting down the runtime.
Ian Rogers365c1022012-06-22 15:05:28 -0700663 void Init(int flags, const char* functionName, bool has_method) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700664 flags_ = flags;
665 function_name_ = functionName;
Elliott Hughes81ff3182012-03-23 20:35:56 -0700666 has_method_ = has_method;
Elliott Hughesa2501992011-08-26 19:39:54 -0700667 }
668
669 /*
670 * Verify that "array" is non-NULL and points to an Array object.
671 *
672 * Since we're dealing with objects, switch to "running" mode.
673 */
Ian Rogersb726dcb2012-09-05 08:57:23 -0700674 void CheckArray(jarray java_array) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700675 if (java_array == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700676 JniAbortF(function_name_, "jarray was NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -0700677 return;
678 }
679
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700680 Array* a = soa_.Decode<Array*>(java_array);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800681 if (!Runtime::Current()->GetHeap()->IsHeapAddress(a)) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700682 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700683 JniAbortF(function_name_, "jarray is an invalid %s: %p (%p)",
684 ToStr<IndirectRefKind>(GetIndirectRefKind(java_array)).c_str(), java_array, a);
Elliott Hughesa2501992011-08-26 19:39:54 -0700685 } else if (!a->IsArrayInstance()) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700686 JniAbortF(function_name_, "jarray argument has non-array type: %s", PrettyTypeOf(a).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700687 }
688 }
689
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700690 void CheckLengthPositive(jsize length) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700691 if (length < 0) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700692 JniAbortF(function_name_, "negative jsize: %d", length);
Elliott Hughesa2501992011-08-26 19:39:54 -0700693 }
694 }
695
Ian Rogersb726dcb2012-09-05 08:57:23 -0700696 Field* CheckFieldID(jfieldID fid) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700697 if (fid == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700698 JniAbortF(function_name_, "jfieldID was NULL");
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700699 return NULL;
700 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700701 Field* f = soa_.DecodeField(fid);
Ian Rogers365c1022012-06-22 15:05:28 -0700702 if (!Runtime::Current()->GetHeap()->IsHeapAddress(f) || !f->IsField()) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700703 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700704 JniAbortF(function_name_, "invalid jfieldID: %p", fid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700705 return NULL;
706 }
707 return f;
708 }
709
Mathieu Chartier66f19252012-09-18 08:57:04 -0700710 AbstractMethod* CheckMethodID(jmethodID mid) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700711 if (mid == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700712 JniAbortF(function_name_, "jmethodID was NULL");
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700713 return NULL;
714 }
Mathieu Chartier66f19252012-09-18 08:57:04 -0700715 AbstractMethod* m = soa_.DecodeMethod(mid);
Ian Rogers365c1022012-06-22 15:05:28 -0700716 if (!Runtime::Current()->GetHeap()->IsHeapAddress(m) || !m->IsMethod()) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700717 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700718 JniAbortF(function_name_, "invalid jmethodID: %p", mid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700719 return NULL;
720 }
721 return m;
722 }
723
Elliott Hughesa2501992011-08-26 19:39:54 -0700724 /*
725 * Verify that "jobj" is a valid object, and that it's an object that JNI
726 * is allowed to know about. We allow NULL references.
727 *
728 * Switches to "running" mode before performing checks.
729 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700730 void CheckObject(jobject java_object)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700731 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700732 if (java_object == NULL) {
733 return;
734 }
735
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700736 Object* o = soa_.Decode<Object*>(java_object);
Elliott Hughes88c5c352012-03-15 18:49:48 -0700737 if (!Runtime::Current()->GetHeap()->IsHeapAddress(o)) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700738 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700739 // TODO: when we remove work_around_app_jni_bugs, this should be impossible.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700740 JniAbortF(function_name_, "native code passing in reference to invalid %s: %p",
741 ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700742 }
743 }
744
745 /*
746 * Verify that the "mode" argument passed to a primitive array Release
747 * function is one of the valid values.
748 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700749 void CheckReleaseMode(jint mode) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700750 if (mode != 0 && mode != JNI_COMMIT && mode != JNI_ABORT) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700751 JniAbortF(function_name_, "bad value for release mode: %d", mode);
Elliott Hughesa2501992011-08-26 19:39:54 -0700752 }
753 }
754
Ian Rogersb726dcb2012-09-05 08:57:23 -0700755 void CheckThread(int flags) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700756 Thread* self = Thread::Current();
757 if (self == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700758 JniAbortF(function_name_, "a thread (tid %d) is making JNI calls without being attached", GetTid());
Elliott Hughesa2501992011-08-26 19:39:54 -0700759 return;
760 }
761
762 // Get the *correct* JNIEnv by going through our TLS pointer.
763 JNIEnvExt* threadEnv = self->GetJniEnv();
764
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700765 // Verify that the current thread is (a) attached and (b) associated with
766 // this particular instance of JNIEnv.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700767 if (soa_.Env() != threadEnv) {
768 if (soa_.Vm()->work_around_app_jni_bugs) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700769 // If we're keeping broken code limping along, we need to suppress the abort...
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700770 LOG(ERROR) << "APP BUG DETECTED: thread " << *self << " using JNIEnv* from thread " << *soa_.Self();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700771 } else {
772 JniAbortF(function_name_, "thread %s using JNIEnv* from thread %s",
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700773 ToStr<Thread>(*self).c_str(), ToStr<Thread>(*soa_.Self()).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700774 return;
775 }
776 }
777
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700778 // Verify that, if this thread previously made a critical "get" call, we
779 // do the corresponding "release" call before we try anything else.
Elliott Hughesa2501992011-08-26 19:39:54 -0700780 switch (flags & kFlag_CritMask) {
781 case kFlag_CritOkay: // okay to call this method
782 break;
783 case kFlag_CritBad: // not okay to call
784 if (threadEnv->critical) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700785 JniAbortF(function_name_, "thread %s using JNI after critical get", ToStr<Thread>(*self).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700786 return;
787 }
788 break;
789 case kFlag_CritGet: // this is a "get" call
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700790 // Don't check here; we allow nested gets.
Elliott Hughesa2501992011-08-26 19:39:54 -0700791 threadEnv->critical++;
792 break;
793 case kFlag_CritRelease: // this is a "release" call
794 threadEnv->critical--;
795 if (threadEnv->critical < 0) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700796 JniAbortF(function_name_, "thread %s called too many critical releases", ToStr<Thread>(*self).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700797 return;
798 }
799 break;
800 default:
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800801 LOG(FATAL) << "Bad flags (internal error): " << flags;
Elliott Hughesa2501992011-08-26 19:39:54 -0700802 }
803
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700804 // Verify that, if an exception has been raised, the native code doesn't
805 // make any JNI calls other than the Exception* methods.
Elliott Hughesa2501992011-08-26 19:39:54 -0700806 if ((flags & kFlag_ExcepOkay) == 0 && self->IsExceptionPending()) {
Elliott Hughes30646832011-10-13 16:59:46 -0700807 std::string type(PrettyTypeOf(self->GetException()));
Elliott Hughes30646832011-10-13 16:59:46 -0700808 // TODO: write native code that doesn't require allocation for dumping an exception.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700809 // TODO: do we care any more? art always dumps pending exceptions on aborting threads.
Elliott Hughes30646832011-10-13 16:59:46 -0700810 if (type != "java.lang.OutOfMemoryError") {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700811 JniAbortF(function_name_, "JNI %s called with pending exception: %s",
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700812 function_name_, type.c_str(), jniGetStackTrace(soa_.Env()).c_str());
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700813 } else {
814 JniAbortF(function_name_, "JNI %s called with %s pending", function_name_, type.c_str());
Elliott Hughes30646832011-10-13 16:59:46 -0700815 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700816 return;
817 }
818 }
819
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700820 // Verifies that "bytes" points to valid Modified UTF-8 data.
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700821 void CheckUtfString(const char* bytes, bool nullable) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700822 if (bytes == NULL) {
823 if (!nullable) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700824 JniAbortF(function_name_, "non-nullable const char* was NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -0700825 return;
826 }
827 return;
828 }
829
830 const char* errorKind = NULL;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700831 uint8_t utf8 = CheckUtfBytes(bytes, &errorKind);
Elliott Hughesa2501992011-08-26 19:39:54 -0700832 if (errorKind != NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700833 JniAbortF(function_name_,
834 "input is not valid Modified UTF-8: illegal %s byte %#x\n"
835 " string: '%s'", errorKind, utf8, bytes);
Elliott Hughesa2501992011-08-26 19:39:54 -0700836 return;
837 }
838 }
839
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700840 static uint8_t CheckUtfBytes(const char* bytes, const char** errorKind) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700841 while (*bytes != '\0') {
842 uint8_t utf8 = *(bytes++);
843 // Switch on the high four bits.
844 switch (utf8 >> 4) {
845 case 0x00:
846 case 0x01:
847 case 0x02:
848 case 0x03:
849 case 0x04:
850 case 0x05:
851 case 0x06:
852 case 0x07:
853 // Bit pattern 0xxx. No need for any extra bytes.
854 break;
855 case 0x08:
856 case 0x09:
857 case 0x0a:
858 case 0x0b:
859 case 0x0f:
860 /*
861 * Bit pattern 10xx or 1111, which are illegal start bytes.
862 * Note: 1111 is valid for normal UTF-8, but not the
Elliott Hughes78090d12011-10-07 14:31:47 -0700863 * Modified UTF-8 used here.
Elliott Hughesa2501992011-08-26 19:39:54 -0700864 */
865 *errorKind = "start";
866 return utf8;
867 case 0x0e:
868 // Bit pattern 1110, so there are two additional bytes.
869 utf8 = *(bytes++);
870 if ((utf8 & 0xc0) != 0x80) {
871 *errorKind = "continuation";
872 return utf8;
873 }
874 // Fall through to take care of the final byte.
875 case 0x0c:
876 case 0x0d:
877 // Bit pattern 110x, so there is one additional byte.
878 utf8 = *(bytes++);
879 if ((utf8 & 0xc0) != 0x80) {
880 *errorKind = "continuation";
881 return utf8;
882 }
883 break;
884 }
885 }
886 return 0;
887 }
888
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700889 const ScopedObjectAccess soa_;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700890 const char* function_name_;
891 int flags_;
892 bool has_method_;
Elliott Hughes92cb4982011-12-16 16:57:28 -0800893 int indent_;
Elliott Hughesa2501992011-08-26 19:39:54 -0700894
895 DISALLOW_COPY_AND_ASSIGN(ScopedCheck);
896};
897
898#define CHECK_JNI_ENTRY(flags, types, args...) \
899 ScopedCheck sc(env, flags, __FUNCTION__); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700900 sc.Check(true, types, ##args)
Elliott Hughesa2501992011-08-26 19:39:54 -0700901
902#define CHECK_JNI_EXIT(type, exp) ({ \
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700903 typeof(exp) _rc = (exp); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700904 sc.Check(false, type, _rc); \
Elliott Hughesa2501992011-08-26 19:39:54 -0700905 _rc; })
906#define CHECK_JNI_EXIT_VOID() \
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700907 sc.Check(false, "V")
Elliott Hughesa2501992011-08-26 19:39:54 -0700908
909/*
910 * ===========================================================================
911 * Guarded arrays
912 * ===========================================================================
913 */
914
915#define kGuardLen 512 /* must be multiple of 2 */
916#define kGuardPattern 0xd5e3 /* uncommon values; d5e3d5e3 invalid addr */
917#define kGuardMagic 0xffd5aa96
918
919/* this gets tucked in at the start of the buffer; struct size must be even */
920struct GuardedCopy {
921 uint32_t magic;
922 uLong adler;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700923 size_t original_length;
924 const void* original_ptr;
Elliott Hughesa2501992011-08-26 19:39:54 -0700925
926 /* find the GuardedCopy given the pointer into the "live" data */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700927 static inline const GuardedCopy* FromData(const void* dataBuf) {
928 return reinterpret_cast<const GuardedCopy*>(ActualBuffer(dataBuf));
Elliott Hughesa2501992011-08-26 19:39:54 -0700929 }
930
931 /*
932 * Create an over-sized buffer to hold the contents of "buf". Copy it in,
933 * filling in the area around it with guard data.
934 *
935 * We use a 16-bit pattern to make a rogue memset less likely to elude us.
936 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700937 static void* Create(const void* buf, size_t len, bool modOkay) {
938 size_t newLen = ActualLength(len);
939 uint8_t* newBuf = DebugAlloc(newLen);
Elliott Hughesa2501992011-08-26 19:39:54 -0700940
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700941 // Fill it in with a pattern.
Elliott Hughesba8eee12012-01-24 20:25:24 -0800942 uint16_t* pat = reinterpret_cast<uint16_t*>(newBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -0700943 for (size_t i = 0; i < newLen / 2; i++) {
944 *pat++ = kGuardPattern;
945 }
946
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700947 // Copy the data in; note "len" could be zero.
Elliott Hughesa2501992011-08-26 19:39:54 -0700948 memcpy(newBuf + kGuardLen / 2, buf, len);
949
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700950 // If modification is not expected, grab a checksum.
Elliott Hughesa2501992011-08-26 19:39:54 -0700951 uLong adler = 0;
952 if (!modOkay) {
953 adler = adler32(0L, Z_NULL, 0);
Elliott Hughesba8eee12012-01-24 20:25:24 -0800954 adler = adler32(adler, reinterpret_cast<const Bytef*>(buf), len);
955 *reinterpret_cast<uLong*>(newBuf) = adler;
Elliott Hughesa2501992011-08-26 19:39:54 -0700956 }
957
958 GuardedCopy* pExtra = reinterpret_cast<GuardedCopy*>(newBuf);
959 pExtra->magic = kGuardMagic;
960 pExtra->adler = adler;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700961 pExtra->original_ptr = buf;
962 pExtra->original_length = len;
Elliott Hughesa2501992011-08-26 19:39:54 -0700963
964 return newBuf + kGuardLen / 2;
965 }
966
967 /*
968 * Free up the guard buffer, scrub it, and return the original pointer.
969 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700970 static void* Destroy(void* dataBuf) {
971 const GuardedCopy* pExtra = GuardedCopy::FromData(dataBuf);
Elliott Hughesba8eee12012-01-24 20:25:24 -0800972 void* original_ptr = const_cast<void*>(pExtra->original_ptr);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700973 size_t len = pExtra->original_length;
974 DebugFree(dataBuf, len);
975 return original_ptr;
Elliott Hughesa2501992011-08-26 19:39:54 -0700976 }
977
978 /*
979 * Verify the guard area and, if "modOkay" is false, that the data itself
980 * has not been altered.
981 *
982 * The caller has already checked that "dataBuf" is non-NULL.
983 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700984 static void Check(const char* functionName, const void* dataBuf, bool modOkay) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700985 static const uint32_t kMagicCmp = kGuardMagic;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700986 const uint8_t* fullBuf = ActualBuffer(dataBuf);
987 const GuardedCopy* pExtra = GuardedCopy::FromData(dataBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -0700988
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700989 // Before we do anything with "pExtra", check the magic number. We
990 // do the check with memcmp rather than "==" in case the pointer is
991 // unaligned. If it points to completely bogus memory we're going
992 // to crash, but there's no easy way around that.
Elliott Hughesa2501992011-08-26 19:39:54 -0700993 if (memcmp(&pExtra->magic, &kMagicCmp, 4) != 0) {
994 uint8_t buf[4];
995 memcpy(buf, &pExtra->magic, 4);
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700996 JniAbortF(functionName,
997 "guard magic does not match (found 0x%02x%02x%02x%02x) -- incorrect data pointer %p?",
998 buf[3], buf[2], buf[1], buf[0], dataBuf); // Assumes little-endian.
Elliott Hughesa2501992011-08-26 19:39:54 -0700999 }
1000
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001001 size_t len = pExtra->original_length;
Elliott Hughesa2501992011-08-26 19:39:54 -07001002
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001003 // Check bottom half of guard; skip over optional checksum storage.
Elliott Hughesba8eee12012-01-24 20:25:24 -08001004 const uint16_t* pat = reinterpret_cast<const uint16_t*>(fullBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -07001005 for (size_t i = sizeof(GuardedCopy) / 2; i < (kGuardLen / 2 - sizeof(GuardedCopy)) / 2; i++) {
1006 if (pat[i] != kGuardPattern) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001007 JniAbortF(functionName, "guard pattern(1) disturbed at %p +%d", fullBuf, i*2);
Elliott Hughesa2501992011-08-26 19:39:54 -07001008 }
1009 }
1010
1011 int offset = kGuardLen / 2 + len;
1012 if (offset & 0x01) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001013 // Odd byte; expected value depends on endian.
Elliott Hughesa2501992011-08-26 19:39:54 -07001014 const uint16_t patSample = kGuardPattern;
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001015 uint8_t expected_byte = reinterpret_cast<const uint8_t*>(&patSample)[1];
1016 if (fullBuf[offset] != expected_byte) {
1017 JniAbortF(functionName, "guard pattern disturbed in odd byte after %p +%d 0x%02x 0x%02x",
1018 fullBuf, offset, fullBuf[offset], expected_byte);
Elliott Hughesa2501992011-08-26 19:39:54 -07001019 }
1020 offset++;
1021 }
1022
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001023 // Check top half of guard.
Elliott Hughesba8eee12012-01-24 20:25:24 -08001024 pat = reinterpret_cast<const uint16_t*>(fullBuf + offset);
Elliott Hughesa2501992011-08-26 19:39:54 -07001025 for (size_t i = 0; i < kGuardLen / 4; i++) {
1026 if (pat[i] != kGuardPattern) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001027 JniAbortF(functionName, "guard pattern(2) disturbed at %p +%d", fullBuf, offset + i*2);
Elliott Hughesa2501992011-08-26 19:39:54 -07001028 }
1029 }
1030
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001031 // If modification is not expected, verify checksum. Strictly speaking
1032 // this is wrong: if we told the client that we made a copy, there's no
1033 // reason they can't alter the buffer.
Elliott Hughesa2501992011-08-26 19:39:54 -07001034 if (!modOkay) {
1035 uLong adler = adler32(0L, Z_NULL, 0);
1036 adler = adler32(adler, (const Bytef*)dataBuf, len);
1037 if (pExtra->adler != adler) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001038 JniAbortF(functionName, "buffer modified (0x%08lx vs 0x%08lx) at address %p",
1039 pExtra->adler, adler, dataBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -07001040 }
1041 }
1042 }
1043
1044 private:
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001045 static uint8_t* DebugAlloc(size_t len) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001046 void* result = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
1047 if (result == MAP_FAILED) {
1048 PLOG(FATAL) << "GuardedCopy::create mmap(" << len << ") failed";
1049 }
1050 return reinterpret_cast<uint8_t*>(result);
1051 }
1052
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001053 static void DebugFree(void* dataBuf, size_t len) {
1054 uint8_t* fullBuf = ActualBuffer(dataBuf);
1055 size_t totalByteCount = ActualLength(len);
Elliott Hughesa2501992011-08-26 19:39:54 -07001056 // TODO: we could mprotect instead, and keep the allocation around for a while.
1057 // This would be even more expensive, but it might catch more errors.
1058 // if (mprotect(fullBuf, totalByteCount, PROT_NONE) != 0) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -07001059 // PLOG(WARNING) << "mprotect(PROT_NONE) failed";
Elliott Hughesa2501992011-08-26 19:39:54 -07001060 // }
1061 if (munmap(fullBuf, totalByteCount) != 0) {
Elliott Hughesba8eee12012-01-24 20:25:24 -08001062 PLOG(FATAL) << "munmap(" << reinterpret_cast<void*>(fullBuf) << ", " << totalByteCount << ") failed";
Elliott Hughesa2501992011-08-26 19:39:54 -07001063 }
1064 }
1065
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001066 static const uint8_t* ActualBuffer(const void* dataBuf) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001067 return reinterpret_cast<const uint8_t*>(dataBuf) - kGuardLen / 2;
1068 }
1069
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001070 static uint8_t* ActualBuffer(void* dataBuf) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001071 return reinterpret_cast<uint8_t*>(dataBuf) - kGuardLen / 2;
1072 }
1073
1074 // Underlying length of a user allocation of 'length' bytes.
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001075 static size_t ActualLength(size_t length) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001076 return (length + kGuardLen + 1) & ~0x01;
1077 }
1078};
1079
1080/*
1081 * Create a guarded copy of a primitive array. Modifications to the copied
1082 * data are allowed. Returns a pointer to the copied data.
1083 */
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001084static void* CreateGuardedPACopy(JNIEnv* env, const jarray java_array, jboolean* isCopy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001085 ScopedObjectAccess soa(env);
Elliott Hughesa2501992011-08-26 19:39:54 -07001086
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001087 Array* a = soa.Decode<Array*>(java_array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001088 size_t component_size = a->GetClass()->GetComponentSize();
1089 size_t byte_count = a->GetLength() * component_size;
1090 void* result = GuardedCopy::Create(a->GetRawData(component_size), byte_count, true);
Elliott Hughesa2501992011-08-26 19:39:54 -07001091 if (isCopy != NULL) {
1092 *isCopy = JNI_TRUE;
1093 }
1094 return result;
1095}
1096
1097/*
1098 * Perform the array "release" operation, which may or may not copy data
Elliott Hughes81ff3182012-03-23 20:35:56 -07001099 * back into the managed heap, and may or may not release the underlying storage.
Elliott Hughesa2501992011-08-26 19:39:54 -07001100 */
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001101static void ReleaseGuardedPACopy(JNIEnv* env, jarray java_array, void* dataBuf, int mode) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001102 if (reinterpret_cast<uintptr_t>(dataBuf) == kNoCopyMagic) {
1103 return;
1104 }
1105
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001106 ScopedObjectAccess soa(env);
1107 Array* a = soa.Decode<Array*>(java_array);
Elliott Hughesa2501992011-08-26 19:39:54 -07001108
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001109 GuardedCopy::Check(__FUNCTION__, dataBuf, true);
Elliott Hughesa2501992011-08-26 19:39:54 -07001110
1111 if (mode != JNI_ABORT) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001112 size_t len = GuardedCopy::FromData(dataBuf)->original_length;
Ian Rogersa15e67d2012-02-28 13:51:55 -08001113 memcpy(a->GetRawData(a->GetClass()->GetComponentSize()), dataBuf, len);
Elliott Hughesa2501992011-08-26 19:39:54 -07001114 }
1115 if (mode != JNI_COMMIT) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001116 GuardedCopy::Destroy(dataBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -07001117 }
1118}
1119
1120/*
1121 * ===========================================================================
1122 * JNI functions
1123 * ===========================================================================
1124 */
1125
1126class CheckJNI {
1127 public:
1128 static jint GetVersion(JNIEnv* env) {
1129 CHECK_JNI_ENTRY(kFlag_Default, "E", env);
1130 return CHECK_JNI_EXIT("I", baseEnv(env)->GetVersion(env));
1131 }
1132
1133 static jclass DefineClass(JNIEnv* env, const char* name, jobject loader, const jbyte* buf, jsize bufLen) {
1134 CHECK_JNI_ENTRY(kFlag_Default, "EuLpz", env, name, loader, buf, bufLen);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001135 sc.CheckClassName(name);
Elliott Hughesa2501992011-08-26 19:39:54 -07001136 return CHECK_JNI_EXIT("c", baseEnv(env)->DefineClass(env, name, loader, buf, bufLen));
1137 }
1138
1139 static jclass FindClass(JNIEnv* env, const char* name) {
1140 CHECK_JNI_ENTRY(kFlag_Default, "Eu", env, name);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001141 sc.CheckClassName(name);
Elliott Hughesa2501992011-08-26 19:39:54 -07001142 return CHECK_JNI_EXIT("c", baseEnv(env)->FindClass(env, name));
1143 }
1144
Elliott Hughese84278b2012-03-22 10:06:53 -07001145 static jclass GetSuperclass(JNIEnv* env, jclass c) {
1146 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1147 return CHECK_JNI_EXIT("c", baseEnv(env)->GetSuperclass(env, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001148 }
1149
Elliott Hughese84278b2012-03-22 10:06:53 -07001150 static jboolean IsAssignableFrom(JNIEnv* env, jclass c1, jclass c2) {
1151 CHECK_JNI_ENTRY(kFlag_Default, "Ecc", env, c1, c2);
1152 return CHECK_JNI_EXIT("b", baseEnv(env)->IsAssignableFrom(env, c1, c2));
Elliott Hughesa2501992011-08-26 19:39:54 -07001153 }
1154
1155 static jmethodID FromReflectedMethod(JNIEnv* env, jobject method) {
1156 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, method);
1157 // TODO: check that 'field' is a java.lang.reflect.Method.
1158 return CHECK_JNI_EXIT("m", baseEnv(env)->FromReflectedMethod(env, method));
1159 }
1160
1161 static jfieldID FromReflectedField(JNIEnv* env, jobject field) {
1162 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, field);
1163 // TODO: check that 'field' is a java.lang.reflect.Field.
1164 return CHECK_JNI_EXIT("f", baseEnv(env)->FromReflectedField(env, field));
1165 }
1166
1167 static jobject ToReflectedMethod(JNIEnv* env, jclass cls, jmethodID mid, jboolean isStatic) {
1168 CHECK_JNI_ENTRY(kFlag_Default, "Ecmb", env, cls, mid, isStatic);
1169 return CHECK_JNI_EXIT("L", baseEnv(env)->ToReflectedMethod(env, cls, mid, isStatic));
1170 }
1171
1172 static jobject ToReflectedField(JNIEnv* env, jclass cls, jfieldID fid, jboolean isStatic) {
1173 CHECK_JNI_ENTRY(kFlag_Default, "Ecfb", env, cls, fid, isStatic);
1174 return CHECK_JNI_EXIT("L", baseEnv(env)->ToReflectedField(env, cls, fid, isStatic));
1175 }
1176
1177 static jint Throw(JNIEnv* env, jthrowable obj) {
1178 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1179 // TODO: check that 'obj' is a java.lang.Throwable.
1180 return CHECK_JNI_EXIT("I", baseEnv(env)->Throw(env, obj));
1181 }
1182
Elliott Hughese84278b2012-03-22 10:06:53 -07001183 static jint ThrowNew(JNIEnv* env, jclass c, const char* message) {
1184 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Ecu", env, c, message);
1185 return CHECK_JNI_EXIT("I", baseEnv(env)->ThrowNew(env, c, message));
Elliott Hughesa2501992011-08-26 19:39:54 -07001186 }
1187
1188 static jthrowable ExceptionOccurred(JNIEnv* env) {
1189 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1190 return CHECK_JNI_EXIT("L", baseEnv(env)->ExceptionOccurred(env));
1191 }
1192
1193 static void ExceptionDescribe(JNIEnv* env) {
1194 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1195 baseEnv(env)->ExceptionDescribe(env);
1196 CHECK_JNI_EXIT_VOID();
1197 }
1198
1199 static void ExceptionClear(JNIEnv* env) {
1200 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1201 baseEnv(env)->ExceptionClear(env);
1202 CHECK_JNI_EXIT_VOID();
1203 }
1204
1205 static void FatalError(JNIEnv* env, const char* msg) {
1206 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Eu", env, msg);
1207 baseEnv(env)->FatalError(env, msg);
1208 CHECK_JNI_EXIT_VOID();
1209 }
1210
1211 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
1212 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EI", env, capacity);
1213 return CHECK_JNI_EXIT("I", baseEnv(env)->PushLocalFrame(env, capacity));
1214 }
1215
1216 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
1217 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, res);
1218 return CHECK_JNI_EXIT("L", baseEnv(env)->PopLocalFrame(env, res));
1219 }
1220
1221 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
1222 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1223 return CHECK_JNI_EXIT("L", baseEnv(env)->NewGlobalRef(env, obj));
1224 }
1225
1226 static jobject NewLocalRef(JNIEnv* env, jobject ref) {
1227 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, ref);
1228 return CHECK_JNI_EXIT("L", baseEnv(env)->NewLocalRef(env, ref));
1229 }
1230
1231 static void DeleteGlobalRef(JNIEnv* env, jobject globalRef) {
1232 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, globalRef);
1233 if (globalRef != NULL && GetIndirectRefKind(globalRef) != kGlobal) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001234 JniAbortF(__FUNCTION__, "DeleteGlobalRef on %s: %p",
1235 ToStr<IndirectRefKind>(GetIndirectRefKind(globalRef)).c_str(), globalRef);
Elliott Hughesa2501992011-08-26 19:39:54 -07001236 } else {
1237 baseEnv(env)->DeleteGlobalRef(env, globalRef);
1238 CHECK_JNI_EXIT_VOID();
1239 }
1240 }
1241
1242 static void DeleteWeakGlobalRef(JNIEnv* env, jweak weakGlobalRef) {
1243 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, weakGlobalRef);
1244 if (weakGlobalRef != NULL && GetIndirectRefKind(weakGlobalRef) != kWeakGlobal) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001245 JniAbortF(__FUNCTION__, "DeleteWeakGlobalRef on %s: %p",
1246 ToStr<IndirectRefKind>(GetIndirectRefKind(weakGlobalRef)).c_str(), weakGlobalRef);
Elliott Hughesa2501992011-08-26 19:39:54 -07001247 } else {
1248 baseEnv(env)->DeleteWeakGlobalRef(env, weakGlobalRef);
1249 CHECK_JNI_EXIT_VOID();
1250 }
1251 }
1252
1253 static void DeleteLocalRef(JNIEnv* env, jobject localRef) {
1254 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, localRef);
Ian Rogers959f8ed2012-02-07 16:33:37 -08001255 if (localRef != NULL && GetIndirectRefKind(localRef) != kLocal && !IsSirtLocalRef(env, localRef)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001256 JniAbortF(__FUNCTION__, "DeleteLocalRef on %s: %p",
1257 ToStr<IndirectRefKind>(GetIndirectRefKind(localRef)).c_str(), localRef);
Elliott Hughesa2501992011-08-26 19:39:54 -07001258 } else {
1259 baseEnv(env)->DeleteLocalRef(env, localRef);
1260 CHECK_JNI_EXIT_VOID();
1261 }
1262 }
1263
1264 static jint EnsureLocalCapacity(JNIEnv *env, jint capacity) {
1265 CHECK_JNI_ENTRY(kFlag_Default, "EI", env, capacity);
1266 return CHECK_JNI_EXIT("I", baseEnv(env)->EnsureLocalCapacity(env, capacity));
1267 }
1268
1269 static jboolean IsSameObject(JNIEnv* env, jobject ref1, jobject ref2) {
1270 CHECK_JNI_ENTRY(kFlag_Default, "ELL", env, ref1, ref2);
1271 return CHECK_JNI_EXIT("b", baseEnv(env)->IsSameObject(env, ref1, ref2));
1272 }
1273
Elliott Hughese84278b2012-03-22 10:06:53 -07001274 static jobject AllocObject(JNIEnv* env, jclass c) {
1275 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1276 return CHECK_JNI_EXIT("L", baseEnv(env)->AllocObject(env, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001277 }
1278
Elliott Hughese84278b2012-03-22 10:06:53 -07001279 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
1280 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
Elliott Hughesa2501992011-08-26 19:39:54 -07001281 va_list args;
1282 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -07001283 jobject result = baseEnv(env)->NewObjectV(env, c, mid, args);
Elliott Hughesa2501992011-08-26 19:39:54 -07001284 va_end(args);
1285 return CHECK_JNI_EXIT("L", result);
1286 }
1287
Elliott Hughese84278b2012-03-22 10:06:53 -07001288 static jobject NewObjectV(JNIEnv* env, jclass c, jmethodID mid, va_list args) {
1289 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
1290 return CHECK_JNI_EXIT("L", baseEnv(env)->NewObjectV(env, c, mid, args));
Elliott Hughesa2501992011-08-26 19:39:54 -07001291 }
1292
Elliott Hughese84278b2012-03-22 10:06:53 -07001293 static jobject NewObjectA(JNIEnv* env, jclass c, jmethodID mid, jvalue* args) {
1294 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
1295 return CHECK_JNI_EXIT("L", baseEnv(env)->NewObjectA(env, c, mid, args));
Elliott Hughesa2501992011-08-26 19:39:54 -07001296 }
1297
1298 static jclass GetObjectClass(JNIEnv* env, jobject obj) {
1299 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1300 return CHECK_JNI_EXIT("c", baseEnv(env)->GetObjectClass(env, obj));
1301 }
1302
Elliott Hughese84278b2012-03-22 10:06:53 -07001303 static jboolean IsInstanceOf(JNIEnv* env, jobject obj, jclass c) {
1304 CHECK_JNI_ENTRY(kFlag_Default, "ELc", env, obj, c);
1305 return CHECK_JNI_EXIT("b", baseEnv(env)->IsInstanceOf(env, obj, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001306 }
1307
Elliott Hughese84278b2012-03-22 10:06:53 -07001308 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1309 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1310 return CHECK_JNI_EXIT("m", baseEnv(env)->GetMethodID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001311 }
1312
Elliott Hughese84278b2012-03-22 10:06:53 -07001313 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1314 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1315 return CHECK_JNI_EXIT("f", baseEnv(env)->GetFieldID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001316 }
1317
Elliott Hughese84278b2012-03-22 10:06:53 -07001318 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1319 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1320 return CHECK_JNI_EXIT("m", baseEnv(env)->GetStaticMethodID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001321 }
1322
Elliott Hughese84278b2012-03-22 10:06:53 -07001323 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1324 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1325 return CHECK_JNI_EXIT("f", baseEnv(env)->GetStaticFieldID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001326 }
1327
1328#define FIELD_ACCESSORS(_ctype, _jname, _type) \
Elliott Hughese84278b2012-03-22 10:06:53 -07001329 static _ctype GetStatic##_jname##Field(JNIEnv* env, jclass c, jfieldID fid) { \
1330 CHECK_JNI_ENTRY(kFlag_Default, "Ecf", env, c, fid); \
1331 sc.CheckStaticFieldID(c, fid); \
1332 return CHECK_JNI_EXIT(_type, baseEnv(env)->GetStatic##_jname##Field(env, c, fid)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001333 } \
1334 static _ctype Get##_jname##Field(JNIEnv* env, jobject obj, jfieldID fid) { \
1335 CHECK_JNI_ENTRY(kFlag_Default, "ELf", env, obj, fid); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001336 sc.CheckInstanceFieldID(obj, fid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001337 return CHECK_JNI_EXIT(_type, baseEnv(env)->Get##_jname##Field(env, obj, fid)); \
1338 } \
Elliott Hughese84278b2012-03-22 10:06:53 -07001339 static void SetStatic##_jname##Field(JNIEnv* env, jclass c, jfieldID fid, _ctype value) { \
1340 CHECK_JNI_ENTRY(kFlag_Default, "Ecf" _type, env, c, fid, value); \
1341 sc.CheckStaticFieldID(c, fid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001342 /* "value" arg only used when type == ref */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001343 sc.CheckFieldType((jobject)(uint32_t)value, fid, _type[0], true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001344 baseEnv(env)->SetStatic##_jname##Field(env, c, fid, value); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001345 CHECK_JNI_EXIT_VOID(); \
1346 } \
1347 static void Set##_jname##Field(JNIEnv* env, jobject obj, jfieldID fid, _ctype value) { \
1348 CHECK_JNI_ENTRY(kFlag_Default, "ELf" _type, env, obj, fid, value); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001349 sc.CheckInstanceFieldID(obj, fid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001350 /* "value" arg only used when type == ref */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001351 sc.CheckFieldType((jobject)(uint32_t) value, fid, _type[0], false); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001352 baseEnv(env)->Set##_jname##Field(env, obj, fid, value); \
1353 CHECK_JNI_EXIT_VOID(); \
1354 }
1355
1356FIELD_ACCESSORS(jobject, Object, "L");
1357FIELD_ACCESSORS(jboolean, Boolean, "Z");
1358FIELD_ACCESSORS(jbyte, Byte, "B");
1359FIELD_ACCESSORS(jchar, Char, "C");
1360FIELD_ACCESSORS(jshort, Short, "S");
1361FIELD_ACCESSORS(jint, Int, "I");
1362FIELD_ACCESSORS(jlong, Long, "J");
1363FIELD_ACCESSORS(jfloat, Float, "F");
1364FIELD_ACCESSORS(jdouble, Double, "D");
1365
1366#define CALL(_ctype, _jname, _retdecl, _retasgn, _retok, _retsig) \
1367 /* Virtual... */ \
1368 static _ctype Call##_jname##Method(JNIEnv* env, jobject obj, \
1369 jmethodID mid, ...) \
1370 { \
1371 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001372 sc.CheckSig(mid, _retsig, false); \
1373 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001374 _retdecl; \
1375 va_list args; \
1376 va_start(args, mid); \
Elliott Hughesba8eee12012-01-24 20:25:24 -08001377 _retasgn(baseEnv(env)->Call##_jname##MethodV(env, obj, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001378 va_end(args); \
1379 _retok; \
1380 } \
1381 static _ctype Call##_jname##MethodV(JNIEnv* env, jobject obj, \
1382 jmethodID mid, va_list args) \
1383 { \
1384 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001385 sc.CheckSig(mid, _retsig, false); \
1386 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001387 _retdecl; \
Elliott Hughesba8eee12012-01-24 20:25:24 -08001388 _retasgn(baseEnv(env)->Call##_jname##MethodV(env, obj, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001389 _retok; \
1390 } \
1391 static _ctype Call##_jname##MethodA(JNIEnv* env, jobject obj, \
1392 jmethodID mid, jvalue* args) \
1393 { \
1394 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001395 sc.CheckSig(mid, _retsig, false); \
1396 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001397 _retdecl; \
Elliott Hughesba8eee12012-01-24 20:25:24 -08001398 _retasgn(baseEnv(env)->Call##_jname##MethodA(env, obj, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001399 _retok; \
1400 } \
1401 /* Non-virtual... */ \
1402 static _ctype CallNonvirtual##_jname##Method(JNIEnv* env, \
Elliott Hughese84278b2012-03-22 10:06:53 -07001403 jobject obj, jclass c, jmethodID mid, ...) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001404 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001405 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001406 sc.CheckSig(mid, _retsig, false); \
1407 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001408 _retdecl; \
1409 va_list args; \
1410 va_start(args, mid); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001411 _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodV(env, obj, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001412 va_end(args); \
1413 _retok; \
1414 } \
1415 static _ctype CallNonvirtual##_jname##MethodV(JNIEnv* env, \
Elliott Hughese84278b2012-03-22 10:06:53 -07001416 jobject obj, jclass c, jmethodID mid, va_list args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001417 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001418 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001419 sc.CheckSig(mid, _retsig, false); \
1420 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001421 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001422 _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodV(env, obj, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001423 _retok; \
1424 } \
1425 static _ctype CallNonvirtual##_jname##MethodA(JNIEnv* env, \
Elliott Hughese84278b2012-03-22 10:06:53 -07001426 jobject obj, jclass c, jmethodID mid, jvalue* args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001427 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001428 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001429 sc.CheckSig(mid, _retsig, false); \
1430 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001431 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001432 _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodA(env, obj, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001433 _retok; \
1434 } \
1435 /* Static... */ \
Elliott Hughese84278b2012-03-22 10:06:53 -07001436 static _ctype CallStatic##_jname##Method(JNIEnv* env, jclass c, jmethodID mid, ...) \
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; \
1442 va_list args; \
1443 va_start(args, mid); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001444 _retasgn(baseEnv(env)->CallStatic##_jname##MethodV(env, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001445 va_end(args); \
1446 _retok; \
1447 } \
Elliott Hughese84278b2012-03-22 10:06:53 -07001448 static _ctype CallStatic##_jname##MethodV(JNIEnv* env, jclass c, jmethodID mid, va_list args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001449 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001450 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001451 sc.CheckSig(mid, _retsig, true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001452 sc.CheckStaticMethod(c, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001453 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001454 _retasgn(baseEnv(env)->CallStatic##_jname##MethodV(env, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001455 _retok; \
1456 } \
Elliott Hughese84278b2012-03-22 10:06:53 -07001457 static _ctype CallStatic##_jname##MethodA(JNIEnv* env, jclass c, jmethodID mid, jvalue* args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001458 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001459 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001460 sc.CheckSig(mid, _retsig, true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001461 sc.CheckStaticMethod(c, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001462 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001463 _retasgn(baseEnv(env)->CallStatic##_jname##MethodA(env, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001464 _retok; \
1465 }
1466
1467#define NON_VOID_RETURN(_retsig, _ctype) return CHECK_JNI_EXIT(_retsig, (_ctype) result)
1468#define VOID_RETURN CHECK_JNI_EXIT_VOID()
1469
Elliott Hughesba8eee12012-01-24 20:25:24 -08001470CALL(jobject, Object, Object* result, result = reinterpret_cast<Object*>, NON_VOID_RETURN("L", jobject), "L");
1471CALL(jboolean, Boolean, jboolean result, result =, NON_VOID_RETURN("Z", jboolean), "Z");
1472CALL(jbyte, Byte, jbyte result, result =, NON_VOID_RETURN("B", jbyte), "B");
1473CALL(jchar, Char, jchar result, result =, NON_VOID_RETURN("C", jchar), "C");
1474CALL(jshort, Short, jshort result, result =, NON_VOID_RETURN("S", jshort), "S");
1475CALL(jint, Int, jint result, result =, NON_VOID_RETURN("I", jint), "I");
1476CALL(jlong, Long, jlong result, result =, NON_VOID_RETURN("J", jlong), "J");
1477CALL(jfloat, Float, jfloat result, result =, NON_VOID_RETURN("F", jfloat), "F");
1478CALL(jdouble, Double, jdouble result, result =, NON_VOID_RETURN("D", jdouble), "D");
Elliott Hughesa2501992011-08-26 19:39:54 -07001479CALL(void, Void, , , VOID_RETURN, "V");
1480
1481 static jstring NewString(JNIEnv* env, const jchar* unicodeChars, jsize len) {
1482 CHECK_JNI_ENTRY(kFlag_Default, "Epz", env, unicodeChars, len);
1483 return CHECK_JNI_EXIT("s", baseEnv(env)->NewString(env, unicodeChars, len));
1484 }
1485
1486 static jsize GetStringLength(JNIEnv* env, jstring string) {
1487 CHECK_JNI_ENTRY(kFlag_CritOkay, "Es", env, string);
1488 return CHECK_JNI_EXIT("I", baseEnv(env)->GetStringLength(env, string));
1489 }
1490
1491 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* isCopy) {
1492 CHECK_JNI_ENTRY(kFlag_CritOkay, "Esp", env, java_string, isCopy);
1493 const jchar* result = baseEnv(env)->GetStringChars(env, java_string, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001494 if (sc.ForceCopy() && result != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001495 String* s = sc.soa().Decode<String*>(java_string);
Elliott Hughesa2501992011-08-26 19:39:54 -07001496 int byteCount = s->GetLength() * 2;
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001497 result = (const jchar*) GuardedCopy::Create(result, byteCount, false);
Elliott Hughesa2501992011-08-26 19:39:54 -07001498 if (isCopy != NULL) {
1499 *isCopy = JNI_TRUE;
1500 }
1501 }
1502 return CHECK_JNI_EXIT("p", result);
1503 }
1504
1505 static void ReleaseStringChars(JNIEnv* env, jstring string, const jchar* chars) {
1506 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "Esp", env, string, chars);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001507 sc.CheckNonNull(chars);
1508 if (sc.ForceCopy()) {
1509 GuardedCopy::Check(__FUNCTION__, chars, false);
Elliott Hughesba8eee12012-01-24 20:25:24 -08001510 chars = reinterpret_cast<const jchar*>(GuardedCopy::Destroy(const_cast<jchar*>(chars)));
Elliott Hughesa2501992011-08-26 19:39:54 -07001511 }
1512 baseEnv(env)->ReleaseStringChars(env, string, chars);
1513 CHECK_JNI_EXIT_VOID();
1514 }
1515
1516 static jstring NewStringUTF(JNIEnv* env, const char* bytes) {
1517 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Eu", env, bytes); // TODO: show pointer and truncate string.
1518 return CHECK_JNI_EXIT("s", baseEnv(env)->NewStringUTF(env, bytes));
1519 }
1520
1521 static jsize GetStringUTFLength(JNIEnv* env, jstring string) {
1522 CHECK_JNI_ENTRY(kFlag_CritOkay, "Es", env, string);
1523 return CHECK_JNI_EXIT("I", baseEnv(env)->GetStringUTFLength(env, string));
1524 }
1525
1526 static const char* GetStringUTFChars(JNIEnv* env, jstring string, jboolean* isCopy) {
1527 CHECK_JNI_ENTRY(kFlag_CritOkay, "Esp", env, string, isCopy);
1528 const char* result = baseEnv(env)->GetStringUTFChars(env, string, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001529 if (sc.ForceCopy() && result != NULL) {
1530 result = (const char*) GuardedCopy::Create(result, strlen(result) + 1, false);
Elliott Hughesa2501992011-08-26 19:39:54 -07001531 if (isCopy != NULL) {
1532 *isCopy = JNI_TRUE;
1533 }
1534 }
1535 return CHECK_JNI_EXIT("u", result); // TODO: show pointer and truncate string.
1536 }
1537
1538 static void ReleaseStringUTFChars(JNIEnv* env, jstring string, const char* utf) {
1539 CHECK_JNI_ENTRY(kFlag_ExcepOkay | kFlag_Release, "Esu", env, string, utf); // TODO: show pointer and truncate string.
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001540 if (sc.ForceCopy()) {
1541 GuardedCopy::Check(__FUNCTION__, utf, false);
Elliott Hughesba8eee12012-01-24 20:25:24 -08001542 utf = reinterpret_cast<const char*>(GuardedCopy::Destroy(const_cast<char*>(utf)));
Elliott Hughesa2501992011-08-26 19:39:54 -07001543 }
1544 baseEnv(env)->ReleaseStringUTFChars(env, string, utf);
1545 CHECK_JNI_EXIT_VOID();
1546 }
1547
1548 static jsize GetArrayLength(JNIEnv* env, jarray array) {
1549 CHECK_JNI_ENTRY(kFlag_CritOkay, "Ea", env, array);
1550 return CHECK_JNI_EXIT("I", baseEnv(env)->GetArrayLength(env, array));
1551 }
1552
1553 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass elementClass, jobject initialElement) {
1554 CHECK_JNI_ENTRY(kFlag_Default, "EzcL", env, length, elementClass, initialElement);
1555 return CHECK_JNI_EXIT("a", baseEnv(env)->NewObjectArray(env, length, elementClass, initialElement));
1556 }
1557
1558 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index) {
1559 CHECK_JNI_ENTRY(kFlag_Default, "EaI", env, array, index);
1560 return CHECK_JNI_EXIT("L", baseEnv(env)->GetObjectArrayElement(env, array, index));
1561 }
1562
1563 static void SetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index, jobject value) {
1564 CHECK_JNI_ENTRY(kFlag_Default, "EaIL", env, array, index, value);
1565 baseEnv(env)->SetObjectArrayElement(env, array, index, value);
1566 CHECK_JNI_EXIT_VOID();
1567 }
1568
1569#define NEW_PRIMITIVE_ARRAY(_artype, _jname) \
1570 static _artype New##_jname##Array(JNIEnv* env, jsize length) { \
1571 CHECK_JNI_ENTRY(kFlag_Default, "Ez", env, length); \
1572 return CHECK_JNI_EXIT("a", baseEnv(env)->New##_jname##Array(env, length)); \
1573 }
1574NEW_PRIMITIVE_ARRAY(jbooleanArray, Boolean);
1575NEW_PRIMITIVE_ARRAY(jbyteArray, Byte);
1576NEW_PRIMITIVE_ARRAY(jcharArray, Char);
1577NEW_PRIMITIVE_ARRAY(jshortArray, Short);
1578NEW_PRIMITIVE_ARRAY(jintArray, Int);
1579NEW_PRIMITIVE_ARRAY(jlongArray, Long);
1580NEW_PRIMITIVE_ARRAY(jfloatArray, Float);
1581NEW_PRIMITIVE_ARRAY(jdoubleArray, Double);
1582
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001583struct ForceCopyGetChecker {
Elliott Hughesba8eee12012-01-24 20:25:24 -08001584 public:
Elliott Hughesa2501992011-08-26 19:39:54 -07001585 ForceCopyGetChecker(ScopedCheck& sc, jboolean* isCopy) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001586 force_copy = sc.ForceCopy();
1587 no_copy = 0;
1588 if (force_copy && isCopy != NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001589 // Capture this before the base call tramples on it.
Elliott Hughesba8eee12012-01-24 20:25:24 -08001590 no_copy = *reinterpret_cast<uint32_t*>(isCopy);
Elliott Hughesa2501992011-08-26 19:39:54 -07001591 }
1592 }
1593
1594 template<typename ResultT>
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001595 ResultT Check(JNIEnv* env, jarray array, jboolean* isCopy, ResultT result) {
1596 if (force_copy && result != NULL) {
1597 if (no_copy != kNoCopyMagic) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001598 result = reinterpret_cast<ResultT>(CreateGuardedPACopy(env, array, isCopy));
1599 }
1600 }
1601 return result;
1602 }
1603
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001604 uint32_t no_copy;
1605 bool force_copy;
Elliott Hughesa2501992011-08-26 19:39:54 -07001606};
1607
1608#define GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \
1609 static _ctype* Get##_jname##ArrayElements(JNIEnv* env, _ctype##Array array, jboolean* isCopy) { \
1610 CHECK_JNI_ENTRY(kFlag_Default, "Eap", env, array, isCopy); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001611 _ctype* result = ForceCopyGetChecker(sc, isCopy).Check(env, array, isCopy, baseEnv(env)->Get##_jname##ArrayElements(env, array, isCopy)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001612 return CHECK_JNI_EXIT("p", result); \
1613 }
1614
1615#define RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \
1616 static void Release##_jname##ArrayElements(JNIEnv* env, _ctype##Array array, _ctype* elems, jint mode) { \
1617 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "Eapr", env, array, elems, mode); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001618 sc.CheckNonNull(elems); \
1619 if (sc.ForceCopy()) { \
Elliott Hughesa2501992011-08-26 19:39:54 -07001620 ReleaseGuardedPACopy(env, array, elems, mode); \
1621 } \
1622 baseEnv(env)->Release##_jname##ArrayElements(env, array, elems, mode); \
1623 CHECK_JNI_EXIT_VOID(); \
1624 }
1625
1626#define GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \
1627 static void Get##_jname##ArrayRegion(JNIEnv* env, _ctype##Array array, jsize start, jsize len, _ctype* buf) { \
1628 CHECK_JNI_ENTRY(kFlag_Default, "EaIIp", env, array, start, len, buf); \
1629 baseEnv(env)->Get##_jname##ArrayRegion(env, array, start, len, buf); \
1630 CHECK_JNI_EXIT_VOID(); \
1631 }
1632
1633#define SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \
1634 static void Set##_jname##ArrayRegion(JNIEnv* env, _ctype##Array array, jsize start, jsize len, const _ctype* buf) { \
1635 CHECK_JNI_ENTRY(kFlag_Default, "EaIIp", env, array, start, len, buf); \
1636 baseEnv(env)->Set##_jname##ArrayRegion(env, array, start, len, buf); \
1637 CHECK_JNI_EXIT_VOID(); \
1638 }
1639
1640#define PRIMITIVE_ARRAY_FUNCTIONS(_ctype, _jname, _typechar) \
1641 GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \
1642 RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \
1643 GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname); \
1644 SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname);
1645
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001646// TODO: verify primitive array type matches call type.
Elliott Hughesa2501992011-08-26 19:39:54 -07001647PRIMITIVE_ARRAY_FUNCTIONS(jboolean, Boolean, 'Z');
1648PRIMITIVE_ARRAY_FUNCTIONS(jbyte, Byte, 'B');
1649PRIMITIVE_ARRAY_FUNCTIONS(jchar, Char, 'C');
1650PRIMITIVE_ARRAY_FUNCTIONS(jshort, Short, 'S');
1651PRIMITIVE_ARRAY_FUNCTIONS(jint, Int, 'I');
1652PRIMITIVE_ARRAY_FUNCTIONS(jlong, Long, 'J');
1653PRIMITIVE_ARRAY_FUNCTIONS(jfloat, Float, 'F');
1654PRIMITIVE_ARRAY_FUNCTIONS(jdouble, Double, 'D');
1655
Elliott Hughese84278b2012-03-22 10:06:53 -07001656 static jint RegisterNatives(JNIEnv* env, jclass c, const JNINativeMethod* methods, jint nMethods) {
1657 CHECK_JNI_ENTRY(kFlag_Default, "EcpI", env, c, methods, nMethods);
1658 return CHECK_JNI_EXIT("I", baseEnv(env)->RegisterNatives(env, c, methods, nMethods));
Elliott Hughesa2501992011-08-26 19:39:54 -07001659 }
1660
Elliott Hughese84278b2012-03-22 10:06:53 -07001661 static jint UnregisterNatives(JNIEnv* env, jclass c) {
1662 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1663 return CHECK_JNI_EXIT("I", baseEnv(env)->UnregisterNatives(env, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001664 }
1665
1666 static jint MonitorEnter(JNIEnv* env, jobject obj) {
1667 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
Elliott Hughesa92853e2012-02-07 16:09:27 -08001668 if (!sc.CheckInstance(ScopedCheck::kObject, obj)) {
1669 return JNI_ERR; // Only for jni_internal_test. Real code will have aborted already.
1670 }
Elliott Hughesa2501992011-08-26 19:39:54 -07001671 return CHECK_JNI_EXIT("I", baseEnv(env)->MonitorEnter(env, obj));
1672 }
1673
1674 static jint MonitorExit(JNIEnv* env, jobject obj) {
1675 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, obj);
Elliott Hughesa92853e2012-02-07 16:09:27 -08001676 if (!sc.CheckInstance(ScopedCheck::kObject, obj)) {
1677 return JNI_ERR; // Only for jni_internal_test. Real code will have aborted already.
1678 }
Elliott Hughesa2501992011-08-26 19:39:54 -07001679 return CHECK_JNI_EXIT("I", baseEnv(env)->MonitorExit(env, obj));
1680 }
1681
1682 static jint GetJavaVM(JNIEnv *env, JavaVM **vm) {
1683 CHECK_JNI_ENTRY(kFlag_Default, "Ep", env, vm);
1684 return CHECK_JNI_EXIT("I", baseEnv(env)->GetJavaVM(env, vm));
1685 }
1686
1687 static void GetStringRegion(JNIEnv* env, jstring str, jsize start, jsize len, jchar* buf) {
1688 CHECK_JNI_ENTRY(kFlag_CritOkay, "EsIIp", env, str, start, len, buf);
1689 baseEnv(env)->GetStringRegion(env, str, start, len, buf);
1690 CHECK_JNI_EXIT_VOID();
1691 }
1692
1693 static void GetStringUTFRegion(JNIEnv* env, jstring str, jsize start, jsize len, char* buf) {
1694 CHECK_JNI_ENTRY(kFlag_CritOkay, "EsIIp", env, str, start, len, buf);
1695 baseEnv(env)->GetStringUTFRegion(env, str, start, len, buf);
1696 CHECK_JNI_EXIT_VOID();
1697 }
1698
1699 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* isCopy) {
1700 CHECK_JNI_ENTRY(kFlag_CritGet, "Eap", env, array, isCopy);
1701 void* result = baseEnv(env)->GetPrimitiveArrayCritical(env, array, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001702 if (sc.ForceCopy() && result != NULL) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001703 result = CreateGuardedPACopy(env, array, isCopy);
1704 }
1705 return CHECK_JNI_EXIT("p", result);
1706 }
1707
1708 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* carray, jint mode) {
1709 CHECK_JNI_ENTRY(kFlag_CritRelease | kFlag_ExcepOkay, "Eapr", env, array, carray, mode);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001710 sc.CheckNonNull(carray);
1711 if (sc.ForceCopy()) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001712 ReleaseGuardedPACopy(env, array, carray, mode);
1713 }
1714 baseEnv(env)->ReleasePrimitiveArrayCritical(env, array, carray, mode);
1715 CHECK_JNI_EXIT_VOID();
1716 }
1717
1718 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* isCopy) {
1719 CHECK_JNI_ENTRY(kFlag_CritGet, "Esp", env, java_string, isCopy);
1720 const jchar* result = baseEnv(env)->GetStringCritical(env, java_string, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001721 if (sc.ForceCopy() && result != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001722 String* s = sc.soa().Decode<String*>(java_string);
Elliott Hughesa2501992011-08-26 19:39:54 -07001723 int byteCount = s->GetLength() * 2;
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001724 result = (const jchar*) GuardedCopy::Create(result, byteCount, false);
Elliott Hughesa2501992011-08-26 19:39:54 -07001725 if (isCopy != NULL) {
1726 *isCopy = JNI_TRUE;
1727 }
1728 }
1729 return CHECK_JNI_EXIT("p", result);
1730 }
1731
1732 static void ReleaseStringCritical(JNIEnv* env, jstring string, const jchar* carray) {
1733 CHECK_JNI_ENTRY(kFlag_CritRelease | kFlag_ExcepOkay, "Esp", env, string, carray);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001734 sc.CheckNonNull(carray);
1735 if (sc.ForceCopy()) {
1736 GuardedCopy::Check(__FUNCTION__, carray, false);
Elliott Hughesba8eee12012-01-24 20:25:24 -08001737 carray = reinterpret_cast<const jchar*>(GuardedCopy::Destroy(const_cast<jchar*>(carray)));
Elliott Hughesa2501992011-08-26 19:39:54 -07001738 }
1739 baseEnv(env)->ReleaseStringCritical(env, string, carray);
1740 CHECK_JNI_EXIT_VOID();
1741 }
1742
1743 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
1744 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1745 return CHECK_JNI_EXIT("L", baseEnv(env)->NewWeakGlobalRef(env, obj));
1746 }
1747
1748 static jboolean ExceptionCheck(JNIEnv* env) {
1749 CHECK_JNI_ENTRY(kFlag_CritOkay | kFlag_ExcepOkay, "E", env);
1750 return CHECK_JNI_EXIT("b", baseEnv(env)->ExceptionCheck(env));
1751 }
1752
1753 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject obj) {
1754 // Note: we use "Ep" rather than "EL" because this is the one JNI function
1755 // that it's okay to pass an invalid reference to.
1756 CHECK_JNI_ENTRY(kFlag_Default, "Ep", env, obj);
1757 // TODO: proper decoding of jobjectRefType!
1758 return CHECK_JNI_EXIT("I", baseEnv(env)->GetObjectRefType(env, obj));
1759 }
1760
1761 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
1762 CHECK_JNI_ENTRY(kFlag_Default, "EpJ", env, address, capacity);
1763 if (address == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001764 JniAbortF(__FUNCTION__, "non-nullable address is NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -07001765 }
1766 if (capacity <= 0) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001767 JniAbortF(__FUNCTION__, "capacity must be greater than 0: %d", capacity);
Elliott Hughesa2501992011-08-26 19:39:54 -07001768 }
1769 return CHECK_JNI_EXIT("L", baseEnv(env)->NewDirectByteBuffer(env, address, capacity));
1770 }
1771
1772 static void* GetDirectBufferAddress(JNIEnv* env, jobject buf) {
1773 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, buf);
1774 // TODO: check that 'buf' is a java.nio.Buffer.
1775 return CHECK_JNI_EXIT("p", baseEnv(env)->GetDirectBufferAddress(env, buf));
1776 }
1777
1778 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject buf) {
1779 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, buf);
1780 // TODO: check that 'buf' is a java.nio.Buffer.
1781 return CHECK_JNI_EXIT("J", baseEnv(env)->GetDirectBufferCapacity(env, buf));
1782 }
1783
1784 private:
1785 static inline const JNINativeInterface* baseEnv(JNIEnv* env) {
1786 return reinterpret_cast<JNIEnvExt*>(env)->unchecked_functions;
1787 }
1788};
1789
1790const JNINativeInterface gCheckNativeInterface = {
1791 NULL, // reserved0.
1792 NULL, // reserved1.
1793 NULL, // reserved2.
1794 NULL, // reserved3.
1795 CheckJNI::GetVersion,
1796 CheckJNI::DefineClass,
1797 CheckJNI::FindClass,
1798 CheckJNI::FromReflectedMethod,
1799 CheckJNI::FromReflectedField,
1800 CheckJNI::ToReflectedMethod,
1801 CheckJNI::GetSuperclass,
1802 CheckJNI::IsAssignableFrom,
1803 CheckJNI::ToReflectedField,
1804 CheckJNI::Throw,
1805 CheckJNI::ThrowNew,
1806 CheckJNI::ExceptionOccurred,
1807 CheckJNI::ExceptionDescribe,
1808 CheckJNI::ExceptionClear,
1809 CheckJNI::FatalError,
1810 CheckJNI::PushLocalFrame,
1811 CheckJNI::PopLocalFrame,
1812 CheckJNI::NewGlobalRef,
1813 CheckJNI::DeleteGlobalRef,
1814 CheckJNI::DeleteLocalRef,
1815 CheckJNI::IsSameObject,
1816 CheckJNI::NewLocalRef,
1817 CheckJNI::EnsureLocalCapacity,
1818 CheckJNI::AllocObject,
1819 CheckJNI::NewObject,
1820 CheckJNI::NewObjectV,
1821 CheckJNI::NewObjectA,
1822 CheckJNI::GetObjectClass,
1823 CheckJNI::IsInstanceOf,
1824 CheckJNI::GetMethodID,
1825 CheckJNI::CallObjectMethod,
1826 CheckJNI::CallObjectMethodV,
1827 CheckJNI::CallObjectMethodA,
1828 CheckJNI::CallBooleanMethod,
1829 CheckJNI::CallBooleanMethodV,
1830 CheckJNI::CallBooleanMethodA,
1831 CheckJNI::CallByteMethod,
1832 CheckJNI::CallByteMethodV,
1833 CheckJNI::CallByteMethodA,
1834 CheckJNI::CallCharMethod,
1835 CheckJNI::CallCharMethodV,
1836 CheckJNI::CallCharMethodA,
1837 CheckJNI::CallShortMethod,
1838 CheckJNI::CallShortMethodV,
1839 CheckJNI::CallShortMethodA,
1840 CheckJNI::CallIntMethod,
1841 CheckJNI::CallIntMethodV,
1842 CheckJNI::CallIntMethodA,
1843 CheckJNI::CallLongMethod,
1844 CheckJNI::CallLongMethodV,
1845 CheckJNI::CallLongMethodA,
1846 CheckJNI::CallFloatMethod,
1847 CheckJNI::CallFloatMethodV,
1848 CheckJNI::CallFloatMethodA,
1849 CheckJNI::CallDoubleMethod,
1850 CheckJNI::CallDoubleMethodV,
1851 CheckJNI::CallDoubleMethodA,
1852 CheckJNI::CallVoidMethod,
1853 CheckJNI::CallVoidMethodV,
1854 CheckJNI::CallVoidMethodA,
1855 CheckJNI::CallNonvirtualObjectMethod,
1856 CheckJNI::CallNonvirtualObjectMethodV,
1857 CheckJNI::CallNonvirtualObjectMethodA,
1858 CheckJNI::CallNonvirtualBooleanMethod,
1859 CheckJNI::CallNonvirtualBooleanMethodV,
1860 CheckJNI::CallNonvirtualBooleanMethodA,
1861 CheckJNI::CallNonvirtualByteMethod,
1862 CheckJNI::CallNonvirtualByteMethodV,
1863 CheckJNI::CallNonvirtualByteMethodA,
1864 CheckJNI::CallNonvirtualCharMethod,
1865 CheckJNI::CallNonvirtualCharMethodV,
1866 CheckJNI::CallNonvirtualCharMethodA,
1867 CheckJNI::CallNonvirtualShortMethod,
1868 CheckJNI::CallNonvirtualShortMethodV,
1869 CheckJNI::CallNonvirtualShortMethodA,
1870 CheckJNI::CallNonvirtualIntMethod,
1871 CheckJNI::CallNonvirtualIntMethodV,
1872 CheckJNI::CallNonvirtualIntMethodA,
1873 CheckJNI::CallNonvirtualLongMethod,
1874 CheckJNI::CallNonvirtualLongMethodV,
1875 CheckJNI::CallNonvirtualLongMethodA,
1876 CheckJNI::CallNonvirtualFloatMethod,
1877 CheckJNI::CallNonvirtualFloatMethodV,
1878 CheckJNI::CallNonvirtualFloatMethodA,
1879 CheckJNI::CallNonvirtualDoubleMethod,
1880 CheckJNI::CallNonvirtualDoubleMethodV,
1881 CheckJNI::CallNonvirtualDoubleMethodA,
1882 CheckJNI::CallNonvirtualVoidMethod,
1883 CheckJNI::CallNonvirtualVoidMethodV,
1884 CheckJNI::CallNonvirtualVoidMethodA,
1885 CheckJNI::GetFieldID,
1886 CheckJNI::GetObjectField,
1887 CheckJNI::GetBooleanField,
1888 CheckJNI::GetByteField,
1889 CheckJNI::GetCharField,
1890 CheckJNI::GetShortField,
1891 CheckJNI::GetIntField,
1892 CheckJNI::GetLongField,
1893 CheckJNI::GetFloatField,
1894 CheckJNI::GetDoubleField,
1895 CheckJNI::SetObjectField,
1896 CheckJNI::SetBooleanField,
1897 CheckJNI::SetByteField,
1898 CheckJNI::SetCharField,
1899 CheckJNI::SetShortField,
1900 CheckJNI::SetIntField,
1901 CheckJNI::SetLongField,
1902 CheckJNI::SetFloatField,
1903 CheckJNI::SetDoubleField,
1904 CheckJNI::GetStaticMethodID,
1905 CheckJNI::CallStaticObjectMethod,
1906 CheckJNI::CallStaticObjectMethodV,
1907 CheckJNI::CallStaticObjectMethodA,
1908 CheckJNI::CallStaticBooleanMethod,
1909 CheckJNI::CallStaticBooleanMethodV,
1910 CheckJNI::CallStaticBooleanMethodA,
1911 CheckJNI::CallStaticByteMethod,
1912 CheckJNI::CallStaticByteMethodV,
1913 CheckJNI::CallStaticByteMethodA,
1914 CheckJNI::CallStaticCharMethod,
1915 CheckJNI::CallStaticCharMethodV,
1916 CheckJNI::CallStaticCharMethodA,
1917 CheckJNI::CallStaticShortMethod,
1918 CheckJNI::CallStaticShortMethodV,
1919 CheckJNI::CallStaticShortMethodA,
1920 CheckJNI::CallStaticIntMethod,
1921 CheckJNI::CallStaticIntMethodV,
1922 CheckJNI::CallStaticIntMethodA,
1923 CheckJNI::CallStaticLongMethod,
1924 CheckJNI::CallStaticLongMethodV,
1925 CheckJNI::CallStaticLongMethodA,
1926 CheckJNI::CallStaticFloatMethod,
1927 CheckJNI::CallStaticFloatMethodV,
1928 CheckJNI::CallStaticFloatMethodA,
1929 CheckJNI::CallStaticDoubleMethod,
1930 CheckJNI::CallStaticDoubleMethodV,
1931 CheckJNI::CallStaticDoubleMethodA,
1932 CheckJNI::CallStaticVoidMethod,
1933 CheckJNI::CallStaticVoidMethodV,
1934 CheckJNI::CallStaticVoidMethodA,
1935 CheckJNI::GetStaticFieldID,
1936 CheckJNI::GetStaticObjectField,
1937 CheckJNI::GetStaticBooleanField,
1938 CheckJNI::GetStaticByteField,
1939 CheckJNI::GetStaticCharField,
1940 CheckJNI::GetStaticShortField,
1941 CheckJNI::GetStaticIntField,
1942 CheckJNI::GetStaticLongField,
1943 CheckJNI::GetStaticFloatField,
1944 CheckJNI::GetStaticDoubleField,
1945 CheckJNI::SetStaticObjectField,
1946 CheckJNI::SetStaticBooleanField,
1947 CheckJNI::SetStaticByteField,
1948 CheckJNI::SetStaticCharField,
1949 CheckJNI::SetStaticShortField,
1950 CheckJNI::SetStaticIntField,
1951 CheckJNI::SetStaticLongField,
1952 CheckJNI::SetStaticFloatField,
1953 CheckJNI::SetStaticDoubleField,
1954 CheckJNI::NewString,
1955 CheckJNI::GetStringLength,
1956 CheckJNI::GetStringChars,
1957 CheckJNI::ReleaseStringChars,
1958 CheckJNI::NewStringUTF,
1959 CheckJNI::GetStringUTFLength,
1960 CheckJNI::GetStringUTFChars,
1961 CheckJNI::ReleaseStringUTFChars,
1962 CheckJNI::GetArrayLength,
1963 CheckJNI::NewObjectArray,
1964 CheckJNI::GetObjectArrayElement,
1965 CheckJNI::SetObjectArrayElement,
1966 CheckJNI::NewBooleanArray,
1967 CheckJNI::NewByteArray,
1968 CheckJNI::NewCharArray,
1969 CheckJNI::NewShortArray,
1970 CheckJNI::NewIntArray,
1971 CheckJNI::NewLongArray,
1972 CheckJNI::NewFloatArray,
1973 CheckJNI::NewDoubleArray,
1974 CheckJNI::GetBooleanArrayElements,
1975 CheckJNI::GetByteArrayElements,
1976 CheckJNI::GetCharArrayElements,
1977 CheckJNI::GetShortArrayElements,
1978 CheckJNI::GetIntArrayElements,
1979 CheckJNI::GetLongArrayElements,
1980 CheckJNI::GetFloatArrayElements,
1981 CheckJNI::GetDoubleArrayElements,
1982 CheckJNI::ReleaseBooleanArrayElements,
1983 CheckJNI::ReleaseByteArrayElements,
1984 CheckJNI::ReleaseCharArrayElements,
1985 CheckJNI::ReleaseShortArrayElements,
1986 CheckJNI::ReleaseIntArrayElements,
1987 CheckJNI::ReleaseLongArrayElements,
1988 CheckJNI::ReleaseFloatArrayElements,
1989 CheckJNI::ReleaseDoubleArrayElements,
1990 CheckJNI::GetBooleanArrayRegion,
1991 CheckJNI::GetByteArrayRegion,
1992 CheckJNI::GetCharArrayRegion,
1993 CheckJNI::GetShortArrayRegion,
1994 CheckJNI::GetIntArrayRegion,
1995 CheckJNI::GetLongArrayRegion,
1996 CheckJNI::GetFloatArrayRegion,
1997 CheckJNI::GetDoubleArrayRegion,
1998 CheckJNI::SetBooleanArrayRegion,
1999 CheckJNI::SetByteArrayRegion,
2000 CheckJNI::SetCharArrayRegion,
2001 CheckJNI::SetShortArrayRegion,
2002 CheckJNI::SetIntArrayRegion,
2003 CheckJNI::SetLongArrayRegion,
2004 CheckJNI::SetFloatArrayRegion,
2005 CheckJNI::SetDoubleArrayRegion,
2006 CheckJNI::RegisterNatives,
2007 CheckJNI::UnregisterNatives,
2008 CheckJNI::MonitorEnter,
2009 CheckJNI::MonitorExit,
2010 CheckJNI::GetJavaVM,
2011 CheckJNI::GetStringRegion,
2012 CheckJNI::GetStringUTFRegion,
2013 CheckJNI::GetPrimitiveArrayCritical,
2014 CheckJNI::ReleasePrimitiveArrayCritical,
2015 CheckJNI::GetStringCritical,
2016 CheckJNI::ReleaseStringCritical,
2017 CheckJNI::NewWeakGlobalRef,
2018 CheckJNI::DeleteWeakGlobalRef,
2019 CheckJNI::ExceptionCheck,
2020 CheckJNI::NewDirectByteBuffer,
2021 CheckJNI::GetDirectBufferAddress,
2022 CheckJNI::GetDirectBufferCapacity,
2023 CheckJNI::GetObjectRefType,
2024};
2025
2026const JNINativeInterface* GetCheckJniNativeInterface() {
2027 return &gCheckNativeInterface;
2028}
2029
2030class CheckJII {
Elliott Hughesba8eee12012-01-24 20:25:24 -08002031 public:
Elliott Hughesa2501992011-08-26 19:39:54 -07002032 static jint DestroyJavaVM(JavaVM* vm) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002033 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002034 sc.Check(true, "v", vm);
2035 return CHECK_JNI_EXIT("I", BaseVm(vm)->DestroyJavaVM(vm));
Elliott Hughesa2501992011-08-26 19:39:54 -07002036 }
2037
2038 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002039 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002040 sc.Check(true, "vpp", vm, p_env, thr_args);
2041 return CHECK_JNI_EXIT("I", BaseVm(vm)->AttachCurrentThread(vm, p_env, thr_args));
Elliott Hughesa2501992011-08-26 19:39:54 -07002042 }
2043
2044 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002045 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002046 sc.Check(true, "vpp", vm, p_env, thr_args);
2047 return CHECK_JNI_EXIT("I", BaseVm(vm)->AttachCurrentThreadAsDaemon(vm, p_env, thr_args));
Elliott Hughesa2501992011-08-26 19:39:54 -07002048 }
2049
2050 static jint DetachCurrentThread(JavaVM* vm) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002051 ScopedCheck sc(vm, true, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002052 sc.Check(true, "v", vm);
2053 return CHECK_JNI_EXIT("I", BaseVm(vm)->DetachCurrentThread(vm));
Elliott Hughesa2501992011-08-26 19:39:54 -07002054 }
2055
2056 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002057 ScopedCheck sc(vm, true, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002058 sc.Check(true, "v", vm);
2059 return CHECK_JNI_EXIT("I", BaseVm(vm)->GetEnv(vm, env, version));
Elliott Hughesa2501992011-08-26 19:39:54 -07002060 }
2061
2062 private:
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002063 static inline const JNIInvokeInterface* BaseVm(JavaVM* vm) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002064 return reinterpret_cast<JavaVMExt*>(vm)->unchecked_functions;
2065 }
2066};
2067
2068const JNIInvokeInterface gCheckInvokeInterface = {
2069 NULL, // reserved0
2070 NULL, // reserved1
2071 NULL, // reserved2
2072 CheckJII::DestroyJavaVM,
2073 CheckJII::AttachCurrentThread,
2074 CheckJII::DetachCurrentThread,
2075 CheckJII::GetEnv,
2076 CheckJII::AttachCurrentThreadAsDaemon
2077};
2078
2079const JNIInvokeInterface* GetCheckJniInvokeInterface() {
2080 return &gCheckInvokeInterface;
2081}
2082
2083} // namespace art