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