blob: c6a628f2a1ffffdf6e0be357ff6d71c3756bd030 [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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080022#include "base/logging.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070023#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "class_linker-inl.h"
25#include "gc/space.h"
26#include "mirror/class-inl.h"
27#include "mirror/field-inl.h"
28#include "mirror/abstract_method-inl.h"
29#include "mirror/object-inl.h"
30#include "mirror/object_array-inl.h"
31#include "mirror/throwable.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080032#include "object_utils.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070033#include "scoped_thread_state_change.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070034#include "thread.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070035#include "runtime.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070036
Elliott Hughese6087632011-09-26 12:18:25 -070037#define LIBCORE_CPP_JNI_HELPERS
38#include <JNIHelp.h> // from libcore
39#undef LIBCORE_CPP_JNI_HELPERS
40
Elliott Hughesa2501992011-08-26 19:39:54 -070041namespace art {
42
Elliott Hughes3f6635a2012-06-19 13:37:49 -070043static void JniAbort(const char* jni_function_name, const char* msg) {
Elliott Hughesa0957642011-09-02 14:27:33 -070044 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070045 ScopedObjectAccess soa(self);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046 mirror::AbstractMethod* current_method = self->GetCurrentMethod();
Elliott Hughesa2501992011-08-26 19:39:54 -070047
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070048 std::ostringstream os;
Elliott Hughes3f6635a2012-06-19 13:37:49 -070049 os << "JNI DETECTED ERROR IN APPLICATION: " << msg;
Elliott Hughesa2501992011-08-26 19:39:54 -070050
51 if (jni_function_name != NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -070052 os << "\n in call to " << jni_function_name;
Elliott Hughesa2501992011-08-26 19:39:54 -070053 }
Elliott Hughesa0957642011-09-02 14:27:33 -070054 // TODO: is this useful given that we're about to dump the calling thread's stack?
55 if (current_method != NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -070056 os << "\n from " << PrettyMethod(current_method);
Elliott Hughesa0957642011-09-02 14:27:33 -070057 }
58 os << "\n";
59 self->Dump(os);
Elliott Hughesa2501992011-08-26 19:39:54 -070060
61 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
62 if (vm->check_jni_abort_hook != NULL) {
Elliott Hughesb264f082012-04-06 17:10:10 -070063 vm->check_jni_abort_hook(vm->check_jni_abort_hook_data, os.str());
Elliott Hughesa2501992011-08-26 19:39:54 -070064 } else {
Ian Rogers9da7f592012-08-20 17:14:28 -070065 // Ensure that we get a native stack trace for this thread.
66 self->TransitionFromRunnableToSuspended(kNative);
Elliott Hughesa2501992011-08-26 19:39:54 -070067 LOG(FATAL) << os.str();
Ian Rogers9da7f592012-08-20 17:14:28 -070068 self->TransitionFromSuspendedToRunnable(); // Unreachable, keep annotalysis happy.
Elliott Hughesa2501992011-08-26 19:39:54 -070069 }
70}
71
Elliott Hughes3f6635a2012-06-19 13:37:49 -070072static void JniAbortV(const char* jni_function_name, const char* fmt, va_list ap) {
73 std::string msg;
74 StringAppendV(&msg, fmt, ap);
75 JniAbort(jni_function_name, msg.c_str());
76}
77
78void JniAbortF(const char* jni_function_name, const char* fmt, ...) {
79 va_list args;
80 va_start(args, fmt);
81 JniAbortV(jni_function_name, fmt, args);
82 va_end(args);
83}
84
Elliott Hughesa2501992011-08-26 19:39:54 -070085/*
86 * ===========================================================================
87 * JNI function helpers
88 * ===========================================================================
89 */
90
Ian Rogers959f8ed2012-02-07 16:33:37 -080091static bool IsSirtLocalRef(JNIEnv* env, jobject localRef) {
92 return GetIndirectRefKind(localRef) == kSirtOrInvalid &&
Ian Rogers0399dde2012-06-06 17:09:28 -070093 reinterpret_cast<JNIEnvExt*>(env)->self->SirtContains(localRef);
Ian Rogers959f8ed2012-02-07 16:33:37 -080094}
95
Elliott Hughes3f6635a2012-06-19 13:37:49 -070096// Hack to allow forcecopy to work with jniGetNonMovableArrayElements.
97// The code deliberately uses an invalid sequence of operations, so we
98// need to pass it through unmodified. Review that code before making
99// any changes here.
Elliott Hughesa2501992011-08-26 19:39:54 -0700100#define kNoCopyMagic 0xd5aab57f
101
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700102// Flags passed into ScopedCheck.
Elliott Hughesa2501992011-08-26 19:39:54 -0700103#define kFlag_Default 0x0000
104
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700105#define kFlag_CritBad 0x0000 // Calling while in critical is not allowed.
106#define kFlag_CritOkay 0x0001 // Calling while in critical is allowed.
107#define kFlag_CritGet 0x0002 // This is a critical "get".
108#define kFlag_CritRelease 0x0003 // This is a critical "release".
109#define kFlag_CritMask 0x0003 // Bit mask to get "crit" value.
Elliott Hughesa2501992011-08-26 19:39:54 -0700110
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700111#define kFlag_ExcepBad 0x0000 // Raised exceptions are not allowed.
112#define kFlag_ExcepOkay 0x0004 // Raised exceptions are allowed.
Elliott Hughesa2501992011-08-26 19:39:54 -0700113
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700114#define kFlag_Release 0x0010 // Are we in a non-critical release function?
115#define kFlag_NullableUtf 0x0020 // Are our UTF parameters nullable?
Elliott Hughesa2501992011-08-26 19:39:54 -0700116
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700117#define kFlag_Invocation 0x8000 // Part of the invocation interface (JavaVM*).
Elliott Hughesa2501992011-08-26 19:39:54 -0700118
Elliott Hughes485cac42011-12-09 17:49:35 -0800119#define kFlag_ForceTrace 0x80000000 // Add this to a JNI function's flags if you want to trace every call.
120
Elliott Hughesa0957642011-09-02 14:27:33 -0700121static const char* gBuiltInPrefixes[] = {
122 "Landroid/",
123 "Lcom/android/",
124 "Lcom/google/android/",
125 "Ldalvik/",
126 "Ljava/",
127 "Ljavax/",
128 "Llibcore/",
129 "Lorg/apache/harmony/",
130 NULL
131};
132
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800133static bool ShouldTrace(JavaVMExt* vm, const mirror::AbstractMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700134 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700135 // If both "-Xcheck:jni" and "-Xjnitrace:" are enabled, we print trace messages
136 // when a native method that matches the -Xjnitrace argument calls a JNI function
137 // such as NewByteArray.
138 // If -verbose:third-party-jni is on, we want to log any JNI function calls
139 // made by a third-party native method.
Elliott Hughes81ff3182012-03-23 20:35:56 -0700140 std::string class_name(MethodHelper(method).GetDeclaringClassDescriptor());
141 if (!vm->trace.empty() && class_name.find(vm->trace) != std::string::npos) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700142 return true;
143 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800144 if (VLOG_IS_ON(third_party_jni)) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700145 // Return true if we're trying to log all third-party JNI activity and 'method' doesn't look
146 // like part of Android.
Elliott Hughesa0957642011-09-02 14:27:33 -0700147 for (size_t i = 0; gBuiltInPrefixes[i] != NULL; ++i) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700148 if (StartsWith(class_name, gBuiltInPrefixes[i])) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700149 return false;
150 }
151 }
152 return true;
153 }
154 return false;
155}
156
Elliott Hughesa2501992011-08-26 19:39:54 -0700157class ScopedCheck {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800158 public:
Elliott Hughesa2501992011-08-26 19:39:54 -0700159 // For JNIEnv* functions.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700160 explicit ScopedCheck(JNIEnv* env, int flags, const char* functionName)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700161 SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700162 : soa_(env) {
Ian Rogers365c1022012-06-22 15:05:28 -0700163 Init(flags, functionName, true);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700164 CheckThread(flags);
Elliott Hughesa2501992011-08-26 19:39:54 -0700165 }
166
167 // For JavaVM* functions.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700168 // TODO: it's not correct that this is a lock function, but making it so aids annotalysis.
169 explicit ScopedCheck(JavaVM* vm, bool has_method, const char* functionName)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700170 SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700171 : soa_(vm) {
Ian Rogers365c1022012-06-22 15:05:28 -0700172 Init(kFlag_Invocation, functionName, has_method);
Elliott Hughesa2501992011-08-26 19:39:54 -0700173 }
174
Ian Rogersb726dcb2012-09-05 08:57:23 -0700175 ~ScopedCheck() UNLOCK_FUNCTION(Locks::mutator_lock_) {}
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700176
177 const ScopedObjectAccess& soa() {
178 return soa_;
179 }
180
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700181 bool ForceCopy() {
Elliott Hughesa2501992011-08-26 19:39:54 -0700182 return Runtime::Current()->GetJavaVM()->force_copy;
183 }
184
Elliott Hughes81ff3182012-03-23 20:35:56 -0700185 // Checks that 'class_name' is a valid "fully-qualified" JNI class name, like "java/lang/Thread"
186 // or "[Ljava/lang/Object;". A ClassLoader can actually normalize class names a couple of
187 // times, so using "java.lang.Thread" instead of "java/lang/Thread" might work in some
188 // circumstances, but this is incorrect.
189 void CheckClassName(const char* class_name) {
190 if (!IsValidJniClassName(class_name)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700191 JniAbortF(function_name_,
192 "illegal class name '%s'\n"
193 " (should be of the form 'package/Class', [Lpackage/Class;' or '[[B')",
194 class_name);
Elliott Hughesa2501992011-08-26 19:39:54 -0700195 }
196 }
197
198 /*
199 * Verify that the field is of the appropriate type. If the field has an
200 * object type, "java_object" is the object we're trying to assign into it.
201 *
202 * Works for both static and instance fields.
203 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700204 void CheckFieldType(jobject java_object, jfieldID fid, char prim, bool isStatic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700205 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800206 mirror::Field* f = CheckFieldID(fid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700207 if (f == NULL) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700208 return;
209 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800210 mirror::Class* field_type = FieldHelper(f).GetType();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700211 if (!field_type->IsPrimitive()) {
212 if (java_object != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800213 mirror::Object* obj = soa_.Decode<mirror::Object*>(java_object);
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700214 // If java_object is a weak global ref whose referent has been cleared,
215 // obj will be NULL. Otherwise, obj should always be non-NULL
216 // and valid.
Elliott Hughes88c5c352012-03-15 18:49:48 -0700217 if (!Runtime::Current()->GetHeap()->IsHeapAddress(obj)) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700218 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700219 JniAbortF(function_name_, "field operation on invalid %s: %p",
220 ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700221 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700222 } else {
Brian Carlstrom16192862011-09-12 17:50:06 -0700223 if (!obj->InstanceOf(field_type)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700224 JniAbortF(function_name_, "attempt to set field %s with value of wrong type: %s",
225 PrettyField(f).c_str(), PrettyTypeOf(obj).c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700226 return;
227 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700228 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700229 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700230 } else if (field_type != Runtime::Current()->GetClassLinker()->FindPrimitiveClass(prim)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700231 JniAbortF(function_name_, "attempt to set field %s with value of wrong type: %c",
232 PrettyField(f).c_str(), prim);
Elliott Hughesa2501992011-08-26 19:39:54 -0700233 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700234 }
235
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700236 if (isStatic != f->IsStatic()) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700237 if (isStatic) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700238 JniAbortF(function_name_, "accessing non-static field %s as static", PrettyField(f).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700239 } else {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700240 JniAbortF(function_name_, "accessing static field %s as non-static", PrettyField(f).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700241 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700242 return;
243 }
244 }
245
246 /*
247 * Verify that this instance field ID is valid for this object.
248 *
249 * Assumes "jobj" has already been validated.
250 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700251 void CheckInstanceFieldID(jobject java_object, jfieldID fid)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700252 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800253 mirror::Object* o = soa_.Decode<mirror::Object*>(java_object);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800254 if (o == NULL || !Runtime::Current()->GetHeap()->IsHeapAddress(o)) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700255 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700256 JniAbortF(function_name_, "field operation on invalid %s: %p",
257 ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700258 return;
259 }
260
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800261 mirror::Field* f = CheckFieldID(fid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700262 if (f == NULL) {
263 return;
264 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800265 mirror::Class* c = o->GetClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800266 FieldHelper fh(f);
267 if (c->FindInstanceField(fh.GetName(), fh.GetTypeDescriptor()) == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700268 JniAbortF(function_name_, "jfieldID %s not valid for an object of class %s",
269 PrettyField(f).c_str(), PrettyTypeOf(o).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700270 }
271 }
272
273 /*
274 * Verify that the pointer value is non-NULL.
275 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700276 void CheckNonNull(const void* ptr) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700277 if (ptr == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700278 JniAbortF(function_name_, "non-nullable argument was NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -0700279 }
280 }
281
282 /*
283 * Verify that the method's return type matches the type of call.
284 * 'expectedType' will be "L" for all objects, including arrays.
285 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700286 void CheckSig(jmethodID mid, const char* expectedType, bool isStatic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700287 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800288 mirror::AbstractMethod* m = CheckMethodID(mid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700289 if (m == NULL) {
290 return;
291 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800292 if (*expectedType != MethodHelper(m).GetShorty()[0]) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700293 JniAbortF(function_name_, "the return type of %s does not match %s",
294 function_name_, PrettyMethod(m).c_str());
295 }
296 if (isStatic != m->IsStatic()) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700297 if (isStatic) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700298 JniAbortF(function_name_, "calling non-static method %s with %s",
299 PrettyMethod(m).c_str(), function_name_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700300 } else {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700301 JniAbortF(function_name_, "calling static method %s with %s",
302 PrettyMethod(m).c_str(), function_name_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700303 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700304 }
305 }
306
307 /*
308 * Verify that this static field ID is valid for this class.
309 *
310 * Assumes "java_class" has already been validated.
311 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700312 void CheckStaticFieldID(jclass java_class, jfieldID fid)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700313 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800314 mirror::Class* c = soa_.Decode<mirror::Class*>(java_class);
315 const mirror::Field* f = CheckFieldID(fid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700316 if (f == NULL) {
317 return;
318 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700319 if (f->GetDeclaringClass() != c) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700320 JniAbortF(function_name_, "static jfieldID %p not valid for class %s",
321 fid, PrettyClass(c).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700322 }
323 }
324
325 /*
Elliott Hughese84278b2012-03-22 10:06:53 -0700326 * Verify that "mid" is appropriate for "java_class".
Elliott Hughesa2501992011-08-26 19:39:54 -0700327 *
328 * A mismatch isn't dangerous, because the jmethodID defines the class. In
Elliott Hughese84278b2012-03-22 10:06:53 -0700329 * fact, java_class is unused in the implementation. It's best if we don't
Elliott Hughesa2501992011-08-26 19:39:54 -0700330 * allow bad code in the system though.
331 *
Elliott Hughese84278b2012-03-22 10:06:53 -0700332 * Instances of "java_class" must be instances of the method's declaring class.
Elliott Hughesa2501992011-08-26 19:39:54 -0700333 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700334 void CheckStaticMethod(jclass java_class, jmethodID mid)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700335 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800336 const mirror::AbstractMethod* m = CheckMethodID(mid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700337 if (m == NULL) {
338 return;
339 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800340 mirror::Class* c = soa_.Decode<mirror::Class*>(java_class);
Elliott Hughesa2501992011-08-26 19:39:54 -0700341 if (!c->IsAssignableFrom(m->GetDeclaringClass())) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700342 JniAbortF(function_name_, "can't call static %s on class %s",
343 PrettyMethod(m).c_str(), PrettyClass(c).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700344 }
345 }
346
347 /*
348 * Verify that "mid" is appropriate for "jobj".
349 *
350 * Make sure the object is an instance of the method's declaring class.
351 * (Note the mid might point to a declaration in an interface; this
352 * will be handled automatically by the instanceof check.)
353 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700354 void CheckVirtualMethod(jobject java_object, jmethodID mid)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700355 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800356 const mirror::AbstractMethod* m = CheckMethodID(mid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700357 if (m == NULL) {
358 return;
359 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800360 mirror::Object* o = soa_.Decode<mirror::Object*>(java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700361 if (!o->InstanceOf(m->GetDeclaringClass())) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700362 JniAbortF(function_name_, "can't call %s on instance of %s",
363 PrettyMethod(m).c_str(), PrettyTypeOf(o).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700364 }
365 }
366
367 /**
368 * The format string is a sequence of the following characters,
369 * and must be followed by arguments of the corresponding types
370 * in the same order.
371 *
372 * Java primitive types:
373 * B - jbyte
374 * C - jchar
375 * D - jdouble
376 * F - jfloat
377 * I - jint
378 * J - jlong
379 * S - jshort
380 * Z - jboolean (shown as true and false)
381 * V - void
382 *
383 * Java reference types:
384 * L - jobject
385 * a - jarray
386 * c - jclass
387 * s - jstring
388 *
389 * JNI types:
390 * b - jboolean (shown as JNI_TRUE and JNI_FALSE)
391 * f - jfieldID
392 * m - jmethodID
393 * p - void*
394 * r - jint (for release mode arguments)
Elliott Hughes78090d12011-10-07 14:31:47 -0700395 * u - const char* (Modified UTF-8)
Elliott Hughesa2501992011-08-26 19:39:54 -0700396 * z - jsize (for lengths; use i if negative values are okay)
397 * v - JavaVM*
398 * E - JNIEnv*
399 * . - no argument; just print "..." (used for varargs JNI calls)
400 *
401 * Use the kFlag_NullableUtf flag where 'u' field(s) are nullable.
402 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700403 void Check(bool entry, const char* fmt0, ...)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700404 SHARED_LOCKS_REQUIRED (Locks::mutator_lock_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700405 va_list ap;
406
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800407 const mirror::AbstractMethod* traceMethod = NULL;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700408 if ((!soa_.Vm()->trace.empty() || VLOG_IS_ON(third_party_jni)) && has_method_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700409 // We need to guard some of the invocation interface's calls: a bad caller might
410 // use DetachCurrentThread or GetEnv on a thread that's not yet attached.
Elliott Hughesa0957642011-09-02 14:27:33 -0700411 Thread* self = Thread::Current();
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700412 if ((flags_ & kFlag_Invocation) == 0 || self != NULL) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700413 traceMethod = self->GetCurrentMethod();
Elliott Hughesa2501992011-08-26 19:39:54 -0700414 }
415 }
Elliott Hughesa0957642011-09-02 14:27:33 -0700416
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700417 if (((flags_ & kFlag_ForceTrace) != 0) || (traceMethod != NULL && ShouldTrace(soa_.Vm(), traceMethod))) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700418 va_start(ap, fmt0);
419 std::string msg;
420 for (const char* fmt = fmt0; *fmt;) {
421 char ch = *fmt++;
422 if (ch == 'B') { // jbyte
423 jbyte b = va_arg(ap, int);
424 if (b >= 0 && b < 10) {
425 StringAppendF(&msg, "%d", b);
426 } else {
427 StringAppendF(&msg, "%#x (%d)", b, b);
428 }
429 } else if (ch == 'C') { // jchar
430 jchar c = va_arg(ap, int);
431 if (c < 0x7f && c >= ' ') {
432 StringAppendF(&msg, "U+%x ('%c')", c, c);
433 } else {
434 StringAppendF(&msg, "U+%x", c);
435 }
436 } else if (ch == 'F' || ch == 'D') { // jfloat, jdouble
437 StringAppendF(&msg, "%g", va_arg(ap, double));
438 } else if (ch == 'I' || ch == 'S') { // jint, jshort
439 StringAppendF(&msg, "%d", va_arg(ap, int));
440 } else if (ch == 'J') { // jlong
441 StringAppendF(&msg, "%lld", va_arg(ap, jlong));
442 } else if (ch == 'Z') { // jboolean
443 StringAppendF(&msg, "%s", va_arg(ap, int) ? "true" : "false");
444 } else if (ch == 'V') { // void
445 msg += "void";
446 } else if (ch == 'v') { // JavaVM*
447 JavaVM* vm = va_arg(ap, JavaVM*);
448 StringAppendF(&msg, "(JavaVM*)%p", vm);
449 } else if (ch == 'E') { // JNIEnv*
450 JNIEnv* env = va_arg(ap, JNIEnv*);
451 StringAppendF(&msg, "(JNIEnv*)%p", env);
452 } else if (ch == 'L' || ch == 'a' || ch == 's') { // jobject, jarray, jstring
453 // For logging purposes, these are identical.
454 jobject o = va_arg(ap, jobject);
455 if (o == NULL) {
456 msg += "NULL";
457 } else {
458 StringAppendF(&msg, "%p", o);
459 }
460 } else if (ch == 'b') { // jboolean (JNI-style)
461 jboolean b = va_arg(ap, int);
462 msg += (b ? "JNI_TRUE" : "JNI_FALSE");
463 } else if (ch == 'c') { // jclass
464 jclass jc = va_arg(ap, jclass);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800465 mirror::Class* c = reinterpret_cast<mirror::Class*>(Thread::Current()->DecodeJObject(jc));
Elliott Hughesa2501992011-08-26 19:39:54 -0700466 if (c == NULL) {
467 msg += "NULL";
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800468 } else if (c == kInvalidIndirectRefObject || !Runtime::Current()->GetHeap()->IsHeapAddress(c)) {
Elliott Hughes485cac42011-12-09 17:49:35 -0800469 StringAppendF(&msg, "INVALID POINTER:%p", jc);
470 } else if (!c->IsClass()) {
471 msg += "INVALID NON-CLASS OBJECT OF TYPE:" + PrettyTypeOf(c);
Elliott Hughesa2501992011-08-26 19:39:54 -0700472 } else {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700473 msg += PrettyClass(c);
Elliott Hughesa2501992011-08-26 19:39:54 -0700474 if (!entry) {
475 StringAppendF(&msg, " (%p)", jc);
476 }
477 }
478 } else if (ch == 'f') { // jfieldID
479 jfieldID fid = va_arg(ap, jfieldID);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800480 mirror::Field* f = reinterpret_cast<mirror::Field*>(fid);
Elliott Hughesa2501992011-08-26 19:39:54 -0700481 msg += PrettyField(f);
482 if (!entry) {
483 StringAppendF(&msg, " (%p)", fid);
484 }
485 } else if (ch == 'z') { // non-negative jsize
486 // You might expect jsize to be size_t, but it's not; it's the same as jint.
487 // We only treat this specially so we can do the non-negative check.
488 // TODO: maybe this wasn't worth it?
489 jint i = va_arg(ap, jint);
490 StringAppendF(&msg, "%d", i);
491 } else if (ch == 'm') { // jmethodID
492 jmethodID mid = va_arg(ap, jmethodID);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800493 mirror::AbstractMethod* m = reinterpret_cast<mirror::AbstractMethod*>(mid);
Elliott Hughesa2501992011-08-26 19:39:54 -0700494 msg += PrettyMethod(m);
495 if (!entry) {
496 StringAppendF(&msg, " (%p)", mid);
497 }
498 } else if (ch == 'p') { // void* ("pointer")
499 void* p = va_arg(ap, void*);
500 if (p == NULL) {
501 msg += "NULL";
502 } else {
503 StringAppendF(&msg, "(void*) %p", p);
504 }
505 } else if (ch == 'r') { // jint (release mode)
506 jint releaseMode = va_arg(ap, jint);
507 if (releaseMode == 0) {
508 msg += "0";
509 } else if (releaseMode == JNI_ABORT) {
510 msg += "JNI_ABORT";
511 } else if (releaseMode == JNI_COMMIT) {
512 msg += "JNI_COMMIT";
513 } else {
514 StringAppendF(&msg, "invalid release mode %d", releaseMode);
515 }
Elliott Hughes78090d12011-10-07 14:31:47 -0700516 } else if (ch == 'u') { // const char* (Modified UTF-8)
Elliott Hughesa2501992011-08-26 19:39:54 -0700517 const char* utf = va_arg(ap, const char*);
518 if (utf == NULL) {
519 msg += "NULL";
520 } else {
521 StringAppendF(&msg, "\"%s\"", utf);
522 }
523 } else if (ch == '.') {
524 msg += "...";
525 } else {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700526 JniAbortF(function_name_, "unknown trace format specifier: %c", ch);
Elliott Hughesa2501992011-08-26 19:39:54 -0700527 return;
528 }
529 if (*fmt) {
530 StringAppendF(&msg, ", ");
531 }
532 }
533 va_end(ap);
534
Elliott Hughes485cac42011-12-09 17:49:35 -0800535 if ((flags_ & kFlag_ForceTrace) != 0) {
536 LOG(INFO) << "JNI: call to " << function_name_ << "(" << msg << ")";
537 } else if (entry) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700538 if (has_method_) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700539 std::string methodName(PrettyMethod(traceMethod, false));
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700540 LOG(INFO) << "JNI: " << methodName << " -> " << function_name_ << "(" << msg << ")";
541 indent_ = methodName.size() + 1;
Elliott Hughesa2501992011-08-26 19:39:54 -0700542 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700543 LOG(INFO) << "JNI: -> " << function_name_ << "(" << msg << ")";
544 indent_ = 0;
Elliott Hughesa2501992011-08-26 19:39:54 -0700545 }
546 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700547 LOG(INFO) << StringPrintf("JNI: %*s<- %s returned %s", indent_, "", function_name_, msg.c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700548 }
549 }
550
551 // We always do the thorough checks on entry, and never on exit...
552 if (entry) {
553 va_start(ap, fmt0);
554 for (const char* fmt = fmt0; *fmt; ++fmt) {
555 char ch = *fmt;
556 if (ch == 'a') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700557 CheckArray(va_arg(ap, jarray));
Elliott Hughesa2501992011-08-26 19:39:54 -0700558 } else if (ch == 'c') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700559 CheckInstance(kClass, va_arg(ap, jclass));
Elliott Hughesa2501992011-08-26 19:39:54 -0700560 } else if (ch == 'L') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700561 CheckObject(va_arg(ap, jobject));
Elliott Hughesa2501992011-08-26 19:39:54 -0700562 } else if (ch == 'r') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700563 CheckReleaseMode(va_arg(ap, jint));
Elliott Hughesa2501992011-08-26 19:39:54 -0700564 } else if (ch == 's') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700565 CheckInstance(kString, va_arg(ap, jstring));
Elliott Hughesa2501992011-08-26 19:39:54 -0700566 } else if (ch == 'u') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700567 if ((flags_ & kFlag_Release) != 0) {
568 CheckNonNull(va_arg(ap, const char*));
Elliott Hughesa2501992011-08-26 19:39:54 -0700569 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700570 bool nullable = ((flags_ & kFlag_NullableUtf) != 0);
571 CheckUtfString(va_arg(ap, const char*), nullable);
Elliott Hughesa2501992011-08-26 19:39:54 -0700572 }
573 } else if (ch == 'z') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700574 CheckLengthPositive(va_arg(ap, jsize));
Elliott Hughesa2501992011-08-26 19:39:54 -0700575 } else if (strchr("BCISZbfmpEv", ch) != NULL) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800576 va_arg(ap, uint32_t); // Skip this argument.
Elliott Hughesa2501992011-08-26 19:39:54 -0700577 } else if (ch == 'D' || ch == 'F') {
578 va_arg(ap, double); // Skip this argument.
579 } else if (ch == 'J') {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800580 va_arg(ap, uint64_t); // Skip this argument.
Elliott Hughesa2501992011-08-26 19:39:54 -0700581 } else if (ch == '.') {
582 } else {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800583 LOG(FATAL) << "Unknown check format specifier: " << ch;
Elliott Hughesa2501992011-08-26 19:39:54 -0700584 }
585 }
586 va_end(ap);
587 }
588 }
589
Elliott Hughesa92853e2012-02-07 16:09:27 -0800590 enum InstanceKind {
591 kClass,
Elliott Hughes0f3c5532012-03-30 14:51:51 -0700592 kDirectByteBuffer,
593 kObject,
594 kString,
595 kThrowable,
Elliott Hughesa92853e2012-02-07 16:09:27 -0800596 };
597
598 /*
599 * Verify that "jobj" is a valid non-NULL object reference, and points to
600 * an instance of expectedClass.
601 *
602 * Because we're looking at an object on the GC heap, we have to switch
603 * to "running" mode before doing the checks.
604 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700605 bool CheckInstance(InstanceKind kind, jobject java_object)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700606 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa92853e2012-02-07 16:09:27 -0800607 const char* what = NULL;
608 switch (kind) {
609 case kClass:
610 what = "jclass";
611 break;
612 case kDirectByteBuffer:
613 what = "direct ByteBuffer";
614 break;
615 case kObject:
616 what = "jobject";
617 break;
618 case kString:
619 what = "jstring";
620 break;
621 case kThrowable:
622 what = "jthrowable";
623 break;
624 default:
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700625 LOG(FATAL) << "Unknown kind " << static_cast<int>(kind);
Elliott Hughesa92853e2012-02-07 16:09:27 -0800626 }
627
628 if (java_object == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700629 JniAbortF(function_name_, "%s received null %s", function_name_, what);
Elliott Hughesa92853e2012-02-07 16:09:27 -0800630 return false;
631 }
632
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800633 mirror::Object* obj = soa_.Decode<mirror::Object*>(java_object);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800634 if (!Runtime::Current()->GetHeap()->IsHeapAddress(obj)) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700635 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700636 JniAbortF(function_name_, "%s is an invalid %s: %p (%p)",
637 what, ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object, obj);
Elliott Hughesa92853e2012-02-07 16:09:27 -0800638 return false;
639 }
640
641 bool okay = true;
642 switch (kind) {
643 case kClass:
644 okay = obj->IsClass();
645 break;
646 case kDirectByteBuffer:
647 UNIMPLEMENTED(FATAL);
648 break;
649 case kString:
650 okay = obj->GetClass()->IsStringClass();
651 break;
652 case kThrowable:
653 okay = obj->GetClass()->IsThrowableClass();
654 break;
655 case kObject:
656 break;
657 }
658 if (!okay) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700659 JniAbortF(function_name_, "%s has wrong type: %s", what, PrettyTypeOf(obj).c_str());
Elliott Hughesa92853e2012-02-07 16:09:27 -0800660 return false;
661 }
662
663 return true;
664 }
665
Elliott Hughesba8eee12012-01-24 20:25:24 -0800666 private:
Elliott Hughes81ff3182012-03-23 20:35:56 -0700667 // Set "has_method" to true if we have a valid thread with a method pointer.
668 // We won't have one before attaching a thread, after detaching a thread, or
669 // when shutting down the runtime.
Ian Rogers365c1022012-06-22 15:05:28 -0700670 void Init(int flags, const char* functionName, bool has_method) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700671 flags_ = flags;
672 function_name_ = functionName;
Elliott Hughes81ff3182012-03-23 20:35:56 -0700673 has_method_ = has_method;
Elliott Hughesa2501992011-08-26 19:39:54 -0700674 }
675
676 /*
677 * Verify that "array" is non-NULL and points to an Array object.
678 *
679 * Since we're dealing with objects, switch to "running" mode.
680 */
Ian Rogersb726dcb2012-09-05 08:57:23 -0700681 void CheckArray(jarray java_array) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700682 if (java_array == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700683 JniAbortF(function_name_, "jarray was NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -0700684 return;
685 }
686
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800687 mirror::Array* a = soa_.Decode<mirror::Array*>(java_array);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800688 if (!Runtime::Current()->GetHeap()->IsHeapAddress(a)) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700689 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700690 JniAbortF(function_name_, "jarray is an invalid %s: %p (%p)",
691 ToStr<IndirectRefKind>(GetIndirectRefKind(java_array)).c_str(), java_array, a);
Elliott Hughesa2501992011-08-26 19:39:54 -0700692 } else if (!a->IsArrayInstance()) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700693 JniAbortF(function_name_, "jarray argument has non-array type: %s", PrettyTypeOf(a).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700694 }
695 }
696
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700697 void CheckLengthPositive(jsize length) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700698 if (length < 0) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700699 JniAbortF(function_name_, "negative jsize: %d", length);
Elliott Hughesa2501992011-08-26 19:39:54 -0700700 }
701 }
702
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800703 mirror::Field* CheckFieldID(jfieldID fid) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700704 if (fid == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700705 JniAbortF(function_name_, "jfieldID was NULL");
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700706 return NULL;
707 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800708 mirror::Field* f = soa_.DecodeField(fid);
Ian Rogers365c1022012-06-22 15:05:28 -0700709 if (!Runtime::Current()->GetHeap()->IsHeapAddress(f) || !f->IsField()) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700710 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700711 JniAbortF(function_name_, "invalid jfieldID: %p", fid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700712 return NULL;
713 }
714 return f;
715 }
716
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800717 mirror::AbstractMethod* CheckMethodID(jmethodID mid) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700718 if (mid == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700719 JniAbortF(function_name_, "jmethodID was NULL");
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700720 return NULL;
721 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800722 mirror::AbstractMethod* m = soa_.DecodeMethod(mid);
Ian Rogers365c1022012-06-22 15:05:28 -0700723 if (!Runtime::Current()->GetHeap()->IsHeapAddress(m) || !m->IsMethod()) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700724 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700725 JniAbortF(function_name_, "invalid jmethodID: %p", mid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700726 return NULL;
727 }
728 return m;
729 }
730
Elliott Hughesa2501992011-08-26 19:39:54 -0700731 /*
732 * Verify that "jobj" is a valid object, and that it's an object that JNI
733 * is allowed to know about. We allow NULL references.
734 *
735 * Switches to "running" mode before performing checks.
736 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700737 void CheckObject(jobject java_object)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700738 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700739 if (java_object == NULL) {
740 return;
741 }
742
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800743 mirror::Object* o = soa_.Decode<mirror::Object*>(java_object);
Elliott Hughes88c5c352012-03-15 18:49:48 -0700744 if (!Runtime::Current()->GetHeap()->IsHeapAddress(o)) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700745 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700746 // TODO: when we remove work_around_app_jni_bugs, this should be impossible.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700747 JniAbortF(function_name_, "native code passing in reference to invalid %s: %p",
748 ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700749 }
750 }
751
752 /*
753 * Verify that the "mode" argument passed to a primitive array Release
754 * function is one of the valid values.
755 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700756 void CheckReleaseMode(jint mode) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700757 if (mode != 0 && mode != JNI_COMMIT && mode != JNI_ABORT) {
Elliott Hughes96a98872012-12-19 14:21:15 -0800758 JniAbortF(function_name_, "unknown value for release mode: %d", mode);
Elliott Hughesa2501992011-08-26 19:39:54 -0700759 }
760 }
761
Ian Rogersb726dcb2012-09-05 08:57:23 -0700762 void CheckThread(int flags) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700763 Thread* self = Thread::Current();
764 if (self == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700765 JniAbortF(function_name_, "a thread (tid %d) is making JNI calls without being attached", GetTid());
Elliott Hughesa2501992011-08-26 19:39:54 -0700766 return;
767 }
768
769 // Get the *correct* JNIEnv by going through our TLS pointer.
770 JNIEnvExt* threadEnv = self->GetJniEnv();
771
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700772 // Verify that the current thread is (a) attached and (b) associated with
773 // this particular instance of JNIEnv.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700774 if (soa_.Env() != threadEnv) {
775 if (soa_.Vm()->work_around_app_jni_bugs) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700776 // If we're keeping broken code limping along, we need to suppress the abort...
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700777 LOG(ERROR) << "APP BUG DETECTED: thread " << *self << " using JNIEnv* from thread " << *soa_.Self();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700778 } else {
779 JniAbortF(function_name_, "thread %s using JNIEnv* from thread %s",
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700780 ToStr<Thread>(*self).c_str(), ToStr<Thread>(*soa_.Self()).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700781 return;
782 }
783 }
784
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700785 // Verify that, if this thread previously made a critical "get" call, we
786 // do the corresponding "release" call before we try anything else.
Elliott Hughesa2501992011-08-26 19:39:54 -0700787 switch (flags & kFlag_CritMask) {
788 case kFlag_CritOkay: // okay to call this method
789 break;
790 case kFlag_CritBad: // not okay to call
791 if (threadEnv->critical) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700792 JniAbortF(function_name_, "thread %s using JNI after critical get", ToStr<Thread>(*self).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700793 return;
794 }
795 break;
796 case kFlag_CritGet: // this is a "get" call
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700797 // Don't check here; we allow nested gets.
Elliott Hughesa2501992011-08-26 19:39:54 -0700798 threadEnv->critical++;
799 break;
800 case kFlag_CritRelease: // this is a "release" call
801 threadEnv->critical--;
802 if (threadEnv->critical < 0) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700803 JniAbortF(function_name_, "thread %s called too many critical releases", ToStr<Thread>(*self).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700804 return;
805 }
806 break;
807 default:
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800808 LOG(FATAL) << "Bad flags (internal error): " << flags;
Elliott Hughesa2501992011-08-26 19:39:54 -0700809 }
810
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700811 // Verify that, if an exception has been raised, the native code doesn't
812 // make any JNI calls other than the Exception* methods.
Elliott Hughesa2501992011-08-26 19:39:54 -0700813 if ((flags & kFlag_ExcepOkay) == 0 && self->IsExceptionPending()) {
Elliott Hughes30646832011-10-13 16:59:46 -0700814 std::string type(PrettyTypeOf(self->GetException()));
Elliott Hughes30646832011-10-13 16:59:46 -0700815 // TODO: write native code that doesn't require allocation for dumping an exception.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700816 // TODO: do we care any more? art always dumps pending exceptions on aborting threads.
Elliott Hughes30646832011-10-13 16:59:46 -0700817 if (type != "java.lang.OutOfMemoryError") {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700818 JniAbortF(function_name_, "JNI %s called with pending exception: %s",
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700819 function_name_, type.c_str(), jniGetStackTrace(soa_.Env()).c_str());
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700820 } else {
821 JniAbortF(function_name_, "JNI %s called with %s pending", function_name_, type.c_str());
Elliott Hughes30646832011-10-13 16:59:46 -0700822 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700823 return;
824 }
825 }
826
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700827 // Verifies that "bytes" points to valid Modified UTF-8 data.
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700828 void CheckUtfString(const char* bytes, bool nullable) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700829 if (bytes == NULL) {
830 if (!nullable) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700831 JniAbortF(function_name_, "non-nullable const char* was NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -0700832 return;
833 }
834 return;
835 }
836
837 const char* errorKind = NULL;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700838 uint8_t utf8 = CheckUtfBytes(bytes, &errorKind);
Elliott Hughesa2501992011-08-26 19:39:54 -0700839 if (errorKind != NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700840 JniAbortF(function_name_,
841 "input is not valid Modified UTF-8: illegal %s byte %#x\n"
842 " string: '%s'", errorKind, utf8, bytes);
Elliott Hughesa2501992011-08-26 19:39:54 -0700843 return;
844 }
845 }
846
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700847 static uint8_t CheckUtfBytes(const char* bytes, const char** errorKind) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700848 while (*bytes != '\0') {
849 uint8_t utf8 = *(bytes++);
850 // Switch on the high four bits.
851 switch (utf8 >> 4) {
852 case 0x00:
853 case 0x01:
854 case 0x02:
855 case 0x03:
856 case 0x04:
857 case 0x05:
858 case 0x06:
859 case 0x07:
860 // Bit pattern 0xxx. No need for any extra bytes.
861 break;
862 case 0x08:
863 case 0x09:
864 case 0x0a:
865 case 0x0b:
866 case 0x0f:
867 /*
868 * Bit pattern 10xx or 1111, which are illegal start bytes.
869 * Note: 1111 is valid for normal UTF-8, but not the
Elliott Hughes78090d12011-10-07 14:31:47 -0700870 * Modified UTF-8 used here.
Elliott Hughesa2501992011-08-26 19:39:54 -0700871 */
872 *errorKind = "start";
873 return utf8;
874 case 0x0e:
875 // Bit pattern 1110, so there are two additional bytes.
876 utf8 = *(bytes++);
877 if ((utf8 & 0xc0) != 0x80) {
878 *errorKind = "continuation";
879 return utf8;
880 }
881 // Fall through to take care of the final byte.
882 case 0x0c:
883 case 0x0d:
884 // Bit pattern 110x, so there is one additional byte.
885 utf8 = *(bytes++);
886 if ((utf8 & 0xc0) != 0x80) {
887 *errorKind = "continuation";
888 return utf8;
889 }
890 break;
891 }
892 }
893 return 0;
894 }
895
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700896 const ScopedObjectAccess soa_;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700897 const char* function_name_;
898 int flags_;
899 bool has_method_;
Elliott Hughes92cb4982011-12-16 16:57:28 -0800900 int indent_;
Elliott Hughesa2501992011-08-26 19:39:54 -0700901
902 DISALLOW_COPY_AND_ASSIGN(ScopedCheck);
903};
904
905#define CHECK_JNI_ENTRY(flags, types, args...) \
906 ScopedCheck sc(env, flags, __FUNCTION__); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700907 sc.Check(true, types, ##args)
Elliott Hughesa2501992011-08-26 19:39:54 -0700908
909#define CHECK_JNI_EXIT(type, exp) ({ \
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700910 typeof(exp) _rc = (exp); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700911 sc.Check(false, type, _rc); \
Elliott Hughesa2501992011-08-26 19:39:54 -0700912 _rc; })
913#define CHECK_JNI_EXIT_VOID() \
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700914 sc.Check(false, "V")
Elliott Hughesa2501992011-08-26 19:39:54 -0700915
916/*
917 * ===========================================================================
918 * Guarded arrays
919 * ===========================================================================
920 */
921
922#define kGuardLen 512 /* must be multiple of 2 */
923#define kGuardPattern 0xd5e3 /* uncommon values; d5e3d5e3 invalid addr */
924#define kGuardMagic 0xffd5aa96
925
926/* this gets tucked in at the start of the buffer; struct size must be even */
927struct GuardedCopy {
928 uint32_t magic;
929 uLong adler;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700930 size_t original_length;
931 const void* original_ptr;
Elliott Hughesa2501992011-08-26 19:39:54 -0700932
933 /* find the GuardedCopy given the pointer into the "live" data */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700934 static inline const GuardedCopy* FromData(const void* dataBuf) {
935 return reinterpret_cast<const GuardedCopy*>(ActualBuffer(dataBuf));
Elliott Hughesa2501992011-08-26 19:39:54 -0700936 }
937
938 /*
939 * Create an over-sized buffer to hold the contents of "buf". Copy it in,
940 * filling in the area around it with guard data.
941 *
942 * We use a 16-bit pattern to make a rogue memset less likely to elude us.
943 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700944 static void* Create(const void* buf, size_t len, bool modOkay) {
945 size_t newLen = ActualLength(len);
946 uint8_t* newBuf = DebugAlloc(newLen);
Elliott Hughesa2501992011-08-26 19:39:54 -0700947
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700948 // Fill it in with a pattern.
Elliott Hughesba8eee12012-01-24 20:25:24 -0800949 uint16_t* pat = reinterpret_cast<uint16_t*>(newBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -0700950 for (size_t i = 0; i < newLen / 2; i++) {
951 *pat++ = kGuardPattern;
952 }
953
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700954 // Copy the data in; note "len" could be zero.
Elliott Hughesa2501992011-08-26 19:39:54 -0700955 memcpy(newBuf + kGuardLen / 2, buf, len);
956
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700957 // If modification is not expected, grab a checksum.
Elliott Hughesa2501992011-08-26 19:39:54 -0700958 uLong adler = 0;
959 if (!modOkay) {
960 adler = adler32(0L, Z_NULL, 0);
Elliott Hughesba8eee12012-01-24 20:25:24 -0800961 adler = adler32(adler, reinterpret_cast<const Bytef*>(buf), len);
962 *reinterpret_cast<uLong*>(newBuf) = adler;
Elliott Hughesa2501992011-08-26 19:39:54 -0700963 }
964
965 GuardedCopy* pExtra = reinterpret_cast<GuardedCopy*>(newBuf);
966 pExtra->magic = kGuardMagic;
967 pExtra->adler = adler;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700968 pExtra->original_ptr = buf;
969 pExtra->original_length = len;
Elliott Hughesa2501992011-08-26 19:39:54 -0700970
971 return newBuf + kGuardLen / 2;
972 }
973
974 /*
975 * Free up the guard buffer, scrub it, and return the original pointer.
976 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700977 static void* Destroy(void* dataBuf) {
978 const GuardedCopy* pExtra = GuardedCopy::FromData(dataBuf);
Elliott Hughesba8eee12012-01-24 20:25:24 -0800979 void* original_ptr = const_cast<void*>(pExtra->original_ptr);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700980 size_t len = pExtra->original_length;
981 DebugFree(dataBuf, len);
982 return original_ptr;
Elliott Hughesa2501992011-08-26 19:39:54 -0700983 }
984
985 /*
986 * Verify the guard area and, if "modOkay" is false, that the data itself
987 * has not been altered.
988 *
989 * The caller has already checked that "dataBuf" is non-NULL.
990 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700991 static void Check(const char* functionName, const void* dataBuf, bool modOkay) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700992 static const uint32_t kMagicCmp = kGuardMagic;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700993 const uint8_t* fullBuf = ActualBuffer(dataBuf);
994 const GuardedCopy* pExtra = GuardedCopy::FromData(dataBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -0700995
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700996 // Before we do anything with "pExtra", check the magic number. We
997 // do the check with memcmp rather than "==" in case the pointer is
998 // unaligned. If it points to completely bogus memory we're going
999 // to crash, but there's no easy way around that.
Elliott Hughesa2501992011-08-26 19:39:54 -07001000 if (memcmp(&pExtra->magic, &kMagicCmp, 4) != 0) {
1001 uint8_t buf[4];
1002 memcpy(buf, &pExtra->magic, 4);
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001003 JniAbortF(functionName,
1004 "guard magic does not match (found 0x%02x%02x%02x%02x) -- incorrect data pointer %p?",
1005 buf[3], buf[2], buf[1], buf[0], dataBuf); // Assumes little-endian.
Elliott Hughesa2501992011-08-26 19:39:54 -07001006 }
1007
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001008 size_t len = pExtra->original_length;
Elliott Hughesa2501992011-08-26 19:39:54 -07001009
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001010 // Check bottom half of guard; skip over optional checksum storage.
Elliott Hughesba8eee12012-01-24 20:25:24 -08001011 const uint16_t* pat = reinterpret_cast<const uint16_t*>(fullBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -07001012 for (size_t i = sizeof(GuardedCopy) / 2; i < (kGuardLen / 2 - sizeof(GuardedCopy)) / 2; i++) {
1013 if (pat[i] != kGuardPattern) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001014 JniAbortF(functionName, "guard pattern(1) disturbed at %p +%d", fullBuf, i*2);
Elliott Hughesa2501992011-08-26 19:39:54 -07001015 }
1016 }
1017
1018 int offset = kGuardLen / 2 + len;
1019 if (offset & 0x01) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001020 // Odd byte; expected value depends on endian.
Elliott Hughesa2501992011-08-26 19:39:54 -07001021 const uint16_t patSample = kGuardPattern;
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001022 uint8_t expected_byte = reinterpret_cast<const uint8_t*>(&patSample)[1];
1023 if (fullBuf[offset] != expected_byte) {
1024 JniAbortF(functionName, "guard pattern disturbed in odd byte after %p +%d 0x%02x 0x%02x",
1025 fullBuf, offset, fullBuf[offset], expected_byte);
Elliott Hughesa2501992011-08-26 19:39:54 -07001026 }
1027 offset++;
1028 }
1029
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001030 // Check top half of guard.
Elliott Hughesba8eee12012-01-24 20:25:24 -08001031 pat = reinterpret_cast<const uint16_t*>(fullBuf + offset);
Elliott Hughesa2501992011-08-26 19:39:54 -07001032 for (size_t i = 0; i < kGuardLen / 4; i++) {
1033 if (pat[i] != kGuardPattern) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001034 JniAbortF(functionName, "guard pattern(2) disturbed at %p +%d", fullBuf, offset + i*2);
Elliott Hughesa2501992011-08-26 19:39:54 -07001035 }
1036 }
1037
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001038 // If modification is not expected, verify checksum. Strictly speaking
1039 // this is wrong: if we told the client that we made a copy, there's no
1040 // reason they can't alter the buffer.
Elliott Hughesa2501992011-08-26 19:39:54 -07001041 if (!modOkay) {
1042 uLong adler = adler32(0L, Z_NULL, 0);
1043 adler = adler32(adler, (const Bytef*)dataBuf, len);
1044 if (pExtra->adler != adler) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001045 JniAbortF(functionName, "buffer modified (0x%08lx vs 0x%08lx) at address %p",
1046 pExtra->adler, adler, dataBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -07001047 }
1048 }
1049 }
1050
1051 private:
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001052 static uint8_t* DebugAlloc(size_t len) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001053 void* result = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
1054 if (result == MAP_FAILED) {
1055 PLOG(FATAL) << "GuardedCopy::create mmap(" << len << ") failed";
1056 }
1057 return reinterpret_cast<uint8_t*>(result);
1058 }
1059
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001060 static void DebugFree(void* dataBuf, size_t len) {
1061 uint8_t* fullBuf = ActualBuffer(dataBuf);
1062 size_t totalByteCount = ActualLength(len);
Elliott Hughesa2501992011-08-26 19:39:54 -07001063 // TODO: we could mprotect instead, and keep the allocation around for a while.
1064 // This would be even more expensive, but it might catch more errors.
1065 // if (mprotect(fullBuf, totalByteCount, PROT_NONE) != 0) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -07001066 // PLOG(WARNING) << "mprotect(PROT_NONE) failed";
Elliott Hughesa2501992011-08-26 19:39:54 -07001067 // }
1068 if (munmap(fullBuf, totalByteCount) != 0) {
Elliott Hughesba8eee12012-01-24 20:25:24 -08001069 PLOG(FATAL) << "munmap(" << reinterpret_cast<void*>(fullBuf) << ", " << totalByteCount << ") failed";
Elliott Hughesa2501992011-08-26 19:39:54 -07001070 }
1071 }
1072
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001073 static const uint8_t* ActualBuffer(const void* dataBuf) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001074 return reinterpret_cast<const uint8_t*>(dataBuf) - kGuardLen / 2;
1075 }
1076
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001077 static uint8_t* ActualBuffer(void* dataBuf) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001078 return reinterpret_cast<uint8_t*>(dataBuf) - kGuardLen / 2;
1079 }
1080
1081 // Underlying length of a user allocation of 'length' bytes.
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001082 static size_t ActualLength(size_t length) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001083 return (length + kGuardLen + 1) & ~0x01;
1084 }
1085};
1086
1087/*
1088 * Create a guarded copy of a primitive array. Modifications to the copied
1089 * data are allowed. Returns a pointer to the copied data.
1090 */
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001091static void* CreateGuardedPACopy(JNIEnv* env, const jarray java_array, jboolean* isCopy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001092 ScopedObjectAccess soa(env);
Elliott Hughesa2501992011-08-26 19:39:54 -07001093
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001094 mirror::Array* a = soa.Decode<mirror::Array*>(java_array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001095 size_t component_size = a->GetClass()->GetComponentSize();
1096 size_t byte_count = a->GetLength() * component_size;
1097 void* result = GuardedCopy::Create(a->GetRawData(component_size), byte_count, true);
Elliott Hughesa2501992011-08-26 19:39:54 -07001098 if (isCopy != NULL) {
1099 *isCopy = JNI_TRUE;
1100 }
1101 return result;
1102}
1103
1104/*
1105 * Perform the array "release" operation, which may or may not copy data
Elliott Hughes81ff3182012-03-23 20:35:56 -07001106 * back into the managed heap, and may or may not release the underlying storage.
Elliott Hughesa2501992011-08-26 19:39:54 -07001107 */
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001108static void ReleaseGuardedPACopy(JNIEnv* env, jarray java_array, void* dataBuf, int mode) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001109 if (reinterpret_cast<uintptr_t>(dataBuf) == kNoCopyMagic) {
1110 return;
1111 }
1112
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001113 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001114 mirror::Array* a = soa.Decode<mirror::Array*>(java_array);
Elliott Hughesa2501992011-08-26 19:39:54 -07001115
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001116 GuardedCopy::Check(__FUNCTION__, dataBuf, true);
Elliott Hughesa2501992011-08-26 19:39:54 -07001117
1118 if (mode != JNI_ABORT) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001119 size_t len = GuardedCopy::FromData(dataBuf)->original_length;
Ian Rogersa15e67d2012-02-28 13:51:55 -08001120 memcpy(a->GetRawData(a->GetClass()->GetComponentSize()), dataBuf, len);
Elliott Hughesa2501992011-08-26 19:39:54 -07001121 }
1122 if (mode != JNI_COMMIT) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001123 GuardedCopy::Destroy(dataBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -07001124 }
1125}
1126
1127/*
1128 * ===========================================================================
1129 * JNI functions
1130 * ===========================================================================
1131 */
1132
1133class CheckJNI {
1134 public:
1135 static jint GetVersion(JNIEnv* env) {
1136 CHECK_JNI_ENTRY(kFlag_Default, "E", env);
1137 return CHECK_JNI_EXIT("I", baseEnv(env)->GetVersion(env));
1138 }
1139
1140 static jclass DefineClass(JNIEnv* env, const char* name, jobject loader, const jbyte* buf, jsize bufLen) {
1141 CHECK_JNI_ENTRY(kFlag_Default, "EuLpz", env, name, loader, buf, bufLen);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001142 sc.CheckClassName(name);
Elliott Hughesa2501992011-08-26 19:39:54 -07001143 return CHECK_JNI_EXIT("c", baseEnv(env)->DefineClass(env, name, loader, buf, bufLen));
1144 }
1145
1146 static jclass FindClass(JNIEnv* env, const char* name) {
1147 CHECK_JNI_ENTRY(kFlag_Default, "Eu", env, name);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001148 sc.CheckClassName(name);
Elliott Hughesa2501992011-08-26 19:39:54 -07001149 return CHECK_JNI_EXIT("c", baseEnv(env)->FindClass(env, name));
1150 }
1151
Elliott Hughese84278b2012-03-22 10:06:53 -07001152 static jclass GetSuperclass(JNIEnv* env, jclass c) {
1153 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1154 return CHECK_JNI_EXIT("c", baseEnv(env)->GetSuperclass(env, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001155 }
1156
Elliott Hughese84278b2012-03-22 10:06:53 -07001157 static jboolean IsAssignableFrom(JNIEnv* env, jclass c1, jclass c2) {
1158 CHECK_JNI_ENTRY(kFlag_Default, "Ecc", env, c1, c2);
1159 return CHECK_JNI_EXIT("b", baseEnv(env)->IsAssignableFrom(env, c1, c2));
Elliott Hughesa2501992011-08-26 19:39:54 -07001160 }
1161
1162 static jmethodID FromReflectedMethod(JNIEnv* env, jobject method) {
1163 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, method);
1164 // TODO: check that 'field' is a java.lang.reflect.Method.
1165 return CHECK_JNI_EXIT("m", baseEnv(env)->FromReflectedMethod(env, method));
1166 }
1167
1168 static jfieldID FromReflectedField(JNIEnv* env, jobject field) {
1169 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, field);
1170 // TODO: check that 'field' is a java.lang.reflect.Field.
1171 return CHECK_JNI_EXIT("f", baseEnv(env)->FromReflectedField(env, field));
1172 }
1173
1174 static jobject ToReflectedMethod(JNIEnv* env, jclass cls, jmethodID mid, jboolean isStatic) {
1175 CHECK_JNI_ENTRY(kFlag_Default, "Ecmb", env, cls, mid, isStatic);
1176 return CHECK_JNI_EXIT("L", baseEnv(env)->ToReflectedMethod(env, cls, mid, isStatic));
1177 }
1178
1179 static jobject ToReflectedField(JNIEnv* env, jclass cls, jfieldID fid, jboolean isStatic) {
1180 CHECK_JNI_ENTRY(kFlag_Default, "Ecfb", env, cls, fid, isStatic);
1181 return CHECK_JNI_EXIT("L", baseEnv(env)->ToReflectedField(env, cls, fid, isStatic));
1182 }
1183
1184 static jint Throw(JNIEnv* env, jthrowable obj) {
1185 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1186 // TODO: check that 'obj' is a java.lang.Throwable.
1187 return CHECK_JNI_EXIT("I", baseEnv(env)->Throw(env, obj));
1188 }
1189
Elliott Hughese84278b2012-03-22 10:06:53 -07001190 static jint ThrowNew(JNIEnv* env, jclass c, const char* message) {
1191 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Ecu", env, c, message);
1192 return CHECK_JNI_EXIT("I", baseEnv(env)->ThrowNew(env, c, message));
Elliott Hughesa2501992011-08-26 19:39:54 -07001193 }
1194
1195 static jthrowable ExceptionOccurred(JNIEnv* env) {
1196 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1197 return CHECK_JNI_EXIT("L", baseEnv(env)->ExceptionOccurred(env));
1198 }
1199
1200 static void ExceptionDescribe(JNIEnv* env) {
1201 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1202 baseEnv(env)->ExceptionDescribe(env);
1203 CHECK_JNI_EXIT_VOID();
1204 }
1205
1206 static void ExceptionClear(JNIEnv* env) {
1207 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1208 baseEnv(env)->ExceptionClear(env);
1209 CHECK_JNI_EXIT_VOID();
1210 }
1211
1212 static void FatalError(JNIEnv* env, const char* msg) {
1213 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Eu", env, msg);
1214 baseEnv(env)->FatalError(env, msg);
1215 CHECK_JNI_EXIT_VOID();
1216 }
1217
1218 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
1219 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EI", env, capacity);
1220 return CHECK_JNI_EXIT("I", baseEnv(env)->PushLocalFrame(env, capacity));
1221 }
1222
1223 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
1224 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, res);
1225 return CHECK_JNI_EXIT("L", baseEnv(env)->PopLocalFrame(env, res));
1226 }
1227
1228 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
1229 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1230 return CHECK_JNI_EXIT("L", baseEnv(env)->NewGlobalRef(env, obj));
1231 }
1232
1233 static jobject NewLocalRef(JNIEnv* env, jobject ref) {
1234 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, ref);
1235 return CHECK_JNI_EXIT("L", baseEnv(env)->NewLocalRef(env, ref));
1236 }
1237
1238 static void DeleteGlobalRef(JNIEnv* env, jobject globalRef) {
1239 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, globalRef);
1240 if (globalRef != NULL && GetIndirectRefKind(globalRef) != kGlobal) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001241 JniAbortF(__FUNCTION__, "DeleteGlobalRef on %s: %p",
1242 ToStr<IndirectRefKind>(GetIndirectRefKind(globalRef)).c_str(), globalRef);
Elliott Hughesa2501992011-08-26 19:39:54 -07001243 } else {
1244 baseEnv(env)->DeleteGlobalRef(env, globalRef);
1245 CHECK_JNI_EXIT_VOID();
1246 }
1247 }
1248
1249 static void DeleteWeakGlobalRef(JNIEnv* env, jweak weakGlobalRef) {
1250 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, weakGlobalRef);
1251 if (weakGlobalRef != NULL && GetIndirectRefKind(weakGlobalRef) != kWeakGlobal) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001252 JniAbortF(__FUNCTION__, "DeleteWeakGlobalRef on %s: %p",
1253 ToStr<IndirectRefKind>(GetIndirectRefKind(weakGlobalRef)).c_str(), weakGlobalRef);
Elliott Hughesa2501992011-08-26 19:39:54 -07001254 } else {
1255 baseEnv(env)->DeleteWeakGlobalRef(env, weakGlobalRef);
1256 CHECK_JNI_EXIT_VOID();
1257 }
1258 }
1259
1260 static void DeleteLocalRef(JNIEnv* env, jobject localRef) {
1261 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, localRef);
Ian Rogers959f8ed2012-02-07 16:33:37 -08001262 if (localRef != NULL && GetIndirectRefKind(localRef) != kLocal && !IsSirtLocalRef(env, localRef)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001263 JniAbortF(__FUNCTION__, "DeleteLocalRef on %s: %p",
1264 ToStr<IndirectRefKind>(GetIndirectRefKind(localRef)).c_str(), localRef);
Elliott Hughesa2501992011-08-26 19:39:54 -07001265 } else {
1266 baseEnv(env)->DeleteLocalRef(env, localRef);
1267 CHECK_JNI_EXIT_VOID();
1268 }
1269 }
1270
1271 static jint EnsureLocalCapacity(JNIEnv *env, jint capacity) {
1272 CHECK_JNI_ENTRY(kFlag_Default, "EI", env, capacity);
1273 return CHECK_JNI_EXIT("I", baseEnv(env)->EnsureLocalCapacity(env, capacity));
1274 }
1275
1276 static jboolean IsSameObject(JNIEnv* env, jobject ref1, jobject ref2) {
1277 CHECK_JNI_ENTRY(kFlag_Default, "ELL", env, ref1, ref2);
1278 return CHECK_JNI_EXIT("b", baseEnv(env)->IsSameObject(env, ref1, ref2));
1279 }
1280
Elliott Hughese84278b2012-03-22 10:06:53 -07001281 static jobject AllocObject(JNIEnv* env, jclass c) {
1282 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1283 return CHECK_JNI_EXIT("L", baseEnv(env)->AllocObject(env, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001284 }
1285
Elliott Hughese84278b2012-03-22 10:06:53 -07001286 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
1287 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
Elliott Hughesa2501992011-08-26 19:39:54 -07001288 va_list args;
1289 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -07001290 jobject result = baseEnv(env)->NewObjectV(env, c, mid, args);
Elliott Hughesa2501992011-08-26 19:39:54 -07001291 va_end(args);
1292 return CHECK_JNI_EXIT("L", result);
1293 }
1294
Elliott Hughese84278b2012-03-22 10:06:53 -07001295 static jobject NewObjectV(JNIEnv* env, jclass c, jmethodID mid, va_list args) {
1296 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
1297 return CHECK_JNI_EXIT("L", baseEnv(env)->NewObjectV(env, c, mid, args));
Elliott Hughesa2501992011-08-26 19:39:54 -07001298 }
1299
Elliott Hughese84278b2012-03-22 10:06:53 -07001300 static jobject NewObjectA(JNIEnv* env, jclass c, jmethodID mid, jvalue* args) {
1301 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
1302 return CHECK_JNI_EXIT("L", baseEnv(env)->NewObjectA(env, c, mid, args));
Elliott Hughesa2501992011-08-26 19:39:54 -07001303 }
1304
1305 static jclass GetObjectClass(JNIEnv* env, jobject obj) {
1306 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1307 return CHECK_JNI_EXIT("c", baseEnv(env)->GetObjectClass(env, obj));
1308 }
1309
Elliott Hughese84278b2012-03-22 10:06:53 -07001310 static jboolean IsInstanceOf(JNIEnv* env, jobject obj, jclass c) {
1311 CHECK_JNI_ENTRY(kFlag_Default, "ELc", env, obj, c);
1312 return CHECK_JNI_EXIT("b", baseEnv(env)->IsInstanceOf(env, obj, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001313 }
1314
Elliott Hughese84278b2012-03-22 10:06:53 -07001315 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1316 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1317 return CHECK_JNI_EXIT("m", baseEnv(env)->GetMethodID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001318 }
1319
Elliott Hughese84278b2012-03-22 10:06:53 -07001320 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1321 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1322 return CHECK_JNI_EXIT("f", baseEnv(env)->GetFieldID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001323 }
1324
Elliott Hughese84278b2012-03-22 10:06:53 -07001325 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1326 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1327 return CHECK_JNI_EXIT("m", baseEnv(env)->GetStaticMethodID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001328 }
1329
Elliott Hughese84278b2012-03-22 10:06:53 -07001330 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1331 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1332 return CHECK_JNI_EXIT("f", baseEnv(env)->GetStaticFieldID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001333 }
1334
1335#define FIELD_ACCESSORS(_ctype, _jname, _type) \
Elliott Hughese84278b2012-03-22 10:06:53 -07001336 static _ctype GetStatic##_jname##Field(JNIEnv* env, jclass c, jfieldID fid) { \
1337 CHECK_JNI_ENTRY(kFlag_Default, "Ecf", env, c, fid); \
1338 sc.CheckStaticFieldID(c, fid); \
1339 return CHECK_JNI_EXIT(_type, baseEnv(env)->GetStatic##_jname##Field(env, c, fid)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001340 } \
1341 static _ctype Get##_jname##Field(JNIEnv* env, jobject obj, jfieldID fid) { \
1342 CHECK_JNI_ENTRY(kFlag_Default, "ELf", env, obj, fid); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001343 sc.CheckInstanceFieldID(obj, fid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001344 return CHECK_JNI_EXIT(_type, baseEnv(env)->Get##_jname##Field(env, obj, fid)); \
1345 } \
Elliott Hughese84278b2012-03-22 10:06:53 -07001346 static void SetStatic##_jname##Field(JNIEnv* env, jclass c, jfieldID fid, _ctype value) { \
1347 CHECK_JNI_ENTRY(kFlag_Default, "Ecf" _type, env, c, fid, value); \
1348 sc.CheckStaticFieldID(c, fid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001349 /* "value" arg only used when type == ref */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001350 sc.CheckFieldType((jobject)(uint32_t)value, fid, _type[0], true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001351 baseEnv(env)->SetStatic##_jname##Field(env, c, fid, value); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001352 CHECK_JNI_EXIT_VOID(); \
1353 } \
1354 static void Set##_jname##Field(JNIEnv* env, jobject obj, jfieldID fid, _ctype value) { \
1355 CHECK_JNI_ENTRY(kFlag_Default, "ELf" _type, env, obj, fid, value); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001356 sc.CheckInstanceFieldID(obj, fid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001357 /* "value" arg only used when type == ref */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001358 sc.CheckFieldType((jobject)(uint32_t) value, fid, _type[0], false); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001359 baseEnv(env)->Set##_jname##Field(env, obj, fid, value); \
1360 CHECK_JNI_EXIT_VOID(); \
1361 }
1362
1363FIELD_ACCESSORS(jobject, Object, "L");
1364FIELD_ACCESSORS(jboolean, Boolean, "Z");
1365FIELD_ACCESSORS(jbyte, Byte, "B");
1366FIELD_ACCESSORS(jchar, Char, "C");
1367FIELD_ACCESSORS(jshort, Short, "S");
1368FIELD_ACCESSORS(jint, Int, "I");
1369FIELD_ACCESSORS(jlong, Long, "J");
1370FIELD_ACCESSORS(jfloat, Float, "F");
1371FIELD_ACCESSORS(jdouble, Double, "D");
1372
1373#define CALL(_ctype, _jname, _retdecl, _retasgn, _retok, _retsig) \
1374 /* Virtual... */ \
1375 static _ctype Call##_jname##Method(JNIEnv* env, jobject obj, \
1376 jmethodID mid, ...) \
1377 { \
1378 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001379 sc.CheckSig(mid, _retsig, false); \
1380 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001381 _retdecl; \
1382 va_list args; \
1383 va_start(args, mid); \
Elliott Hughesba8eee12012-01-24 20:25:24 -08001384 _retasgn(baseEnv(env)->Call##_jname##MethodV(env, obj, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001385 va_end(args); \
1386 _retok; \
1387 } \
1388 static _ctype Call##_jname##MethodV(JNIEnv* env, jobject obj, \
1389 jmethodID mid, va_list args) \
1390 { \
1391 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001392 sc.CheckSig(mid, _retsig, false); \
1393 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001394 _retdecl; \
Elliott Hughesba8eee12012-01-24 20:25:24 -08001395 _retasgn(baseEnv(env)->Call##_jname##MethodV(env, obj, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001396 _retok; \
1397 } \
1398 static _ctype Call##_jname##MethodA(JNIEnv* env, jobject obj, \
1399 jmethodID mid, jvalue* args) \
1400 { \
1401 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001402 sc.CheckSig(mid, _retsig, false); \
1403 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001404 _retdecl; \
Elliott Hughesba8eee12012-01-24 20:25:24 -08001405 _retasgn(baseEnv(env)->Call##_jname##MethodA(env, obj, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001406 _retok; \
1407 } \
1408 /* Non-virtual... */ \
1409 static _ctype CallNonvirtual##_jname##Method(JNIEnv* env, \
Elliott Hughese84278b2012-03-22 10:06:53 -07001410 jobject obj, jclass c, jmethodID mid, ...) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001411 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001412 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001413 sc.CheckSig(mid, _retsig, false); \
1414 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001415 _retdecl; \
1416 va_list args; \
1417 va_start(args, mid); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001418 _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodV(env, obj, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001419 va_end(args); \
1420 _retok; \
1421 } \
1422 static _ctype CallNonvirtual##_jname##MethodV(JNIEnv* env, \
Elliott Hughese84278b2012-03-22 10:06:53 -07001423 jobject obj, jclass c, jmethodID mid, va_list args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001424 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001425 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001426 sc.CheckSig(mid, _retsig, false); \
1427 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001428 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001429 _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodV(env, obj, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001430 _retok; \
1431 } \
1432 static _ctype CallNonvirtual##_jname##MethodA(JNIEnv* env, \
Elliott Hughese84278b2012-03-22 10:06:53 -07001433 jobject obj, jclass c, jmethodID mid, jvalue* args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001434 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001435 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001436 sc.CheckSig(mid, _retsig, false); \
1437 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001438 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001439 _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodA(env, obj, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001440 _retok; \
1441 } \
1442 /* Static... */ \
Elliott Hughese84278b2012-03-22 10:06:53 -07001443 static _ctype CallStatic##_jname##Method(JNIEnv* env, jclass c, jmethodID mid, ...) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001444 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001445 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001446 sc.CheckSig(mid, _retsig, true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001447 sc.CheckStaticMethod(c, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001448 _retdecl; \
1449 va_list args; \
1450 va_start(args, mid); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001451 _retasgn(baseEnv(env)->CallStatic##_jname##MethodV(env, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001452 va_end(args); \
1453 _retok; \
1454 } \
Elliott Hughese84278b2012-03-22 10:06:53 -07001455 static _ctype CallStatic##_jname##MethodV(JNIEnv* env, jclass c, jmethodID mid, va_list args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001456 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001457 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001458 sc.CheckSig(mid, _retsig, true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001459 sc.CheckStaticMethod(c, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001460 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001461 _retasgn(baseEnv(env)->CallStatic##_jname##MethodV(env, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001462 _retok; \
1463 } \
Elliott Hughese84278b2012-03-22 10:06:53 -07001464 static _ctype CallStatic##_jname##MethodA(JNIEnv* env, jclass c, jmethodID mid, jvalue* args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001465 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001466 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001467 sc.CheckSig(mid, _retsig, true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001468 sc.CheckStaticMethod(c, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001469 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001470 _retasgn(baseEnv(env)->CallStatic##_jname##MethodA(env, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001471 _retok; \
1472 }
1473
1474#define NON_VOID_RETURN(_retsig, _ctype) return CHECK_JNI_EXIT(_retsig, (_ctype) result)
1475#define VOID_RETURN CHECK_JNI_EXIT_VOID()
1476
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001477CALL(jobject, Object, mirror::Object* result, result = reinterpret_cast<mirror::Object*>, NON_VOID_RETURN("L", jobject), "L");
Elliott Hughesba8eee12012-01-24 20:25:24 -08001478CALL(jboolean, Boolean, jboolean result, result =, NON_VOID_RETURN("Z", jboolean), "Z");
1479CALL(jbyte, Byte, jbyte result, result =, NON_VOID_RETURN("B", jbyte), "B");
1480CALL(jchar, Char, jchar result, result =, NON_VOID_RETURN("C", jchar), "C");
1481CALL(jshort, Short, jshort result, result =, NON_VOID_RETURN("S", jshort), "S");
1482CALL(jint, Int, jint result, result =, NON_VOID_RETURN("I", jint), "I");
1483CALL(jlong, Long, jlong result, result =, NON_VOID_RETURN("J", jlong), "J");
1484CALL(jfloat, Float, jfloat result, result =, NON_VOID_RETURN("F", jfloat), "F");
1485CALL(jdouble, Double, jdouble result, result =, NON_VOID_RETURN("D", jdouble), "D");
Elliott Hughesa2501992011-08-26 19:39:54 -07001486CALL(void, Void, , , VOID_RETURN, "V");
1487
1488 static jstring NewString(JNIEnv* env, const jchar* unicodeChars, jsize len) {
1489 CHECK_JNI_ENTRY(kFlag_Default, "Epz", env, unicodeChars, len);
1490 return CHECK_JNI_EXIT("s", baseEnv(env)->NewString(env, unicodeChars, len));
1491 }
1492
1493 static jsize GetStringLength(JNIEnv* env, jstring string) {
1494 CHECK_JNI_ENTRY(kFlag_CritOkay, "Es", env, string);
1495 return CHECK_JNI_EXIT("I", baseEnv(env)->GetStringLength(env, string));
1496 }
1497
1498 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* isCopy) {
1499 CHECK_JNI_ENTRY(kFlag_CritOkay, "Esp", env, java_string, isCopy);
1500 const jchar* result = baseEnv(env)->GetStringChars(env, java_string, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001501 if (sc.ForceCopy() && result != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001502 mirror::String* s = sc.soa().Decode<mirror::String*>(java_string);
Elliott Hughesa2501992011-08-26 19:39:54 -07001503 int byteCount = s->GetLength() * 2;
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001504 result = (const jchar*) GuardedCopy::Create(result, byteCount, false);
Elliott Hughesa2501992011-08-26 19:39:54 -07001505 if (isCopy != NULL) {
1506 *isCopy = JNI_TRUE;
1507 }
1508 }
1509 return CHECK_JNI_EXIT("p", result);
1510 }
1511
1512 static void ReleaseStringChars(JNIEnv* env, jstring string, const jchar* chars) {
1513 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "Esp", env, string, chars);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001514 sc.CheckNonNull(chars);
1515 if (sc.ForceCopy()) {
1516 GuardedCopy::Check(__FUNCTION__, chars, false);
Elliott Hughesba8eee12012-01-24 20:25:24 -08001517 chars = reinterpret_cast<const jchar*>(GuardedCopy::Destroy(const_cast<jchar*>(chars)));
Elliott Hughesa2501992011-08-26 19:39:54 -07001518 }
1519 baseEnv(env)->ReleaseStringChars(env, string, chars);
1520 CHECK_JNI_EXIT_VOID();
1521 }
1522
1523 static jstring NewStringUTF(JNIEnv* env, const char* bytes) {
1524 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Eu", env, bytes); // TODO: show pointer and truncate string.
1525 return CHECK_JNI_EXIT("s", baseEnv(env)->NewStringUTF(env, bytes));
1526 }
1527
1528 static jsize GetStringUTFLength(JNIEnv* env, jstring string) {
1529 CHECK_JNI_ENTRY(kFlag_CritOkay, "Es", env, string);
1530 return CHECK_JNI_EXIT("I", baseEnv(env)->GetStringUTFLength(env, string));
1531 }
1532
1533 static const char* GetStringUTFChars(JNIEnv* env, jstring string, jboolean* isCopy) {
1534 CHECK_JNI_ENTRY(kFlag_CritOkay, "Esp", env, string, isCopy);
1535 const char* result = baseEnv(env)->GetStringUTFChars(env, string, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001536 if (sc.ForceCopy() && result != NULL) {
1537 result = (const char*) GuardedCopy::Create(result, strlen(result) + 1, false);
Elliott Hughesa2501992011-08-26 19:39:54 -07001538 if (isCopy != NULL) {
1539 *isCopy = JNI_TRUE;
1540 }
1541 }
1542 return CHECK_JNI_EXIT("u", result); // TODO: show pointer and truncate string.
1543 }
1544
1545 static void ReleaseStringUTFChars(JNIEnv* env, jstring string, const char* utf) {
1546 CHECK_JNI_ENTRY(kFlag_ExcepOkay | kFlag_Release, "Esu", env, string, utf); // TODO: show pointer and truncate string.
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001547 if (sc.ForceCopy()) {
1548 GuardedCopy::Check(__FUNCTION__, utf, false);
Elliott Hughesba8eee12012-01-24 20:25:24 -08001549 utf = reinterpret_cast<const char*>(GuardedCopy::Destroy(const_cast<char*>(utf)));
Elliott Hughesa2501992011-08-26 19:39:54 -07001550 }
1551 baseEnv(env)->ReleaseStringUTFChars(env, string, utf);
1552 CHECK_JNI_EXIT_VOID();
1553 }
1554
1555 static jsize GetArrayLength(JNIEnv* env, jarray array) {
1556 CHECK_JNI_ENTRY(kFlag_CritOkay, "Ea", env, array);
1557 return CHECK_JNI_EXIT("I", baseEnv(env)->GetArrayLength(env, array));
1558 }
1559
1560 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass elementClass, jobject initialElement) {
1561 CHECK_JNI_ENTRY(kFlag_Default, "EzcL", env, length, elementClass, initialElement);
1562 return CHECK_JNI_EXIT("a", baseEnv(env)->NewObjectArray(env, length, elementClass, initialElement));
1563 }
1564
1565 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index) {
1566 CHECK_JNI_ENTRY(kFlag_Default, "EaI", env, array, index);
1567 return CHECK_JNI_EXIT("L", baseEnv(env)->GetObjectArrayElement(env, array, index));
1568 }
1569
1570 static void SetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index, jobject value) {
1571 CHECK_JNI_ENTRY(kFlag_Default, "EaIL", env, array, index, value);
1572 baseEnv(env)->SetObjectArrayElement(env, array, index, value);
1573 CHECK_JNI_EXIT_VOID();
1574 }
1575
1576#define NEW_PRIMITIVE_ARRAY(_artype, _jname) \
1577 static _artype New##_jname##Array(JNIEnv* env, jsize length) { \
1578 CHECK_JNI_ENTRY(kFlag_Default, "Ez", env, length); \
1579 return CHECK_JNI_EXIT("a", baseEnv(env)->New##_jname##Array(env, length)); \
1580 }
1581NEW_PRIMITIVE_ARRAY(jbooleanArray, Boolean);
1582NEW_PRIMITIVE_ARRAY(jbyteArray, Byte);
1583NEW_PRIMITIVE_ARRAY(jcharArray, Char);
1584NEW_PRIMITIVE_ARRAY(jshortArray, Short);
1585NEW_PRIMITIVE_ARRAY(jintArray, Int);
1586NEW_PRIMITIVE_ARRAY(jlongArray, Long);
1587NEW_PRIMITIVE_ARRAY(jfloatArray, Float);
1588NEW_PRIMITIVE_ARRAY(jdoubleArray, Double);
1589
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001590struct ForceCopyGetChecker {
Elliott Hughesba8eee12012-01-24 20:25:24 -08001591 public:
Elliott Hughesa2501992011-08-26 19:39:54 -07001592 ForceCopyGetChecker(ScopedCheck& sc, jboolean* isCopy) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001593 force_copy = sc.ForceCopy();
1594 no_copy = 0;
1595 if (force_copy && isCopy != NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001596 // Capture this before the base call tramples on it.
Elliott Hughesba8eee12012-01-24 20:25:24 -08001597 no_copy = *reinterpret_cast<uint32_t*>(isCopy);
Elliott Hughesa2501992011-08-26 19:39:54 -07001598 }
1599 }
1600
1601 template<typename ResultT>
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001602 ResultT Check(JNIEnv* env, jarray array, jboolean* isCopy, ResultT result) {
1603 if (force_copy && result != NULL) {
1604 if (no_copy != kNoCopyMagic) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001605 result = reinterpret_cast<ResultT>(CreateGuardedPACopy(env, array, isCopy));
1606 }
1607 }
1608 return result;
1609 }
1610
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001611 uint32_t no_copy;
1612 bool force_copy;
Elliott Hughesa2501992011-08-26 19:39:54 -07001613};
1614
1615#define GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \
1616 static _ctype* Get##_jname##ArrayElements(JNIEnv* env, _ctype##Array array, jboolean* isCopy) { \
1617 CHECK_JNI_ENTRY(kFlag_Default, "Eap", env, array, isCopy); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001618 _ctype* result = ForceCopyGetChecker(sc, isCopy).Check(env, array, isCopy, baseEnv(env)->Get##_jname##ArrayElements(env, array, isCopy)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001619 return CHECK_JNI_EXIT("p", result); \
1620 }
1621
1622#define RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \
1623 static void Release##_jname##ArrayElements(JNIEnv* env, _ctype##Array array, _ctype* elems, jint mode) { \
1624 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "Eapr", env, array, elems, mode); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001625 sc.CheckNonNull(elems); \
1626 if (sc.ForceCopy()) { \
Elliott Hughesa2501992011-08-26 19:39:54 -07001627 ReleaseGuardedPACopy(env, array, elems, mode); \
1628 } \
1629 baseEnv(env)->Release##_jname##ArrayElements(env, array, elems, mode); \
1630 CHECK_JNI_EXIT_VOID(); \
1631 }
1632
1633#define GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \
1634 static void Get##_jname##ArrayRegion(JNIEnv* env, _ctype##Array array, jsize start, jsize len, _ctype* buf) { \
1635 CHECK_JNI_ENTRY(kFlag_Default, "EaIIp", env, array, start, len, buf); \
1636 baseEnv(env)->Get##_jname##ArrayRegion(env, array, start, len, buf); \
1637 CHECK_JNI_EXIT_VOID(); \
1638 }
1639
1640#define SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \
1641 static void Set##_jname##ArrayRegion(JNIEnv* env, _ctype##Array array, jsize start, jsize len, const _ctype* buf) { \
1642 CHECK_JNI_ENTRY(kFlag_Default, "EaIIp", env, array, start, len, buf); \
1643 baseEnv(env)->Set##_jname##ArrayRegion(env, array, start, len, buf); \
1644 CHECK_JNI_EXIT_VOID(); \
1645 }
1646
1647#define PRIMITIVE_ARRAY_FUNCTIONS(_ctype, _jname, _typechar) \
1648 GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \
1649 RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \
1650 GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname); \
1651 SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname);
1652
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001653// TODO: verify primitive array type matches call type.
Elliott Hughesa2501992011-08-26 19:39:54 -07001654PRIMITIVE_ARRAY_FUNCTIONS(jboolean, Boolean, 'Z');
1655PRIMITIVE_ARRAY_FUNCTIONS(jbyte, Byte, 'B');
1656PRIMITIVE_ARRAY_FUNCTIONS(jchar, Char, 'C');
1657PRIMITIVE_ARRAY_FUNCTIONS(jshort, Short, 'S');
1658PRIMITIVE_ARRAY_FUNCTIONS(jint, Int, 'I');
1659PRIMITIVE_ARRAY_FUNCTIONS(jlong, Long, 'J');
1660PRIMITIVE_ARRAY_FUNCTIONS(jfloat, Float, 'F');
1661PRIMITIVE_ARRAY_FUNCTIONS(jdouble, Double, 'D');
1662
Elliott Hughese84278b2012-03-22 10:06:53 -07001663 static jint RegisterNatives(JNIEnv* env, jclass c, const JNINativeMethod* methods, jint nMethods) {
1664 CHECK_JNI_ENTRY(kFlag_Default, "EcpI", env, c, methods, nMethods);
1665 return CHECK_JNI_EXIT("I", baseEnv(env)->RegisterNatives(env, c, methods, nMethods));
Elliott Hughesa2501992011-08-26 19:39:54 -07001666 }
1667
Elliott Hughese84278b2012-03-22 10:06:53 -07001668 static jint UnregisterNatives(JNIEnv* env, jclass c) {
1669 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1670 return CHECK_JNI_EXIT("I", baseEnv(env)->UnregisterNatives(env, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001671 }
1672
1673 static jint MonitorEnter(JNIEnv* env, jobject obj) {
1674 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
Elliott Hughesa92853e2012-02-07 16:09:27 -08001675 if (!sc.CheckInstance(ScopedCheck::kObject, obj)) {
1676 return JNI_ERR; // Only for jni_internal_test. Real code will have aborted already.
1677 }
Elliott Hughesa2501992011-08-26 19:39:54 -07001678 return CHECK_JNI_EXIT("I", baseEnv(env)->MonitorEnter(env, obj));
1679 }
1680
1681 static jint MonitorExit(JNIEnv* env, jobject obj) {
1682 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, obj);
Elliott Hughesa92853e2012-02-07 16:09:27 -08001683 if (!sc.CheckInstance(ScopedCheck::kObject, obj)) {
1684 return JNI_ERR; // Only for jni_internal_test. Real code will have aborted already.
1685 }
Elliott Hughesa2501992011-08-26 19:39:54 -07001686 return CHECK_JNI_EXIT("I", baseEnv(env)->MonitorExit(env, obj));
1687 }
1688
1689 static jint GetJavaVM(JNIEnv *env, JavaVM **vm) {
1690 CHECK_JNI_ENTRY(kFlag_Default, "Ep", env, vm);
1691 return CHECK_JNI_EXIT("I", baseEnv(env)->GetJavaVM(env, vm));
1692 }
1693
1694 static void GetStringRegion(JNIEnv* env, jstring str, jsize start, jsize len, jchar* buf) {
1695 CHECK_JNI_ENTRY(kFlag_CritOkay, "EsIIp", env, str, start, len, buf);
1696 baseEnv(env)->GetStringRegion(env, str, start, len, buf);
1697 CHECK_JNI_EXIT_VOID();
1698 }
1699
1700 static void GetStringUTFRegion(JNIEnv* env, jstring str, jsize start, jsize len, char* buf) {
1701 CHECK_JNI_ENTRY(kFlag_CritOkay, "EsIIp", env, str, start, len, buf);
1702 baseEnv(env)->GetStringUTFRegion(env, str, start, len, buf);
1703 CHECK_JNI_EXIT_VOID();
1704 }
1705
1706 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* isCopy) {
1707 CHECK_JNI_ENTRY(kFlag_CritGet, "Eap", env, array, isCopy);
1708 void* result = baseEnv(env)->GetPrimitiveArrayCritical(env, array, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001709 if (sc.ForceCopy() && result != NULL) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001710 result = CreateGuardedPACopy(env, array, isCopy);
1711 }
1712 return CHECK_JNI_EXIT("p", result);
1713 }
1714
1715 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* carray, jint mode) {
1716 CHECK_JNI_ENTRY(kFlag_CritRelease | kFlag_ExcepOkay, "Eapr", env, array, carray, mode);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001717 sc.CheckNonNull(carray);
1718 if (sc.ForceCopy()) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001719 ReleaseGuardedPACopy(env, array, carray, mode);
1720 }
1721 baseEnv(env)->ReleasePrimitiveArrayCritical(env, array, carray, mode);
1722 CHECK_JNI_EXIT_VOID();
1723 }
1724
1725 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* isCopy) {
1726 CHECK_JNI_ENTRY(kFlag_CritGet, "Esp", env, java_string, isCopy);
1727 const jchar* result = baseEnv(env)->GetStringCritical(env, java_string, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001728 if (sc.ForceCopy() && result != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001729 mirror::String* s = sc.soa().Decode<mirror::String*>(java_string);
Elliott Hughesa2501992011-08-26 19:39:54 -07001730 int byteCount = s->GetLength() * 2;
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001731 result = (const jchar*) GuardedCopy::Create(result, byteCount, false);
Elliott Hughesa2501992011-08-26 19:39:54 -07001732 if (isCopy != NULL) {
1733 *isCopy = JNI_TRUE;
1734 }
1735 }
1736 return CHECK_JNI_EXIT("p", result);
1737 }
1738
1739 static void ReleaseStringCritical(JNIEnv* env, jstring string, const jchar* carray) {
1740 CHECK_JNI_ENTRY(kFlag_CritRelease | kFlag_ExcepOkay, "Esp", env, string, carray);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001741 sc.CheckNonNull(carray);
1742 if (sc.ForceCopy()) {
1743 GuardedCopy::Check(__FUNCTION__, carray, false);
Elliott Hughesba8eee12012-01-24 20:25:24 -08001744 carray = reinterpret_cast<const jchar*>(GuardedCopy::Destroy(const_cast<jchar*>(carray)));
Elliott Hughesa2501992011-08-26 19:39:54 -07001745 }
1746 baseEnv(env)->ReleaseStringCritical(env, string, carray);
1747 CHECK_JNI_EXIT_VOID();
1748 }
1749
1750 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
1751 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1752 return CHECK_JNI_EXIT("L", baseEnv(env)->NewWeakGlobalRef(env, obj));
1753 }
1754
1755 static jboolean ExceptionCheck(JNIEnv* env) {
1756 CHECK_JNI_ENTRY(kFlag_CritOkay | kFlag_ExcepOkay, "E", env);
1757 return CHECK_JNI_EXIT("b", baseEnv(env)->ExceptionCheck(env));
1758 }
1759
1760 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject obj) {
1761 // Note: we use "Ep" rather than "EL" because this is the one JNI function
1762 // that it's okay to pass an invalid reference to.
1763 CHECK_JNI_ENTRY(kFlag_Default, "Ep", env, obj);
1764 // TODO: proper decoding of jobjectRefType!
1765 return CHECK_JNI_EXIT("I", baseEnv(env)->GetObjectRefType(env, obj));
1766 }
1767
1768 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
1769 CHECK_JNI_ENTRY(kFlag_Default, "EpJ", env, address, capacity);
1770 if (address == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001771 JniAbortF(__FUNCTION__, "non-nullable address is NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -07001772 }
1773 if (capacity <= 0) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001774 JniAbortF(__FUNCTION__, "capacity must be greater than 0: %d", capacity);
Elliott Hughesa2501992011-08-26 19:39:54 -07001775 }
1776 return CHECK_JNI_EXIT("L", baseEnv(env)->NewDirectByteBuffer(env, address, capacity));
1777 }
1778
1779 static void* GetDirectBufferAddress(JNIEnv* env, jobject buf) {
1780 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, buf);
1781 // TODO: check that 'buf' is a java.nio.Buffer.
1782 return CHECK_JNI_EXIT("p", baseEnv(env)->GetDirectBufferAddress(env, buf));
1783 }
1784
1785 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject buf) {
1786 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, buf);
1787 // TODO: check that 'buf' is a java.nio.Buffer.
1788 return CHECK_JNI_EXIT("J", baseEnv(env)->GetDirectBufferCapacity(env, buf));
1789 }
1790
1791 private:
1792 static inline const JNINativeInterface* baseEnv(JNIEnv* env) {
1793 return reinterpret_cast<JNIEnvExt*>(env)->unchecked_functions;
1794 }
1795};
1796
1797const JNINativeInterface gCheckNativeInterface = {
1798 NULL, // reserved0.
1799 NULL, // reserved1.
1800 NULL, // reserved2.
1801 NULL, // reserved3.
1802 CheckJNI::GetVersion,
1803 CheckJNI::DefineClass,
1804 CheckJNI::FindClass,
1805 CheckJNI::FromReflectedMethod,
1806 CheckJNI::FromReflectedField,
1807 CheckJNI::ToReflectedMethod,
1808 CheckJNI::GetSuperclass,
1809 CheckJNI::IsAssignableFrom,
1810 CheckJNI::ToReflectedField,
1811 CheckJNI::Throw,
1812 CheckJNI::ThrowNew,
1813 CheckJNI::ExceptionOccurred,
1814 CheckJNI::ExceptionDescribe,
1815 CheckJNI::ExceptionClear,
1816 CheckJNI::FatalError,
1817 CheckJNI::PushLocalFrame,
1818 CheckJNI::PopLocalFrame,
1819 CheckJNI::NewGlobalRef,
1820 CheckJNI::DeleteGlobalRef,
1821 CheckJNI::DeleteLocalRef,
1822 CheckJNI::IsSameObject,
1823 CheckJNI::NewLocalRef,
1824 CheckJNI::EnsureLocalCapacity,
1825 CheckJNI::AllocObject,
1826 CheckJNI::NewObject,
1827 CheckJNI::NewObjectV,
1828 CheckJNI::NewObjectA,
1829 CheckJNI::GetObjectClass,
1830 CheckJNI::IsInstanceOf,
1831 CheckJNI::GetMethodID,
1832 CheckJNI::CallObjectMethod,
1833 CheckJNI::CallObjectMethodV,
1834 CheckJNI::CallObjectMethodA,
1835 CheckJNI::CallBooleanMethod,
1836 CheckJNI::CallBooleanMethodV,
1837 CheckJNI::CallBooleanMethodA,
1838 CheckJNI::CallByteMethod,
1839 CheckJNI::CallByteMethodV,
1840 CheckJNI::CallByteMethodA,
1841 CheckJNI::CallCharMethod,
1842 CheckJNI::CallCharMethodV,
1843 CheckJNI::CallCharMethodA,
1844 CheckJNI::CallShortMethod,
1845 CheckJNI::CallShortMethodV,
1846 CheckJNI::CallShortMethodA,
1847 CheckJNI::CallIntMethod,
1848 CheckJNI::CallIntMethodV,
1849 CheckJNI::CallIntMethodA,
1850 CheckJNI::CallLongMethod,
1851 CheckJNI::CallLongMethodV,
1852 CheckJNI::CallLongMethodA,
1853 CheckJNI::CallFloatMethod,
1854 CheckJNI::CallFloatMethodV,
1855 CheckJNI::CallFloatMethodA,
1856 CheckJNI::CallDoubleMethod,
1857 CheckJNI::CallDoubleMethodV,
1858 CheckJNI::CallDoubleMethodA,
1859 CheckJNI::CallVoidMethod,
1860 CheckJNI::CallVoidMethodV,
1861 CheckJNI::CallVoidMethodA,
1862 CheckJNI::CallNonvirtualObjectMethod,
1863 CheckJNI::CallNonvirtualObjectMethodV,
1864 CheckJNI::CallNonvirtualObjectMethodA,
1865 CheckJNI::CallNonvirtualBooleanMethod,
1866 CheckJNI::CallNonvirtualBooleanMethodV,
1867 CheckJNI::CallNonvirtualBooleanMethodA,
1868 CheckJNI::CallNonvirtualByteMethod,
1869 CheckJNI::CallNonvirtualByteMethodV,
1870 CheckJNI::CallNonvirtualByteMethodA,
1871 CheckJNI::CallNonvirtualCharMethod,
1872 CheckJNI::CallNonvirtualCharMethodV,
1873 CheckJNI::CallNonvirtualCharMethodA,
1874 CheckJNI::CallNonvirtualShortMethod,
1875 CheckJNI::CallNonvirtualShortMethodV,
1876 CheckJNI::CallNonvirtualShortMethodA,
1877 CheckJNI::CallNonvirtualIntMethod,
1878 CheckJNI::CallNonvirtualIntMethodV,
1879 CheckJNI::CallNonvirtualIntMethodA,
1880 CheckJNI::CallNonvirtualLongMethod,
1881 CheckJNI::CallNonvirtualLongMethodV,
1882 CheckJNI::CallNonvirtualLongMethodA,
1883 CheckJNI::CallNonvirtualFloatMethod,
1884 CheckJNI::CallNonvirtualFloatMethodV,
1885 CheckJNI::CallNonvirtualFloatMethodA,
1886 CheckJNI::CallNonvirtualDoubleMethod,
1887 CheckJNI::CallNonvirtualDoubleMethodV,
1888 CheckJNI::CallNonvirtualDoubleMethodA,
1889 CheckJNI::CallNonvirtualVoidMethod,
1890 CheckJNI::CallNonvirtualVoidMethodV,
1891 CheckJNI::CallNonvirtualVoidMethodA,
1892 CheckJNI::GetFieldID,
1893 CheckJNI::GetObjectField,
1894 CheckJNI::GetBooleanField,
1895 CheckJNI::GetByteField,
1896 CheckJNI::GetCharField,
1897 CheckJNI::GetShortField,
1898 CheckJNI::GetIntField,
1899 CheckJNI::GetLongField,
1900 CheckJNI::GetFloatField,
1901 CheckJNI::GetDoubleField,
1902 CheckJNI::SetObjectField,
1903 CheckJNI::SetBooleanField,
1904 CheckJNI::SetByteField,
1905 CheckJNI::SetCharField,
1906 CheckJNI::SetShortField,
1907 CheckJNI::SetIntField,
1908 CheckJNI::SetLongField,
1909 CheckJNI::SetFloatField,
1910 CheckJNI::SetDoubleField,
1911 CheckJNI::GetStaticMethodID,
1912 CheckJNI::CallStaticObjectMethod,
1913 CheckJNI::CallStaticObjectMethodV,
1914 CheckJNI::CallStaticObjectMethodA,
1915 CheckJNI::CallStaticBooleanMethod,
1916 CheckJNI::CallStaticBooleanMethodV,
1917 CheckJNI::CallStaticBooleanMethodA,
1918 CheckJNI::CallStaticByteMethod,
1919 CheckJNI::CallStaticByteMethodV,
1920 CheckJNI::CallStaticByteMethodA,
1921 CheckJNI::CallStaticCharMethod,
1922 CheckJNI::CallStaticCharMethodV,
1923 CheckJNI::CallStaticCharMethodA,
1924 CheckJNI::CallStaticShortMethod,
1925 CheckJNI::CallStaticShortMethodV,
1926 CheckJNI::CallStaticShortMethodA,
1927 CheckJNI::CallStaticIntMethod,
1928 CheckJNI::CallStaticIntMethodV,
1929 CheckJNI::CallStaticIntMethodA,
1930 CheckJNI::CallStaticLongMethod,
1931 CheckJNI::CallStaticLongMethodV,
1932 CheckJNI::CallStaticLongMethodA,
1933 CheckJNI::CallStaticFloatMethod,
1934 CheckJNI::CallStaticFloatMethodV,
1935 CheckJNI::CallStaticFloatMethodA,
1936 CheckJNI::CallStaticDoubleMethod,
1937 CheckJNI::CallStaticDoubleMethodV,
1938 CheckJNI::CallStaticDoubleMethodA,
1939 CheckJNI::CallStaticVoidMethod,
1940 CheckJNI::CallStaticVoidMethodV,
1941 CheckJNI::CallStaticVoidMethodA,
1942 CheckJNI::GetStaticFieldID,
1943 CheckJNI::GetStaticObjectField,
1944 CheckJNI::GetStaticBooleanField,
1945 CheckJNI::GetStaticByteField,
1946 CheckJNI::GetStaticCharField,
1947 CheckJNI::GetStaticShortField,
1948 CheckJNI::GetStaticIntField,
1949 CheckJNI::GetStaticLongField,
1950 CheckJNI::GetStaticFloatField,
1951 CheckJNI::GetStaticDoubleField,
1952 CheckJNI::SetStaticObjectField,
1953 CheckJNI::SetStaticBooleanField,
1954 CheckJNI::SetStaticByteField,
1955 CheckJNI::SetStaticCharField,
1956 CheckJNI::SetStaticShortField,
1957 CheckJNI::SetStaticIntField,
1958 CheckJNI::SetStaticLongField,
1959 CheckJNI::SetStaticFloatField,
1960 CheckJNI::SetStaticDoubleField,
1961 CheckJNI::NewString,
1962 CheckJNI::GetStringLength,
1963 CheckJNI::GetStringChars,
1964 CheckJNI::ReleaseStringChars,
1965 CheckJNI::NewStringUTF,
1966 CheckJNI::GetStringUTFLength,
1967 CheckJNI::GetStringUTFChars,
1968 CheckJNI::ReleaseStringUTFChars,
1969 CheckJNI::GetArrayLength,
1970 CheckJNI::NewObjectArray,
1971 CheckJNI::GetObjectArrayElement,
1972 CheckJNI::SetObjectArrayElement,
1973 CheckJNI::NewBooleanArray,
1974 CheckJNI::NewByteArray,
1975 CheckJNI::NewCharArray,
1976 CheckJNI::NewShortArray,
1977 CheckJNI::NewIntArray,
1978 CheckJNI::NewLongArray,
1979 CheckJNI::NewFloatArray,
1980 CheckJNI::NewDoubleArray,
1981 CheckJNI::GetBooleanArrayElements,
1982 CheckJNI::GetByteArrayElements,
1983 CheckJNI::GetCharArrayElements,
1984 CheckJNI::GetShortArrayElements,
1985 CheckJNI::GetIntArrayElements,
1986 CheckJNI::GetLongArrayElements,
1987 CheckJNI::GetFloatArrayElements,
1988 CheckJNI::GetDoubleArrayElements,
1989 CheckJNI::ReleaseBooleanArrayElements,
1990 CheckJNI::ReleaseByteArrayElements,
1991 CheckJNI::ReleaseCharArrayElements,
1992 CheckJNI::ReleaseShortArrayElements,
1993 CheckJNI::ReleaseIntArrayElements,
1994 CheckJNI::ReleaseLongArrayElements,
1995 CheckJNI::ReleaseFloatArrayElements,
1996 CheckJNI::ReleaseDoubleArrayElements,
1997 CheckJNI::GetBooleanArrayRegion,
1998 CheckJNI::GetByteArrayRegion,
1999 CheckJNI::GetCharArrayRegion,
2000 CheckJNI::GetShortArrayRegion,
2001 CheckJNI::GetIntArrayRegion,
2002 CheckJNI::GetLongArrayRegion,
2003 CheckJNI::GetFloatArrayRegion,
2004 CheckJNI::GetDoubleArrayRegion,
2005 CheckJNI::SetBooleanArrayRegion,
2006 CheckJNI::SetByteArrayRegion,
2007 CheckJNI::SetCharArrayRegion,
2008 CheckJNI::SetShortArrayRegion,
2009 CheckJNI::SetIntArrayRegion,
2010 CheckJNI::SetLongArrayRegion,
2011 CheckJNI::SetFloatArrayRegion,
2012 CheckJNI::SetDoubleArrayRegion,
2013 CheckJNI::RegisterNatives,
2014 CheckJNI::UnregisterNatives,
2015 CheckJNI::MonitorEnter,
2016 CheckJNI::MonitorExit,
2017 CheckJNI::GetJavaVM,
2018 CheckJNI::GetStringRegion,
2019 CheckJNI::GetStringUTFRegion,
2020 CheckJNI::GetPrimitiveArrayCritical,
2021 CheckJNI::ReleasePrimitiveArrayCritical,
2022 CheckJNI::GetStringCritical,
2023 CheckJNI::ReleaseStringCritical,
2024 CheckJNI::NewWeakGlobalRef,
2025 CheckJNI::DeleteWeakGlobalRef,
2026 CheckJNI::ExceptionCheck,
2027 CheckJNI::NewDirectByteBuffer,
2028 CheckJNI::GetDirectBufferAddress,
2029 CheckJNI::GetDirectBufferCapacity,
2030 CheckJNI::GetObjectRefType,
2031};
2032
2033const JNINativeInterface* GetCheckJniNativeInterface() {
2034 return &gCheckNativeInterface;
2035}
2036
2037class CheckJII {
Elliott Hughesba8eee12012-01-24 20:25:24 -08002038 public:
Elliott Hughesa2501992011-08-26 19:39:54 -07002039 static jint DestroyJavaVM(JavaVM* vm) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002040 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002041 sc.Check(true, "v", vm);
2042 return CHECK_JNI_EXIT("I", BaseVm(vm)->DestroyJavaVM(vm));
Elliott Hughesa2501992011-08-26 19:39:54 -07002043 }
2044
2045 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002046 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002047 sc.Check(true, "vpp", vm, p_env, thr_args);
2048 return CHECK_JNI_EXIT("I", BaseVm(vm)->AttachCurrentThread(vm, p_env, thr_args));
Elliott Hughesa2501992011-08-26 19:39:54 -07002049 }
2050
2051 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002052 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002053 sc.Check(true, "vpp", vm, p_env, thr_args);
2054 return CHECK_JNI_EXIT("I", BaseVm(vm)->AttachCurrentThreadAsDaemon(vm, p_env, thr_args));
Elliott Hughesa2501992011-08-26 19:39:54 -07002055 }
2056
2057 static jint DetachCurrentThread(JavaVM* vm) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002058 ScopedCheck sc(vm, true, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002059 sc.Check(true, "v", vm);
2060 return CHECK_JNI_EXIT("I", BaseVm(vm)->DetachCurrentThread(vm));
Elliott Hughesa2501992011-08-26 19:39:54 -07002061 }
2062
2063 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002064 ScopedCheck sc(vm, true, __FUNCTION__);
Elliott Hughes83a25322013-03-14 11:18:53 -07002065 sc.Check(true, "vpI", vm);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002066 return CHECK_JNI_EXIT("I", BaseVm(vm)->GetEnv(vm, env, version));
Elliott Hughesa2501992011-08-26 19:39:54 -07002067 }
2068
2069 private:
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002070 static inline const JNIInvokeInterface* BaseVm(JavaVM* vm) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002071 return reinterpret_cast<JavaVMExt*>(vm)->unchecked_functions;
2072 }
2073};
2074
2075const JNIInvokeInterface gCheckInvokeInterface = {
2076 NULL, // reserved0
2077 NULL, // reserved1
2078 NULL, // reserved2
2079 CheckJII::DestroyJavaVM,
2080 CheckJII::AttachCurrentThread,
2081 CheckJII::DetachCurrentThread,
2082 CheckJII::GetEnv,
2083 CheckJII::AttachCurrentThreadAsDaemon
2084};
2085
2086const JNIInvokeInterface* GetCheckJniInvokeInterface() {
2087 return &gCheckInvokeInterface;
2088}
2089
2090} // namespace art