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