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