blob: 57ce43265ed5495987eae445093f7e899f1ad776 [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"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "gc/space.h"
27#include "mirror/class-inl.h"
28#include "mirror/field-inl.h"
29#include "mirror/abstract_method-inl.h"
30#include "mirror/object-inl.h"
31#include "mirror/object_array-inl.h"
32#include "mirror/throwable.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080033#include "object_utils.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070034#include "scoped_thread_state_change.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070035#include "thread.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070036#include "runtime.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070037
Elliott Hughese6087632011-09-26 12:18:25 -070038#define LIBCORE_CPP_JNI_HELPERS
39#include <JNIHelp.h> // from libcore
40#undef LIBCORE_CPP_JNI_HELPERS
41
Elliott Hughesa2501992011-08-26 19:39:54 -070042namespace art {
43
Elliott Hughes3f6635a2012-06-19 13:37:49 -070044static void JniAbort(const char* jni_function_name, const char* msg) {
Elliott Hughesa0957642011-09-02 14:27:33 -070045 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070046 ScopedObjectAccess soa(self);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047 mirror::AbstractMethod* current_method = self->GetCurrentMethod();
Elliott Hughesa2501992011-08-26 19:39:54 -070048
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070049 std::ostringstream os;
Elliott Hughes3f6635a2012-06-19 13:37:49 -070050 os << "JNI DETECTED ERROR IN APPLICATION: " << msg;
Elliott Hughesa2501992011-08-26 19:39:54 -070051
52 if (jni_function_name != NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -070053 os << "\n in call to " << jni_function_name;
Elliott Hughesa2501992011-08-26 19:39:54 -070054 }
Elliott Hughesa0957642011-09-02 14:27:33 -070055 // TODO: is this useful given that we're about to dump the calling thread's stack?
56 if (current_method != NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -070057 os << "\n from " << PrettyMethod(current_method);
Elliott Hughesa0957642011-09-02 14:27:33 -070058 }
59 os << "\n";
60 self->Dump(os);
Elliott Hughesa2501992011-08-26 19:39:54 -070061
62 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
63 if (vm->check_jni_abort_hook != NULL) {
Elliott Hughesb264f082012-04-06 17:10:10 -070064 vm->check_jni_abort_hook(vm->check_jni_abort_hook_data, os.str());
Elliott Hughesa2501992011-08-26 19:39:54 -070065 } else {
Ian Rogers9da7f592012-08-20 17:14:28 -070066 // Ensure that we get a native stack trace for this thread.
67 self->TransitionFromRunnableToSuspended(kNative);
Elliott Hughesa2501992011-08-26 19:39:54 -070068 LOG(FATAL) << os.str();
Ian Rogers9da7f592012-08-20 17:14:28 -070069 self->TransitionFromSuspendedToRunnable(); // Unreachable, keep annotalysis happy.
Elliott Hughesa2501992011-08-26 19:39:54 -070070 }
71}
72
Elliott Hughes3f6635a2012-06-19 13:37:49 -070073static void JniAbortV(const char* jni_function_name, const char* fmt, va_list ap) {
74 std::string msg;
75 StringAppendV(&msg, fmt, ap);
76 JniAbort(jni_function_name, msg.c_str());
77}
78
79void JniAbortF(const char* jni_function_name, const char* fmt, ...) {
80 va_list args;
81 va_start(args, fmt);
82 JniAbortV(jni_function_name, fmt, args);
83 va_end(args);
84}
85
Elliott Hughesa2501992011-08-26 19:39:54 -070086/*
87 * ===========================================================================
88 * JNI function helpers
89 * ===========================================================================
90 */
91
Ian Rogers959f8ed2012-02-07 16:33:37 -080092static bool IsSirtLocalRef(JNIEnv* env, jobject localRef) {
93 return GetIndirectRefKind(localRef) == kSirtOrInvalid &&
Ian Rogers0399dde2012-06-06 17:09:28 -070094 reinterpret_cast<JNIEnvExt*>(env)->self->SirtContains(localRef);
Ian Rogers959f8ed2012-02-07 16:33:37 -080095}
96
Elliott Hughes3f6635a2012-06-19 13:37:49 -070097// Hack to allow forcecopy to work with jniGetNonMovableArrayElements.
98// The code deliberately uses an invalid sequence of operations, so we
99// need to pass it through unmodified. Review that code before making
100// any changes here.
Elliott Hughesa2501992011-08-26 19:39:54 -0700101#define kNoCopyMagic 0xd5aab57f
102
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700103// Flags passed into ScopedCheck.
Elliott Hughesa2501992011-08-26 19:39:54 -0700104#define kFlag_Default 0x0000
105
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700106#define kFlag_CritBad 0x0000 // Calling while in critical is not allowed.
107#define kFlag_CritOkay 0x0001 // Calling while in critical is allowed.
108#define kFlag_CritGet 0x0002 // This is a critical "get".
109#define kFlag_CritRelease 0x0003 // This is a critical "release".
110#define kFlag_CritMask 0x0003 // Bit mask to get "crit" value.
Elliott Hughesa2501992011-08-26 19:39:54 -0700111
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700112#define kFlag_ExcepBad 0x0000 // Raised exceptions are not allowed.
113#define kFlag_ExcepOkay 0x0004 // Raised exceptions are allowed.
Elliott Hughesa2501992011-08-26 19:39:54 -0700114
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700115#define kFlag_Release 0x0010 // Are we in a non-critical release function?
116#define kFlag_NullableUtf 0x0020 // Are our UTF parameters nullable?
Elliott Hughesa2501992011-08-26 19:39:54 -0700117
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700118#define kFlag_Invocation 0x8000 // Part of the invocation interface (JavaVM*).
Elliott Hughesa2501992011-08-26 19:39:54 -0700119
Elliott Hughes485cac42011-12-09 17:49:35 -0800120#define kFlag_ForceTrace 0x80000000 // Add this to a JNI function's flags if you want to trace every call.
121
Elliott Hughesa0957642011-09-02 14:27:33 -0700122static const char* gBuiltInPrefixes[] = {
123 "Landroid/",
124 "Lcom/android/",
125 "Lcom/google/android/",
126 "Ldalvik/",
127 "Ljava/",
128 "Ljavax/",
129 "Llibcore/",
130 "Lorg/apache/harmony/",
131 NULL
132};
133
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800134static bool ShouldTrace(JavaVMExt* vm, const mirror::AbstractMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700135 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700136 // If both "-Xcheck:jni" and "-Xjnitrace:" are enabled, we print trace messages
137 // when a native method that matches the -Xjnitrace argument calls a JNI function
138 // such as NewByteArray.
139 // If -verbose:third-party-jni is on, we want to log any JNI function calls
140 // made by a third-party native method.
Elliott Hughes81ff3182012-03-23 20:35:56 -0700141 std::string class_name(MethodHelper(method).GetDeclaringClassDescriptor());
142 if (!vm->trace.empty() && class_name.find(vm->trace) != std::string::npos) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700143 return true;
144 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800145 if (VLOG_IS_ON(third_party_jni)) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700146 // Return true if we're trying to log all third-party JNI activity and 'method' doesn't look
147 // like part of Android.
Elliott Hughesa0957642011-09-02 14:27:33 -0700148 for (size_t i = 0; gBuiltInPrefixes[i] != NULL; ++i) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700149 if (StartsWith(class_name, gBuiltInPrefixes[i])) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700150 return false;
151 }
152 }
153 return true;
154 }
155 return false;
156}
157
Elliott Hughesa2501992011-08-26 19:39:54 -0700158class ScopedCheck {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800159 public:
Elliott Hughesa2501992011-08-26 19:39:54 -0700160 // For JNIEnv* functions.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700161 explicit ScopedCheck(JNIEnv* env, int flags, const char* functionName)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700162 SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700163 : soa_(env) {
Ian Rogers365c1022012-06-22 15:05:28 -0700164 Init(flags, functionName, true);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700165 CheckThread(flags);
Elliott Hughesa2501992011-08-26 19:39:54 -0700166 }
167
168 // For JavaVM* functions.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700169 // TODO: it's not correct that this is a lock function, but making it so aids annotalysis.
170 explicit ScopedCheck(JavaVM* vm, bool has_method, const char* functionName)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700171 SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700172 : soa_(vm) {
Ian Rogers365c1022012-06-22 15:05:28 -0700173 Init(kFlag_Invocation, functionName, has_method);
Elliott Hughesa2501992011-08-26 19:39:54 -0700174 }
175
Ian Rogersb726dcb2012-09-05 08:57:23 -0700176 ~ScopedCheck() UNLOCK_FUNCTION(Locks::mutator_lock_) {}
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700177
178 const ScopedObjectAccess& soa() {
179 return soa_;
180 }
181
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700182 bool ForceCopy() {
Elliott Hughesa2501992011-08-26 19:39:54 -0700183 return Runtime::Current()->GetJavaVM()->force_copy;
184 }
185
Elliott Hughes81ff3182012-03-23 20:35:56 -0700186 // Checks that 'class_name' is a valid "fully-qualified" JNI class name, like "java/lang/Thread"
187 // or "[Ljava/lang/Object;". A ClassLoader can actually normalize class names a couple of
188 // times, so using "java.lang.Thread" instead of "java/lang/Thread" might work in some
189 // circumstances, but this is incorrect.
190 void CheckClassName(const char* class_name) {
191 if (!IsValidJniClassName(class_name)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700192 JniAbortF(function_name_,
193 "illegal class name '%s'\n"
194 " (should be of the form 'package/Class', [Lpackage/Class;' or '[[B')",
195 class_name);
Elliott Hughesa2501992011-08-26 19:39:54 -0700196 }
197 }
198
199 /*
200 * Verify that the field is of the appropriate type. If the field has an
201 * object type, "java_object" is the object we're trying to assign into it.
202 *
203 * Works for both static and instance fields.
204 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700205 void CheckFieldType(jobject java_object, jfieldID fid, char prim, bool isStatic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700206 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800207 mirror::Field* f = CheckFieldID(fid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700208 if (f == NULL) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700209 return;
210 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800211 mirror::Class* field_type = FieldHelper(f).GetType();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700212 if (!field_type->IsPrimitive()) {
213 if (java_object != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800214 mirror::Object* obj = soa_.Decode<mirror::Object*>(java_object);
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700215 // If java_object is a weak global ref whose referent has been cleared,
216 // obj will be NULL. Otherwise, obj should always be non-NULL
217 // and valid.
Elliott Hughes88c5c352012-03-15 18:49:48 -0700218 if (!Runtime::Current()->GetHeap()->IsHeapAddress(obj)) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700219 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700220 JniAbortF(function_name_, "field operation on invalid %s: %p",
221 ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700222 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700223 } else {
Brian Carlstrom16192862011-09-12 17:50:06 -0700224 if (!obj->InstanceOf(field_type)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700225 JniAbortF(function_name_, "attempt to set field %s with value of wrong type: %s",
226 PrettyField(f).c_str(), PrettyTypeOf(obj).c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700227 return;
228 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700229 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700230 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700231 } else if (field_type != Runtime::Current()->GetClassLinker()->FindPrimitiveClass(prim)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700232 JniAbortF(function_name_, "attempt to set field %s with value of wrong type: %c",
233 PrettyField(f).c_str(), prim);
Elliott Hughesa2501992011-08-26 19:39:54 -0700234 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700235 }
236
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700237 if (isStatic != f->IsStatic()) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700238 if (isStatic) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700239 JniAbortF(function_name_, "accessing non-static field %s as static", PrettyField(f).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700240 } else {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700241 JniAbortF(function_name_, "accessing static field %s as non-static", PrettyField(f).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700242 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700243 return;
244 }
245 }
246
247 /*
248 * Verify that this instance field ID is valid for this object.
249 *
250 * Assumes "jobj" has already been validated.
251 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700252 void CheckInstanceFieldID(jobject java_object, jfieldID fid)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700253 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800254 mirror::Object* o = soa_.Decode<mirror::Object*>(java_object);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800255 if (o == NULL || !Runtime::Current()->GetHeap()->IsHeapAddress(o)) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700256 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700257 JniAbortF(function_name_, "field operation on invalid %s: %p",
258 ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700259 return;
260 }
261
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800262 mirror::Field* f = CheckFieldID(fid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700263 if (f == NULL) {
264 return;
265 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800266 mirror::Class* c = o->GetClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800267 FieldHelper fh(f);
268 if (c->FindInstanceField(fh.GetName(), fh.GetTypeDescriptor()) == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700269 JniAbortF(function_name_, "jfieldID %s not valid for an object of class %s",
270 PrettyField(f).c_str(), PrettyTypeOf(o).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700271 }
272 }
273
274 /*
275 * Verify that the pointer value is non-NULL.
276 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700277 void CheckNonNull(const void* ptr) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700278 if (ptr == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700279 JniAbortF(function_name_, "non-nullable argument was NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -0700280 }
281 }
282
283 /*
284 * Verify that the method's return type matches the type of call.
285 * 'expectedType' will be "L" for all objects, including arrays.
286 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700287 void CheckSig(jmethodID mid, const char* expectedType, bool isStatic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700288 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800289 mirror::AbstractMethod* m = CheckMethodID(mid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700290 if (m == NULL) {
291 return;
292 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800293 if (*expectedType != MethodHelper(m).GetShorty()[0]) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700294 JniAbortF(function_name_, "the return type of %s does not match %s",
295 function_name_, PrettyMethod(m).c_str());
296 }
297 if (isStatic != m->IsStatic()) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700298 if (isStatic) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700299 JniAbortF(function_name_, "calling non-static method %s with %s",
300 PrettyMethod(m).c_str(), function_name_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700301 } else {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700302 JniAbortF(function_name_, "calling static method %s with %s",
303 PrettyMethod(m).c_str(), function_name_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700304 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700305 }
306 }
307
308 /*
309 * Verify that this static field ID is valid for this class.
310 *
311 * Assumes "java_class" has already been validated.
312 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700313 void CheckStaticFieldID(jclass java_class, jfieldID fid)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700314 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800315 mirror::Class* c = soa_.Decode<mirror::Class*>(java_class);
316 const mirror::Field* f = CheckFieldID(fid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700317 if (f == NULL) {
318 return;
319 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700320 if (f->GetDeclaringClass() != c) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700321 JniAbortF(function_name_, "static jfieldID %p not valid for class %s",
322 fid, PrettyClass(c).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700323 }
324 }
325
326 /*
Elliott Hughese84278b2012-03-22 10:06:53 -0700327 * Verify that "mid" is appropriate for "java_class".
Elliott Hughesa2501992011-08-26 19:39:54 -0700328 *
329 * A mismatch isn't dangerous, because the jmethodID defines the class. In
Elliott Hughese84278b2012-03-22 10:06:53 -0700330 * fact, java_class is unused in the implementation. It's best if we don't
Elliott Hughesa2501992011-08-26 19:39:54 -0700331 * allow bad code in the system though.
332 *
Elliott Hughese84278b2012-03-22 10:06:53 -0700333 * Instances of "java_class" must be instances of the method's declaring class.
Elliott Hughesa2501992011-08-26 19:39:54 -0700334 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700335 void CheckStaticMethod(jclass java_class, jmethodID mid)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700336 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800337 const mirror::AbstractMethod* m = CheckMethodID(mid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700338 if (m == NULL) {
339 return;
340 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800341 mirror::Class* c = soa_.Decode<mirror::Class*>(java_class);
Elliott Hughesa2501992011-08-26 19:39:54 -0700342 if (!c->IsAssignableFrom(m->GetDeclaringClass())) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700343 JniAbortF(function_name_, "can't call static %s on class %s",
344 PrettyMethod(m).c_str(), PrettyClass(c).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700345 }
346 }
347
348 /*
349 * Verify that "mid" is appropriate for "jobj".
350 *
351 * Make sure the object is an instance of the method's declaring class.
352 * (Note the mid might point to a declaration in an interface; this
353 * will be handled automatically by the instanceof check.)
354 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700355 void CheckVirtualMethod(jobject java_object, jmethodID mid)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700356 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800357 const mirror::AbstractMethod* m = CheckMethodID(mid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700358 if (m == NULL) {
359 return;
360 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800361 mirror::Object* o = soa_.Decode<mirror::Object*>(java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700362 if (!o->InstanceOf(m->GetDeclaringClass())) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700363 JniAbortF(function_name_, "can't call %s on instance of %s",
364 PrettyMethod(m).c_str(), PrettyTypeOf(o).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700365 }
366 }
367
368 /**
369 * The format string is a sequence of the following characters,
370 * and must be followed by arguments of the corresponding types
371 * in the same order.
372 *
373 * Java primitive types:
374 * B - jbyte
375 * C - jchar
376 * D - jdouble
377 * F - jfloat
378 * I - jint
379 * J - jlong
380 * S - jshort
381 * Z - jboolean (shown as true and false)
382 * V - void
383 *
384 * Java reference types:
385 * L - jobject
386 * a - jarray
387 * c - jclass
388 * s - jstring
389 *
390 * JNI types:
391 * b - jboolean (shown as JNI_TRUE and JNI_FALSE)
392 * f - jfieldID
393 * m - jmethodID
394 * p - void*
395 * r - jint (for release mode arguments)
Elliott Hughes78090d12011-10-07 14:31:47 -0700396 * u - const char* (Modified UTF-8)
Elliott Hughesa2501992011-08-26 19:39:54 -0700397 * z - jsize (for lengths; use i if negative values are okay)
398 * v - JavaVM*
399 * E - JNIEnv*
400 * . - no argument; just print "..." (used for varargs JNI calls)
401 *
402 * Use the kFlag_NullableUtf flag where 'u' field(s) are nullable.
403 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700404 void Check(bool entry, const char* fmt0, ...)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700405 SHARED_LOCKS_REQUIRED (Locks::mutator_lock_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700406 va_list ap;
407
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800408 const mirror::AbstractMethod* traceMethod = NULL;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700409 if ((!soa_.Vm()->trace.empty() || VLOG_IS_ON(third_party_jni)) && has_method_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700410 // We need to guard some of the invocation interface's calls: a bad caller might
411 // use DetachCurrentThread or GetEnv on a thread that's not yet attached.
Elliott Hughesa0957642011-09-02 14:27:33 -0700412 Thread* self = Thread::Current();
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700413 if ((flags_ & kFlag_Invocation) == 0 || self != NULL) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700414 traceMethod = self->GetCurrentMethod();
Elliott Hughesa2501992011-08-26 19:39:54 -0700415 }
416 }
Elliott Hughesa0957642011-09-02 14:27:33 -0700417
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700418 if (((flags_ & kFlag_ForceTrace) != 0) || (traceMethod != NULL && ShouldTrace(soa_.Vm(), traceMethod))) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700419 va_start(ap, fmt0);
420 std::string msg;
421 for (const char* fmt = fmt0; *fmt;) {
422 char ch = *fmt++;
423 if (ch == 'B') { // jbyte
424 jbyte b = va_arg(ap, int);
425 if (b >= 0 && b < 10) {
426 StringAppendF(&msg, "%d", b);
427 } else {
428 StringAppendF(&msg, "%#x (%d)", b, b);
429 }
430 } else if (ch == 'C') { // jchar
431 jchar c = va_arg(ap, int);
432 if (c < 0x7f && c >= ' ') {
433 StringAppendF(&msg, "U+%x ('%c')", c, c);
434 } else {
435 StringAppendF(&msg, "U+%x", c);
436 }
437 } else if (ch == 'F' || ch == 'D') { // jfloat, jdouble
438 StringAppendF(&msg, "%g", va_arg(ap, double));
439 } else if (ch == 'I' || ch == 'S') { // jint, jshort
440 StringAppendF(&msg, "%d", va_arg(ap, int));
441 } else if (ch == 'J') { // jlong
442 StringAppendF(&msg, "%lld", va_arg(ap, jlong));
443 } else if (ch == 'Z') { // jboolean
444 StringAppendF(&msg, "%s", va_arg(ap, int) ? "true" : "false");
445 } else if (ch == 'V') { // void
446 msg += "void";
447 } else if (ch == 'v') { // JavaVM*
448 JavaVM* vm = va_arg(ap, JavaVM*);
449 StringAppendF(&msg, "(JavaVM*)%p", vm);
450 } else if (ch == 'E') { // JNIEnv*
451 JNIEnv* env = va_arg(ap, JNIEnv*);
452 StringAppendF(&msg, "(JNIEnv*)%p", env);
453 } else if (ch == 'L' || ch == 'a' || ch == 's') { // jobject, jarray, jstring
454 // For logging purposes, these are identical.
455 jobject o = va_arg(ap, jobject);
456 if (o == NULL) {
457 msg += "NULL";
458 } else {
459 StringAppendF(&msg, "%p", o);
460 }
461 } else if (ch == 'b') { // jboolean (JNI-style)
462 jboolean b = va_arg(ap, int);
463 msg += (b ? "JNI_TRUE" : "JNI_FALSE");
464 } else if (ch == 'c') { // jclass
465 jclass jc = va_arg(ap, jclass);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800466 mirror::Class* c = reinterpret_cast<mirror::Class*>(Thread::Current()->DecodeJObject(jc));
Elliott Hughesa2501992011-08-26 19:39:54 -0700467 if (c == NULL) {
468 msg += "NULL";
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800469 } else if (c == kInvalidIndirectRefObject || !Runtime::Current()->GetHeap()->IsHeapAddress(c)) {
Elliott Hughes485cac42011-12-09 17:49:35 -0800470 StringAppendF(&msg, "INVALID POINTER:%p", jc);
471 } else if (!c->IsClass()) {
472 msg += "INVALID NON-CLASS OBJECT OF TYPE:" + PrettyTypeOf(c);
Elliott Hughesa2501992011-08-26 19:39:54 -0700473 } else {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700474 msg += PrettyClass(c);
Elliott Hughesa2501992011-08-26 19:39:54 -0700475 if (!entry) {
476 StringAppendF(&msg, " (%p)", jc);
477 }
478 }
479 } else if (ch == 'f') { // jfieldID
480 jfieldID fid = va_arg(ap, jfieldID);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800481 mirror::Field* f = reinterpret_cast<mirror::Field*>(fid);
Elliott Hughesa2501992011-08-26 19:39:54 -0700482 msg += PrettyField(f);
483 if (!entry) {
484 StringAppendF(&msg, " (%p)", fid);
485 }
486 } else if (ch == 'z') { // non-negative jsize
487 // You might expect jsize to be size_t, but it's not; it's the same as jint.
488 // We only treat this specially so we can do the non-negative check.
489 // TODO: maybe this wasn't worth it?
490 jint i = va_arg(ap, jint);
491 StringAppendF(&msg, "%d", i);
492 } else if (ch == 'm') { // jmethodID
493 jmethodID mid = va_arg(ap, jmethodID);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800494 mirror::AbstractMethod* m = reinterpret_cast<mirror::AbstractMethod*>(mid);
Elliott Hughesa2501992011-08-26 19:39:54 -0700495 msg += PrettyMethod(m);
496 if (!entry) {
497 StringAppendF(&msg, " (%p)", mid);
498 }
499 } else if (ch == 'p') { // void* ("pointer")
500 void* p = va_arg(ap, void*);
501 if (p == NULL) {
502 msg += "NULL";
503 } else {
504 StringAppendF(&msg, "(void*) %p", p);
505 }
506 } else if (ch == 'r') { // jint (release mode)
507 jint releaseMode = va_arg(ap, jint);
508 if (releaseMode == 0) {
509 msg += "0";
510 } else if (releaseMode == JNI_ABORT) {
511 msg += "JNI_ABORT";
512 } else if (releaseMode == JNI_COMMIT) {
513 msg += "JNI_COMMIT";
514 } else {
515 StringAppendF(&msg, "invalid release mode %d", releaseMode);
516 }
Elliott Hughes78090d12011-10-07 14:31:47 -0700517 } else if (ch == 'u') { // const char* (Modified UTF-8)
Elliott Hughesa2501992011-08-26 19:39:54 -0700518 const char* utf = va_arg(ap, const char*);
519 if (utf == NULL) {
520 msg += "NULL";
521 } else {
522 StringAppendF(&msg, "\"%s\"", utf);
523 }
524 } else if (ch == '.') {
525 msg += "...";
526 } else {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700527 JniAbortF(function_name_, "unknown trace format specifier: %c", ch);
Elliott Hughesa2501992011-08-26 19:39:54 -0700528 return;
529 }
530 if (*fmt) {
531 StringAppendF(&msg, ", ");
532 }
533 }
534 va_end(ap);
535
Elliott Hughes485cac42011-12-09 17:49:35 -0800536 if ((flags_ & kFlag_ForceTrace) != 0) {
537 LOG(INFO) << "JNI: call to " << function_name_ << "(" << msg << ")";
538 } else if (entry) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700539 if (has_method_) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700540 std::string methodName(PrettyMethod(traceMethod, false));
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700541 LOG(INFO) << "JNI: " << methodName << " -> " << function_name_ << "(" << msg << ")";
542 indent_ = methodName.size() + 1;
Elliott Hughesa2501992011-08-26 19:39:54 -0700543 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700544 LOG(INFO) << "JNI: -> " << function_name_ << "(" << msg << ")";
545 indent_ = 0;
Elliott Hughesa2501992011-08-26 19:39:54 -0700546 }
547 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700548 LOG(INFO) << StringPrintf("JNI: %*s<- %s returned %s", indent_, "", function_name_, msg.c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700549 }
550 }
551
552 // We always do the thorough checks on entry, and never on exit...
553 if (entry) {
554 va_start(ap, fmt0);
555 for (const char* fmt = fmt0; *fmt; ++fmt) {
556 char ch = *fmt;
557 if (ch == 'a') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700558 CheckArray(va_arg(ap, jarray));
Elliott Hughesa2501992011-08-26 19:39:54 -0700559 } else if (ch == 'c') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700560 CheckInstance(kClass, va_arg(ap, jclass));
Elliott Hughesa2501992011-08-26 19:39:54 -0700561 } else if (ch == 'L') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700562 CheckObject(va_arg(ap, jobject));
Elliott Hughesa2501992011-08-26 19:39:54 -0700563 } else if (ch == 'r') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700564 CheckReleaseMode(va_arg(ap, jint));
Elliott Hughesa2501992011-08-26 19:39:54 -0700565 } else if (ch == 's') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700566 CheckInstance(kString, va_arg(ap, jstring));
Elliott Hughesa2501992011-08-26 19:39:54 -0700567 } else if (ch == 'u') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700568 if ((flags_ & kFlag_Release) != 0) {
569 CheckNonNull(va_arg(ap, const char*));
Elliott Hughesa2501992011-08-26 19:39:54 -0700570 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700571 bool nullable = ((flags_ & kFlag_NullableUtf) != 0);
572 CheckUtfString(va_arg(ap, const char*), nullable);
Elliott Hughesa2501992011-08-26 19:39:54 -0700573 }
574 } else if (ch == 'z') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700575 CheckLengthPositive(va_arg(ap, jsize));
Elliott Hughesa2501992011-08-26 19:39:54 -0700576 } else if (strchr("BCISZbfmpEv", ch) != NULL) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800577 va_arg(ap, uint32_t); // Skip this argument.
Elliott Hughesa2501992011-08-26 19:39:54 -0700578 } else if (ch == 'D' || ch == 'F') {
579 va_arg(ap, double); // Skip this argument.
580 } else if (ch == 'J') {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800581 va_arg(ap, uint64_t); // Skip this argument.
Elliott Hughesa2501992011-08-26 19:39:54 -0700582 } else if (ch == '.') {
583 } else {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800584 LOG(FATAL) << "Unknown check format specifier: " << ch;
Elliott Hughesa2501992011-08-26 19:39:54 -0700585 }
586 }
587 va_end(ap);
588 }
589 }
590
Elliott Hughesa92853e2012-02-07 16:09:27 -0800591 enum InstanceKind {
592 kClass,
Elliott Hughes0f3c5532012-03-30 14:51:51 -0700593 kDirectByteBuffer,
594 kObject,
595 kString,
596 kThrowable,
Elliott Hughesa92853e2012-02-07 16:09:27 -0800597 };
598
599 /*
600 * Verify that "jobj" is a valid non-NULL object reference, and points to
601 * an instance of expectedClass.
602 *
603 * Because we're looking at an object on the GC heap, we have to switch
604 * to "running" mode before doing the checks.
605 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700606 bool CheckInstance(InstanceKind kind, jobject java_object)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700607 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa92853e2012-02-07 16:09:27 -0800608 const char* what = NULL;
609 switch (kind) {
610 case kClass:
611 what = "jclass";
612 break;
613 case kDirectByteBuffer:
614 what = "direct ByteBuffer";
615 break;
616 case kObject:
617 what = "jobject";
618 break;
619 case kString:
620 what = "jstring";
621 break;
622 case kThrowable:
623 what = "jthrowable";
624 break;
625 default:
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700626 LOG(FATAL) << "Unknown kind " << static_cast<int>(kind);
Elliott Hughesa92853e2012-02-07 16:09:27 -0800627 }
628
629 if (java_object == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700630 JniAbortF(function_name_, "%s received null %s", function_name_, what);
Elliott Hughesa92853e2012-02-07 16:09:27 -0800631 return false;
632 }
633
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800634 mirror::Object* obj = soa_.Decode<mirror::Object*>(java_object);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800635 if (!Runtime::Current()->GetHeap()->IsHeapAddress(obj)) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700636 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700637 JniAbortF(function_name_, "%s is an invalid %s: %p (%p)",
638 what, ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object, obj);
Elliott Hughesa92853e2012-02-07 16:09:27 -0800639 return false;
640 }
641
642 bool okay = true;
643 switch (kind) {
644 case kClass:
645 okay = obj->IsClass();
646 break;
647 case kDirectByteBuffer:
648 UNIMPLEMENTED(FATAL);
649 break;
650 case kString:
651 okay = obj->GetClass()->IsStringClass();
652 break;
653 case kThrowable:
654 okay = obj->GetClass()->IsThrowableClass();
655 break;
656 case kObject:
657 break;
658 }
659 if (!okay) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700660 JniAbortF(function_name_, "%s has wrong type: %s", what, PrettyTypeOf(obj).c_str());
Elliott Hughesa92853e2012-02-07 16:09:27 -0800661 return false;
662 }
663
664 return true;
665 }
666
Elliott Hughesba8eee12012-01-24 20:25:24 -0800667 private:
Elliott Hughes81ff3182012-03-23 20:35:56 -0700668 // Set "has_method" to true if we have a valid thread with a method pointer.
669 // We won't have one before attaching a thread, after detaching a thread, or
670 // when shutting down the runtime.
Ian Rogers365c1022012-06-22 15:05:28 -0700671 void Init(int flags, const char* functionName, bool has_method) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700672 flags_ = flags;
673 function_name_ = functionName;
Elliott Hughes81ff3182012-03-23 20:35:56 -0700674 has_method_ = has_method;
Elliott Hughesa2501992011-08-26 19:39:54 -0700675 }
676
677 /*
678 * Verify that "array" is non-NULL and points to an Array object.
679 *
680 * Since we're dealing with objects, switch to "running" mode.
681 */
Ian Rogersb726dcb2012-09-05 08:57:23 -0700682 void CheckArray(jarray java_array) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700683 if (java_array == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700684 JniAbortF(function_name_, "jarray was NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -0700685 return;
686 }
687
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800688 mirror::Array* a = soa_.Decode<mirror::Array*>(java_array);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800689 if (!Runtime::Current()->GetHeap()->IsHeapAddress(a)) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700690 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700691 JniAbortF(function_name_, "jarray is an invalid %s: %p (%p)",
692 ToStr<IndirectRefKind>(GetIndirectRefKind(java_array)).c_str(), java_array, a);
Elliott Hughesa2501992011-08-26 19:39:54 -0700693 } else if (!a->IsArrayInstance()) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700694 JniAbortF(function_name_, "jarray argument has non-array type: %s", PrettyTypeOf(a).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700695 }
696 }
697
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700698 void CheckLengthPositive(jsize length) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700699 if (length < 0) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700700 JniAbortF(function_name_, "negative jsize: %d", length);
Elliott Hughesa2501992011-08-26 19:39:54 -0700701 }
702 }
703
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800704 mirror::Field* CheckFieldID(jfieldID fid) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700705 if (fid == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700706 JniAbortF(function_name_, "jfieldID was NULL");
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700707 return NULL;
708 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800709 mirror::Field* f = soa_.DecodeField(fid);
Ian Rogers365c1022012-06-22 15:05:28 -0700710 if (!Runtime::Current()->GetHeap()->IsHeapAddress(f) || !f->IsField()) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700711 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700712 JniAbortF(function_name_, "invalid jfieldID: %p", fid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700713 return NULL;
714 }
715 return f;
716 }
717
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800718 mirror::AbstractMethod* CheckMethodID(jmethodID mid) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700719 if (mid == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700720 JniAbortF(function_name_, "jmethodID was NULL");
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700721 return NULL;
722 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800723 mirror::AbstractMethod* m = soa_.DecodeMethod(mid);
Ian Rogers365c1022012-06-22 15:05:28 -0700724 if (!Runtime::Current()->GetHeap()->IsHeapAddress(m) || !m->IsMethod()) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700725 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700726 JniAbortF(function_name_, "invalid jmethodID: %p", mid);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700727 return NULL;
728 }
729 return m;
730 }
731
Elliott Hughesa2501992011-08-26 19:39:54 -0700732 /*
733 * Verify that "jobj" is a valid object, and that it's an object that JNI
734 * is allowed to know about. We allow NULL references.
735 *
736 * Switches to "running" mode before performing checks.
737 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700738 void CheckObject(jobject java_object)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700739 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700740 if (java_object == NULL) {
741 return;
742 }
743
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800744 mirror::Object* o = soa_.Decode<mirror::Object*>(java_object);
Elliott Hughes88c5c352012-03-15 18:49:48 -0700745 if (!Runtime::Current()->GetHeap()->IsHeapAddress(o)) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700746 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700747 // TODO: when we remove work_around_app_jni_bugs, this should be impossible.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700748 JniAbortF(function_name_, "native code passing in reference to invalid %s: %p",
749 ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700750 }
751 }
752
753 /*
754 * Verify that the "mode" argument passed to a primitive array Release
755 * function is one of the valid values.
756 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700757 void CheckReleaseMode(jint mode) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700758 if (mode != 0 && mode != JNI_COMMIT && mode != JNI_ABORT) {
Elliott Hughes96a98872012-12-19 14:21:15 -0800759 JniAbortF(function_name_, "unknown value for release mode: %d", mode);
Elliott Hughesa2501992011-08-26 19:39:54 -0700760 }
761 }
762
Ian Rogersb726dcb2012-09-05 08:57:23 -0700763 void CheckThread(int flags) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700764 Thread* self = Thread::Current();
765 if (self == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700766 JniAbortF(function_name_, "a thread (tid %d) is making JNI calls without being attached", GetTid());
Elliott Hughesa2501992011-08-26 19:39:54 -0700767 return;
768 }
769
770 // Get the *correct* JNIEnv by going through our TLS pointer.
771 JNIEnvExt* threadEnv = self->GetJniEnv();
772
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700773 // Verify that the current thread is (a) attached and (b) associated with
774 // this particular instance of JNIEnv.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700775 if (soa_.Env() != threadEnv) {
776 if (soa_.Vm()->work_around_app_jni_bugs) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700777 // If we're keeping broken code limping along, we need to suppress the abort...
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700778 LOG(ERROR) << "APP BUG DETECTED: thread " << *self << " using JNIEnv* from thread " << *soa_.Self();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700779 } else {
780 JniAbortF(function_name_, "thread %s using JNIEnv* from thread %s",
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700781 ToStr<Thread>(*self).c_str(), ToStr<Thread>(*soa_.Self()).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700782 return;
783 }
784 }
785
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700786 // Verify that, if this thread previously made a critical "get" call, we
787 // do the corresponding "release" call before we try anything else.
Elliott Hughesa2501992011-08-26 19:39:54 -0700788 switch (flags & kFlag_CritMask) {
789 case kFlag_CritOkay: // okay to call this method
790 break;
791 case kFlag_CritBad: // not okay to call
792 if (threadEnv->critical) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700793 JniAbortF(function_name_, "thread %s using JNI after critical get", ToStr<Thread>(*self).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700794 return;
795 }
796 break;
797 case kFlag_CritGet: // this is a "get" call
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700798 // Don't check here; we allow nested gets.
Elliott Hughesa2501992011-08-26 19:39:54 -0700799 threadEnv->critical++;
800 break;
801 case kFlag_CritRelease: // this is a "release" call
802 threadEnv->critical--;
803 if (threadEnv->critical < 0) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700804 JniAbortF(function_name_, "thread %s called too many critical releases", ToStr<Thread>(*self).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700805 return;
806 }
807 break;
808 default:
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800809 LOG(FATAL) << "Bad flags (internal error): " << flags;
Elliott Hughesa2501992011-08-26 19:39:54 -0700810 }
811
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700812 // Verify that, if an exception has been raised, the native code doesn't
813 // make any JNI calls other than the Exception* methods.
Elliott Hughesa2501992011-08-26 19:39:54 -0700814 if ((flags & kFlag_ExcepOkay) == 0 && self->IsExceptionPending()) {
Elliott Hughes30646832011-10-13 16:59:46 -0700815 std::string type(PrettyTypeOf(self->GetException()));
Elliott Hughes30646832011-10-13 16:59:46 -0700816 // TODO: write native code that doesn't require allocation for dumping an exception.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700817 // TODO: do we care any more? art always dumps pending exceptions on aborting threads.
Elliott Hughes30646832011-10-13 16:59:46 -0700818 if (type != "java.lang.OutOfMemoryError") {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700819 JniAbortF(function_name_, "JNI %s called with pending exception: %s",
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700820 function_name_, type.c_str(), jniGetStackTrace(soa_.Env()).c_str());
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700821 } else {
822 JniAbortF(function_name_, "JNI %s called with %s pending", function_name_, type.c_str());
Elliott Hughes30646832011-10-13 16:59:46 -0700823 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700824 return;
825 }
826 }
827
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700828 // Verifies that "bytes" points to valid Modified UTF-8 data.
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700829 void CheckUtfString(const char* bytes, bool nullable) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700830 if (bytes == NULL) {
831 if (!nullable) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700832 JniAbortF(function_name_, "non-nullable const char* was NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -0700833 return;
834 }
835 return;
836 }
837
838 const char* errorKind = NULL;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700839 uint8_t utf8 = CheckUtfBytes(bytes, &errorKind);
Elliott Hughesa2501992011-08-26 19:39:54 -0700840 if (errorKind != NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700841 JniAbortF(function_name_,
842 "input is not valid Modified UTF-8: illegal %s byte %#x\n"
843 " string: '%s'", errorKind, utf8, bytes);
Elliott Hughesa2501992011-08-26 19:39:54 -0700844 return;
845 }
846 }
847
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700848 static uint8_t CheckUtfBytes(const char* bytes, const char** errorKind) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700849 while (*bytes != '\0') {
850 uint8_t utf8 = *(bytes++);
851 // Switch on the high four bits.
852 switch (utf8 >> 4) {
853 case 0x00:
854 case 0x01:
855 case 0x02:
856 case 0x03:
857 case 0x04:
858 case 0x05:
859 case 0x06:
860 case 0x07:
861 // Bit pattern 0xxx. No need for any extra bytes.
862 break;
863 case 0x08:
864 case 0x09:
865 case 0x0a:
866 case 0x0b:
867 case 0x0f:
868 /*
869 * Bit pattern 10xx or 1111, which are illegal start bytes.
870 * Note: 1111 is valid for normal UTF-8, but not the
Elliott Hughes78090d12011-10-07 14:31:47 -0700871 * Modified UTF-8 used here.
Elliott Hughesa2501992011-08-26 19:39:54 -0700872 */
873 *errorKind = "start";
874 return utf8;
875 case 0x0e:
876 // Bit pattern 1110, so there are two additional bytes.
877 utf8 = *(bytes++);
878 if ((utf8 & 0xc0) != 0x80) {
879 *errorKind = "continuation";
880 return utf8;
881 }
882 // Fall through to take care of the final byte.
883 case 0x0c:
884 case 0x0d:
885 // Bit pattern 110x, so there is one additional byte.
886 utf8 = *(bytes++);
887 if ((utf8 & 0xc0) != 0x80) {
888 *errorKind = "continuation";
889 return utf8;
890 }
891 break;
892 }
893 }
894 return 0;
895 }
896
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700897 const ScopedObjectAccess soa_;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700898 const char* function_name_;
899 int flags_;
900 bool has_method_;
Elliott Hughes92cb4982011-12-16 16:57:28 -0800901 int indent_;
Elliott Hughesa2501992011-08-26 19:39:54 -0700902
903 DISALLOW_COPY_AND_ASSIGN(ScopedCheck);
904};
905
906#define CHECK_JNI_ENTRY(flags, types, args...) \
907 ScopedCheck sc(env, flags, __FUNCTION__); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700908 sc.Check(true, types, ##args)
Elliott Hughesa2501992011-08-26 19:39:54 -0700909
910#define CHECK_JNI_EXIT(type, exp) ({ \
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700911 typeof(exp) _rc = (exp); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700912 sc.Check(false, type, _rc); \
Elliott Hughesa2501992011-08-26 19:39:54 -0700913 _rc; })
914#define CHECK_JNI_EXIT_VOID() \
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700915 sc.Check(false, "V")
Elliott Hughesa2501992011-08-26 19:39:54 -0700916
917/*
918 * ===========================================================================
919 * Guarded arrays
920 * ===========================================================================
921 */
922
923#define kGuardLen 512 /* must be multiple of 2 */
924#define kGuardPattern 0xd5e3 /* uncommon values; d5e3d5e3 invalid addr */
925#define kGuardMagic 0xffd5aa96
926
927/* this gets tucked in at the start of the buffer; struct size must be even */
928struct GuardedCopy {
929 uint32_t magic;
930 uLong adler;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700931 size_t original_length;
932 const void* original_ptr;
Elliott Hughesa2501992011-08-26 19:39:54 -0700933
934 /* find the GuardedCopy given the pointer into the "live" data */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700935 static inline const GuardedCopy* FromData(const void* dataBuf) {
936 return reinterpret_cast<const GuardedCopy*>(ActualBuffer(dataBuf));
Elliott Hughesa2501992011-08-26 19:39:54 -0700937 }
938
939 /*
940 * Create an over-sized buffer to hold the contents of "buf". Copy it in,
941 * filling in the area around it with guard data.
942 *
943 * We use a 16-bit pattern to make a rogue memset less likely to elude us.
944 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700945 static void* Create(const void* buf, size_t len, bool modOkay) {
946 size_t newLen = ActualLength(len);
947 uint8_t* newBuf = DebugAlloc(newLen);
Elliott Hughesa2501992011-08-26 19:39:54 -0700948
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700949 // Fill it in with a pattern.
Elliott Hughesba8eee12012-01-24 20:25:24 -0800950 uint16_t* pat = reinterpret_cast<uint16_t*>(newBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -0700951 for (size_t i = 0; i < newLen / 2; i++) {
952 *pat++ = kGuardPattern;
953 }
954
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700955 // Copy the data in; note "len" could be zero.
Elliott Hughesa2501992011-08-26 19:39:54 -0700956 memcpy(newBuf + kGuardLen / 2, buf, len);
957
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700958 // If modification is not expected, grab a checksum.
Elliott Hughesa2501992011-08-26 19:39:54 -0700959 uLong adler = 0;
960 if (!modOkay) {
961 adler = adler32(0L, Z_NULL, 0);
Elliott Hughesba8eee12012-01-24 20:25:24 -0800962 adler = adler32(adler, reinterpret_cast<const Bytef*>(buf), len);
963 *reinterpret_cast<uLong*>(newBuf) = adler;
Elliott Hughesa2501992011-08-26 19:39:54 -0700964 }
965
966 GuardedCopy* pExtra = reinterpret_cast<GuardedCopy*>(newBuf);
967 pExtra->magic = kGuardMagic;
968 pExtra->adler = adler;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700969 pExtra->original_ptr = buf;
970 pExtra->original_length = len;
Elliott Hughesa2501992011-08-26 19:39:54 -0700971
972 return newBuf + kGuardLen / 2;
973 }
974
975 /*
976 * Free up the guard buffer, scrub it, and return the original pointer.
977 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700978 static void* Destroy(void* dataBuf) {
979 const GuardedCopy* pExtra = GuardedCopy::FromData(dataBuf);
Elliott Hughesba8eee12012-01-24 20:25:24 -0800980 void* original_ptr = const_cast<void*>(pExtra->original_ptr);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700981 size_t len = pExtra->original_length;
982 DebugFree(dataBuf, len);
983 return original_ptr;
Elliott Hughesa2501992011-08-26 19:39:54 -0700984 }
985
986 /*
987 * Verify the guard area and, if "modOkay" is false, that the data itself
988 * has not been altered.
989 *
990 * The caller has already checked that "dataBuf" is non-NULL.
991 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700992 static void Check(const char* functionName, const void* dataBuf, bool modOkay) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700993 static const uint32_t kMagicCmp = kGuardMagic;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700994 const uint8_t* fullBuf = ActualBuffer(dataBuf);
995 const GuardedCopy* pExtra = GuardedCopy::FromData(dataBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -0700996
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700997 // Before we do anything with "pExtra", check the magic number. We
998 // do the check with memcmp rather than "==" in case the pointer is
999 // unaligned. If it points to completely bogus memory we're going
1000 // to crash, but there's no easy way around that.
Elliott Hughesa2501992011-08-26 19:39:54 -07001001 if (memcmp(&pExtra->magic, &kMagicCmp, 4) != 0) {
1002 uint8_t buf[4];
1003 memcpy(buf, &pExtra->magic, 4);
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001004 JniAbortF(functionName,
1005 "guard magic does not match (found 0x%02x%02x%02x%02x) -- incorrect data pointer %p?",
1006 buf[3], buf[2], buf[1], buf[0], dataBuf); // Assumes little-endian.
Elliott Hughesa2501992011-08-26 19:39:54 -07001007 }
1008
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001009 size_t len = pExtra->original_length;
Elliott Hughesa2501992011-08-26 19:39:54 -07001010
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001011 // Check bottom half of guard; skip over optional checksum storage.
Elliott Hughesba8eee12012-01-24 20:25:24 -08001012 const uint16_t* pat = reinterpret_cast<const uint16_t*>(fullBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -07001013 for (size_t i = sizeof(GuardedCopy) / 2; i < (kGuardLen / 2 - sizeof(GuardedCopy)) / 2; i++) {
1014 if (pat[i] != kGuardPattern) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001015 JniAbortF(functionName, "guard pattern(1) disturbed at %p +%d", fullBuf, i*2);
Elliott Hughesa2501992011-08-26 19:39:54 -07001016 }
1017 }
1018
1019 int offset = kGuardLen / 2 + len;
1020 if (offset & 0x01) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001021 // Odd byte; expected value depends on endian.
Elliott Hughesa2501992011-08-26 19:39:54 -07001022 const uint16_t patSample = kGuardPattern;
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001023 uint8_t expected_byte = reinterpret_cast<const uint8_t*>(&patSample)[1];
1024 if (fullBuf[offset] != expected_byte) {
1025 JniAbortF(functionName, "guard pattern disturbed in odd byte after %p +%d 0x%02x 0x%02x",
1026 fullBuf, offset, fullBuf[offset], expected_byte);
Elliott Hughesa2501992011-08-26 19:39:54 -07001027 }
1028 offset++;
1029 }
1030
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001031 // Check top half of guard.
Elliott Hughesba8eee12012-01-24 20:25:24 -08001032 pat = reinterpret_cast<const uint16_t*>(fullBuf + offset);
Elliott Hughesa2501992011-08-26 19:39:54 -07001033 for (size_t i = 0; i < kGuardLen / 4; i++) {
1034 if (pat[i] != kGuardPattern) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001035 JniAbortF(functionName, "guard pattern(2) disturbed at %p +%d", fullBuf, offset + i*2);
Elliott Hughesa2501992011-08-26 19:39:54 -07001036 }
1037 }
1038
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001039 // If modification is not expected, verify checksum. Strictly speaking
1040 // this is wrong: if we told the client that we made a copy, there's no
1041 // reason they can't alter the buffer.
Elliott Hughesa2501992011-08-26 19:39:54 -07001042 if (!modOkay) {
1043 uLong adler = adler32(0L, Z_NULL, 0);
1044 adler = adler32(adler, (const Bytef*)dataBuf, len);
1045 if (pExtra->adler != adler) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001046 JniAbortF(functionName, "buffer modified (0x%08lx vs 0x%08lx) at address %p",
1047 pExtra->adler, adler, dataBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -07001048 }
1049 }
1050 }
1051
1052 private:
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001053 static uint8_t* DebugAlloc(size_t len) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001054 void* result = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
1055 if (result == MAP_FAILED) {
1056 PLOG(FATAL) << "GuardedCopy::create mmap(" << len << ") failed";
1057 }
1058 return reinterpret_cast<uint8_t*>(result);
1059 }
1060
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001061 static void DebugFree(void* dataBuf, size_t len) {
1062 uint8_t* fullBuf = ActualBuffer(dataBuf);
1063 size_t totalByteCount = ActualLength(len);
Elliott Hughesa2501992011-08-26 19:39:54 -07001064 // TODO: we could mprotect instead, and keep the allocation around for a while.
1065 // This would be even more expensive, but it might catch more errors.
1066 // if (mprotect(fullBuf, totalByteCount, PROT_NONE) != 0) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -07001067 // PLOG(WARNING) << "mprotect(PROT_NONE) failed";
Elliott Hughesa2501992011-08-26 19:39:54 -07001068 // }
1069 if (munmap(fullBuf, totalByteCount) != 0) {
Elliott Hughesba8eee12012-01-24 20:25:24 -08001070 PLOG(FATAL) << "munmap(" << reinterpret_cast<void*>(fullBuf) << ", " << totalByteCount << ") failed";
Elliott Hughesa2501992011-08-26 19:39:54 -07001071 }
1072 }
1073
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001074 static const uint8_t* ActualBuffer(const void* dataBuf) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001075 return reinterpret_cast<const uint8_t*>(dataBuf) - kGuardLen / 2;
1076 }
1077
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001078 static uint8_t* ActualBuffer(void* dataBuf) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001079 return reinterpret_cast<uint8_t*>(dataBuf) - kGuardLen / 2;
1080 }
1081
1082 // Underlying length of a user allocation of 'length' bytes.
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001083 static size_t ActualLength(size_t length) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001084 return (length + kGuardLen + 1) & ~0x01;
1085 }
1086};
1087
1088/*
1089 * Create a guarded copy of a primitive array. Modifications to the copied
1090 * data are allowed. Returns a pointer to the copied data.
1091 */
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001092static void* CreateGuardedPACopy(JNIEnv* env, const jarray java_array, jboolean* isCopy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001093 ScopedObjectAccess soa(env);
Elliott Hughesa2501992011-08-26 19:39:54 -07001094
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001095 mirror::Array* a = soa.Decode<mirror::Array*>(java_array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001096 size_t component_size = a->GetClass()->GetComponentSize();
1097 size_t byte_count = a->GetLength() * component_size;
1098 void* result = GuardedCopy::Create(a->GetRawData(component_size), byte_count, true);
Elliott Hughesa2501992011-08-26 19:39:54 -07001099 if (isCopy != NULL) {
1100 *isCopy = JNI_TRUE;
1101 }
1102 return result;
1103}
1104
1105/*
1106 * Perform the array "release" operation, which may or may not copy data
Elliott Hughes81ff3182012-03-23 20:35:56 -07001107 * back into the managed heap, and may or may not release the underlying storage.
Elliott Hughesa2501992011-08-26 19:39:54 -07001108 */
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001109static void ReleaseGuardedPACopy(JNIEnv* env, jarray java_array, void* dataBuf, int mode) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001110 if (reinterpret_cast<uintptr_t>(dataBuf) == kNoCopyMagic) {
1111 return;
1112 }
1113
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001114 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001115 mirror::Array* a = soa.Decode<mirror::Array*>(java_array);
Elliott Hughesa2501992011-08-26 19:39:54 -07001116
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001117 GuardedCopy::Check(__FUNCTION__, dataBuf, true);
Elliott Hughesa2501992011-08-26 19:39:54 -07001118
1119 if (mode != JNI_ABORT) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001120 size_t len = GuardedCopy::FromData(dataBuf)->original_length;
Ian Rogersa15e67d2012-02-28 13:51:55 -08001121 memcpy(a->GetRawData(a->GetClass()->GetComponentSize()), dataBuf, len);
Elliott Hughesa2501992011-08-26 19:39:54 -07001122 }
1123 if (mode != JNI_COMMIT) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001124 GuardedCopy::Destroy(dataBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -07001125 }
1126}
1127
1128/*
1129 * ===========================================================================
1130 * JNI functions
1131 * ===========================================================================
1132 */
1133
1134class CheckJNI {
1135 public:
1136 static jint GetVersion(JNIEnv* env) {
1137 CHECK_JNI_ENTRY(kFlag_Default, "E", env);
1138 return CHECK_JNI_EXIT("I", baseEnv(env)->GetVersion(env));
1139 }
1140
1141 static jclass DefineClass(JNIEnv* env, const char* name, jobject loader, const jbyte* buf, jsize bufLen) {
1142 CHECK_JNI_ENTRY(kFlag_Default, "EuLpz", env, name, loader, buf, bufLen);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001143 sc.CheckClassName(name);
Elliott Hughesa2501992011-08-26 19:39:54 -07001144 return CHECK_JNI_EXIT("c", baseEnv(env)->DefineClass(env, name, loader, buf, bufLen));
1145 }
1146
1147 static jclass FindClass(JNIEnv* env, const char* name) {
1148 CHECK_JNI_ENTRY(kFlag_Default, "Eu", env, name);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001149 sc.CheckClassName(name);
Elliott Hughesa2501992011-08-26 19:39:54 -07001150 return CHECK_JNI_EXIT("c", baseEnv(env)->FindClass(env, name));
1151 }
1152
Elliott Hughese84278b2012-03-22 10:06:53 -07001153 static jclass GetSuperclass(JNIEnv* env, jclass c) {
1154 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1155 return CHECK_JNI_EXIT("c", baseEnv(env)->GetSuperclass(env, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001156 }
1157
Elliott Hughese84278b2012-03-22 10:06:53 -07001158 static jboolean IsAssignableFrom(JNIEnv* env, jclass c1, jclass c2) {
1159 CHECK_JNI_ENTRY(kFlag_Default, "Ecc", env, c1, c2);
1160 return CHECK_JNI_EXIT("b", baseEnv(env)->IsAssignableFrom(env, c1, c2));
Elliott Hughesa2501992011-08-26 19:39:54 -07001161 }
1162
1163 static jmethodID FromReflectedMethod(JNIEnv* env, jobject method) {
1164 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, method);
1165 // TODO: check that 'field' is a java.lang.reflect.Method.
1166 return CHECK_JNI_EXIT("m", baseEnv(env)->FromReflectedMethod(env, method));
1167 }
1168
1169 static jfieldID FromReflectedField(JNIEnv* env, jobject field) {
1170 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, field);
1171 // TODO: check that 'field' is a java.lang.reflect.Field.
1172 return CHECK_JNI_EXIT("f", baseEnv(env)->FromReflectedField(env, field));
1173 }
1174
1175 static jobject ToReflectedMethod(JNIEnv* env, jclass cls, jmethodID mid, jboolean isStatic) {
1176 CHECK_JNI_ENTRY(kFlag_Default, "Ecmb", env, cls, mid, isStatic);
1177 return CHECK_JNI_EXIT("L", baseEnv(env)->ToReflectedMethod(env, cls, mid, isStatic));
1178 }
1179
1180 static jobject ToReflectedField(JNIEnv* env, jclass cls, jfieldID fid, jboolean isStatic) {
1181 CHECK_JNI_ENTRY(kFlag_Default, "Ecfb", env, cls, fid, isStatic);
1182 return CHECK_JNI_EXIT("L", baseEnv(env)->ToReflectedField(env, cls, fid, isStatic));
1183 }
1184
1185 static jint Throw(JNIEnv* env, jthrowable obj) {
1186 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1187 // TODO: check that 'obj' is a java.lang.Throwable.
1188 return CHECK_JNI_EXIT("I", baseEnv(env)->Throw(env, obj));
1189 }
1190
Elliott Hughese84278b2012-03-22 10:06:53 -07001191 static jint ThrowNew(JNIEnv* env, jclass c, const char* message) {
1192 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Ecu", env, c, message);
1193 return CHECK_JNI_EXIT("I", baseEnv(env)->ThrowNew(env, c, message));
Elliott Hughesa2501992011-08-26 19:39:54 -07001194 }
1195
1196 static jthrowable ExceptionOccurred(JNIEnv* env) {
1197 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1198 return CHECK_JNI_EXIT("L", baseEnv(env)->ExceptionOccurred(env));
1199 }
1200
1201 static void ExceptionDescribe(JNIEnv* env) {
1202 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1203 baseEnv(env)->ExceptionDescribe(env);
1204 CHECK_JNI_EXIT_VOID();
1205 }
1206
1207 static void ExceptionClear(JNIEnv* env) {
1208 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1209 baseEnv(env)->ExceptionClear(env);
1210 CHECK_JNI_EXIT_VOID();
1211 }
1212
1213 static void FatalError(JNIEnv* env, const char* msg) {
1214 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Eu", env, msg);
1215 baseEnv(env)->FatalError(env, msg);
1216 CHECK_JNI_EXIT_VOID();
1217 }
1218
1219 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
1220 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EI", env, capacity);
1221 return CHECK_JNI_EXIT("I", baseEnv(env)->PushLocalFrame(env, capacity));
1222 }
1223
1224 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
1225 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, res);
1226 return CHECK_JNI_EXIT("L", baseEnv(env)->PopLocalFrame(env, res));
1227 }
1228
1229 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
1230 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1231 return CHECK_JNI_EXIT("L", baseEnv(env)->NewGlobalRef(env, obj));
1232 }
1233
1234 static jobject NewLocalRef(JNIEnv* env, jobject ref) {
1235 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, ref);
1236 return CHECK_JNI_EXIT("L", baseEnv(env)->NewLocalRef(env, ref));
1237 }
1238
1239 static void DeleteGlobalRef(JNIEnv* env, jobject globalRef) {
1240 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, globalRef);
1241 if (globalRef != NULL && GetIndirectRefKind(globalRef) != kGlobal) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001242 JniAbortF(__FUNCTION__, "DeleteGlobalRef on %s: %p",
1243 ToStr<IndirectRefKind>(GetIndirectRefKind(globalRef)).c_str(), globalRef);
Elliott Hughesa2501992011-08-26 19:39:54 -07001244 } else {
1245 baseEnv(env)->DeleteGlobalRef(env, globalRef);
1246 CHECK_JNI_EXIT_VOID();
1247 }
1248 }
1249
1250 static void DeleteWeakGlobalRef(JNIEnv* env, jweak weakGlobalRef) {
1251 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, weakGlobalRef);
1252 if (weakGlobalRef != NULL && GetIndirectRefKind(weakGlobalRef) != kWeakGlobal) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001253 JniAbortF(__FUNCTION__, "DeleteWeakGlobalRef on %s: %p",
1254 ToStr<IndirectRefKind>(GetIndirectRefKind(weakGlobalRef)).c_str(), weakGlobalRef);
Elliott Hughesa2501992011-08-26 19:39:54 -07001255 } else {
1256 baseEnv(env)->DeleteWeakGlobalRef(env, weakGlobalRef);
1257 CHECK_JNI_EXIT_VOID();
1258 }
1259 }
1260
1261 static void DeleteLocalRef(JNIEnv* env, jobject localRef) {
1262 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, localRef);
Ian Rogers959f8ed2012-02-07 16:33:37 -08001263 if (localRef != NULL && GetIndirectRefKind(localRef) != kLocal && !IsSirtLocalRef(env, localRef)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001264 JniAbortF(__FUNCTION__, "DeleteLocalRef on %s: %p",
1265 ToStr<IndirectRefKind>(GetIndirectRefKind(localRef)).c_str(), localRef);
Elliott Hughesa2501992011-08-26 19:39:54 -07001266 } else {
1267 baseEnv(env)->DeleteLocalRef(env, localRef);
1268 CHECK_JNI_EXIT_VOID();
1269 }
1270 }
1271
1272 static jint EnsureLocalCapacity(JNIEnv *env, jint capacity) {
1273 CHECK_JNI_ENTRY(kFlag_Default, "EI", env, capacity);
1274 return CHECK_JNI_EXIT("I", baseEnv(env)->EnsureLocalCapacity(env, capacity));
1275 }
1276
1277 static jboolean IsSameObject(JNIEnv* env, jobject ref1, jobject ref2) {
1278 CHECK_JNI_ENTRY(kFlag_Default, "ELL", env, ref1, ref2);
1279 return CHECK_JNI_EXIT("b", baseEnv(env)->IsSameObject(env, ref1, ref2));
1280 }
1281
Elliott Hughese84278b2012-03-22 10:06:53 -07001282 static jobject AllocObject(JNIEnv* env, jclass c) {
1283 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1284 return CHECK_JNI_EXIT("L", baseEnv(env)->AllocObject(env, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001285 }
1286
Elliott Hughese84278b2012-03-22 10:06:53 -07001287 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
1288 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
Elliott Hughesa2501992011-08-26 19:39:54 -07001289 va_list args;
1290 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -07001291 jobject result = baseEnv(env)->NewObjectV(env, c, mid, args);
Elliott Hughesa2501992011-08-26 19:39:54 -07001292 va_end(args);
1293 return CHECK_JNI_EXIT("L", result);
1294 }
1295
Elliott Hughese84278b2012-03-22 10:06:53 -07001296 static jobject NewObjectV(JNIEnv* env, jclass c, jmethodID mid, va_list args) {
1297 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
1298 return CHECK_JNI_EXIT("L", baseEnv(env)->NewObjectV(env, c, mid, args));
Elliott Hughesa2501992011-08-26 19:39:54 -07001299 }
1300
Elliott Hughese84278b2012-03-22 10:06:53 -07001301 static jobject NewObjectA(JNIEnv* env, jclass c, jmethodID mid, jvalue* args) {
1302 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
1303 return CHECK_JNI_EXIT("L", baseEnv(env)->NewObjectA(env, c, mid, args));
Elliott Hughesa2501992011-08-26 19:39:54 -07001304 }
1305
1306 static jclass GetObjectClass(JNIEnv* env, jobject obj) {
1307 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1308 return CHECK_JNI_EXIT("c", baseEnv(env)->GetObjectClass(env, obj));
1309 }
1310
Elliott Hughese84278b2012-03-22 10:06:53 -07001311 static jboolean IsInstanceOf(JNIEnv* env, jobject obj, jclass c) {
1312 CHECK_JNI_ENTRY(kFlag_Default, "ELc", env, obj, c);
1313 return CHECK_JNI_EXIT("b", baseEnv(env)->IsInstanceOf(env, obj, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001314 }
1315
Elliott Hughese84278b2012-03-22 10:06:53 -07001316 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1317 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1318 return CHECK_JNI_EXIT("m", baseEnv(env)->GetMethodID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001319 }
1320
Elliott Hughese84278b2012-03-22 10:06:53 -07001321 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1322 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1323 return CHECK_JNI_EXIT("f", baseEnv(env)->GetFieldID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001324 }
1325
Elliott Hughese84278b2012-03-22 10:06:53 -07001326 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1327 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1328 return CHECK_JNI_EXIT("m", baseEnv(env)->GetStaticMethodID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001329 }
1330
Elliott Hughese84278b2012-03-22 10:06:53 -07001331 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1332 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1333 return CHECK_JNI_EXIT("f", baseEnv(env)->GetStaticFieldID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001334 }
1335
1336#define FIELD_ACCESSORS(_ctype, _jname, _type) \
Elliott Hughese84278b2012-03-22 10:06:53 -07001337 static _ctype GetStatic##_jname##Field(JNIEnv* env, jclass c, jfieldID fid) { \
1338 CHECK_JNI_ENTRY(kFlag_Default, "Ecf", env, c, fid); \
1339 sc.CheckStaticFieldID(c, fid); \
1340 return CHECK_JNI_EXIT(_type, baseEnv(env)->GetStatic##_jname##Field(env, c, fid)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001341 } \
1342 static _ctype Get##_jname##Field(JNIEnv* env, jobject obj, jfieldID fid) { \
1343 CHECK_JNI_ENTRY(kFlag_Default, "ELf", env, obj, fid); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001344 sc.CheckInstanceFieldID(obj, fid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001345 return CHECK_JNI_EXIT(_type, baseEnv(env)->Get##_jname##Field(env, obj, fid)); \
1346 } \
Elliott Hughese84278b2012-03-22 10:06:53 -07001347 static void SetStatic##_jname##Field(JNIEnv* env, jclass c, jfieldID fid, _ctype value) { \
1348 CHECK_JNI_ENTRY(kFlag_Default, "Ecf" _type, env, c, fid, value); \
1349 sc.CheckStaticFieldID(c, fid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001350 /* "value" arg only used when type == ref */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001351 sc.CheckFieldType((jobject)(uint32_t)value, fid, _type[0], true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001352 baseEnv(env)->SetStatic##_jname##Field(env, c, fid, value); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001353 CHECK_JNI_EXIT_VOID(); \
1354 } \
1355 static void Set##_jname##Field(JNIEnv* env, jobject obj, jfieldID fid, _ctype value) { \
1356 CHECK_JNI_ENTRY(kFlag_Default, "ELf" _type, env, obj, fid, value); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001357 sc.CheckInstanceFieldID(obj, fid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001358 /* "value" arg only used when type == ref */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001359 sc.CheckFieldType((jobject)(uint32_t) value, fid, _type[0], false); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001360 baseEnv(env)->Set##_jname##Field(env, obj, fid, value); \
1361 CHECK_JNI_EXIT_VOID(); \
1362 }
1363
1364FIELD_ACCESSORS(jobject, Object, "L");
1365FIELD_ACCESSORS(jboolean, Boolean, "Z");
1366FIELD_ACCESSORS(jbyte, Byte, "B");
1367FIELD_ACCESSORS(jchar, Char, "C");
1368FIELD_ACCESSORS(jshort, Short, "S");
1369FIELD_ACCESSORS(jint, Int, "I");
1370FIELD_ACCESSORS(jlong, Long, "J");
1371FIELD_ACCESSORS(jfloat, Float, "F");
1372FIELD_ACCESSORS(jdouble, Double, "D");
1373
1374#define CALL(_ctype, _jname, _retdecl, _retasgn, _retok, _retsig) \
1375 /* Virtual... */ \
1376 static _ctype Call##_jname##Method(JNIEnv* env, jobject obj, \
1377 jmethodID mid, ...) \
1378 { \
1379 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001380 sc.CheckSig(mid, _retsig, false); \
1381 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001382 _retdecl; \
1383 va_list args; \
1384 va_start(args, mid); \
Elliott Hughesba8eee12012-01-24 20:25:24 -08001385 _retasgn(baseEnv(env)->Call##_jname##MethodV(env, obj, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001386 va_end(args); \
1387 _retok; \
1388 } \
1389 static _ctype Call##_jname##MethodV(JNIEnv* env, jobject obj, \
1390 jmethodID mid, va_list args) \
1391 { \
1392 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001393 sc.CheckSig(mid, _retsig, false); \
1394 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001395 _retdecl; \
Elliott Hughesba8eee12012-01-24 20:25:24 -08001396 _retasgn(baseEnv(env)->Call##_jname##MethodV(env, obj, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001397 _retok; \
1398 } \
1399 static _ctype Call##_jname##MethodA(JNIEnv* env, jobject obj, \
1400 jmethodID mid, jvalue* args) \
1401 { \
1402 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001403 sc.CheckSig(mid, _retsig, false); \
1404 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001405 _retdecl; \
Elliott Hughesba8eee12012-01-24 20:25:24 -08001406 _retasgn(baseEnv(env)->Call##_jname##MethodA(env, obj, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001407 _retok; \
1408 } \
1409 /* Non-virtual... */ \
1410 static _ctype CallNonvirtual##_jname##Method(JNIEnv* env, \
Elliott Hughese84278b2012-03-22 10:06:53 -07001411 jobject obj, jclass c, jmethodID mid, ...) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001412 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001413 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001414 sc.CheckSig(mid, _retsig, false); \
1415 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001416 _retdecl; \
1417 va_list args; \
1418 va_start(args, mid); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001419 _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodV(env, obj, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001420 va_end(args); \
1421 _retok; \
1422 } \
1423 static _ctype CallNonvirtual##_jname##MethodV(JNIEnv* env, \
Elliott Hughese84278b2012-03-22 10:06:53 -07001424 jobject obj, jclass c, jmethodID mid, va_list args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001425 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001426 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001427 sc.CheckSig(mid, _retsig, false); \
1428 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001429 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001430 _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodV(env, obj, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001431 _retok; \
1432 } \
1433 static _ctype CallNonvirtual##_jname##MethodA(JNIEnv* env, \
Elliott Hughese84278b2012-03-22 10:06:53 -07001434 jobject obj, jclass c, jmethodID mid, jvalue* args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001435 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001436 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001437 sc.CheckSig(mid, _retsig, false); \
1438 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001439 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001440 _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodA(env, obj, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001441 _retok; \
1442 } \
1443 /* Static... */ \
Elliott Hughese84278b2012-03-22 10:06:53 -07001444 static _ctype CallStatic##_jname##Method(JNIEnv* env, jclass c, jmethodID mid, ...) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001445 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001446 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001447 sc.CheckSig(mid, _retsig, true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001448 sc.CheckStaticMethod(c, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001449 _retdecl; \
1450 va_list args; \
1451 va_start(args, mid); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001452 _retasgn(baseEnv(env)->CallStatic##_jname##MethodV(env, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001453 va_end(args); \
1454 _retok; \
1455 } \
Elliott Hughese84278b2012-03-22 10:06:53 -07001456 static _ctype CallStatic##_jname##MethodV(JNIEnv* env, jclass c, jmethodID mid, va_list args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001457 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001458 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001459 sc.CheckSig(mid, _retsig, true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001460 sc.CheckStaticMethod(c, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001461 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001462 _retasgn(baseEnv(env)->CallStatic##_jname##MethodV(env, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001463 _retok; \
1464 } \
Elliott Hughese84278b2012-03-22 10:06:53 -07001465 static _ctype CallStatic##_jname##MethodA(JNIEnv* env, jclass c, jmethodID mid, jvalue* args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001466 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001467 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001468 sc.CheckSig(mid, _retsig, true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001469 sc.CheckStaticMethod(c, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001470 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001471 _retasgn(baseEnv(env)->CallStatic##_jname##MethodA(env, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001472 _retok; \
1473 }
1474
1475#define NON_VOID_RETURN(_retsig, _ctype) return CHECK_JNI_EXIT(_retsig, (_ctype) result)
1476#define VOID_RETURN CHECK_JNI_EXIT_VOID()
1477
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001478CALL(jobject, Object, mirror::Object* result, result = reinterpret_cast<mirror::Object*>, NON_VOID_RETURN("L", jobject), "L");
Elliott Hughesba8eee12012-01-24 20:25:24 -08001479CALL(jboolean, Boolean, jboolean result, result =, NON_VOID_RETURN("Z", jboolean), "Z");
1480CALL(jbyte, Byte, jbyte result, result =, NON_VOID_RETURN("B", jbyte), "B");
1481CALL(jchar, Char, jchar result, result =, NON_VOID_RETURN("C", jchar), "C");
1482CALL(jshort, Short, jshort result, result =, NON_VOID_RETURN("S", jshort), "S");
1483CALL(jint, Int, jint result, result =, NON_VOID_RETURN("I", jint), "I");
1484CALL(jlong, Long, jlong result, result =, NON_VOID_RETURN("J", jlong), "J");
1485CALL(jfloat, Float, jfloat result, result =, NON_VOID_RETURN("F", jfloat), "F");
1486CALL(jdouble, Double, jdouble result, result =, NON_VOID_RETURN("D", jdouble), "D");
Elliott Hughesa2501992011-08-26 19:39:54 -07001487CALL(void, Void, , , VOID_RETURN, "V");
1488
1489 static jstring NewString(JNIEnv* env, const jchar* unicodeChars, jsize len) {
1490 CHECK_JNI_ENTRY(kFlag_Default, "Epz", env, unicodeChars, len);
1491 return CHECK_JNI_EXIT("s", baseEnv(env)->NewString(env, unicodeChars, len));
1492 }
1493
1494 static jsize GetStringLength(JNIEnv* env, jstring string) {
1495 CHECK_JNI_ENTRY(kFlag_CritOkay, "Es", env, string);
1496 return CHECK_JNI_EXIT("I", baseEnv(env)->GetStringLength(env, string));
1497 }
1498
1499 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* isCopy) {
1500 CHECK_JNI_ENTRY(kFlag_CritOkay, "Esp", env, java_string, isCopy);
1501 const jchar* result = baseEnv(env)->GetStringChars(env, java_string, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001502 if (sc.ForceCopy() && result != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001503 mirror::String* s = sc.soa().Decode<mirror::String*>(java_string);
Elliott Hughesa2501992011-08-26 19:39:54 -07001504 int byteCount = s->GetLength() * 2;
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001505 result = (const jchar*) GuardedCopy::Create(result, byteCount, false);
Elliott Hughesa2501992011-08-26 19:39:54 -07001506 if (isCopy != NULL) {
1507 *isCopy = JNI_TRUE;
1508 }
1509 }
1510 return CHECK_JNI_EXIT("p", result);
1511 }
1512
1513 static void ReleaseStringChars(JNIEnv* env, jstring string, const jchar* chars) {
1514 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "Esp", env, string, chars);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001515 sc.CheckNonNull(chars);
1516 if (sc.ForceCopy()) {
1517 GuardedCopy::Check(__FUNCTION__, chars, false);
Elliott Hughesba8eee12012-01-24 20:25:24 -08001518 chars = reinterpret_cast<const jchar*>(GuardedCopy::Destroy(const_cast<jchar*>(chars)));
Elliott Hughesa2501992011-08-26 19:39:54 -07001519 }
1520 baseEnv(env)->ReleaseStringChars(env, string, chars);
1521 CHECK_JNI_EXIT_VOID();
1522 }
1523
1524 static jstring NewStringUTF(JNIEnv* env, const char* bytes) {
1525 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Eu", env, bytes); // TODO: show pointer and truncate string.
1526 return CHECK_JNI_EXIT("s", baseEnv(env)->NewStringUTF(env, bytes));
1527 }
1528
1529 static jsize GetStringUTFLength(JNIEnv* env, jstring string) {
1530 CHECK_JNI_ENTRY(kFlag_CritOkay, "Es", env, string);
1531 return CHECK_JNI_EXIT("I", baseEnv(env)->GetStringUTFLength(env, string));
1532 }
1533
1534 static const char* GetStringUTFChars(JNIEnv* env, jstring string, jboolean* isCopy) {
1535 CHECK_JNI_ENTRY(kFlag_CritOkay, "Esp", env, string, isCopy);
1536 const char* result = baseEnv(env)->GetStringUTFChars(env, string, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001537 if (sc.ForceCopy() && result != NULL) {
1538 result = (const char*) GuardedCopy::Create(result, strlen(result) + 1, false);
Elliott Hughesa2501992011-08-26 19:39:54 -07001539 if (isCopy != NULL) {
1540 *isCopy = JNI_TRUE;
1541 }
1542 }
1543 return CHECK_JNI_EXIT("u", result); // TODO: show pointer and truncate string.
1544 }
1545
1546 static void ReleaseStringUTFChars(JNIEnv* env, jstring string, const char* utf) {
1547 CHECK_JNI_ENTRY(kFlag_ExcepOkay | kFlag_Release, "Esu", env, string, utf); // TODO: show pointer and truncate string.
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001548 if (sc.ForceCopy()) {
1549 GuardedCopy::Check(__FUNCTION__, utf, false);
Elliott Hughesba8eee12012-01-24 20:25:24 -08001550 utf = reinterpret_cast<const char*>(GuardedCopy::Destroy(const_cast<char*>(utf)));
Elliott Hughesa2501992011-08-26 19:39:54 -07001551 }
1552 baseEnv(env)->ReleaseStringUTFChars(env, string, utf);
1553 CHECK_JNI_EXIT_VOID();
1554 }
1555
1556 static jsize GetArrayLength(JNIEnv* env, jarray array) {
1557 CHECK_JNI_ENTRY(kFlag_CritOkay, "Ea", env, array);
1558 return CHECK_JNI_EXIT("I", baseEnv(env)->GetArrayLength(env, array));
1559 }
1560
1561 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass elementClass, jobject initialElement) {
1562 CHECK_JNI_ENTRY(kFlag_Default, "EzcL", env, length, elementClass, initialElement);
1563 return CHECK_JNI_EXIT("a", baseEnv(env)->NewObjectArray(env, length, elementClass, initialElement));
1564 }
1565
1566 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index) {
1567 CHECK_JNI_ENTRY(kFlag_Default, "EaI", env, array, index);
1568 return CHECK_JNI_EXIT("L", baseEnv(env)->GetObjectArrayElement(env, array, index));
1569 }
1570
1571 static void SetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index, jobject value) {
1572 CHECK_JNI_ENTRY(kFlag_Default, "EaIL", env, array, index, value);
1573 baseEnv(env)->SetObjectArrayElement(env, array, index, value);
1574 CHECK_JNI_EXIT_VOID();
1575 }
1576
1577#define NEW_PRIMITIVE_ARRAY(_artype, _jname) \
1578 static _artype New##_jname##Array(JNIEnv* env, jsize length) { \
1579 CHECK_JNI_ENTRY(kFlag_Default, "Ez", env, length); \
1580 return CHECK_JNI_EXIT("a", baseEnv(env)->New##_jname##Array(env, length)); \
1581 }
1582NEW_PRIMITIVE_ARRAY(jbooleanArray, Boolean);
1583NEW_PRIMITIVE_ARRAY(jbyteArray, Byte);
1584NEW_PRIMITIVE_ARRAY(jcharArray, Char);
1585NEW_PRIMITIVE_ARRAY(jshortArray, Short);
1586NEW_PRIMITIVE_ARRAY(jintArray, Int);
1587NEW_PRIMITIVE_ARRAY(jlongArray, Long);
1588NEW_PRIMITIVE_ARRAY(jfloatArray, Float);
1589NEW_PRIMITIVE_ARRAY(jdoubleArray, Double);
1590
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001591struct ForceCopyGetChecker {
Elliott Hughesba8eee12012-01-24 20:25:24 -08001592 public:
Elliott Hughesa2501992011-08-26 19:39:54 -07001593 ForceCopyGetChecker(ScopedCheck& sc, jboolean* isCopy) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001594 force_copy = sc.ForceCopy();
1595 no_copy = 0;
1596 if (force_copy && isCopy != NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001597 // Capture this before the base call tramples on it.
Elliott Hughesba8eee12012-01-24 20:25:24 -08001598 no_copy = *reinterpret_cast<uint32_t*>(isCopy);
Elliott Hughesa2501992011-08-26 19:39:54 -07001599 }
1600 }
1601
1602 template<typename ResultT>
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001603 ResultT Check(JNIEnv* env, jarray array, jboolean* isCopy, ResultT result) {
1604 if (force_copy && result != NULL) {
1605 if (no_copy != kNoCopyMagic) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001606 result = reinterpret_cast<ResultT>(CreateGuardedPACopy(env, array, isCopy));
1607 }
1608 }
1609 return result;
1610 }
1611
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001612 uint32_t no_copy;
1613 bool force_copy;
Elliott Hughesa2501992011-08-26 19:39:54 -07001614};
1615
1616#define GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \
1617 static _ctype* Get##_jname##ArrayElements(JNIEnv* env, _ctype##Array array, jboolean* isCopy) { \
1618 CHECK_JNI_ENTRY(kFlag_Default, "Eap", env, array, isCopy); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001619 _ctype* result = ForceCopyGetChecker(sc, isCopy).Check(env, array, isCopy, baseEnv(env)->Get##_jname##ArrayElements(env, array, isCopy)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001620 return CHECK_JNI_EXIT("p", result); \
1621 }
1622
1623#define RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \
1624 static void Release##_jname##ArrayElements(JNIEnv* env, _ctype##Array array, _ctype* elems, jint mode) { \
1625 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "Eapr", env, array, elems, mode); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001626 sc.CheckNonNull(elems); \
1627 if (sc.ForceCopy()) { \
Elliott Hughesa2501992011-08-26 19:39:54 -07001628 ReleaseGuardedPACopy(env, array, elems, mode); \
1629 } \
1630 baseEnv(env)->Release##_jname##ArrayElements(env, array, elems, mode); \
1631 CHECK_JNI_EXIT_VOID(); \
1632 }
1633
1634#define GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \
1635 static void Get##_jname##ArrayRegion(JNIEnv* env, _ctype##Array array, jsize start, jsize len, _ctype* buf) { \
1636 CHECK_JNI_ENTRY(kFlag_Default, "EaIIp", env, array, start, len, buf); \
1637 baseEnv(env)->Get##_jname##ArrayRegion(env, array, start, len, buf); \
1638 CHECK_JNI_EXIT_VOID(); \
1639 }
1640
1641#define SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \
1642 static void Set##_jname##ArrayRegion(JNIEnv* env, _ctype##Array array, jsize start, jsize len, const _ctype* buf) { \
1643 CHECK_JNI_ENTRY(kFlag_Default, "EaIIp", env, array, start, len, buf); \
1644 baseEnv(env)->Set##_jname##ArrayRegion(env, array, start, len, buf); \
1645 CHECK_JNI_EXIT_VOID(); \
1646 }
1647
1648#define PRIMITIVE_ARRAY_FUNCTIONS(_ctype, _jname, _typechar) \
1649 GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \
1650 RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \
1651 GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname); \
1652 SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname);
1653
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001654// TODO: verify primitive array type matches call type.
Elliott Hughesa2501992011-08-26 19:39:54 -07001655PRIMITIVE_ARRAY_FUNCTIONS(jboolean, Boolean, 'Z');
1656PRIMITIVE_ARRAY_FUNCTIONS(jbyte, Byte, 'B');
1657PRIMITIVE_ARRAY_FUNCTIONS(jchar, Char, 'C');
1658PRIMITIVE_ARRAY_FUNCTIONS(jshort, Short, 'S');
1659PRIMITIVE_ARRAY_FUNCTIONS(jint, Int, 'I');
1660PRIMITIVE_ARRAY_FUNCTIONS(jlong, Long, 'J');
1661PRIMITIVE_ARRAY_FUNCTIONS(jfloat, Float, 'F');
1662PRIMITIVE_ARRAY_FUNCTIONS(jdouble, Double, 'D');
1663
Elliott Hughese84278b2012-03-22 10:06:53 -07001664 static jint RegisterNatives(JNIEnv* env, jclass c, const JNINativeMethod* methods, jint nMethods) {
1665 CHECK_JNI_ENTRY(kFlag_Default, "EcpI", env, c, methods, nMethods);
1666 return CHECK_JNI_EXIT("I", baseEnv(env)->RegisterNatives(env, c, methods, nMethods));
Elliott Hughesa2501992011-08-26 19:39:54 -07001667 }
1668
Elliott Hughese84278b2012-03-22 10:06:53 -07001669 static jint UnregisterNatives(JNIEnv* env, jclass c) {
1670 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1671 return CHECK_JNI_EXIT("I", baseEnv(env)->UnregisterNatives(env, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001672 }
1673
1674 static jint MonitorEnter(JNIEnv* env, jobject obj) {
1675 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
Elliott Hughesa92853e2012-02-07 16:09:27 -08001676 if (!sc.CheckInstance(ScopedCheck::kObject, obj)) {
1677 return JNI_ERR; // Only for jni_internal_test. Real code will have aborted already.
1678 }
Elliott Hughesa2501992011-08-26 19:39:54 -07001679 return CHECK_JNI_EXIT("I", baseEnv(env)->MonitorEnter(env, obj));
1680 }
1681
1682 static jint MonitorExit(JNIEnv* env, jobject obj) {
1683 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, obj);
Elliott Hughesa92853e2012-02-07 16:09:27 -08001684 if (!sc.CheckInstance(ScopedCheck::kObject, obj)) {
1685 return JNI_ERR; // Only for jni_internal_test. Real code will have aborted already.
1686 }
Elliott Hughesa2501992011-08-26 19:39:54 -07001687 return CHECK_JNI_EXIT("I", baseEnv(env)->MonitorExit(env, obj));
1688 }
1689
1690 static jint GetJavaVM(JNIEnv *env, JavaVM **vm) {
1691 CHECK_JNI_ENTRY(kFlag_Default, "Ep", env, vm);
1692 return CHECK_JNI_EXIT("I", baseEnv(env)->GetJavaVM(env, vm));
1693 }
1694
1695 static void GetStringRegion(JNIEnv* env, jstring str, jsize start, jsize len, jchar* buf) {
1696 CHECK_JNI_ENTRY(kFlag_CritOkay, "EsIIp", env, str, start, len, buf);
1697 baseEnv(env)->GetStringRegion(env, str, start, len, buf);
1698 CHECK_JNI_EXIT_VOID();
1699 }
1700
1701 static void GetStringUTFRegion(JNIEnv* env, jstring str, jsize start, jsize len, char* buf) {
1702 CHECK_JNI_ENTRY(kFlag_CritOkay, "EsIIp", env, str, start, len, buf);
1703 baseEnv(env)->GetStringUTFRegion(env, str, start, len, buf);
1704 CHECK_JNI_EXIT_VOID();
1705 }
1706
1707 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* isCopy) {
1708 CHECK_JNI_ENTRY(kFlag_CritGet, "Eap", env, array, isCopy);
1709 void* result = baseEnv(env)->GetPrimitiveArrayCritical(env, array, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001710 if (sc.ForceCopy() && result != NULL) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001711 result = CreateGuardedPACopy(env, array, isCopy);
1712 }
1713 return CHECK_JNI_EXIT("p", result);
1714 }
1715
1716 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* carray, jint mode) {
1717 CHECK_JNI_ENTRY(kFlag_CritRelease | kFlag_ExcepOkay, "Eapr", env, array, carray, mode);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001718 sc.CheckNonNull(carray);
1719 if (sc.ForceCopy()) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001720 ReleaseGuardedPACopy(env, array, carray, mode);
1721 }
1722 baseEnv(env)->ReleasePrimitiveArrayCritical(env, array, carray, mode);
1723 CHECK_JNI_EXIT_VOID();
1724 }
1725
1726 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* isCopy) {
1727 CHECK_JNI_ENTRY(kFlag_CritGet, "Esp", env, java_string, isCopy);
1728 const jchar* result = baseEnv(env)->GetStringCritical(env, java_string, isCopy);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001729 if (sc.ForceCopy() && result != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001730 mirror::String* s = sc.soa().Decode<mirror::String*>(java_string);
Elliott Hughesa2501992011-08-26 19:39:54 -07001731 int byteCount = s->GetLength() * 2;
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001732 result = (const jchar*) GuardedCopy::Create(result, byteCount, false);
Elliott Hughesa2501992011-08-26 19:39:54 -07001733 if (isCopy != NULL) {
1734 *isCopy = JNI_TRUE;
1735 }
1736 }
1737 return CHECK_JNI_EXIT("p", result);
1738 }
1739
1740 static void ReleaseStringCritical(JNIEnv* env, jstring string, const jchar* carray) {
1741 CHECK_JNI_ENTRY(kFlag_CritRelease | kFlag_ExcepOkay, "Esp", env, string, carray);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001742 sc.CheckNonNull(carray);
1743 if (sc.ForceCopy()) {
1744 GuardedCopy::Check(__FUNCTION__, carray, false);
Elliott Hughesba8eee12012-01-24 20:25:24 -08001745 carray = reinterpret_cast<const jchar*>(GuardedCopy::Destroy(const_cast<jchar*>(carray)));
Elliott Hughesa2501992011-08-26 19:39:54 -07001746 }
1747 baseEnv(env)->ReleaseStringCritical(env, string, carray);
1748 CHECK_JNI_EXIT_VOID();
1749 }
1750
1751 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
1752 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1753 return CHECK_JNI_EXIT("L", baseEnv(env)->NewWeakGlobalRef(env, obj));
1754 }
1755
1756 static jboolean ExceptionCheck(JNIEnv* env) {
1757 CHECK_JNI_ENTRY(kFlag_CritOkay | kFlag_ExcepOkay, "E", env);
1758 return CHECK_JNI_EXIT("b", baseEnv(env)->ExceptionCheck(env));
1759 }
1760
1761 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject obj) {
1762 // Note: we use "Ep" rather than "EL" because this is the one JNI function
1763 // that it's okay to pass an invalid reference to.
1764 CHECK_JNI_ENTRY(kFlag_Default, "Ep", env, obj);
1765 // TODO: proper decoding of jobjectRefType!
1766 return CHECK_JNI_EXIT("I", baseEnv(env)->GetObjectRefType(env, obj));
1767 }
1768
1769 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
1770 CHECK_JNI_ENTRY(kFlag_Default, "EpJ", env, address, capacity);
1771 if (address == NULL) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001772 JniAbortF(__FUNCTION__, "non-nullable address is NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -07001773 }
1774 if (capacity <= 0) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001775 JniAbortF(__FUNCTION__, "capacity must be greater than 0: %d", capacity);
Elliott Hughesa2501992011-08-26 19:39:54 -07001776 }
1777 return CHECK_JNI_EXIT("L", baseEnv(env)->NewDirectByteBuffer(env, address, capacity));
1778 }
1779
1780 static void* GetDirectBufferAddress(JNIEnv* env, jobject buf) {
1781 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, buf);
1782 // TODO: check that 'buf' is a java.nio.Buffer.
1783 return CHECK_JNI_EXIT("p", baseEnv(env)->GetDirectBufferAddress(env, buf));
1784 }
1785
1786 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject buf) {
1787 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, buf);
1788 // TODO: check that 'buf' is a java.nio.Buffer.
1789 return CHECK_JNI_EXIT("J", baseEnv(env)->GetDirectBufferCapacity(env, buf));
1790 }
1791
1792 private:
1793 static inline const JNINativeInterface* baseEnv(JNIEnv* env) {
1794 return reinterpret_cast<JNIEnvExt*>(env)->unchecked_functions;
1795 }
1796};
1797
1798const JNINativeInterface gCheckNativeInterface = {
1799 NULL, // reserved0.
1800 NULL, // reserved1.
1801 NULL, // reserved2.
1802 NULL, // reserved3.
1803 CheckJNI::GetVersion,
1804 CheckJNI::DefineClass,
1805 CheckJNI::FindClass,
1806 CheckJNI::FromReflectedMethod,
1807 CheckJNI::FromReflectedField,
1808 CheckJNI::ToReflectedMethod,
1809 CheckJNI::GetSuperclass,
1810 CheckJNI::IsAssignableFrom,
1811 CheckJNI::ToReflectedField,
1812 CheckJNI::Throw,
1813 CheckJNI::ThrowNew,
1814 CheckJNI::ExceptionOccurred,
1815 CheckJNI::ExceptionDescribe,
1816 CheckJNI::ExceptionClear,
1817 CheckJNI::FatalError,
1818 CheckJNI::PushLocalFrame,
1819 CheckJNI::PopLocalFrame,
1820 CheckJNI::NewGlobalRef,
1821 CheckJNI::DeleteGlobalRef,
1822 CheckJNI::DeleteLocalRef,
1823 CheckJNI::IsSameObject,
1824 CheckJNI::NewLocalRef,
1825 CheckJNI::EnsureLocalCapacity,
1826 CheckJNI::AllocObject,
1827 CheckJNI::NewObject,
1828 CheckJNI::NewObjectV,
1829 CheckJNI::NewObjectA,
1830 CheckJNI::GetObjectClass,
1831 CheckJNI::IsInstanceOf,
1832 CheckJNI::GetMethodID,
1833 CheckJNI::CallObjectMethod,
1834 CheckJNI::CallObjectMethodV,
1835 CheckJNI::CallObjectMethodA,
1836 CheckJNI::CallBooleanMethod,
1837 CheckJNI::CallBooleanMethodV,
1838 CheckJNI::CallBooleanMethodA,
1839 CheckJNI::CallByteMethod,
1840 CheckJNI::CallByteMethodV,
1841 CheckJNI::CallByteMethodA,
1842 CheckJNI::CallCharMethod,
1843 CheckJNI::CallCharMethodV,
1844 CheckJNI::CallCharMethodA,
1845 CheckJNI::CallShortMethod,
1846 CheckJNI::CallShortMethodV,
1847 CheckJNI::CallShortMethodA,
1848 CheckJNI::CallIntMethod,
1849 CheckJNI::CallIntMethodV,
1850 CheckJNI::CallIntMethodA,
1851 CheckJNI::CallLongMethod,
1852 CheckJNI::CallLongMethodV,
1853 CheckJNI::CallLongMethodA,
1854 CheckJNI::CallFloatMethod,
1855 CheckJNI::CallFloatMethodV,
1856 CheckJNI::CallFloatMethodA,
1857 CheckJNI::CallDoubleMethod,
1858 CheckJNI::CallDoubleMethodV,
1859 CheckJNI::CallDoubleMethodA,
1860 CheckJNI::CallVoidMethod,
1861 CheckJNI::CallVoidMethodV,
1862 CheckJNI::CallVoidMethodA,
1863 CheckJNI::CallNonvirtualObjectMethod,
1864 CheckJNI::CallNonvirtualObjectMethodV,
1865 CheckJNI::CallNonvirtualObjectMethodA,
1866 CheckJNI::CallNonvirtualBooleanMethod,
1867 CheckJNI::CallNonvirtualBooleanMethodV,
1868 CheckJNI::CallNonvirtualBooleanMethodA,
1869 CheckJNI::CallNonvirtualByteMethod,
1870 CheckJNI::CallNonvirtualByteMethodV,
1871 CheckJNI::CallNonvirtualByteMethodA,
1872 CheckJNI::CallNonvirtualCharMethod,
1873 CheckJNI::CallNonvirtualCharMethodV,
1874 CheckJNI::CallNonvirtualCharMethodA,
1875 CheckJNI::CallNonvirtualShortMethod,
1876 CheckJNI::CallNonvirtualShortMethodV,
1877 CheckJNI::CallNonvirtualShortMethodA,
1878 CheckJNI::CallNonvirtualIntMethod,
1879 CheckJNI::CallNonvirtualIntMethodV,
1880 CheckJNI::CallNonvirtualIntMethodA,
1881 CheckJNI::CallNonvirtualLongMethod,
1882 CheckJNI::CallNonvirtualLongMethodV,
1883 CheckJNI::CallNonvirtualLongMethodA,
1884 CheckJNI::CallNonvirtualFloatMethod,
1885 CheckJNI::CallNonvirtualFloatMethodV,
1886 CheckJNI::CallNonvirtualFloatMethodA,
1887 CheckJNI::CallNonvirtualDoubleMethod,
1888 CheckJNI::CallNonvirtualDoubleMethodV,
1889 CheckJNI::CallNonvirtualDoubleMethodA,
1890 CheckJNI::CallNonvirtualVoidMethod,
1891 CheckJNI::CallNonvirtualVoidMethodV,
1892 CheckJNI::CallNonvirtualVoidMethodA,
1893 CheckJNI::GetFieldID,
1894 CheckJNI::GetObjectField,
1895 CheckJNI::GetBooleanField,
1896 CheckJNI::GetByteField,
1897 CheckJNI::GetCharField,
1898 CheckJNI::GetShortField,
1899 CheckJNI::GetIntField,
1900 CheckJNI::GetLongField,
1901 CheckJNI::GetFloatField,
1902 CheckJNI::GetDoubleField,
1903 CheckJNI::SetObjectField,
1904 CheckJNI::SetBooleanField,
1905 CheckJNI::SetByteField,
1906 CheckJNI::SetCharField,
1907 CheckJNI::SetShortField,
1908 CheckJNI::SetIntField,
1909 CheckJNI::SetLongField,
1910 CheckJNI::SetFloatField,
1911 CheckJNI::SetDoubleField,
1912 CheckJNI::GetStaticMethodID,
1913 CheckJNI::CallStaticObjectMethod,
1914 CheckJNI::CallStaticObjectMethodV,
1915 CheckJNI::CallStaticObjectMethodA,
1916 CheckJNI::CallStaticBooleanMethod,
1917 CheckJNI::CallStaticBooleanMethodV,
1918 CheckJNI::CallStaticBooleanMethodA,
1919 CheckJNI::CallStaticByteMethod,
1920 CheckJNI::CallStaticByteMethodV,
1921 CheckJNI::CallStaticByteMethodA,
1922 CheckJNI::CallStaticCharMethod,
1923 CheckJNI::CallStaticCharMethodV,
1924 CheckJNI::CallStaticCharMethodA,
1925 CheckJNI::CallStaticShortMethod,
1926 CheckJNI::CallStaticShortMethodV,
1927 CheckJNI::CallStaticShortMethodA,
1928 CheckJNI::CallStaticIntMethod,
1929 CheckJNI::CallStaticIntMethodV,
1930 CheckJNI::CallStaticIntMethodA,
1931 CheckJNI::CallStaticLongMethod,
1932 CheckJNI::CallStaticLongMethodV,
1933 CheckJNI::CallStaticLongMethodA,
1934 CheckJNI::CallStaticFloatMethod,
1935 CheckJNI::CallStaticFloatMethodV,
1936 CheckJNI::CallStaticFloatMethodA,
1937 CheckJNI::CallStaticDoubleMethod,
1938 CheckJNI::CallStaticDoubleMethodV,
1939 CheckJNI::CallStaticDoubleMethodA,
1940 CheckJNI::CallStaticVoidMethod,
1941 CheckJNI::CallStaticVoidMethodV,
1942 CheckJNI::CallStaticVoidMethodA,
1943 CheckJNI::GetStaticFieldID,
1944 CheckJNI::GetStaticObjectField,
1945 CheckJNI::GetStaticBooleanField,
1946 CheckJNI::GetStaticByteField,
1947 CheckJNI::GetStaticCharField,
1948 CheckJNI::GetStaticShortField,
1949 CheckJNI::GetStaticIntField,
1950 CheckJNI::GetStaticLongField,
1951 CheckJNI::GetStaticFloatField,
1952 CheckJNI::GetStaticDoubleField,
1953 CheckJNI::SetStaticObjectField,
1954 CheckJNI::SetStaticBooleanField,
1955 CheckJNI::SetStaticByteField,
1956 CheckJNI::SetStaticCharField,
1957 CheckJNI::SetStaticShortField,
1958 CheckJNI::SetStaticIntField,
1959 CheckJNI::SetStaticLongField,
1960 CheckJNI::SetStaticFloatField,
1961 CheckJNI::SetStaticDoubleField,
1962 CheckJNI::NewString,
1963 CheckJNI::GetStringLength,
1964 CheckJNI::GetStringChars,
1965 CheckJNI::ReleaseStringChars,
1966 CheckJNI::NewStringUTF,
1967 CheckJNI::GetStringUTFLength,
1968 CheckJNI::GetStringUTFChars,
1969 CheckJNI::ReleaseStringUTFChars,
1970 CheckJNI::GetArrayLength,
1971 CheckJNI::NewObjectArray,
1972 CheckJNI::GetObjectArrayElement,
1973 CheckJNI::SetObjectArrayElement,
1974 CheckJNI::NewBooleanArray,
1975 CheckJNI::NewByteArray,
1976 CheckJNI::NewCharArray,
1977 CheckJNI::NewShortArray,
1978 CheckJNI::NewIntArray,
1979 CheckJNI::NewLongArray,
1980 CheckJNI::NewFloatArray,
1981 CheckJNI::NewDoubleArray,
1982 CheckJNI::GetBooleanArrayElements,
1983 CheckJNI::GetByteArrayElements,
1984 CheckJNI::GetCharArrayElements,
1985 CheckJNI::GetShortArrayElements,
1986 CheckJNI::GetIntArrayElements,
1987 CheckJNI::GetLongArrayElements,
1988 CheckJNI::GetFloatArrayElements,
1989 CheckJNI::GetDoubleArrayElements,
1990 CheckJNI::ReleaseBooleanArrayElements,
1991 CheckJNI::ReleaseByteArrayElements,
1992 CheckJNI::ReleaseCharArrayElements,
1993 CheckJNI::ReleaseShortArrayElements,
1994 CheckJNI::ReleaseIntArrayElements,
1995 CheckJNI::ReleaseLongArrayElements,
1996 CheckJNI::ReleaseFloatArrayElements,
1997 CheckJNI::ReleaseDoubleArrayElements,
1998 CheckJNI::GetBooleanArrayRegion,
1999 CheckJNI::GetByteArrayRegion,
2000 CheckJNI::GetCharArrayRegion,
2001 CheckJNI::GetShortArrayRegion,
2002 CheckJNI::GetIntArrayRegion,
2003 CheckJNI::GetLongArrayRegion,
2004 CheckJNI::GetFloatArrayRegion,
2005 CheckJNI::GetDoubleArrayRegion,
2006 CheckJNI::SetBooleanArrayRegion,
2007 CheckJNI::SetByteArrayRegion,
2008 CheckJNI::SetCharArrayRegion,
2009 CheckJNI::SetShortArrayRegion,
2010 CheckJNI::SetIntArrayRegion,
2011 CheckJNI::SetLongArrayRegion,
2012 CheckJNI::SetFloatArrayRegion,
2013 CheckJNI::SetDoubleArrayRegion,
2014 CheckJNI::RegisterNatives,
2015 CheckJNI::UnregisterNatives,
2016 CheckJNI::MonitorEnter,
2017 CheckJNI::MonitorExit,
2018 CheckJNI::GetJavaVM,
2019 CheckJNI::GetStringRegion,
2020 CheckJNI::GetStringUTFRegion,
2021 CheckJNI::GetPrimitiveArrayCritical,
2022 CheckJNI::ReleasePrimitiveArrayCritical,
2023 CheckJNI::GetStringCritical,
2024 CheckJNI::ReleaseStringCritical,
2025 CheckJNI::NewWeakGlobalRef,
2026 CheckJNI::DeleteWeakGlobalRef,
2027 CheckJNI::ExceptionCheck,
2028 CheckJNI::NewDirectByteBuffer,
2029 CheckJNI::GetDirectBufferAddress,
2030 CheckJNI::GetDirectBufferCapacity,
2031 CheckJNI::GetObjectRefType,
2032};
2033
2034const JNINativeInterface* GetCheckJniNativeInterface() {
2035 return &gCheckNativeInterface;
2036}
2037
2038class CheckJII {
Elliott Hughesba8eee12012-01-24 20:25:24 -08002039 public:
Elliott Hughesa2501992011-08-26 19:39:54 -07002040 static jint DestroyJavaVM(JavaVM* vm) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002041 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002042 sc.Check(true, "v", vm);
2043 return CHECK_JNI_EXIT("I", BaseVm(vm)->DestroyJavaVM(vm));
Elliott Hughesa2501992011-08-26 19:39:54 -07002044 }
2045
2046 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002047 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002048 sc.Check(true, "vpp", vm, p_env, thr_args);
2049 return CHECK_JNI_EXIT("I", BaseVm(vm)->AttachCurrentThread(vm, p_env, thr_args));
Elliott Hughesa2501992011-08-26 19:39:54 -07002050 }
2051
2052 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002053 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002054 sc.Check(true, "vpp", vm, p_env, thr_args);
2055 return CHECK_JNI_EXIT("I", BaseVm(vm)->AttachCurrentThreadAsDaemon(vm, p_env, thr_args));
Elliott Hughesa2501992011-08-26 19:39:54 -07002056 }
2057
2058 static jint DetachCurrentThread(JavaVM* vm) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002059 ScopedCheck sc(vm, true, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002060 sc.Check(true, "v", vm);
2061 return CHECK_JNI_EXIT("I", BaseVm(vm)->DetachCurrentThread(vm));
Elliott Hughesa2501992011-08-26 19:39:54 -07002062 }
2063
2064 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002065 ScopedCheck sc(vm, true, __FUNCTION__);
Elliott Hughes83a25322013-03-14 11:18:53 -07002066 sc.Check(true, "vpI", vm);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002067 return CHECK_JNI_EXIT("I", BaseVm(vm)->GetEnv(vm, env, version));
Elliott Hughesa2501992011-08-26 19:39:54 -07002068 }
2069
2070 private:
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002071 static inline const JNIInvokeInterface* BaseVm(JavaVM* vm) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002072 return reinterpret_cast<JavaVMExt*>(vm)->unchecked_functions;
2073 }
2074};
2075
2076const JNIInvokeInterface gCheckInvokeInterface = {
2077 NULL, // reserved0
2078 NULL, // reserved1
2079 NULL, // reserved2
2080 CheckJII::DestroyJavaVM,
2081 CheckJII::AttachCurrentThread,
2082 CheckJII::DetachCurrentThread,
2083 CheckJII::GetEnv,
2084 CheckJII::AttachCurrentThreadAsDaemon
2085};
2086
2087const JNIInvokeInterface* GetCheckJniInvokeInterface() {
2088 return &gCheckInvokeInterface;
2089}
2090
2091} // namespace art