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