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 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 17 | #include "check_jni.h" |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 18 | |
Andreas Gampe | ef4afe9 | 2015-07-27 21:03:25 -0700 | [diff] [blame] | 19 | #include <iomanip> |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 20 | #include <sys/mman.h> |
| 21 | #include <zlib.h> |
| 22 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 23 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 24 | #include "art_method-inl.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 25 | #include "base/logging.h" |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 26 | #include "base/to_str.h" |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 27 | #include "class_linker.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | #include "class_linker-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 29 | #include "dex_file-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 30 | #include "gc/space/space.h" |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 31 | #include "java_vm_ext.h" |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 32 | #include "jni_internal.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 33 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 34 | #include "mirror/object-inl.h" |
| 35 | #include "mirror/object_array-inl.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 36 | #include "mirror/string-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 37 | #include "mirror/throwable.h" |
Jeff Hao | 58df327 | 2013-04-22 15:28:53 -0700 | [diff] [blame] | 38 | #include "runtime.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 39 | #include "scoped_thread_state_change-inl.h" |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 40 | #include "thread.h" |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 41 | #include "well_known_classes.h" |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 42 | |
| 43 | namespace art { |
| 44 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 45 | /* |
| 46 | * =========================================================================== |
| 47 | * JNI function helpers |
| 48 | * =========================================================================== |
| 49 | */ |
| 50 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 51 | // Flags passed into ScopedCheck. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 52 | #define kFlag_Default 0x0000 |
| 53 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 54 | #define kFlag_CritBad 0x0000 // Calling while in critical is not allowed. |
| 55 | #define kFlag_CritOkay 0x0001 // Calling while in critical is allowed. |
| 56 | #define kFlag_CritGet 0x0002 // This is a critical "get". |
| 57 | #define kFlag_CritRelease 0x0003 // This is a critical "release". |
| 58 | #define kFlag_CritMask 0x0003 // Bit mask to get "crit" value. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 59 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 60 | #define kFlag_ExcepBad 0x0000 // Raised exceptions are not allowed. |
| 61 | #define kFlag_ExcepOkay 0x0004 // Raised exceptions are allowed. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 62 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 63 | #define kFlag_Release 0x0010 // Are we in a non-critical release function? |
| 64 | #define kFlag_NullableUtf 0x0020 // Are our UTF parameters nullable? |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 65 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 66 | #define kFlag_Invocation 0x8000 // Part of the invocation interface (JavaVM*). |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 67 | |
Elliott Hughes | 485cac4 | 2011-12-09 17:49:35 -0800 | [diff] [blame] | 68 | #define kFlag_ForceTrace 0x80000000 // Add this to a JNI function's flags if you want to trace every call. |
Alex Light | 48ffe06 | 2015-08-19 15:23:23 -0700 | [diff] [blame] | 69 | |
| 70 | class VarArgs; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 71 | /* |
| 72 | * Java primitive types: |
| 73 | * B - jbyte |
| 74 | * C - jchar |
| 75 | * D - jdouble |
| 76 | * F - jfloat |
| 77 | * I - jint |
| 78 | * J - jlong |
| 79 | * S - jshort |
| 80 | * Z - jboolean (shown as true and false) |
| 81 | * V - void |
| 82 | * |
| 83 | * Java reference types: |
| 84 | * L - jobject |
| 85 | * a - jarray |
| 86 | * c - jclass |
| 87 | * s - jstring |
| 88 | * t - jthrowable |
| 89 | * |
| 90 | * JNI types: |
| 91 | * b - jboolean (shown as JNI_TRUE and JNI_FALSE) |
| 92 | * f - jfieldID |
| 93 | * i - JNI error value (JNI_OK, JNI_ERR, JNI_EDETACHED, JNI_EVERSION) |
| 94 | * m - jmethodID |
| 95 | * p - void* |
| 96 | * r - jint (for release mode arguments) |
| 97 | * u - const char* (Modified UTF-8) |
| 98 | * z - jsize (for lengths; use i if negative values are okay) |
| 99 | * v - JavaVM* |
| 100 | * w - jobjectRefType |
| 101 | * E - JNIEnv* |
| 102 | * . - no argument; just print "..." (used for varargs JNI calls) |
| 103 | * |
| 104 | */ |
| 105 | union JniValueType { |
| 106 | jarray a; |
| 107 | jboolean b; |
| 108 | jclass c; |
| 109 | jfieldID f; |
| 110 | jint i; |
| 111 | jmethodID m; |
| 112 | const void* p; // Pointer. |
| 113 | jint r; // Release mode. |
| 114 | jstring s; |
| 115 | jthrowable t; |
| 116 | const char* u; // Modified UTF-8. |
| 117 | JavaVM* v; |
| 118 | jobjectRefType w; |
| 119 | jsize z; |
| 120 | jbyte B; |
| 121 | jchar C; |
| 122 | jdouble D; |
| 123 | JNIEnv* E; |
| 124 | jfloat F; |
| 125 | jint I; |
| 126 | jlong J; |
| 127 | jobject L; |
| 128 | jshort S; |
| 129 | const void* V; // void |
| 130 | jboolean Z; |
Alex Light | 48ffe06 | 2015-08-19 15:23:23 -0700 | [diff] [blame] | 131 | const VarArgs* va; |
| 132 | }; |
| 133 | |
| 134 | /* |
| 135 | * A structure containing all the information needed to validate varargs arguments. |
| 136 | * |
| 137 | * Note that actually getting the arguments from this structure mutates it so should only be done on |
| 138 | * owned copies. |
| 139 | */ |
| 140 | class VarArgs { |
| 141 | public: |
| 142 | VarArgs(jmethodID m, va_list var) : m_(m), type_(kTypeVaList), cnt_(0) { |
| 143 | va_copy(vargs_, var); |
| 144 | } |
| 145 | |
| 146 | VarArgs(jmethodID m, const jvalue* vals) : m_(m), type_(kTypePtr), cnt_(0), ptr_(vals) {} |
| 147 | |
| 148 | ~VarArgs() { |
| 149 | if (type_ == kTypeVaList) { |
| 150 | va_end(vargs_); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | VarArgs(VarArgs&& other) { |
| 155 | m_ = other.m_; |
| 156 | cnt_ = other.cnt_; |
| 157 | type_ = other.type_; |
| 158 | if (other.type_ == kTypeVaList) { |
| 159 | va_copy(vargs_, other.vargs_); |
| 160 | } else { |
| 161 | ptr_ = other.ptr_; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // This method is const because we need to ensure that one only uses the GetValue method on an |
| 166 | // owned copy of the VarArgs. This is because getting the next argument from a va_list is a |
| 167 | // mutating operation. Therefore we pass around these VarArgs with the 'const' qualifier and when |
| 168 | // we want to use one we need to Clone() it. |
| 169 | VarArgs Clone() const { |
| 170 | if (type_ == kTypeVaList) { |
| 171 | // const_cast needed to make sure the compiler is okay with va_copy, which (being a macro) is |
| 172 | // messed up if the source argument is not the exact type 'va_list'. |
| 173 | return VarArgs(m_, cnt_, const_cast<VarArgs*>(this)->vargs_); |
| 174 | } else { |
| 175 | return VarArgs(m_, cnt_, ptr_); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | jmethodID GetMethodID() const { |
| 180 | return m_; |
| 181 | } |
| 182 | |
| 183 | JniValueType GetValue(char fmt) { |
| 184 | JniValueType o; |
| 185 | if (type_ == kTypeVaList) { |
| 186 | switch (fmt) { |
| 187 | case 'Z': o.Z = static_cast<jboolean>(va_arg(vargs_, jint)); break; |
| 188 | case 'B': o.B = static_cast<jbyte>(va_arg(vargs_, jint)); break; |
| 189 | case 'C': o.C = static_cast<jchar>(va_arg(vargs_, jint)); break; |
| 190 | case 'S': o.S = static_cast<jshort>(va_arg(vargs_, jint)); break; |
| 191 | case 'I': o.I = va_arg(vargs_, jint); break; |
| 192 | case 'J': o.J = va_arg(vargs_, jlong); break; |
| 193 | case 'F': o.F = static_cast<jfloat>(va_arg(vargs_, jdouble)); break; |
| 194 | case 'D': o.D = va_arg(vargs_, jdouble); break; |
| 195 | case 'L': o.L = va_arg(vargs_, jobject); break; |
| 196 | default: |
| 197 | LOG(FATAL) << "Illegal type format char " << fmt; |
| 198 | UNREACHABLE(); |
| 199 | } |
| 200 | } else { |
| 201 | CHECK(type_ == kTypePtr); |
| 202 | jvalue v = ptr_[cnt_]; |
| 203 | cnt_++; |
| 204 | switch (fmt) { |
| 205 | case 'Z': o.Z = v.z; break; |
| 206 | case 'B': o.B = v.b; break; |
| 207 | case 'C': o.C = v.c; break; |
| 208 | case 'S': o.S = v.s; break; |
| 209 | case 'I': o.I = v.i; break; |
| 210 | case 'J': o.J = v.j; break; |
| 211 | case 'F': o.F = v.f; break; |
| 212 | case 'D': o.D = v.d; break; |
| 213 | case 'L': o.L = v.l; break; |
| 214 | default: |
| 215 | LOG(FATAL) << "Illegal type format char " << fmt; |
| 216 | UNREACHABLE(); |
| 217 | } |
| 218 | } |
| 219 | return o; |
| 220 | } |
| 221 | |
| 222 | private: |
| 223 | VarArgs(jmethodID m, uint32_t cnt, va_list var) : m_(m), type_(kTypeVaList), cnt_(cnt) { |
| 224 | va_copy(vargs_, var); |
| 225 | } |
| 226 | |
| 227 | VarArgs(jmethodID m, uint32_t cnt, const jvalue* vals) : m_(m), type_(kTypePtr), cnt_(cnt), ptr_(vals) {} |
| 228 | |
| 229 | enum VarArgsType { |
| 230 | kTypeVaList, |
| 231 | kTypePtr, |
| 232 | }; |
| 233 | |
| 234 | jmethodID m_; |
| 235 | VarArgsType type_; |
| 236 | uint32_t cnt_; |
| 237 | union { |
| 238 | va_list vargs_; |
| 239 | const jvalue* ptr_; |
| 240 | }; |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 241 | }; |
| 242 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 243 | class ScopedCheck { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 244 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 245 | ScopedCheck(int flags, const char* functionName, bool has_method = true) |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 246 | : function_name_(functionName), flags_(flags), indent_(0), has_method_(has_method) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 249 | ~ScopedCheck() {} |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 250 | |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 251 | // Checks that 'class_name' is a valid "fully-qualified" JNI class name, like "java/lang/Thread" |
| 252 | // or "[Ljava/lang/Object;". A ClassLoader can actually normalize class names a couple of |
| 253 | // times, so using "java.lang.Thread" instead of "java/lang/Thread" might work in some |
| 254 | // circumstances, but this is incorrect. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 255 | bool CheckClassName(const char* class_name) { |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 256 | if ((class_name == nullptr) || !IsValidJniClassName(class_name)) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 257 | AbortF("illegal class name '%s'\n" |
| 258 | " (should be of the form 'package/Class', [Lpackage/Class;' or '[[B')", |
| 259 | class_name); |
| 260 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 261 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 262 | return true; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /* |
| 266 | * Verify that this instance field ID is valid for this object. |
| 267 | * |
| 268 | * Assumes "jobj" has already been validated. |
| 269 | */ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 270 | bool CheckInstanceFieldID(ScopedObjectAccess& soa, jobject java_object, jfieldID fid) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 271 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 272 | ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(java_object); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 273 | if (o == nullptr) { |
| 274 | AbortF("field operation on NULL object: %p", java_object); |
| 275 | return false; |
| 276 | } |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 277 | if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(o.Ptr())) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 278 | Runtime::Current()->GetHeap()->DumpSpaces(LOG_STREAM(ERROR)); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 279 | AbortF("field operation on invalid %s: %p", |
Andreas Gampe | dc061d0 | 2016-10-24 13:19:37 -0700 | [diff] [blame] | 280 | GetIndirectRefKindString(IndirectReferenceTable::GetIndirectRefKind(java_object)), |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 281 | java_object); |
| 282 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 285 | ArtField* f = CheckFieldID(soa, fid); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 286 | if (f == nullptr) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 287 | return false; |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 288 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 289 | mirror::Class* c = o->GetClass(); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 290 | if (c->FindInstanceField(f->GetName(), f->GetTypeDescriptor()) == nullptr) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 291 | AbortF("jfieldID %s not valid for an object of class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 292 | f->PrettyField().c_str(), o->PrettyTypeOf().c_str()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 293 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 294 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 295 | return true; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /* |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 299 | * Verify that the pointer value is non-null. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 300 | */ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 301 | bool CheckNonNull(const void* ptr) { |
| 302 | if (UNLIKELY(ptr == nullptr)) { |
| 303 | AbortF("non-nullable argument was NULL"); |
| 304 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 305 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 306 | return true; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /* |
| 310 | * Verify that the method's return type matches the type of call. |
| 311 | * 'expectedType' will be "L" for all objects, including arrays. |
| 312 | */ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 313 | bool CheckMethodAndSig(ScopedObjectAccess& soa, jobject jobj, jclass jc, |
| 314 | jmethodID mid, Primitive::Type type, InvokeType invoke) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 315 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame^] | 316 | ArtMethod* m = CheckMethodID(mid); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 317 | if (m == nullptr) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 318 | return false; |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 319 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 320 | if (type != Primitive::GetType(m->GetShorty()[0])) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 321 | AbortF("the return type of %s does not match %s", function_name_, m->PrettyMethod().c_str()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 322 | return false; |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 323 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 324 | bool is_static = (invoke == kStatic); |
| 325 | if (is_static != m->IsStatic()) { |
| 326 | if (is_static) { |
| 327 | AbortF("calling non-static method %s with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 328 | m->PrettyMethod().c_str(), function_name_); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 329 | } else { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 330 | AbortF("calling static method %s with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 331 | m->PrettyMethod().c_str(), function_name_); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 332 | } |
| 333 | return false; |
| 334 | } |
| 335 | if (invoke != kVirtual) { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 336 | ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(jc); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 337 | if (!m->GetDeclaringClass()->IsAssignableFrom(c)) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 338 | AbortF("can't call %s %s with class %s", invoke == kStatic ? "static" : "nonvirtual", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 339 | m->PrettyMethod().c_str(), mirror::Class::PrettyClass(c).c_str()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 340 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 341 | } |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 342 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 343 | if (invoke != kStatic) { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 344 | ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(jobj); |
Mathieu Chartier | a1e78fa | 2014-08-20 12:09:38 -0700 | [diff] [blame] | 345 | if (o == nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 346 | AbortF("can't call %s on null object", m->PrettyMethod().c_str()); |
Mathieu Chartier | a1e78fa | 2014-08-20 12:09:38 -0700 | [diff] [blame] | 347 | return false; |
| 348 | } else if (!o->InstanceOf(m->GetDeclaringClass())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 349 | AbortF("can't call %s on instance of %s", m->PrettyMethod().c_str(), |
| 350 | o->PrettyTypeOf().c_str()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 351 | return false; |
| 352 | } |
| 353 | } |
| 354 | return true; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /* |
| 358 | * Verify that this static field ID is valid for this class. |
| 359 | * |
| 360 | * Assumes "java_class" has already been validated. |
| 361 | */ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 362 | bool CheckStaticFieldID(ScopedObjectAccess& soa, jclass java_class, jfieldID fid) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 363 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 364 | ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(java_class); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 365 | ArtField* f = CheckFieldID(soa, fid); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 366 | if (f == nullptr) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 367 | return false; |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 368 | } |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 369 | if (c != f->GetDeclaringClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 370 | AbortF("static jfieldID %p not valid for class %s", fid, |
| 371 | mirror::Class::PrettyClass(c).c_str()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 372 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 373 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 374 | return true; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | /* |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 378 | * Verify that "mid" is appropriate for "java_class". |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 379 | * |
| 380 | * A mismatch isn't dangerous, because the jmethodID defines the class. In |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 381 | * 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] | 382 | * allow bad code in the system though. |
| 383 | * |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 384 | * 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] | 385 | */ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 386 | bool CheckStaticMethod(ScopedObjectAccess& soa, jclass java_class, jmethodID mid) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 387 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame^] | 388 | ArtMethod* m = CheckMethodID(mid); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 389 | if (m == nullptr) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 390 | return false; |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 391 | } |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 392 | ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(java_class); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 393 | if (!m->GetDeclaringClass()->IsAssignableFrom(c)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 394 | AbortF("can't call static %s on class %s", m->PrettyMethod().c_str(), |
| 395 | mirror::Class::PrettyClass(c).c_str()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 396 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 397 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 398 | return true; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /* |
| 402 | * Verify that "mid" is appropriate for "jobj". |
| 403 | * |
| 404 | * Make sure the object is an instance of the method's declaring class. |
| 405 | * (Note the mid might point to a declaration in an interface; this |
| 406 | * will be handled automatically by the instanceof check.) |
| 407 | */ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 408 | bool CheckVirtualMethod(ScopedObjectAccess& soa, jobject java_object, jmethodID mid) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 409 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame^] | 410 | ArtMethod* m = CheckMethodID(mid); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 411 | if (m == nullptr) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 412 | return false; |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 413 | } |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 414 | ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(java_object); |
Mathieu Chartier | a1e78fa | 2014-08-20 12:09:38 -0700 | [diff] [blame] | 415 | if (o == nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 416 | AbortF("can't call %s on null object", m->PrettyMethod().c_str()); |
Mathieu Chartier | a1e78fa | 2014-08-20 12:09:38 -0700 | [diff] [blame] | 417 | return false; |
| 418 | } else if (!o->InstanceOf(m->GetDeclaringClass())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 419 | AbortF("can't call %s on instance of %s", m->PrettyMethod().c_str(), |
| 420 | o->PrettyTypeOf().c_str()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 421 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 422 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 423 | return true; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /** |
| 427 | * The format string is a sequence of the following characters, |
| 428 | * and must be followed by arguments of the corresponding types |
| 429 | * in the same order. |
| 430 | * |
| 431 | * Java primitive types: |
| 432 | * B - jbyte |
| 433 | * C - jchar |
| 434 | * D - jdouble |
| 435 | * F - jfloat |
| 436 | * I - jint |
| 437 | * J - jlong |
| 438 | * S - jshort |
| 439 | * Z - jboolean (shown as true and false) |
| 440 | * V - void |
| 441 | * |
| 442 | * Java reference types: |
| 443 | * L - jobject |
| 444 | * a - jarray |
| 445 | * c - jclass |
| 446 | * s - jstring |
| 447 | * |
| 448 | * JNI types: |
| 449 | * b - jboolean (shown as JNI_TRUE and JNI_FALSE) |
| 450 | * f - jfieldID |
| 451 | * m - jmethodID |
| 452 | * p - void* |
| 453 | * r - jint (for release mode arguments) |
Elliott Hughes | 78090d1 | 2011-10-07 14:31:47 -0700 | [diff] [blame] | 454 | * u - const char* (Modified UTF-8) |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 455 | * z - jsize (for lengths; use i if negative values are okay) |
| 456 | * v - JavaVM* |
| 457 | * E - JNIEnv* |
Alex Light | 48ffe06 | 2015-08-19 15:23:23 -0700 | [diff] [blame] | 458 | * . - VarArgs* for Jni calls with variable length arguments |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 459 | * |
| 460 | * Use the kFlag_NullableUtf flag where 'u' field(s) are nullable. |
| 461 | */ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 462 | bool Check(ScopedObjectAccess& soa, bool entry, const char* fmt, JniValueType* args) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 463 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 464 | ArtMethod* traceMethod = nullptr; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 465 | if (has_method_ && soa.Vm()->IsTracingEnabled()) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 466 | // We need to guard some of the invocation interface's calls: a bad caller might |
| 467 | // use DetachCurrentThread or GetEnv on a thread that's not yet attached. |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 468 | Thread* self = Thread::Current(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 469 | if ((flags_ & kFlag_Invocation) == 0 || self != nullptr) { |
| 470 | traceMethod = self->GetCurrentMethod(nullptr); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 471 | } |
| 472 | } |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 473 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 474 | if (((flags_ & kFlag_ForceTrace) != 0) || |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 475 | (traceMethod != nullptr && soa.Vm()->ShouldTrace(traceMethod))) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 476 | std::string msg; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 477 | for (size_t i = 0; fmt[i] != '\0'; ++i) { |
| 478 | TracePossibleHeapValue(soa, entry, fmt[i], args[i], &msg); |
| 479 | if (fmt[i + 1] != '\0') { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 480 | StringAppendF(&msg, ", "); |
| 481 | } |
| 482 | } |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 483 | |
Elliott Hughes | 485cac4 | 2011-12-09 17:49:35 -0800 | [diff] [blame] | 484 | if ((flags_ & kFlag_ForceTrace) != 0) { |
| 485 | LOG(INFO) << "JNI: call to " << function_name_ << "(" << msg << ")"; |
| 486 | } else if (entry) { |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 487 | if (has_method_) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 488 | std::string methodName(ArtMethod::PrettyMethod(traceMethod, false)); |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 489 | LOG(INFO) << "JNI: " << methodName << " -> " << function_name_ << "(" << msg << ")"; |
| 490 | indent_ = methodName.size() + 1; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 491 | } else { |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 492 | LOG(INFO) << "JNI: -> " << function_name_ << "(" << msg << ")"; |
| 493 | indent_ = 0; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 494 | } |
| 495 | } else { |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 496 | 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] | 497 | } |
| 498 | } |
| 499 | |
| 500 | // We always do the thorough checks on entry, and never on exit... |
| 501 | if (entry) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 502 | for (size_t i = 0; fmt[i] != '\0'; ++i) { |
| 503 | if (!CheckPossibleHeapValue(soa, fmt[i], args[i])) { |
| 504 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 505 | } |
| 506 | } |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 507 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 508 | return true; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 511 | bool CheckNonHeap(JavaVMExt* vm, bool entry, const char* fmt, JniValueType* args) { |
| 512 | bool should_trace = (flags_ & kFlag_ForceTrace) != 0; |
Andreas Gampe | def194e | 2015-02-19 15:19:50 -0800 | [diff] [blame] | 513 | if (!should_trace && vm != nullptr && vm->IsTracingEnabled()) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 514 | // We need to guard some of the invocation interface's calls: a bad caller might |
| 515 | // use DetachCurrentThread or GetEnv on a thread that's not yet attached. |
| 516 | Thread* self = Thread::Current(); |
| 517 | if ((flags_ & kFlag_Invocation) == 0 || self != nullptr) { |
| 518 | ScopedObjectAccess soa(self); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 519 | ArtMethod* traceMethod = self->GetCurrentMethod(nullptr); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 520 | should_trace = (traceMethod != nullptr && vm->ShouldTrace(traceMethod)); |
| 521 | } |
| 522 | } |
| 523 | if (should_trace) { |
| 524 | std::string msg; |
| 525 | for (size_t i = 0; fmt[i] != '\0'; ++i) { |
| 526 | TraceNonHeapValue(fmt[i], args[i], &msg); |
| 527 | if (fmt[i + 1] != '\0') { |
| 528 | StringAppendF(&msg, ", "); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | if ((flags_ & kFlag_ForceTrace) != 0) { |
| 533 | LOG(INFO) << "JNI: call to " << function_name_ << "(" << msg << ")"; |
| 534 | } else if (entry) { |
| 535 | if (has_method_) { |
| 536 | Thread* self = Thread::Current(); |
| 537 | ScopedObjectAccess soa(self); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 538 | ArtMethod* traceMethod = self->GetCurrentMethod(nullptr); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 539 | std::string methodName(ArtMethod::PrettyMethod(traceMethod, false)); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 540 | LOG(INFO) << "JNI: " << methodName << " -> " << function_name_ << "(" << msg << ")"; |
| 541 | indent_ = methodName.size() + 1; |
| 542 | } else { |
| 543 | LOG(INFO) << "JNI: -> " << function_name_ << "(" << msg << ")"; |
| 544 | indent_ = 0; |
| 545 | } |
| 546 | } else { |
| 547 | LOG(INFO) << StringPrintf("JNI: %*s<- %s returned %s", indent_, "", function_name_, msg.c_str()); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | // We always do the thorough checks on entry, and never on exit... |
| 552 | if (entry) { |
| 553 | for (size_t i = 0; fmt[i] != '\0'; ++i) { |
| 554 | if (!CheckNonHeapValue(fmt[i], args[i])) { |
| 555 | return false; |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | return true; |
| 560 | } |
| 561 | |
| 562 | bool CheckReflectedMethod(ScopedObjectAccess& soa, jobject jmethod) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 563 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 564 | ObjPtr<mirror::Object> method = soa.Decode<mirror::Object>(jmethod); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 565 | if (method == nullptr) { |
| 566 | AbortF("expected non-null method"); |
| 567 | return false; |
| 568 | } |
| 569 | mirror::Class* c = method->GetClass(); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 570 | if (soa.Decode<mirror::Class>(WellKnownClasses::java_lang_reflect_Method) != c && |
| 571 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_reflect_Constructor) != c) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 572 | AbortF("expected java.lang.reflect.Method or " |
| 573 | "java.lang.reflect.Constructor but got object of type %s: %p", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 574 | method->PrettyTypeOf().c_str(), jmethod); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 575 | return false; |
| 576 | } |
| 577 | return true; |
| 578 | } |
| 579 | |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame^] | 580 | bool CheckConstructor(jmethodID mid) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 581 | ArtMethod* method = jni::DecodeArtMethod(mid); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 582 | if (method == nullptr) { |
| 583 | AbortF("expected non-null constructor"); |
| 584 | return false; |
| 585 | } |
| 586 | if (!method->IsConstructor() || method->IsStatic()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 587 | AbortF("expected a constructor but %s: %p", method->PrettyMethod().c_str(), mid); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 588 | return false; |
| 589 | } |
| 590 | return true; |
| 591 | } |
| 592 | |
| 593 | bool CheckReflectedField(ScopedObjectAccess& soa, jobject jfield) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 594 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 595 | ObjPtr<mirror::Object> field = soa.Decode<mirror::Object>(jfield); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 596 | if (field == nullptr) { |
| 597 | AbortF("expected non-null java.lang.reflect.Field"); |
| 598 | return false; |
| 599 | } |
| 600 | mirror::Class* c = field->GetClass(); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 601 | if (soa.Decode<mirror::Class>(WellKnownClasses::java_lang_reflect_Field) != c) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 602 | AbortF("expected java.lang.reflect.Field but got object of type %s: %p", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 603 | field->PrettyTypeOf().c_str(), jfield); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 604 | return false; |
| 605 | } |
| 606 | return true; |
| 607 | } |
| 608 | |
| 609 | bool CheckThrowable(ScopedObjectAccess& soa, jthrowable jobj) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 610 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 611 | ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(jobj); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 612 | if (!obj->GetClass()->IsThrowableClass()) { |
| 613 | AbortF("expected java.lang.Throwable but got object of type " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 614 | "%s: %p", obj->PrettyTypeOf().c_str(), obj.Ptr()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 615 | return false; |
| 616 | } |
| 617 | return true; |
| 618 | } |
| 619 | |
| 620 | bool CheckThrowableClass(ScopedObjectAccess& soa, jclass jc) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 621 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 622 | ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(jc); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 623 | if (!c->IsThrowableClass()) { |
| 624 | AbortF("expected java.lang.Throwable class but got object of " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 625 | "type %s: %p", c->PrettyDescriptor().c_str(), c.Ptr()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 626 | return false; |
| 627 | } |
| 628 | return true; |
| 629 | } |
| 630 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 631 | bool CheckReferenceKind(IndirectRefKind expected_kind, Thread* self, jobject obj) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 632 | IndirectRefKind found_kind; |
| 633 | if (expected_kind == kLocal) { |
Andreas Gampe | dc061d0 | 2016-10-24 13:19:37 -0700 | [diff] [blame] | 634 | found_kind = IndirectReferenceTable::GetIndirectRefKind(obj); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 635 | if (found_kind == kHandleScopeOrInvalid && self->HandleScopeContains(obj)) { |
| 636 | found_kind = kLocal; |
| 637 | } |
| 638 | } else { |
Andreas Gampe | dc061d0 | 2016-10-24 13:19:37 -0700 | [diff] [blame] | 639 | found_kind = IndirectReferenceTable::GetIndirectRefKind(obj); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 640 | } |
| 641 | if (obj != nullptr && found_kind != expected_kind) { |
| 642 | AbortF("expected reference of kind %s but found %s: %p", |
Andreas Gampe | dc061d0 | 2016-10-24 13:19:37 -0700 | [diff] [blame] | 643 | GetIndirectRefKindString(expected_kind), |
| 644 | GetIndirectRefKindString(IndirectReferenceTable::GetIndirectRefKind(obj)), |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 645 | obj); |
| 646 | return false; |
| 647 | } |
| 648 | return true; |
| 649 | } |
| 650 | |
| 651 | bool CheckInstantiableNonArray(ScopedObjectAccess& soa, jclass jc) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 652 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 653 | ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(jc); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 654 | if (!c->IsInstantiableNonArray()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 655 | AbortF("can't make objects of type %s: %p", c->PrettyDescriptor().c_str(), c.Ptr()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 656 | return false; |
| 657 | } |
| 658 | return true; |
| 659 | } |
| 660 | |
| 661 | bool CheckPrimitiveArrayType(ScopedObjectAccess& soa, jarray array, Primitive::Type type) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 662 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 663 | if (!CheckArray(soa, array)) { |
| 664 | return false; |
| 665 | } |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 666 | ObjPtr<mirror::Array> a = soa.Decode<mirror::Array>(array); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 667 | if (a->GetClass()->GetComponentType()->GetPrimitiveType() != type) { |
| 668 | AbortF("incompatible array type %s expected %s[]: %p", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 669 | a->GetClass()->PrettyDescriptor().c_str(), PrettyDescriptor(type).c_str(), array); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 670 | return false; |
| 671 | } |
| 672 | return true; |
| 673 | } |
| 674 | |
| 675 | bool CheckFieldAccess(ScopedObjectAccess& soa, jobject obj, jfieldID fid, bool is_static, |
| 676 | Primitive::Type type) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 677 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 678 | if (is_static && !CheckStaticFieldID(soa, down_cast<jclass>(obj), fid)) { |
| 679 | return false; |
| 680 | } |
| 681 | if (!is_static && !CheckInstanceFieldID(soa, obj, fid)) { |
| 682 | return false; |
| 683 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 684 | ArtField* field = soa.DecodeField(fid); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 685 | DCHECK(field != nullptr); // Already checked by Check. |
| 686 | if (is_static != field->IsStatic()) { |
| 687 | AbortF("attempt to access %s field %s: %p", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 688 | field->IsStatic() ? "static" : "non-static", field->PrettyField().c_str(), fid); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 689 | return false; |
| 690 | } |
| 691 | if (type != field->GetTypeAsPrimitiveType()) { |
| 692 | AbortF("attempt to access field %s of type %s with the wrong type %s: %p", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 693 | field->PrettyField().c_str(), |
| 694 | PrettyDescriptor(field->GetTypeDescriptor()).c_str(), |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 695 | PrettyDescriptor(type).c_str(), fid); |
| 696 | return false; |
| 697 | } |
| 698 | if (is_static) { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 699 | ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(obj); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 700 | if (o == nullptr || !o->IsClass()) { |
| 701 | AbortF("attempt to access static field %s with a class argument of type %s: %p", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 702 | field->PrettyField().c_str(), o->PrettyTypeOf().c_str(), fid); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 703 | return false; |
| 704 | } |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 705 | ObjPtr<mirror::Class> c = o->AsClass(); |
| 706 | if (c != field->GetDeclaringClass()) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 707 | AbortF("attempt to access static field %s with an incompatible class argument of %s: %p", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 708 | field->PrettyField().c_str(), mirror::Class::PrettyDescriptor(c).c_str(), fid); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 709 | return false; |
| 710 | } |
| 711 | } else { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 712 | ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(obj); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 713 | if (o == nullptr || !field->GetDeclaringClass()->IsAssignableFrom(o->GetClass())) { |
| 714 | AbortF("attempt to access field %s from an object argument of type %s: %p", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 715 | field->PrettyField().c_str(), o->PrettyTypeOf().c_str(), fid); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 716 | return false; |
| 717 | } |
| 718 | } |
| 719 | return true; |
| 720 | } |
| 721 | |
| 722 | private: |
Elliott Hughes | a92853e | 2012-02-07 16:09:27 -0800 | [diff] [blame] | 723 | enum InstanceKind { |
| 724 | kClass, |
Elliott Hughes | 0f3c553 | 2012-03-30 14:51:51 -0700 | [diff] [blame] | 725 | kDirectByteBuffer, |
| 726 | kObject, |
| 727 | kString, |
| 728 | kThrowable, |
Elliott Hughes | a92853e | 2012-02-07 16:09:27 -0800 | [diff] [blame] | 729 | }; |
| 730 | |
| 731 | /* |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 732 | * Verify that "jobj" is a valid non-null object reference, and points to |
Elliott Hughes | a92853e | 2012-02-07 16:09:27 -0800 | [diff] [blame] | 733 | * an instance of expectedClass. |
| 734 | * |
| 735 | * Because we're looking at an object on the GC heap, we have to switch |
| 736 | * to "running" mode before doing the checks. |
| 737 | */ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 738 | bool CheckInstance(ScopedObjectAccess& soa, InstanceKind kind, jobject java_object, bool null_ok) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 739 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 740 | const char* what = nullptr; |
Elliott Hughes | a92853e | 2012-02-07 16:09:27 -0800 | [diff] [blame] | 741 | switch (kind) { |
| 742 | case kClass: |
| 743 | what = "jclass"; |
| 744 | break; |
| 745 | case kDirectByteBuffer: |
| 746 | what = "direct ByteBuffer"; |
| 747 | break; |
| 748 | case kObject: |
| 749 | what = "jobject"; |
| 750 | break; |
| 751 | case kString: |
| 752 | what = "jstring"; |
| 753 | break; |
| 754 | case kThrowable: |
| 755 | what = "jthrowable"; |
| 756 | break; |
| 757 | default: |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 758 | LOG(FATAL) << "Unknown kind " << static_cast<int>(kind); |
Elliott Hughes | a92853e | 2012-02-07 16:09:27 -0800 | [diff] [blame] | 759 | } |
| 760 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 761 | if (java_object == nullptr) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 762 | if (null_ok) { |
| 763 | return true; |
| 764 | } else { |
| 765 | AbortF("%s received NULL %s", function_name_, what); |
| 766 | return false; |
| 767 | } |
Elliott Hughes | a92853e | 2012-02-07 16:09:27 -0800 | [diff] [blame] | 768 | } |
| 769 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 770 | ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(java_object); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 771 | if (obj == nullptr) { |
| 772 | // Either java_object is invalid or is a cleared weak. |
| 773 | IndirectRef ref = reinterpret_cast<IndirectRef>(java_object); |
| 774 | bool okay; |
Andreas Gampe | dc061d0 | 2016-10-24 13:19:37 -0700 | [diff] [blame] | 775 | if (IndirectReferenceTable::GetIndirectRefKind(ref) != kWeakGlobal) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 776 | okay = false; |
| 777 | } else { |
| 778 | obj = soa.Vm()->DecodeWeakGlobal(soa.Self(), ref); |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 779 | okay = Runtime::Current()->IsClearedJniWeakGlobal(obj); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 780 | } |
| 781 | if (!okay) { |
| 782 | AbortF("%s is an invalid %s: %p (%p)", |
Andreas Gampe | dc061d0 | 2016-10-24 13:19:37 -0700 | [diff] [blame] | 783 | what, |
| 784 | GetIndirectRefKindString(IndirectReferenceTable::GetIndirectRefKind(java_object)), |
| 785 | java_object, |
| 786 | obj.Ptr()); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 787 | return false; |
| 788 | } |
| 789 | } |
| 790 | |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 791 | if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(obj.Ptr())) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 792 | Runtime::Current()->GetHeap()->DumpSpaces(LOG_STREAM(ERROR)); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 793 | AbortF("%s is an invalid %s: %p (%p)", |
Andreas Gampe | dc061d0 | 2016-10-24 13:19:37 -0700 | [diff] [blame] | 794 | what, |
| 795 | GetIndirectRefKindString(IndirectReferenceTable::GetIndirectRefKind(java_object)), |
| 796 | java_object, |
| 797 | obj.Ptr()); |
Elliott Hughes | a92853e | 2012-02-07 16:09:27 -0800 | [diff] [blame] | 798 | return false; |
| 799 | } |
| 800 | |
| 801 | bool okay = true; |
| 802 | switch (kind) { |
| 803 | case kClass: |
| 804 | okay = obj->IsClass(); |
| 805 | break; |
| 806 | case kDirectByteBuffer: |
| 807 | UNIMPLEMENTED(FATAL); |
| 808 | break; |
| 809 | case kString: |
| 810 | okay = obj->GetClass()->IsStringClass(); |
| 811 | break; |
| 812 | case kThrowable: |
| 813 | okay = obj->GetClass()->IsThrowableClass(); |
| 814 | break; |
| 815 | case kObject: |
| 816 | break; |
| 817 | } |
| 818 | if (!okay) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 819 | AbortF("%s has wrong type: %s", what, mirror::Object::PrettyTypeOf(obj).c_str()); |
Elliott Hughes | a92853e | 2012-02-07 16:09:27 -0800 | [diff] [blame] | 820 | return false; |
| 821 | } |
| 822 | |
| 823 | return true; |
| 824 | } |
| 825 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 826 | /* |
| 827 | * Verify that the "mode" argument passed to a primitive array Release |
| 828 | * function is one of the valid values. |
| 829 | */ |
| 830 | bool CheckReleaseMode(jint mode) { |
| 831 | if (mode != 0 && mode != JNI_COMMIT && mode != JNI_ABORT) { |
| 832 | AbortF("unknown value for release mode: %d", mode); |
| 833 | return false; |
| 834 | } |
| 835 | return true; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 836 | } |
| 837 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 838 | bool CheckPossibleHeapValue(ScopedObjectAccess& soa, char fmt, JniValueType arg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 839 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 840 | switch (fmt) { |
| 841 | case 'a': // jarray |
| 842 | return CheckArray(soa, arg.a); |
| 843 | case 'c': // jclass |
| 844 | return CheckInstance(soa, kClass, arg.c, false); |
| 845 | case 'f': // jfieldID |
| 846 | return CheckFieldID(soa, arg.f) != nullptr; |
| 847 | case 'm': // jmethodID |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame^] | 848 | return CheckMethodID(arg.m) != nullptr; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 849 | case 'r': // release int |
| 850 | return CheckReleaseMode(arg.r); |
| 851 | case 's': // jstring |
| 852 | return CheckInstance(soa, kString, arg.s, false); |
| 853 | case 't': // jthrowable |
| 854 | return CheckInstance(soa, kThrowable, arg.t, false); |
| 855 | case 'E': // JNIEnv* |
| 856 | return CheckThread(arg.E); |
| 857 | case 'L': // jobject |
| 858 | return CheckInstance(soa, kObject, arg.L, true); |
Alex Light | 48ffe06 | 2015-08-19 15:23:23 -0700 | [diff] [blame] | 859 | case '.': // A VarArgs list |
| 860 | return CheckVarArgs(soa, arg.va); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 861 | default: |
| 862 | return CheckNonHeapValue(fmt, arg); |
| 863 | } |
| 864 | } |
| 865 | |
Alex Light | 48ffe06 | 2015-08-19 15:23:23 -0700 | [diff] [blame] | 866 | bool CheckVarArgs(ScopedObjectAccess& soa, const VarArgs* args_p) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 867 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 48ffe06 | 2015-08-19 15:23:23 -0700 | [diff] [blame] | 868 | CHECK(args_p != nullptr); |
| 869 | VarArgs args(args_p->Clone()); |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame^] | 870 | ArtMethod* m = CheckMethodID(args.GetMethodID()); |
Alex Light | 48ffe06 | 2015-08-19 15:23:23 -0700 | [diff] [blame] | 871 | if (m == nullptr) { |
| 872 | return false; |
| 873 | } |
| 874 | uint32_t len = 0; |
| 875 | const char* shorty = m->GetShorty(&len); |
| 876 | // Skip the return type |
| 877 | CHECK_GE(len, 1u); |
| 878 | len--; |
| 879 | shorty++; |
| 880 | for (uint32_t i = 0; i < len; i++) { |
| 881 | if (!CheckPossibleHeapValue(soa, shorty[i], args.GetValue(shorty[i]))) { |
| 882 | return false; |
| 883 | } |
| 884 | } |
| 885 | return true; |
| 886 | } |
| 887 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 888 | bool CheckNonHeapValue(char fmt, JniValueType arg) { |
| 889 | switch (fmt) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 890 | case 'p': // TODO: pointer - null or readable? |
| 891 | case 'v': // JavaVM* |
| 892 | case 'B': // jbyte |
| 893 | case 'C': // jchar |
| 894 | case 'D': // jdouble |
| 895 | case 'F': // jfloat |
| 896 | case 'I': // jint |
| 897 | case 'J': // jlong |
| 898 | case 'S': // jshort |
| 899 | break; // Ignored. |
| 900 | case 'b': // jboolean, why two? Fall-through. |
| 901 | case 'Z': |
| 902 | return CheckBoolean(arg.Z); |
| 903 | case 'u': // utf8 |
| 904 | if ((flags_ & kFlag_Release) != 0) { |
| 905 | return CheckNonNull(arg.u); |
| 906 | } else { |
| 907 | bool nullable = ((flags_ & kFlag_NullableUtf) != 0); |
| 908 | return CheckUtfString(arg.u, nullable); |
| 909 | } |
| 910 | case 'w': // jobjectRefType |
| 911 | switch (arg.w) { |
| 912 | case JNIInvalidRefType: |
| 913 | case JNILocalRefType: |
| 914 | case JNIGlobalRefType: |
| 915 | case JNIWeakGlobalRefType: |
| 916 | break; |
| 917 | default: |
| 918 | AbortF("Unknown reference type"); |
| 919 | return false; |
| 920 | } |
| 921 | break; |
| 922 | case 'z': // jsize |
| 923 | return CheckLengthPositive(arg.z); |
| 924 | default: |
| 925 | AbortF("unknown format specifier: '%c'", fmt); |
| 926 | return false; |
| 927 | } |
| 928 | return true; |
| 929 | } |
| 930 | |
| 931 | void TracePossibleHeapValue(ScopedObjectAccess& soa, bool entry, char fmt, JniValueType arg, |
| 932 | std::string* msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 933 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 934 | switch (fmt) { |
| 935 | case 'L': // jobject fall-through. |
| 936 | case 'a': // jarray fall-through. |
| 937 | case 's': // jstring fall-through. |
| 938 | case 't': // jthrowable fall-through. |
| 939 | if (arg.L == nullptr) { |
| 940 | *msg += "NULL"; |
| 941 | } else { |
| 942 | StringAppendF(msg, "%p", arg.L); |
| 943 | } |
| 944 | break; |
| 945 | case 'c': { // jclass |
| 946 | jclass jc = arg.c; |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 947 | ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(jc); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 948 | if (c == nullptr) { |
| 949 | *msg += "NULL"; |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 950 | } else if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(c.Ptr())) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 951 | StringAppendF(msg, "INVALID POINTER:%p", jc); |
| 952 | } else if (!c->IsClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 953 | *msg += "INVALID NON-CLASS OBJECT OF TYPE:" + c->PrettyTypeOf(); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 954 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 955 | *msg += c->PrettyClass(); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 956 | if (!entry) { |
| 957 | StringAppendF(msg, " (%p)", jc); |
| 958 | } |
| 959 | } |
| 960 | break; |
| 961 | } |
| 962 | case 'f': { // jfieldID |
| 963 | jfieldID fid = arg.f; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 964 | ArtField* f = soa.DecodeField(fid); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 965 | *msg += ArtField::PrettyField(f); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 966 | if (!entry) { |
| 967 | StringAppendF(msg, " (%p)", fid); |
| 968 | } |
| 969 | break; |
| 970 | } |
| 971 | case 'm': { // jmethodID |
| 972 | jmethodID mid = arg.m; |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame^] | 973 | ArtMethod* m = jni::DecodeArtMethod(mid); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 974 | *msg += ArtMethod::PrettyMethod(m); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 975 | if (!entry) { |
| 976 | StringAppendF(msg, " (%p)", mid); |
| 977 | } |
| 978 | break; |
| 979 | } |
Alex Light | 48ffe06 | 2015-08-19 15:23:23 -0700 | [diff] [blame] | 980 | case '.': { |
| 981 | const VarArgs* va = arg.va; |
| 982 | VarArgs args(va->Clone()); |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame^] | 983 | ArtMethod* m = jni::DecodeArtMethod(args.GetMethodID()); |
Alex Light | 48ffe06 | 2015-08-19 15:23:23 -0700 | [diff] [blame] | 984 | uint32_t len; |
| 985 | const char* shorty = m->GetShorty(&len); |
| 986 | CHECK_GE(len, 1u); |
| 987 | // Skip past return value. |
| 988 | len--; |
| 989 | shorty++; |
| 990 | // Remove the previous ', ' from the message. |
| 991 | msg->erase(msg->length() - 2); |
| 992 | for (uint32_t i = 0; i < len; i++) { |
| 993 | *msg += ", "; |
| 994 | TracePossibleHeapValue(soa, entry, shorty[i], args.GetValue(shorty[i]), msg); |
| 995 | } |
| 996 | break; |
| 997 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 998 | default: |
| 999 | TraceNonHeapValue(fmt, arg, msg); |
| 1000 | break; |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | void TraceNonHeapValue(char fmt, JniValueType arg, std::string* msg) { |
| 1005 | switch (fmt) { |
| 1006 | case 'B': // jbyte |
| 1007 | if (arg.B >= 0 && arg.B < 10) { |
| 1008 | StringAppendF(msg, "%d", arg.B); |
| 1009 | } else { |
| 1010 | StringAppendF(msg, "%#x (%d)", arg.B, arg.B); |
| 1011 | } |
| 1012 | break; |
| 1013 | case 'C': // jchar |
| 1014 | if (arg.C < 0x7f && arg.C >= ' ') { |
| 1015 | StringAppendF(msg, "U+%x ('%c')", arg.C, arg.C); |
| 1016 | } else { |
| 1017 | StringAppendF(msg, "U+%x", arg.C); |
| 1018 | } |
| 1019 | break; |
| 1020 | case 'F': // jfloat |
| 1021 | StringAppendF(msg, "%g", arg.F); |
| 1022 | break; |
| 1023 | case 'D': // jdouble |
| 1024 | StringAppendF(msg, "%g", arg.D); |
| 1025 | break; |
| 1026 | case 'S': // jshort |
| 1027 | StringAppendF(msg, "%d", arg.S); |
| 1028 | break; |
| 1029 | case 'i': // jint - fall-through. |
| 1030 | case 'I': // jint |
| 1031 | StringAppendF(msg, "%d", arg.I); |
| 1032 | break; |
| 1033 | case 'J': // jlong |
| 1034 | StringAppendF(msg, "%" PRId64, arg.J); |
| 1035 | break; |
| 1036 | case 'Z': // jboolean |
| 1037 | case 'b': // jboolean (JNI-style) |
| 1038 | *msg += arg.b == JNI_TRUE ? "true" : "false"; |
| 1039 | break; |
| 1040 | case 'V': // void |
| 1041 | DCHECK(arg.V == nullptr); |
| 1042 | *msg += "void"; |
| 1043 | break; |
| 1044 | case 'v': // JavaVM* |
| 1045 | StringAppendF(msg, "(JavaVM*)%p", arg.v); |
| 1046 | break; |
| 1047 | case 'E': |
| 1048 | StringAppendF(msg, "(JNIEnv*)%p", arg.E); |
| 1049 | break; |
| 1050 | case 'z': // non-negative jsize |
| 1051 | // You might expect jsize to be size_t, but it's not; it's the same as jint. |
| 1052 | // We only treat this specially so we can do the non-negative check. |
| 1053 | // TODO: maybe this wasn't worth it? |
| 1054 | StringAppendF(msg, "%d", arg.z); |
| 1055 | break; |
| 1056 | case 'p': // void* ("pointer") |
| 1057 | if (arg.p == nullptr) { |
| 1058 | *msg += "NULL"; |
| 1059 | } else { |
| 1060 | StringAppendF(msg, "(void*) %p", arg.p); |
| 1061 | } |
| 1062 | break; |
| 1063 | case 'r': { // jint (release mode) |
| 1064 | jint releaseMode = arg.r; |
| 1065 | if (releaseMode == 0) { |
| 1066 | *msg += "0"; |
| 1067 | } else if (releaseMode == JNI_ABORT) { |
| 1068 | *msg += "JNI_ABORT"; |
| 1069 | } else if (releaseMode == JNI_COMMIT) { |
| 1070 | *msg += "JNI_COMMIT"; |
| 1071 | } else { |
| 1072 | StringAppendF(msg, "invalid release mode %d", releaseMode); |
| 1073 | } |
| 1074 | break; |
| 1075 | } |
| 1076 | case 'u': // const char* (Modified UTF-8) |
| 1077 | if (arg.u == nullptr) { |
| 1078 | *msg += "NULL"; |
| 1079 | } else { |
| 1080 | StringAppendF(msg, "\"%s\"", arg.u); |
| 1081 | } |
| 1082 | break; |
| 1083 | case 'w': // jobjectRefType |
| 1084 | switch (arg.w) { |
| 1085 | case JNIInvalidRefType: |
| 1086 | *msg += "invalid reference type"; |
| 1087 | break; |
| 1088 | case JNILocalRefType: |
| 1089 | *msg += "local ref type"; |
| 1090 | break; |
| 1091 | case JNIGlobalRefType: |
| 1092 | *msg += "global ref type"; |
| 1093 | break; |
| 1094 | case JNIWeakGlobalRefType: |
| 1095 | *msg += "weak global ref type"; |
| 1096 | break; |
| 1097 | default: |
| 1098 | *msg += "unknown ref type"; |
| 1099 | break; |
| 1100 | } |
| 1101 | break; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1102 | default: |
| 1103 | LOG(FATAL) << function_name_ << ": unknown trace format specifier: '" << fmt << "'"; |
| 1104 | } |
| 1105 | } |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1106 | /* |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1107 | * Verify that "array" is non-null and points to an Array object. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1108 | * |
| 1109 | * Since we're dealing with objects, switch to "running" mode. |
| 1110 | */ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1111 | bool CheckArray(ScopedObjectAccess& soa, jarray java_array) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1112 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1113 | if (UNLIKELY(java_array == nullptr)) { |
| 1114 | AbortF("jarray was NULL"); |
| 1115 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1118 | ObjPtr<mirror::Array> a = soa.Decode<mirror::Array>(java_array); |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 1119 | if (UNLIKELY(!Runtime::Current()->GetHeap()->IsValidObjectAddress(a.Ptr()))) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 1120 | Runtime::Current()->GetHeap()->DumpSpaces(LOG_STREAM(ERROR)); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1121 | AbortF("jarray is an invalid %s: %p (%p)", |
Andreas Gampe | dc061d0 | 2016-10-24 13:19:37 -0700 | [diff] [blame] | 1122 | GetIndirectRefKindString(IndirectReferenceTable::GetIndirectRefKind(java_array)), |
| 1123 | java_array, |
| 1124 | a.Ptr()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1125 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1126 | } else if (!a->IsArrayInstance()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1127 | AbortF("jarray argument has non-array type: %s", a->PrettyTypeOf().c_str()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1128 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1129 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1130 | return true; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1133 | bool CheckBoolean(jboolean z) { |
| 1134 | if (z != JNI_TRUE && z != JNI_FALSE) { |
| 1135 | AbortF("unexpected jboolean value: %d", z); |
| 1136 | return false; |
| 1137 | } |
| 1138 | return true; |
| 1139 | } |
| 1140 | |
| 1141 | bool CheckLengthPositive(jsize length) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1142 | if (length < 0) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1143 | AbortF("negative jsize: %d", length); |
| 1144 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1145 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1146 | return true; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1149 | ArtField* CheckFieldID(ScopedObjectAccess& soa, jfieldID fid) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1150 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1151 | if (fid == nullptr) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1152 | AbortF("jfieldID was NULL"); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1153 | return nullptr; |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1154 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1155 | ArtField* f = soa.DecodeField(fid); |
| 1156 | // TODO: Better check here. |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 1157 | if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(f->GetDeclaringClass().Ptr())) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 1158 | Runtime::Current()->GetHeap()->DumpSpaces(LOG_STREAM(ERROR)); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1159 | AbortF("invalid jfieldID: %p", fid); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1160 | return nullptr; |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1161 | } |
| 1162 | return f; |
| 1163 | } |
| 1164 | |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame^] | 1165 | ArtMethod* CheckMethodID(jmethodID mid) REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1166 | if (mid == nullptr) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1167 | AbortF("jmethodID was NULL"); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1168 | return nullptr; |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1169 | } |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame^] | 1170 | ArtMethod* m = jni::DecodeArtMethod(mid); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1171 | // TODO: Better check here. |
| 1172 | if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(m->GetDeclaringClass())) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 1173 | Runtime::Current()->GetHeap()->DumpSpaces(LOG_STREAM(ERROR)); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1174 | AbortF("invalid jmethodID: %p", mid); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1175 | return nullptr; |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1176 | } |
| 1177 | return m; |
| 1178 | } |
| 1179 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1180 | bool CheckThread(JNIEnv* env) REQUIRES_SHARED(Locks::mutator_lock_) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1181 | Thread* self = Thread::Current(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1182 | if (self == nullptr) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1183 | AbortF("a thread (tid %d) is making JNI calls without being attached", GetTid()); |
| 1184 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
Daniel Erat | 35e827a | 2016-05-10 18:07:18 -0600 | [diff] [blame] | 1187 | // Get the current thread's JNIEnv by going through our TLS pointer. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1188 | JNIEnvExt* threadEnv = self->GetJniEnv(); |
| 1189 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 1190 | // Verify that the current thread is (a) attached and (b) associated with |
| 1191 | // this particular instance of JNIEnv. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1192 | if (env != threadEnv) { |
Daniel Erat | 35e827a | 2016-05-10 18:07:18 -0600 | [diff] [blame] | 1193 | // Get the thread owning the JNIEnv that's being used. |
| 1194 | Thread* envThread = reinterpret_cast<JNIEnvExt*>(env)->self; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1195 | AbortF("thread %s using JNIEnv* from thread %s", |
Daniel Erat | 35e827a | 2016-05-10 18:07:18 -0600 | [diff] [blame] | 1196 | ToStr<Thread>(*self).c_str(), ToStr<Thread>(*envThread).c_str()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1197 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1198 | } |
| 1199 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 1200 | // Verify that, if this thread previously made a critical "get" call, we |
| 1201 | // do the corresponding "release" call before we try anything else. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1202 | switch (flags_ & kFlag_CritMask) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1203 | case kFlag_CritOkay: // okay to call this method |
| 1204 | break; |
| 1205 | case kFlag_CritBad: // not okay to call |
| 1206 | if (threadEnv->critical) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1207 | AbortF("thread %s using JNI after critical get", |
| 1208 | ToStr<Thread>(*self).c_str()); |
| 1209 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1210 | } |
| 1211 | break; |
| 1212 | case kFlag_CritGet: // this is a "get" call |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 1213 | // Don't check here; we allow nested gets. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1214 | threadEnv->critical++; |
| 1215 | break; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1216 | case kFlag_CritRelease: // this is a "release" call |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1217 | threadEnv->critical--; |
| 1218 | if (threadEnv->critical < 0) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1219 | AbortF("thread %s called too many critical releases", |
| 1220 | ToStr<Thread>(*self).c_str()); |
| 1221 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1222 | } |
| 1223 | break; |
| 1224 | default: |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1225 | LOG(FATAL) << "Bad flags (internal error): " << flags_; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1226 | } |
| 1227 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 1228 | // Verify that, if an exception has been raised, the native code doesn't |
| 1229 | // make any JNI calls other than the Exception* methods. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1230 | if ((flags_ & kFlag_ExcepOkay) == 0 && self->IsExceptionPending()) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 1231 | mirror::Throwable* exception = self->GetException(); |
| 1232 | AbortF("JNI %s called with pending exception %s", |
| 1233 | function_name_, |
| 1234 | exception->Dump().c_str()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1235 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1236 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1237 | return true; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 1240 | // Verifies that "bytes" points to valid Modified UTF-8 data. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1241 | bool CheckUtfString(const char* bytes, bool nullable) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1242 | if (bytes == nullptr) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1243 | if (!nullable) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1244 | AbortF("non-nullable const char* was NULL"); |
| 1245 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1246 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1247 | return true; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1250 | const char* errorKind = nullptr; |
Andreas Gampe | ef4afe9 | 2015-07-27 21:03:25 -0700 | [diff] [blame] | 1251 | const uint8_t* utf8 = CheckUtfBytes(bytes, &errorKind); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1252 | if (errorKind != nullptr) { |
Andreas Gampe | ef4afe9 | 2015-07-27 21:03:25 -0700 | [diff] [blame] | 1253 | // This is an expensive loop that will resize often, but this isn't supposed to hit in |
| 1254 | // practice anyways. |
| 1255 | std::ostringstream oss; |
| 1256 | oss << std::hex; |
| 1257 | const uint8_t* tmp = reinterpret_cast<const uint8_t*>(bytes); |
| 1258 | while (*tmp != 0) { |
| 1259 | if (tmp == utf8) { |
| 1260 | oss << "<"; |
| 1261 | } |
| 1262 | oss << "0x" << std::setfill('0') << std::setw(2) << static_cast<uint32_t>(*tmp); |
| 1263 | if (tmp == utf8) { |
| 1264 | oss << '>'; |
| 1265 | } |
| 1266 | tmp++; |
| 1267 | if (*tmp != 0) { |
| 1268 | oss << ' '; |
| 1269 | } |
| 1270 | } |
| 1271 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1272 | AbortF("input is not valid Modified UTF-8: illegal %s byte %#x\n" |
Andreas Gampe | ef4afe9 | 2015-07-27 21:03:25 -0700 | [diff] [blame] | 1273 | " string: '%s'\n input: '%s'", errorKind, *utf8, bytes, oss.str().c_str()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1274 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1275 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1276 | return true; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 1279 | // Checks whether |bytes| is valid modified UTF-8. We also accept 4 byte UTF |
| 1280 | // sequences in place of encoded surrogate pairs. |
Andreas Gampe | ef4afe9 | 2015-07-27 21:03:25 -0700 | [diff] [blame] | 1281 | static const uint8_t* CheckUtfBytes(const char* bytes, const char** errorKind) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1282 | while (*bytes != '\0') { |
Andreas Gampe | ef4afe9 | 2015-07-27 21:03:25 -0700 | [diff] [blame] | 1283 | const uint8_t* utf8 = reinterpret_cast<const uint8_t*>(bytes++); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1284 | // Switch on the high four bits. |
Andreas Gampe | ef4afe9 | 2015-07-27 21:03:25 -0700 | [diff] [blame] | 1285 | switch (*utf8 >> 4) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1286 | case 0x00: |
| 1287 | case 0x01: |
| 1288 | case 0x02: |
| 1289 | case 0x03: |
| 1290 | case 0x04: |
| 1291 | case 0x05: |
| 1292 | case 0x06: |
| 1293 | case 0x07: |
| 1294 | // Bit pattern 0xxx. No need for any extra bytes. |
| 1295 | break; |
| 1296 | case 0x08: |
| 1297 | case 0x09: |
| 1298 | case 0x0a: |
| 1299 | case 0x0b: |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 1300 | // Bit patterns 10xx, which are illegal start bytes. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1301 | *errorKind = "start"; |
| 1302 | return utf8; |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 1303 | case 0x0f: |
| 1304 | // Bit pattern 1111, which might be the start of a 4 byte sequence. |
Andreas Gampe | ef4afe9 | 2015-07-27 21:03:25 -0700 | [diff] [blame] | 1305 | if ((*utf8 & 0x08) == 0) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 1306 | // Bit pattern 1111 0xxx, which is the start of a 4 byte sequence. |
| 1307 | // We consume one continuation byte here, and fall through to consume two more. |
Andreas Gampe | ef4afe9 | 2015-07-27 21:03:25 -0700 | [diff] [blame] | 1308 | utf8 = reinterpret_cast<const uint8_t*>(bytes++); |
| 1309 | if ((*utf8 & 0xc0) != 0x80) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 1310 | *errorKind = "continuation"; |
| 1311 | return utf8; |
| 1312 | } |
| 1313 | } else { |
| 1314 | *errorKind = "start"; |
| 1315 | return utf8; |
| 1316 | } |
| 1317 | |
| 1318 | // Fall through to the cases below to consume two more continuation bytes. |
| 1319 | FALLTHROUGH_INTENDED; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1320 | case 0x0e: |
| 1321 | // Bit pattern 1110, so there are two additional bytes. |
Andreas Gampe | ef4afe9 | 2015-07-27 21:03:25 -0700 | [diff] [blame] | 1322 | utf8 = reinterpret_cast<const uint8_t*>(bytes++); |
| 1323 | if ((*utf8 & 0xc0) != 0x80) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1324 | *errorKind = "continuation"; |
| 1325 | return utf8; |
| 1326 | } |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 1327 | |
| 1328 | // Fall through to consume one more continuation byte. |
| 1329 | FALLTHROUGH_INTENDED; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1330 | case 0x0c: |
| 1331 | case 0x0d: |
| 1332 | // Bit pattern 110x, so there is one additional byte. |
Andreas Gampe | ef4afe9 | 2015-07-27 21:03:25 -0700 | [diff] [blame] | 1333 | utf8 = reinterpret_cast<const uint8_t*>(bytes++); |
| 1334 | if ((*utf8 & 0xc0) != 0x80) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1335 | *errorKind = "continuation"; |
| 1336 | return utf8; |
| 1337 | } |
| 1338 | break; |
| 1339 | } |
| 1340 | } |
| 1341 | return 0; |
| 1342 | } |
| 1343 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1344 | void AbortF(const char* fmt, ...) __attribute__((__format__(__printf__, 2, 3))) { |
| 1345 | va_list args; |
| 1346 | va_start(args, fmt); |
| 1347 | Runtime::Current()->GetJavaVM()->JniAbortV(function_name_, fmt, args); |
| 1348 | va_end(args); |
| 1349 | } |
| 1350 | |
| 1351 | // The name of the JNI function being checked. |
| 1352 | const char* const function_name_; |
| 1353 | |
| 1354 | const int flags_; |
Elliott Hughes | 92cb498 | 2011-12-16 16:57:28 -0800 | [diff] [blame] | 1355 | int indent_; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1356 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1357 | const bool has_method_; |
| 1358 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1359 | DISALLOW_COPY_AND_ASSIGN(ScopedCheck); |
| 1360 | }; |
| 1361 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1362 | /* |
| 1363 | * =========================================================================== |
| 1364 | * Guarded arrays |
| 1365 | * =========================================================================== |
| 1366 | */ |
| 1367 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1368 | /* this gets tucked in at the start of the buffer; struct size must be even */ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1369 | class GuardedCopy { |
| 1370 | public: |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1371 | /* |
| 1372 | * Create an over-sized buffer to hold the contents of "buf". Copy it in, |
| 1373 | * filling in the area around it with guard data. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1374 | */ |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 1375 | static void* Create(void* original_buf, size_t len, bool mod_okay) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1376 | const size_t new_len = LengthIncludingRedZones(len); |
| 1377 | uint8_t* const new_buf = DebugAlloc(new_len); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1378 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 1379 | // If modification is not expected, grab a checksum. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1380 | uLong adler = 0; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1381 | if (!mod_okay) { |
| 1382 | adler = adler32(adler32(0L, Z_NULL, 0), reinterpret_cast<const Bytef*>(original_buf), len); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1383 | } |
| 1384 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1385 | GuardedCopy* copy = new (new_buf) GuardedCopy(original_buf, len, adler); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1386 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1387 | // Fill begin region with canary pattern. |
| 1388 | const size_t kStartCanaryLength = (GuardedCopy::kRedZoneSize / 2) - sizeof(GuardedCopy); |
| 1389 | for (size_t i = 0, j = 0; i < kStartCanaryLength; ++i) { |
| 1390 | const_cast<char*>(copy->StartRedZone())[i] = kCanary[j]; |
| 1391 | if (kCanary[j] == '\0') { |
| 1392 | j = 0; |
tony.ys_liu | 068361a | 2015-07-14 16:25:16 +0800 | [diff] [blame] | 1393 | } else { |
| 1394 | j++; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1395 | } |
| 1396 | } |
| 1397 | |
| 1398 | // Copy the data in; note "len" could be zero. |
| 1399 | memcpy(const_cast<uint8_t*>(copy->BufferWithinRedZones()), original_buf, len); |
| 1400 | |
| 1401 | // Fill end region with canary pattern. |
| 1402 | for (size_t i = 0, j = 0; i < kEndCanaryLength; ++i) { |
| 1403 | const_cast<char*>(copy->EndRedZone())[i] = kCanary[j]; |
| 1404 | if (kCanary[j] == '\0') { |
| 1405 | j = 0; |
tony.ys_liu | 068361a | 2015-07-14 16:25:16 +0800 | [diff] [blame] | 1406 | } else { |
| 1407 | j++; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | return const_cast<uint8_t*>(copy->BufferWithinRedZones()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1412 | } |
| 1413 | |
| 1414 | /* |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1415 | * Create a guarded copy of a primitive array. Modifications to the copied |
| 1416 | * data are allowed. Returns a pointer to the copied data. |
| 1417 | */ |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 1418 | static void* CreateGuardedPACopy(JNIEnv* env, const jarray java_array, jboolean* is_copy, |
| 1419 | void* original_ptr) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1420 | ScopedObjectAccess soa(env); |
| 1421 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1422 | ObjPtr<mirror::Array> a = soa.Decode<mirror::Array>(java_array); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1423 | size_t component_size = a->GetClass()->GetComponentSize(); |
| 1424 | size_t byte_count = a->GetLength() * component_size; |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 1425 | void* result = Create(original_ptr, byte_count, true); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1426 | if (is_copy != nullptr) { |
| 1427 | *is_copy = JNI_TRUE; |
| 1428 | } |
| 1429 | return result; |
| 1430 | } |
| 1431 | |
| 1432 | /* |
| 1433 | * Perform the array "release" operation, which may or may not copy data |
| 1434 | * back into the managed heap, and may or may not release the underlying storage. |
| 1435 | */ |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 1436 | static void* ReleaseGuardedPACopy(const char* function_name, JNIEnv* env, |
| 1437 | jarray java_array ATTRIBUTE_UNUSED, void* embedded_buf, |
| 1438 | int mode) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1439 | ScopedObjectAccess soa(env); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1440 | if (!GuardedCopy::Check(function_name, embedded_buf, true)) { |
| 1441 | return nullptr; |
| 1442 | } |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 1443 | GuardedCopy* const copy = FromEmbedded(embedded_buf); |
| 1444 | void* original_ptr = copy->original_ptr_; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1445 | if (mode != JNI_ABORT) { |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 1446 | memcpy(original_ptr, embedded_buf, copy->original_length_); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1447 | } |
| 1448 | if (mode != JNI_COMMIT) { |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 1449 | Destroy(embedded_buf); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1450 | } |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 1451 | return original_ptr; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | |
| 1455 | /* |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1456 | * Free up the guard buffer, scrub it, and return the original pointer. |
| 1457 | */ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1458 | static void* Destroy(void* embedded_buf) { |
| 1459 | GuardedCopy* copy = FromEmbedded(embedded_buf); |
| 1460 | void* original_ptr = const_cast<void*>(copy->original_ptr_); |
| 1461 | size_t len = LengthIncludingRedZones(copy->original_length_); |
| 1462 | DebugFree(copy, len); |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1463 | return original_ptr; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | /* |
| 1467 | * Verify the guard area and, if "modOkay" is false, that the data itself |
| 1468 | * has not been altered. |
| 1469 | * |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1470 | * The caller has already checked that "dataBuf" is non-null. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1471 | */ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1472 | static bool Check(const char* function_name, const void* embedded_buf, bool mod_okay) { |
| 1473 | const GuardedCopy* copy = FromEmbedded(embedded_buf); |
| 1474 | return copy->CheckHeader(function_name, mod_okay) && copy->CheckRedZones(function_name); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | private: |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 1478 | GuardedCopy(void* original_buf, size_t len, uLong adler) : |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1479 | magic_(kGuardMagic), adler_(adler), original_ptr_(original_buf), original_length_(len) { |
| 1480 | } |
| 1481 | |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1482 | static uint8_t* DebugAlloc(size_t len) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1483 | 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] | 1484 | if (result == MAP_FAILED) { |
| 1485 | PLOG(FATAL) << "GuardedCopy::create mmap(" << len << ") failed"; |
| 1486 | } |
| 1487 | return reinterpret_cast<uint8_t*>(result); |
| 1488 | } |
| 1489 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1490 | static void DebugFree(void* buf, size_t len) { |
| 1491 | if (munmap(buf, len) != 0) { |
| 1492 | PLOG(FATAL) << "munmap(" << buf << ", " << len << ") failed"; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1493 | } |
| 1494 | } |
| 1495 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1496 | static size_t LengthIncludingRedZones(size_t len) { |
| 1497 | return len + kRedZoneSize; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1498 | } |
| 1499 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1500 | // Get the GuardedCopy from the interior pointer. |
| 1501 | static GuardedCopy* FromEmbedded(void* embedded_buf) { |
| 1502 | return reinterpret_cast<GuardedCopy*>( |
| 1503 | reinterpret_cast<uint8_t*>(embedded_buf) - (kRedZoneSize / 2)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1504 | } |
| 1505 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1506 | static const GuardedCopy* FromEmbedded(const void* embedded_buf) { |
| 1507 | return reinterpret_cast<const GuardedCopy*>( |
| 1508 | reinterpret_cast<const uint8_t*>(embedded_buf) - (kRedZoneSize / 2)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1509 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1510 | |
| 1511 | static void AbortF(const char* jni_function_name, const char* fmt, ...) { |
| 1512 | va_list args; |
| 1513 | va_start(args, fmt); |
| 1514 | Runtime::Current()->GetJavaVM()->JniAbortV(jni_function_name, fmt, args); |
| 1515 | va_end(args); |
| 1516 | } |
| 1517 | |
| 1518 | bool CheckHeader(const char* function_name, bool mod_okay) const { |
| 1519 | static const uint32_t kMagicCmp = kGuardMagic; |
| 1520 | |
| 1521 | // Before we do anything with "pExtra", check the magic number. We |
| 1522 | // do the check with memcmp rather than "==" in case the pointer is |
| 1523 | // unaligned. If it points to completely bogus memory we're going |
| 1524 | // to crash, but there's no easy way around that. |
| 1525 | if (UNLIKELY(memcmp(&magic_, &kMagicCmp, 4) != 0)) { |
| 1526 | uint8_t buf[4]; |
| 1527 | memcpy(buf, &magic_, 4); |
| 1528 | AbortF(function_name, |
| 1529 | "guard magic does not match (found 0x%02x%02x%02x%02x) -- incorrect data pointer %p?", |
| 1530 | buf[3], buf[2], buf[1], buf[0], this); // Assumes little-endian. |
| 1531 | return false; |
| 1532 | } |
| 1533 | |
| 1534 | // If modification is not expected, verify checksum. Strictly speaking this is wrong: if we |
| 1535 | // told the client that we made a copy, there's no reason they can't alter the buffer. |
| 1536 | if (!mod_okay) { |
| 1537 | uLong computed_adler = |
| 1538 | adler32(adler32(0L, Z_NULL, 0), BufferWithinRedZones(), original_length_); |
| 1539 | if (computed_adler != adler_) { |
| 1540 | AbortF(function_name, "buffer modified (0x%08lx vs 0x%08lx) at address %p", |
| 1541 | computed_adler, adler_, this); |
| 1542 | return false; |
| 1543 | } |
| 1544 | } |
| 1545 | return true; |
| 1546 | } |
| 1547 | |
| 1548 | bool CheckRedZones(const char* function_name) const { |
| 1549 | // Check the begin red zone. |
| 1550 | const size_t kStartCanaryLength = (GuardedCopy::kRedZoneSize / 2) - sizeof(GuardedCopy); |
| 1551 | for (size_t i = 0, j = 0; i < kStartCanaryLength; ++i) { |
| 1552 | if (UNLIKELY(StartRedZone()[i] != kCanary[j])) { |
| 1553 | AbortF(function_name, "guard pattern before buffer disturbed at %p +%zd", this, i); |
| 1554 | return false; |
| 1555 | } |
| 1556 | if (kCanary[j] == '\0') { |
| 1557 | j = 0; |
tony.ys_liu | 068361a | 2015-07-14 16:25:16 +0800 | [diff] [blame] | 1558 | } else { |
| 1559 | j++; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | // Check end region. |
| 1564 | for (size_t i = 0, j = 0; i < kEndCanaryLength; ++i) { |
| 1565 | if (UNLIKELY(EndRedZone()[i] != kCanary[j])) { |
| 1566 | size_t offset_from_buffer_start = |
| 1567 | &(EndRedZone()[i]) - &(StartRedZone()[kStartCanaryLength]); |
| 1568 | AbortF(function_name, "guard pattern after buffer disturbed at %p +%zd", this, |
| 1569 | offset_from_buffer_start); |
| 1570 | return false; |
| 1571 | } |
| 1572 | if (kCanary[j] == '\0') { |
| 1573 | j = 0; |
tony.ys_liu | 068361a | 2015-07-14 16:25:16 +0800 | [diff] [blame] | 1574 | } else { |
| 1575 | j++; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1576 | } |
| 1577 | } |
| 1578 | return true; |
| 1579 | } |
| 1580 | |
| 1581 | // Location that canary value will be written before the guarded region. |
| 1582 | const char* StartRedZone() const { |
| 1583 | const uint8_t* buf = reinterpret_cast<const uint8_t*>(this); |
| 1584 | return reinterpret_cast<const char*>(buf + sizeof(GuardedCopy)); |
| 1585 | } |
| 1586 | |
| 1587 | // Return the interior embedded buffer. |
| 1588 | const uint8_t* BufferWithinRedZones() const { |
| 1589 | const uint8_t* embedded_buf = reinterpret_cast<const uint8_t*>(this) + (kRedZoneSize / 2); |
| 1590 | return embedded_buf; |
| 1591 | } |
| 1592 | |
| 1593 | // Location that canary value will be written after the guarded region. |
| 1594 | const char* EndRedZone() const { |
| 1595 | const uint8_t* buf = reinterpret_cast<const uint8_t*>(this); |
| 1596 | size_t buf_len = LengthIncludingRedZones(original_length_); |
| 1597 | return reinterpret_cast<const char*>(buf + (buf_len - (kRedZoneSize / 2))); |
| 1598 | } |
| 1599 | |
| 1600 | static constexpr size_t kRedZoneSize = 512; |
| 1601 | static constexpr size_t kEndCanaryLength = kRedZoneSize / 2; |
| 1602 | |
| 1603 | // Value written before and after the guarded array. |
| 1604 | static const char* const kCanary; |
| 1605 | |
| 1606 | static constexpr uint32_t kGuardMagic = 0xffd5aa96; |
| 1607 | |
| 1608 | const uint32_t magic_; |
| 1609 | const uLong adler_; |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 1610 | void* const original_ptr_; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1611 | const size_t original_length_; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1612 | }; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1613 | const char* const GuardedCopy::kCanary = "JNI BUFFER RED ZONE"; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1614 | |
| 1615 | /* |
| 1616 | * =========================================================================== |
| 1617 | * JNI functions |
| 1618 | * =========================================================================== |
| 1619 | */ |
| 1620 | |
| 1621 | class CheckJNI { |
| 1622 | public: |
| 1623 | static jint GetVersion(JNIEnv* env) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1624 | ScopedObjectAccess soa(env); |
| 1625 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 1626 | JniValueType args[1] = {{.E = env }}; |
| 1627 | if (sc.Check(soa, true, "E", args)) { |
| 1628 | JniValueType result; |
| 1629 | result.I = baseEnv(env)->GetVersion(env); |
| 1630 | if (sc.Check(soa, false, "I", &result)) { |
| 1631 | return result.I; |
| 1632 | } |
| 1633 | } |
| 1634 | return JNI_ERR; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1635 | } |
| 1636 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1637 | static jint GetJavaVM(JNIEnv *env, JavaVM **vm) { |
| 1638 | ScopedObjectAccess soa(env); |
| 1639 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 1640 | JniValueType args[2] = {{.E = env }, {.p = vm}}; |
| 1641 | if (sc.Check(soa, true, "Ep", args)) { |
| 1642 | JniValueType result; |
| 1643 | result.i = baseEnv(env)->GetJavaVM(env, vm); |
| 1644 | if (sc.Check(soa, false, "i", &result)) { |
| 1645 | return result.i; |
| 1646 | } |
| 1647 | } |
| 1648 | return JNI_ERR; |
| 1649 | } |
| 1650 | |
| 1651 | static jint RegisterNatives(JNIEnv* env, jclass c, const JNINativeMethod* methods, jint nMethods) { |
| 1652 | ScopedObjectAccess soa(env); |
| 1653 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 1654 | JniValueType args[4] = {{.E = env }, {.c = c}, {.p = methods}, {.I = nMethods}}; |
| 1655 | if (sc.Check(soa, true, "EcpI", args)) { |
| 1656 | JniValueType result; |
| 1657 | result.i = baseEnv(env)->RegisterNatives(env, c, methods, nMethods); |
| 1658 | if (sc.Check(soa, false, "i", &result)) { |
| 1659 | return result.i; |
| 1660 | } |
| 1661 | } |
| 1662 | return JNI_ERR; |
| 1663 | } |
| 1664 | |
| 1665 | static jint UnregisterNatives(JNIEnv* env, jclass c) { |
| 1666 | ScopedObjectAccess soa(env); |
| 1667 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 1668 | JniValueType args[2] = {{.E = env }, {.c = c}}; |
| 1669 | if (sc.Check(soa, true, "Ec", args)) { |
| 1670 | JniValueType result; |
| 1671 | result.i = baseEnv(env)->UnregisterNatives(env, c); |
| 1672 | if (sc.Check(soa, false, "i", &result)) { |
| 1673 | return result.i; |
| 1674 | } |
| 1675 | } |
| 1676 | return JNI_ERR; |
| 1677 | } |
| 1678 | |
| 1679 | static jobjectRefType GetObjectRefType(JNIEnv* env, jobject obj) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1680 | // Note: we use "EL" here but "Ep" has been used in the past on the basis that we'd like to |
| 1681 | // know the object is invalid. The spec says that passing invalid objects or even ones that |
| 1682 | // are deleted isn't supported. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1683 | ScopedObjectAccess soa(env); |
| 1684 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1685 | JniValueType args[2] = {{.E = env }, {.L = obj}}; |
| 1686 | if (sc.Check(soa, true, "EL", args)) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1687 | JniValueType result; |
| 1688 | result.w = baseEnv(env)->GetObjectRefType(env, obj); |
| 1689 | if (sc.Check(soa, false, "w", &result)) { |
| 1690 | return result.w; |
| 1691 | } |
| 1692 | } |
| 1693 | return JNIInvalidRefType; |
| 1694 | } |
| 1695 | |
| 1696 | static jclass DefineClass(JNIEnv* env, const char* name, jobject loader, const jbyte* buf, |
| 1697 | jsize bufLen) { |
| 1698 | ScopedObjectAccess soa(env); |
| 1699 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 1700 | JniValueType args[5] = {{.E = env}, {.u = name}, {.L = loader}, {.p = buf}, {.z = bufLen}}; |
| 1701 | if (sc.Check(soa, true, "EuLpz", args) && sc.CheckClassName(name)) { |
| 1702 | JniValueType result; |
| 1703 | result.c = baseEnv(env)->DefineClass(env, name, loader, buf, bufLen); |
| 1704 | if (sc.Check(soa, false, "c", &result)) { |
| 1705 | return result.c; |
| 1706 | } |
| 1707 | } |
| 1708 | return nullptr; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | static jclass FindClass(JNIEnv* env, const char* name) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1712 | ScopedObjectAccess soa(env); |
| 1713 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 1714 | JniValueType args[2] = {{.E = env}, {.u = name}}; |
| 1715 | if (sc.Check(soa, true, "Eu", args) && sc.CheckClassName(name)) { |
| 1716 | JniValueType result; |
| 1717 | result.c = baseEnv(env)->FindClass(env, name); |
| 1718 | if (sc.Check(soa, false, "c", &result)) { |
| 1719 | return result.c; |
| 1720 | } |
| 1721 | } |
| 1722 | return nullptr; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1723 | } |
| 1724 | |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1725 | static jclass GetSuperclass(JNIEnv* env, jclass c) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1726 | ScopedObjectAccess soa(env); |
| 1727 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 1728 | JniValueType args[2] = {{.E = env}, {.c = c}}; |
| 1729 | if (sc.Check(soa, true, "Ec", args)) { |
| 1730 | JniValueType result; |
| 1731 | result.c = baseEnv(env)->GetSuperclass(env, c); |
| 1732 | if (sc.Check(soa, false, "c", &result)) { |
| 1733 | return result.c; |
| 1734 | } |
| 1735 | } |
| 1736 | return nullptr; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1737 | } |
| 1738 | |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1739 | static jboolean IsAssignableFrom(JNIEnv* env, jclass c1, jclass c2) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1740 | ScopedObjectAccess soa(env); |
| 1741 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 1742 | JniValueType args[3] = {{.E = env}, {.c = c1}, {.c = c2}}; |
| 1743 | if (sc.Check(soa, true, "Ecc", args)) { |
| 1744 | JniValueType result; |
| 1745 | result.b = baseEnv(env)->IsAssignableFrom(env, c1, c2); |
| 1746 | if (sc.Check(soa, false, "b", &result)) { |
| 1747 | return result.b; |
| 1748 | } |
| 1749 | } |
| 1750 | return JNI_FALSE; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | static jmethodID FromReflectedMethod(JNIEnv* env, jobject method) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1754 | ScopedObjectAccess soa(env); |
| 1755 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 1756 | JniValueType args[2] = {{.E = env}, {.L = method}}; |
| 1757 | if (sc.Check(soa, true, "EL", args) && sc.CheckReflectedMethod(soa, method)) { |
| 1758 | JniValueType result; |
| 1759 | result.m = baseEnv(env)->FromReflectedMethod(env, method); |
| 1760 | if (sc.Check(soa, false, "m", &result)) { |
| 1761 | return result.m; |
| 1762 | } |
| 1763 | } |
| 1764 | return nullptr; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1765 | } |
| 1766 | |
| 1767 | static jfieldID FromReflectedField(JNIEnv* env, jobject field) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1768 | ScopedObjectAccess soa(env); |
| 1769 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 1770 | JniValueType args[2] = {{.E = env}, {.L = field}}; |
| 1771 | if (sc.Check(soa, true, "EL", args) && sc.CheckReflectedField(soa, field)) { |
| 1772 | JniValueType result; |
| 1773 | result.f = baseEnv(env)->FromReflectedField(env, field); |
| 1774 | if (sc.Check(soa, false, "f", &result)) { |
| 1775 | return result.f; |
| 1776 | } |
| 1777 | } |
| 1778 | return nullptr; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | static jobject ToReflectedMethod(JNIEnv* env, jclass cls, jmethodID mid, jboolean isStatic) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1782 | ScopedObjectAccess soa(env); |
| 1783 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 1784 | JniValueType args[4] = {{.E = env}, {.c = cls}, {.m = mid}, {.b = isStatic}}; |
| 1785 | if (sc.Check(soa, true, "Ecmb", args)) { |
| 1786 | JniValueType result; |
| 1787 | result.L = baseEnv(env)->ToReflectedMethod(env, cls, mid, isStatic); |
| 1788 | if (sc.Check(soa, false, "L", &result) && (result.L != nullptr)) { |
| 1789 | DCHECK(sc.CheckReflectedMethod(soa, result.L)); |
| 1790 | return result.L; |
| 1791 | } |
| 1792 | } |
| 1793 | return nullptr; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | static jobject ToReflectedField(JNIEnv* env, jclass cls, jfieldID fid, jboolean isStatic) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1797 | ScopedObjectAccess soa(env); |
| 1798 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 1799 | JniValueType args[4] = {{.E = env}, {.c = cls}, {.f = fid}, {.b = isStatic}}; |
| 1800 | if (sc.Check(soa, true, "Ecfb", args)) { |
| 1801 | JniValueType result; |
| 1802 | result.L = baseEnv(env)->ToReflectedField(env, cls, fid, isStatic); |
| 1803 | if (sc.Check(soa, false, "L", &result) && (result.L != nullptr)) { |
| 1804 | DCHECK(sc.CheckReflectedField(soa, result.L)); |
| 1805 | return result.L; |
| 1806 | } |
| 1807 | } |
| 1808 | return nullptr; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1809 | } |
| 1810 | |
| 1811 | static jint Throw(JNIEnv* env, jthrowable obj) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1812 | ScopedObjectAccess soa(env); |
| 1813 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 1814 | JniValueType args[2] = {{.E = env}, {.t = obj}}; |
| 1815 | if (sc.Check(soa, true, "Et", args) && sc.CheckThrowable(soa, obj)) { |
| 1816 | JniValueType result; |
| 1817 | result.i = baseEnv(env)->Throw(env, obj); |
| 1818 | if (sc.Check(soa, false, "i", &result)) { |
| 1819 | return result.i; |
| 1820 | } |
| 1821 | } |
| 1822 | return JNI_ERR; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1823 | } |
| 1824 | |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1825 | static jint ThrowNew(JNIEnv* env, jclass c, const char* message) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1826 | ScopedObjectAccess soa(env); |
| 1827 | ScopedCheck sc(kFlag_NullableUtf, __FUNCTION__); |
Christopher Ferris | c50358b | 2015-04-17 13:26:09 -0700 | [diff] [blame] | 1828 | JniValueType args[3] = {{.E = env}, {.c = c}, {.u = message}}; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1829 | if (sc.Check(soa, true, "Ecu", args) && sc.CheckThrowableClass(soa, c)) { |
| 1830 | JniValueType result; |
| 1831 | result.i = baseEnv(env)->ThrowNew(env, c, message); |
| 1832 | if (sc.Check(soa, false, "i", &result)) { |
| 1833 | return result.i; |
| 1834 | } |
| 1835 | } |
| 1836 | return JNI_ERR; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | static jthrowable ExceptionOccurred(JNIEnv* env) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1840 | ScopedObjectAccess soa(env); |
| 1841 | ScopedCheck sc(kFlag_ExcepOkay, __FUNCTION__); |
| 1842 | JniValueType args[1] = {{.E = env}}; |
| 1843 | if (sc.Check(soa, true, "E", args)) { |
| 1844 | JniValueType result; |
| 1845 | result.t = baseEnv(env)->ExceptionOccurred(env); |
| 1846 | if (sc.Check(soa, false, "t", &result)) { |
| 1847 | return result.t; |
| 1848 | } |
| 1849 | } |
| 1850 | return nullptr; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | static void ExceptionDescribe(JNIEnv* env) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1854 | ScopedObjectAccess soa(env); |
| 1855 | ScopedCheck sc(kFlag_ExcepOkay, __FUNCTION__); |
| 1856 | JniValueType args[1] = {{.E = env}}; |
| 1857 | if (sc.Check(soa, true, "E", args)) { |
| 1858 | JniValueType result; |
| 1859 | baseEnv(env)->ExceptionDescribe(env); |
| 1860 | result.V = nullptr; |
| 1861 | sc.Check(soa, false, "V", &result); |
| 1862 | } |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | static void ExceptionClear(JNIEnv* env) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1866 | ScopedObjectAccess soa(env); |
| 1867 | ScopedCheck sc(kFlag_ExcepOkay, __FUNCTION__); |
| 1868 | JniValueType args[1] = {{.E = env}}; |
| 1869 | if (sc.Check(soa, true, "E", args)) { |
| 1870 | JniValueType result; |
| 1871 | baseEnv(env)->ExceptionClear(env); |
| 1872 | result.V = nullptr; |
| 1873 | sc.Check(soa, false, "V", &result); |
| 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | static jboolean ExceptionCheck(JNIEnv* env) { |
| 1878 | ScopedObjectAccess soa(env); |
| 1879 | ScopedCheck sc(kFlag_CritOkay | kFlag_ExcepOkay, __FUNCTION__); |
| 1880 | JniValueType args[1] = {{.E = env}}; |
| 1881 | if (sc.Check(soa, true, "E", args)) { |
| 1882 | JniValueType result; |
| 1883 | result.b = baseEnv(env)->ExceptionCheck(env); |
| 1884 | if (sc.Check(soa, false, "b", &result)) { |
| 1885 | return result.b; |
| 1886 | } |
| 1887 | } |
| 1888 | return JNI_FALSE; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | static void FatalError(JNIEnv* env, const char* msg) { |
Elliott Hughes | c4378df | 2013-06-14 17:05:13 -0700 | [diff] [blame] | 1892 | // The JNI specification doesn't say it's okay to call FatalError with a pending exception, |
| 1893 | // but you're about to abort anyway, and it's quite likely that you have a pending exception, |
| 1894 | // and it's not unimaginable that you don't know that you do. So we allow it. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1895 | ScopedObjectAccess soa(env); |
| 1896 | ScopedCheck sc(kFlag_ExcepOkay | kFlag_NullableUtf, __FUNCTION__); |
| 1897 | JniValueType args[2] = {{.E = env}, {.u = msg}}; |
| 1898 | if (sc.Check(soa, true, "Eu", args)) { |
| 1899 | JniValueType result; |
| 1900 | baseEnv(env)->FatalError(env, msg); |
| 1901 | // Unreachable. |
| 1902 | result.V = nullptr; |
| 1903 | sc.Check(soa, false, "V", &result); |
| 1904 | } |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1905 | } |
| 1906 | |
| 1907 | static jint PushLocalFrame(JNIEnv* env, jint capacity) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1908 | ScopedObjectAccess soa(env); |
| 1909 | ScopedCheck sc(kFlag_ExcepOkay, __FUNCTION__); |
| 1910 | JniValueType args[2] = {{.E = env}, {.I = capacity}}; |
| 1911 | if (sc.Check(soa, true, "EI", args)) { |
| 1912 | JniValueType result; |
| 1913 | result.i = baseEnv(env)->PushLocalFrame(env, capacity); |
| 1914 | if (sc.Check(soa, false, "i", &result)) { |
| 1915 | return result.i; |
| 1916 | } |
| 1917 | } |
| 1918 | return JNI_ERR; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | static jobject PopLocalFrame(JNIEnv* env, jobject res) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1922 | ScopedObjectAccess soa(env); |
| 1923 | ScopedCheck sc(kFlag_ExcepOkay, __FUNCTION__); |
| 1924 | JniValueType args[2] = {{.E = env}, {.L = res}}; |
| 1925 | if (sc.Check(soa, true, "EL", args)) { |
| 1926 | JniValueType result; |
| 1927 | result.L = baseEnv(env)->PopLocalFrame(env, res); |
| 1928 | sc.Check(soa, false, "L", &result); |
| 1929 | return result.L; |
| 1930 | } |
| 1931 | return nullptr; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | static jobject NewGlobalRef(JNIEnv* env, jobject obj) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1935 | return NewRef(__FUNCTION__, env, obj, kGlobal); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1936 | } |
| 1937 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1938 | static jobject NewLocalRef(JNIEnv* env, jobject obj) { |
| 1939 | return NewRef(__FUNCTION__, env, obj, kLocal); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1943 | return NewRef(__FUNCTION__, env, obj, kWeakGlobal); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1944 | } |
| 1945 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1946 | static void DeleteGlobalRef(JNIEnv* env, jobject obj) { |
| 1947 | DeleteRef(__FUNCTION__, env, obj, kGlobal); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1948 | } |
| 1949 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1950 | static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) { |
| 1951 | DeleteRef(__FUNCTION__, env, obj, kWeakGlobal); |
| 1952 | } |
| 1953 | |
| 1954 | static void DeleteLocalRef(JNIEnv* env, jobject obj) { |
| 1955 | DeleteRef(__FUNCTION__, env, obj, kLocal); |
| 1956 | } |
| 1957 | |
| 1958 | static jint EnsureLocalCapacity(JNIEnv *env, jint capacity) { |
| 1959 | ScopedObjectAccess soa(env); |
| 1960 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 1961 | JniValueType args[2] = {{.E = env}, {.I = capacity}}; |
| 1962 | if (sc.Check(soa, true, "EI", args)) { |
| 1963 | JniValueType result; |
| 1964 | result.i = baseEnv(env)->EnsureLocalCapacity(env, capacity); |
| 1965 | if (sc.Check(soa, false, "i", &result)) { |
| 1966 | return result.i; |
| 1967 | } |
| 1968 | } |
| 1969 | return JNI_ERR; |
| 1970 | } |
| 1971 | |
| 1972 | static jboolean IsSameObject(JNIEnv* env, jobject ref1, jobject ref2) { |
| 1973 | ScopedObjectAccess soa(env); |
| 1974 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 1975 | JniValueType args[3] = {{.E = env}, {.L = ref1}, {.L = ref2}}; |
| 1976 | if (sc.Check(soa, true, "ELL", args)) { |
| 1977 | JniValueType result; |
| 1978 | result.b = baseEnv(env)->IsSameObject(env, ref1, ref2); |
| 1979 | if (sc.Check(soa, false, "b", &result)) { |
| 1980 | return result.b; |
| 1981 | } |
| 1982 | } |
| 1983 | return JNI_FALSE; |
| 1984 | } |
| 1985 | |
| 1986 | static jobject AllocObject(JNIEnv* env, jclass c) { |
| 1987 | ScopedObjectAccess soa(env); |
| 1988 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 1989 | JniValueType args[2] = {{.E = env}, {.c = c}}; |
| 1990 | if (sc.Check(soa, true, "Ec", args) && sc.CheckInstantiableNonArray(soa, c)) { |
| 1991 | JniValueType result; |
| 1992 | result.L = baseEnv(env)->AllocObject(env, c); |
| 1993 | if (sc.Check(soa, false, "L", &result)) { |
| 1994 | return result.L; |
| 1995 | } |
| 1996 | } |
| 1997 | return nullptr; |
| 1998 | } |
| 1999 | |
| 2000 | static jobject NewObjectV(JNIEnv* env, jclass c, jmethodID mid, va_list vargs) { |
| 2001 | ScopedObjectAccess soa(env); |
| 2002 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
Alex Light | 48ffe06 | 2015-08-19 15:23:23 -0700 | [diff] [blame] | 2003 | VarArgs rest(mid, vargs); |
| 2004 | JniValueType args[4] = {{.E = env}, {.c = c}, {.m = mid}, {.va = &rest}}; |
| 2005 | if (sc.Check(soa, true, "Ecm.", args) && sc.CheckInstantiableNonArray(soa, c) && |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame^] | 2006 | sc.CheckConstructor(mid)) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2007 | JniValueType result; |
| 2008 | result.L = baseEnv(env)->NewObjectV(env, c, mid, vargs); |
| 2009 | if (sc.Check(soa, false, "L", &result)) { |
| 2010 | return result.L; |
| 2011 | } |
| 2012 | } |
| 2013 | return nullptr; |
| 2014 | } |
| 2015 | |
| 2016 | static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) { |
| 2017 | va_list args; |
| 2018 | va_start(args, mid); |
| 2019 | jobject result = NewObjectV(env, c, mid, args); |
| 2020 | va_end(args); |
| 2021 | return result; |
| 2022 | } |
| 2023 | |
| 2024 | static jobject NewObjectA(JNIEnv* env, jclass c, jmethodID mid, jvalue* vargs) { |
| 2025 | ScopedObjectAccess soa(env); |
| 2026 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
Alex Light | 48ffe06 | 2015-08-19 15:23:23 -0700 | [diff] [blame] | 2027 | VarArgs rest(mid, vargs); |
| 2028 | JniValueType args[4] = {{.E = env}, {.c = c}, {.m = mid}, {.va = &rest}}; |
| 2029 | if (sc.Check(soa, true, "Ecm.", args) && sc.CheckInstantiableNonArray(soa, c) && |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame^] | 2030 | sc.CheckConstructor(mid)) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2031 | JniValueType result; |
| 2032 | result.L = baseEnv(env)->NewObjectA(env, c, mid, vargs); |
| 2033 | if (sc.Check(soa, false, "L", &result)) { |
| 2034 | return result.L; |
| 2035 | } |
| 2036 | } |
| 2037 | return nullptr; |
| 2038 | } |
| 2039 | |
| 2040 | static jclass GetObjectClass(JNIEnv* env, jobject obj) { |
| 2041 | ScopedObjectAccess soa(env); |
| 2042 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 2043 | JniValueType args[2] = {{.E = env}, {.L = obj}}; |
| 2044 | if (sc.Check(soa, true, "EL", args)) { |
| 2045 | JniValueType result; |
| 2046 | result.c = baseEnv(env)->GetObjectClass(env, obj); |
| 2047 | if (sc.Check(soa, false, "c", &result)) { |
| 2048 | return result.c; |
| 2049 | } |
| 2050 | } |
| 2051 | return nullptr; |
| 2052 | } |
| 2053 | |
| 2054 | static jboolean IsInstanceOf(JNIEnv* env, jobject obj, jclass c) { |
| 2055 | ScopedObjectAccess soa(env); |
| 2056 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 2057 | JniValueType args[3] = {{.E = env}, {.L = obj}, {.c = c}}; |
| 2058 | if (sc.Check(soa, true, "ELc", args)) { |
| 2059 | JniValueType result; |
| 2060 | result.b = baseEnv(env)->IsInstanceOf(env, obj, c); |
| 2061 | if (sc.Check(soa, false, "b", &result)) { |
| 2062 | return result.b; |
| 2063 | } |
| 2064 | } |
| 2065 | return JNI_FALSE; |
| 2066 | } |
| 2067 | |
| 2068 | static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) { |
| 2069 | return GetMethodIDInternal(__FUNCTION__, env, c, name, sig, false); |
| 2070 | } |
| 2071 | |
| 2072 | static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) { |
| 2073 | return GetMethodIDInternal(__FUNCTION__, env, c, name, sig, true); |
| 2074 | } |
| 2075 | |
| 2076 | static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) { |
| 2077 | return GetFieldIDInternal(__FUNCTION__, env, c, name, sig, false); |
| 2078 | } |
| 2079 | |
| 2080 | static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) { |
| 2081 | return GetFieldIDInternal(__FUNCTION__, env, c, name, sig, true); |
| 2082 | } |
| 2083 | |
| 2084 | #define FIELD_ACCESSORS(jtype, name, ptype, shorty) \ |
| 2085 | static jtype GetStatic##name##Field(JNIEnv* env, jclass c, jfieldID fid) { \ |
| 2086 | return GetField(__FUNCTION__, env, c, fid, true, ptype).shorty; \ |
| 2087 | } \ |
| 2088 | \ |
| 2089 | static jtype Get##name##Field(JNIEnv* env, jobject obj, jfieldID fid) { \ |
| 2090 | return GetField(__FUNCTION__, env, obj, fid, false, ptype).shorty; \ |
| 2091 | } \ |
| 2092 | \ |
| 2093 | static void SetStatic##name##Field(JNIEnv* env, jclass c, jfieldID fid, jtype v) { \ |
| 2094 | JniValueType value; \ |
| 2095 | value.shorty = v; \ |
| 2096 | SetField(__FUNCTION__, env, c, fid, true, ptype, value); \ |
| 2097 | } \ |
| 2098 | \ |
| 2099 | static void Set##name##Field(JNIEnv* env, jobject obj, jfieldID fid, jtype v) { \ |
| 2100 | JniValueType value; \ |
| 2101 | value.shorty = v; \ |
| 2102 | SetField(__FUNCTION__, env, obj, fid, false, ptype, value); \ |
| 2103 | } |
| 2104 | |
| 2105 | FIELD_ACCESSORS(jobject, Object, Primitive::kPrimNot, L) |
| 2106 | FIELD_ACCESSORS(jboolean, Boolean, Primitive::kPrimBoolean, Z) |
| 2107 | FIELD_ACCESSORS(jbyte, Byte, Primitive::kPrimByte, B) |
| 2108 | FIELD_ACCESSORS(jchar, Char, Primitive::kPrimChar, C) |
| 2109 | FIELD_ACCESSORS(jshort, Short, Primitive::kPrimShort, S) |
| 2110 | FIELD_ACCESSORS(jint, Int, Primitive::kPrimInt, I) |
| 2111 | FIELD_ACCESSORS(jlong, Long, Primitive::kPrimLong, J) |
| 2112 | FIELD_ACCESSORS(jfloat, Float, Primitive::kPrimFloat, F) |
| 2113 | FIELD_ACCESSORS(jdouble, Double, Primitive::kPrimDouble, D) |
| 2114 | #undef FIELD_ACCESSORS |
| 2115 | |
| 2116 | static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* vargs) { |
| 2117 | CallMethodA(__FUNCTION__, env, obj, nullptr, mid, vargs, Primitive::kPrimVoid, kVirtual); |
| 2118 | } |
| 2119 | |
| 2120 | static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass c, jmethodID mid, |
| 2121 | jvalue* vargs) { |
| 2122 | CallMethodA(__FUNCTION__, env, obj, c, mid, vargs, Primitive::kPrimVoid, kDirect); |
| 2123 | } |
| 2124 | |
| 2125 | static void CallStaticVoidMethodA(JNIEnv* env, jclass c, jmethodID mid, jvalue* vargs) { |
Marcus Oakland | 51be0a7 | 2014-09-24 15:23:06 +0100 | [diff] [blame] | 2126 | CallMethodA(__FUNCTION__, env, nullptr, c, mid, vargs, Primitive::kPrimVoid, kStatic); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2127 | } |
| 2128 | |
| 2129 | static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list vargs) { |
| 2130 | CallMethodV(__FUNCTION__, env, obj, nullptr, mid, vargs, Primitive::kPrimVoid, kVirtual); |
| 2131 | } |
| 2132 | |
| 2133 | static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass c, jmethodID mid, |
| 2134 | va_list vargs) { |
| 2135 | CallMethodV(__FUNCTION__, env, obj, c, mid, vargs, Primitive::kPrimVoid, kDirect); |
| 2136 | } |
| 2137 | |
| 2138 | static void CallStaticVoidMethodV(JNIEnv* env, jclass c, jmethodID mid, va_list vargs) { |
| 2139 | CallMethodV(__FUNCTION__, env, nullptr, c, mid, vargs, Primitive::kPrimVoid, kStatic); |
| 2140 | } |
| 2141 | |
| 2142 | static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) { |
| 2143 | va_list vargs; |
| 2144 | va_start(vargs, mid); |
| 2145 | CallMethodV(__FUNCTION__, env, obj, nullptr, mid, vargs, Primitive::kPrimVoid, kVirtual); |
| 2146 | va_end(vargs); |
| 2147 | } |
| 2148 | |
| 2149 | static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass c, jmethodID mid, ...) { |
| 2150 | va_list vargs; |
| 2151 | va_start(vargs, mid); |
| 2152 | CallMethodV(__FUNCTION__, env, obj, c, mid, vargs, Primitive::kPrimVoid, kDirect); |
| 2153 | va_end(vargs); |
| 2154 | } |
| 2155 | |
| 2156 | static void CallStaticVoidMethod(JNIEnv* env, jclass c, jmethodID mid, ...) { |
| 2157 | va_list vargs; |
| 2158 | va_start(vargs, mid); |
| 2159 | CallMethodV(__FUNCTION__, env, nullptr, c, mid, vargs, Primitive::kPrimVoid, kStatic); |
| 2160 | va_end(vargs); |
| 2161 | } |
| 2162 | |
| 2163 | #define CALL(rtype, name, ptype, shorty) \ |
| 2164 | static rtype Call##name##MethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* vargs) { \ |
| 2165 | return CallMethodA(__FUNCTION__, env, obj, nullptr, mid, vargs, ptype, kVirtual).shorty; \ |
| 2166 | } \ |
| 2167 | \ |
| 2168 | static rtype CallNonvirtual##name##MethodA(JNIEnv* env, jobject obj, jclass c, jmethodID mid, \ |
| 2169 | jvalue* vargs) { \ |
| 2170 | return CallMethodA(__FUNCTION__, env, obj, c, mid, vargs, ptype, kDirect).shorty; \ |
| 2171 | } \ |
| 2172 | \ |
| 2173 | static rtype CallStatic##name##MethodA(JNIEnv* env, jclass c, jmethodID mid, jvalue* vargs) { \ |
| 2174 | return CallMethodA(__FUNCTION__, env, nullptr, c, mid, vargs, ptype, kStatic).shorty; \ |
| 2175 | } \ |
| 2176 | \ |
| 2177 | static rtype Call##name##MethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list vargs) { \ |
| 2178 | return CallMethodV(__FUNCTION__, env, obj, nullptr, mid, vargs, ptype, kVirtual).shorty; \ |
| 2179 | } \ |
| 2180 | \ |
| 2181 | static rtype CallNonvirtual##name##MethodV(JNIEnv* env, jobject obj, jclass c, jmethodID mid, \ |
| 2182 | va_list vargs) { \ |
| 2183 | return CallMethodV(__FUNCTION__, env, obj, c, mid, vargs, ptype, kDirect).shorty; \ |
| 2184 | } \ |
| 2185 | \ |
| 2186 | static rtype CallStatic##name##MethodV(JNIEnv* env, jclass c, jmethodID mid, va_list vargs) { \ |
| 2187 | return CallMethodV(__FUNCTION__, env, nullptr, c, mid, vargs, ptype, kStatic).shorty; \ |
| 2188 | } \ |
| 2189 | \ |
| 2190 | static rtype Call##name##Method(JNIEnv* env, jobject obj, jmethodID mid, ...) { \ |
| 2191 | va_list vargs; \ |
| 2192 | va_start(vargs, mid); \ |
| 2193 | rtype result = \ |
| 2194 | CallMethodV(__FUNCTION__, env, obj, nullptr, mid, vargs, ptype, kVirtual).shorty; \ |
| 2195 | va_end(vargs); \ |
| 2196 | return result; \ |
| 2197 | } \ |
| 2198 | \ |
| 2199 | static rtype CallNonvirtual##name##Method(JNIEnv* env, jobject obj, jclass c, jmethodID mid, \ |
| 2200 | ...) { \ |
| 2201 | va_list vargs; \ |
| 2202 | va_start(vargs, mid); \ |
| 2203 | rtype result = \ |
| 2204 | CallMethodV(__FUNCTION__, env, obj, c, mid, vargs, ptype, kDirect).shorty; \ |
| 2205 | va_end(vargs); \ |
| 2206 | return result; \ |
| 2207 | } \ |
| 2208 | \ |
| 2209 | static rtype CallStatic##name##Method(JNIEnv* env, jclass c, jmethodID mid, ...) { \ |
| 2210 | va_list vargs; \ |
| 2211 | va_start(vargs, mid); \ |
| 2212 | rtype result = \ |
| 2213 | CallMethodV(__FUNCTION__, env, nullptr, c, mid, vargs, ptype, kStatic).shorty; \ |
| 2214 | va_end(vargs); \ |
| 2215 | return result; \ |
| 2216 | } |
| 2217 | |
| 2218 | CALL(jobject, Object, Primitive::kPrimNot, L) |
| 2219 | CALL(jboolean, Boolean, Primitive::kPrimBoolean, Z) |
| 2220 | CALL(jbyte, Byte, Primitive::kPrimByte, B) |
| 2221 | CALL(jchar, Char, Primitive::kPrimChar, C) |
| 2222 | CALL(jshort, Short, Primitive::kPrimShort, S) |
| 2223 | CALL(jint, Int, Primitive::kPrimInt, I) |
| 2224 | CALL(jlong, Long, Primitive::kPrimLong, J) |
| 2225 | CALL(jfloat, Float, Primitive::kPrimFloat, F) |
| 2226 | CALL(jdouble, Double, Primitive::kPrimDouble, D) |
| 2227 | #undef CALL |
| 2228 | |
| 2229 | static jstring NewString(JNIEnv* env, const jchar* unicode_chars, jsize len) { |
| 2230 | ScopedObjectAccess soa(env); |
| 2231 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 2232 | JniValueType args[3] = {{.E = env}, {.p = unicode_chars}, {.z = len}}; |
| 2233 | if (sc.Check(soa, true, "Epz", args)) { |
| 2234 | JniValueType result; |
| 2235 | result.s = baseEnv(env)->NewString(env, unicode_chars, len); |
| 2236 | if (sc.Check(soa, false, "s", &result)) { |
| 2237 | return result.s; |
| 2238 | } |
| 2239 | } |
| 2240 | return nullptr; |
| 2241 | } |
| 2242 | |
| 2243 | static jstring NewStringUTF(JNIEnv* env, const char* chars) { |
| 2244 | ScopedObjectAccess soa(env); |
| 2245 | ScopedCheck sc(kFlag_NullableUtf, __FUNCTION__); |
| 2246 | JniValueType args[2] = {{.E = env}, {.u = chars}}; |
| 2247 | if (sc.Check(soa, true, "Eu", args)) { |
| 2248 | JniValueType result; |
| 2249 | // TODO: stale? show pointer and truncate string. |
| 2250 | result.s = baseEnv(env)->NewStringUTF(env, chars); |
| 2251 | if (sc.Check(soa, false, "s", &result)) { |
| 2252 | return result.s; |
| 2253 | } |
| 2254 | } |
| 2255 | return nullptr; |
| 2256 | } |
| 2257 | |
| 2258 | static jsize GetStringLength(JNIEnv* env, jstring string) { |
| 2259 | ScopedObjectAccess soa(env); |
| 2260 | ScopedCheck sc(kFlag_CritOkay, __FUNCTION__); |
| 2261 | JniValueType args[2] = {{.E = env}, {.s = string}}; |
| 2262 | if (sc.Check(soa, true, "Es", args)) { |
| 2263 | JniValueType result; |
| 2264 | result.z = baseEnv(env)->GetStringLength(env, string); |
| 2265 | if (sc.Check(soa, false, "z", &result)) { |
| 2266 | return result.z; |
| 2267 | } |
| 2268 | } |
| 2269 | return JNI_ERR; |
| 2270 | } |
| 2271 | |
| 2272 | static jsize GetStringUTFLength(JNIEnv* env, jstring string) { |
| 2273 | ScopedObjectAccess soa(env); |
| 2274 | ScopedCheck sc(kFlag_CritOkay, __FUNCTION__); |
| 2275 | JniValueType args[2] = {{.E = env}, {.s = string}}; |
| 2276 | if (sc.Check(soa, true, "Es", args)) { |
| 2277 | JniValueType result; |
| 2278 | result.z = baseEnv(env)->GetStringUTFLength(env, string); |
| 2279 | if (sc.Check(soa, false, "z", &result)) { |
| 2280 | return result.z; |
| 2281 | } |
| 2282 | } |
| 2283 | return JNI_ERR; |
| 2284 | } |
| 2285 | |
| 2286 | static const jchar* GetStringChars(JNIEnv* env, jstring string, jboolean* is_copy) { |
| 2287 | return reinterpret_cast<const jchar*>(GetStringCharsInternal(__FUNCTION__, env, string, |
| 2288 | is_copy, false, false)); |
| 2289 | } |
| 2290 | |
| 2291 | static const char* GetStringUTFChars(JNIEnv* env, jstring string, jboolean* is_copy) { |
| 2292 | return reinterpret_cast<const char*>(GetStringCharsInternal(__FUNCTION__, env, string, |
| 2293 | is_copy, true, false)); |
| 2294 | } |
| 2295 | |
| 2296 | static const jchar* GetStringCritical(JNIEnv* env, jstring string, jboolean* is_copy) { |
| 2297 | return reinterpret_cast<const jchar*>(GetStringCharsInternal(__FUNCTION__, env, string, |
| 2298 | is_copy, false, true)); |
| 2299 | } |
| 2300 | |
| 2301 | static void ReleaseStringChars(JNIEnv* env, jstring string, const jchar* chars) { |
| 2302 | ReleaseStringCharsInternal(__FUNCTION__, env, string, chars, false, false); |
| 2303 | } |
| 2304 | |
| 2305 | static void ReleaseStringUTFChars(JNIEnv* env, jstring string, const char* utf) { |
| 2306 | ReleaseStringCharsInternal(__FUNCTION__, env, string, utf, true, false); |
| 2307 | } |
| 2308 | |
| 2309 | static void ReleaseStringCritical(JNIEnv* env, jstring string, const jchar* chars) { |
| 2310 | ReleaseStringCharsInternal(__FUNCTION__, env, string, chars, false, true); |
| 2311 | } |
| 2312 | |
| 2313 | static void GetStringRegion(JNIEnv* env, jstring string, jsize start, jsize len, jchar* buf) { |
| 2314 | ScopedObjectAccess soa(env); |
| 2315 | ScopedCheck sc(kFlag_CritOkay, __FUNCTION__); |
| 2316 | JniValueType args[5] = {{.E = env}, {.s = string}, {.z = start}, {.z = len}, {.p = buf}}; |
| 2317 | // Note: the start and len arguments are checked as 'I' rather than 'z' as invalid indices |
| 2318 | // result in ArrayIndexOutOfBoundsExceptions in the base implementation. |
| 2319 | if (sc.Check(soa, true, "EsIIp", args)) { |
| 2320 | baseEnv(env)->GetStringRegion(env, string, start, len, buf); |
| 2321 | JniValueType result; |
| 2322 | result.V = nullptr; |
| 2323 | sc.Check(soa, false, "V", &result); |
| 2324 | } |
| 2325 | } |
| 2326 | |
| 2327 | static void GetStringUTFRegion(JNIEnv* env, jstring string, jsize start, jsize len, char* buf) { |
| 2328 | ScopedObjectAccess soa(env); |
| 2329 | ScopedCheck sc(kFlag_CritOkay, __FUNCTION__); |
| 2330 | JniValueType args[5] = {{.E = env}, {.s = string}, {.z = start}, {.z = len}, {.p = buf}}; |
| 2331 | // Note: the start and len arguments are checked as 'I' rather than 'z' as invalid indices |
| 2332 | // result in ArrayIndexOutOfBoundsExceptions in the base implementation. |
| 2333 | if (sc.Check(soa, true, "EsIIp", args)) { |
| 2334 | baseEnv(env)->GetStringUTFRegion(env, string, start, len, buf); |
| 2335 | JniValueType result; |
| 2336 | result.V = nullptr; |
| 2337 | sc.Check(soa, false, "V", &result); |
| 2338 | } |
| 2339 | } |
| 2340 | |
| 2341 | static jsize GetArrayLength(JNIEnv* env, jarray array) { |
| 2342 | ScopedObjectAccess soa(env); |
| 2343 | ScopedCheck sc(kFlag_CritOkay, __FUNCTION__); |
| 2344 | JniValueType args[2] = {{.E = env}, {.a = array}}; |
| 2345 | if (sc.Check(soa, true, "Ea", args)) { |
| 2346 | JniValueType result; |
| 2347 | result.z = baseEnv(env)->GetArrayLength(env, array); |
| 2348 | if (sc.Check(soa, false, "z", &result)) { |
| 2349 | return result.z; |
| 2350 | } |
| 2351 | } |
| 2352 | return JNI_ERR; |
| 2353 | } |
| 2354 | |
| 2355 | static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_class, |
| 2356 | jobject initial_element) { |
| 2357 | ScopedObjectAccess soa(env); |
| 2358 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 2359 | JniValueType args[4] = |
| 2360 | {{.E = env}, {.z = length}, {.c = element_class}, {.L = initial_element}}; |
| 2361 | if (sc.Check(soa, true, "EzcL", args)) { |
| 2362 | JniValueType result; |
| 2363 | // Note: assignability tests of initial_element are done in the base implementation. |
| 2364 | result.a = baseEnv(env)->NewObjectArray(env, length, element_class, initial_element); |
| 2365 | if (sc.Check(soa, false, "a", &result)) { |
| 2366 | return down_cast<jobjectArray>(result.a); |
| 2367 | } |
| 2368 | } |
| 2369 | return nullptr; |
| 2370 | } |
| 2371 | |
| 2372 | static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index) { |
| 2373 | ScopedObjectAccess soa(env); |
| 2374 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 2375 | JniValueType args[3] = {{.E = env}, {.a = array}, {.z = index}}; |
| 2376 | if (sc.Check(soa, true, "Eaz", args)) { |
| 2377 | JniValueType result; |
| 2378 | result.L = baseEnv(env)->GetObjectArrayElement(env, array, index); |
| 2379 | if (sc.Check(soa, false, "L", &result)) { |
| 2380 | return result.L; |
| 2381 | } |
| 2382 | } |
| 2383 | return nullptr; |
| 2384 | } |
| 2385 | |
| 2386 | static void SetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index, jobject value) { |
| 2387 | ScopedObjectAccess soa(env); |
| 2388 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 2389 | JniValueType args[4] = {{.E = env}, {.a = array}, {.z = index}, {.L = value}}; |
| 2390 | // Note: the index arguments is checked as 'I' rather than 'z' as invalid indices result in |
| 2391 | // ArrayIndexOutOfBoundsExceptions in the base implementation. Similarly invalid stores result |
| 2392 | // in ArrayStoreExceptions. |
| 2393 | if (sc.Check(soa, true, "EaIL", args)) { |
| 2394 | baseEnv(env)->SetObjectArrayElement(env, array, index, value); |
| 2395 | JniValueType result; |
| 2396 | result.V = nullptr; |
| 2397 | sc.Check(soa, false, "V", &result); |
| 2398 | } |
| 2399 | } |
| 2400 | |
| 2401 | static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) { |
| 2402 | return down_cast<jbooleanArray>(NewPrimitiveArray(__FUNCTION__, env, length, |
| 2403 | Primitive::kPrimBoolean)); |
| 2404 | } |
| 2405 | |
| 2406 | static jbyteArray NewByteArray(JNIEnv* env, jsize length) { |
| 2407 | return down_cast<jbyteArray>(NewPrimitiveArray(__FUNCTION__, env, length, |
| 2408 | Primitive::kPrimByte)); |
| 2409 | } |
| 2410 | |
| 2411 | static jcharArray NewCharArray(JNIEnv* env, jsize length) { |
| 2412 | return down_cast<jcharArray>(NewPrimitiveArray(__FUNCTION__, env, length, |
| 2413 | Primitive::kPrimChar)); |
| 2414 | } |
| 2415 | |
| 2416 | static jshortArray NewShortArray(JNIEnv* env, jsize length) { |
| 2417 | return down_cast<jshortArray>(NewPrimitiveArray(__FUNCTION__, env, length, |
| 2418 | Primitive::kPrimShort)); |
| 2419 | } |
| 2420 | |
| 2421 | static jintArray NewIntArray(JNIEnv* env, jsize length) { |
| 2422 | return down_cast<jintArray>(NewPrimitiveArray(__FUNCTION__, env, length, Primitive::kPrimInt)); |
| 2423 | } |
| 2424 | |
| 2425 | static jlongArray NewLongArray(JNIEnv* env, jsize length) { |
| 2426 | return down_cast<jlongArray>(NewPrimitiveArray(__FUNCTION__, env, length, |
| 2427 | Primitive::kPrimLong)); |
| 2428 | } |
| 2429 | |
| 2430 | static jfloatArray NewFloatArray(JNIEnv* env, jsize length) { |
| 2431 | return down_cast<jfloatArray>(NewPrimitiveArray(__FUNCTION__, env, length, |
| 2432 | Primitive::kPrimFloat)); |
| 2433 | } |
| 2434 | |
| 2435 | static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) { |
| 2436 | return down_cast<jdoubleArray>(NewPrimitiveArray(__FUNCTION__, env, length, |
| 2437 | Primitive::kPrimDouble)); |
| 2438 | } |
| 2439 | |
Chih-Hung Hsieh | fba3997 | 2016-05-11 11:26:48 -0700 | [diff] [blame] | 2440 | // NOLINT added to avoid wrong warning/fix from clang-tidy. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2441 | #define PRIMITIVE_ARRAY_FUNCTIONS(ctype, name, ptype) \ |
Chih-Hung Hsieh | fba3997 | 2016-05-11 11:26:48 -0700 | [diff] [blame] | 2442 | static ctype* Get##name##ArrayElements(JNIEnv* env, ctype##Array array, jboolean* is_copy) { /* NOLINT */ \ |
| 2443 | return reinterpret_cast<ctype*>( /* NOLINT */ \ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2444 | GetPrimitiveArrayElements(__FUNCTION__, ptype, env, array, is_copy)); \ |
| 2445 | } \ |
| 2446 | \ |
Chih-Hung Hsieh | fba3997 | 2016-05-11 11:26:48 -0700 | [diff] [blame] | 2447 | static void Release##name##ArrayElements(JNIEnv* env, ctype##Array array, ctype* elems, /* NOLINT */ \ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2448 | jint mode) { \ |
| 2449 | ReleasePrimitiveArrayElements(__FUNCTION__, ptype, env, array, elems, mode); \ |
| 2450 | } \ |
| 2451 | \ |
| 2452 | static void Get##name##ArrayRegion(JNIEnv* env, ctype##Array array, jsize start, jsize len, \ |
Chih-Hung Hsieh | fba3997 | 2016-05-11 11:26:48 -0700 | [diff] [blame] | 2453 | ctype* buf) { /* NOLINT */ \ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2454 | GetPrimitiveArrayRegion(__FUNCTION__, ptype, env, array, start, len, buf); \ |
| 2455 | } \ |
| 2456 | \ |
| 2457 | static void Set##name##ArrayRegion(JNIEnv* env, ctype##Array array, jsize start, jsize len, \ |
| 2458 | const ctype* buf) { \ |
| 2459 | SetPrimitiveArrayRegion(__FUNCTION__, ptype, env, array, start, len, buf); \ |
| 2460 | } |
| 2461 | |
| 2462 | PRIMITIVE_ARRAY_FUNCTIONS(jboolean, Boolean, Primitive::kPrimBoolean) |
| 2463 | PRIMITIVE_ARRAY_FUNCTIONS(jbyte, Byte, Primitive::kPrimByte) |
| 2464 | PRIMITIVE_ARRAY_FUNCTIONS(jchar, Char, Primitive::kPrimChar) |
| 2465 | PRIMITIVE_ARRAY_FUNCTIONS(jshort, Short, Primitive::kPrimShort) |
| 2466 | PRIMITIVE_ARRAY_FUNCTIONS(jint, Int, Primitive::kPrimInt) |
| 2467 | PRIMITIVE_ARRAY_FUNCTIONS(jlong, Long, Primitive::kPrimLong) |
| 2468 | PRIMITIVE_ARRAY_FUNCTIONS(jfloat, Float, Primitive::kPrimFloat) |
| 2469 | PRIMITIVE_ARRAY_FUNCTIONS(jdouble, Double, Primitive::kPrimDouble) |
| 2470 | #undef PRIMITIVE_ARRAY_FUNCTIONS |
| 2471 | |
| 2472 | static jint MonitorEnter(JNIEnv* env, jobject obj) { |
| 2473 | ScopedObjectAccess soa(env); |
| 2474 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 2475 | JniValueType args[2] = {{.E = env}, {.L = obj}}; |
| 2476 | if (sc.Check(soa, true, "EL", args)) { |
Andreas Gampe | 5f4a09a | 2015-09-28 13:16:33 -0700 | [diff] [blame] | 2477 | if (obj != nullptr) { |
| 2478 | down_cast<JNIEnvExt*>(env)->RecordMonitorEnter(obj); |
| 2479 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2480 | JniValueType result; |
| 2481 | result.i = baseEnv(env)->MonitorEnter(env, obj); |
| 2482 | if (sc.Check(soa, false, "i", &result)) { |
| 2483 | return result.i; |
| 2484 | } |
| 2485 | } |
| 2486 | return JNI_ERR; |
| 2487 | } |
| 2488 | |
| 2489 | static jint MonitorExit(JNIEnv* env, jobject obj) { |
| 2490 | ScopedObjectAccess soa(env); |
| 2491 | ScopedCheck sc(kFlag_ExcepOkay, __FUNCTION__); |
| 2492 | JniValueType args[2] = {{.E = env}, {.L = obj}}; |
| 2493 | if (sc.Check(soa, true, "EL", args)) { |
Andreas Gampe | 5f4a09a | 2015-09-28 13:16:33 -0700 | [diff] [blame] | 2494 | if (obj != nullptr) { |
| 2495 | down_cast<JNIEnvExt*>(env)->CheckMonitorRelease(obj); |
| 2496 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2497 | JniValueType result; |
| 2498 | result.i = baseEnv(env)->MonitorExit(env, obj); |
| 2499 | if (sc.Check(soa, false, "i", &result)) { |
| 2500 | return result.i; |
| 2501 | } |
| 2502 | } |
| 2503 | return JNI_ERR; |
| 2504 | } |
| 2505 | |
| 2506 | static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) { |
| 2507 | ScopedObjectAccess soa(env); |
| 2508 | ScopedCheck sc(kFlag_CritGet, __FUNCTION__); |
| 2509 | JniValueType args[3] = {{.E = env}, {.a = array}, {.p = is_copy}}; |
| 2510 | if (sc.Check(soa, true, "Eap", args)) { |
| 2511 | JniValueType result; |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 2512 | void* ptr = baseEnv(env)->GetPrimitiveArrayCritical(env, array, is_copy); |
| 2513 | if (ptr != nullptr && soa.ForceCopy()) { |
| 2514 | ptr = GuardedCopy::CreateGuardedPACopy(env, array, is_copy, ptr); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2515 | } |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 2516 | result.p = ptr; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2517 | if (sc.Check(soa, false, "p", &result)) { |
| 2518 | return const_cast<void*>(result.p); |
| 2519 | } |
| 2520 | } |
| 2521 | return nullptr; |
| 2522 | } |
| 2523 | |
| 2524 | static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* carray, jint mode) { |
| 2525 | ScopedObjectAccess soa(env); |
| 2526 | ScopedCheck sc(kFlag_CritRelease | kFlag_ExcepOkay, __FUNCTION__); |
| 2527 | sc.CheckNonNull(carray); |
| 2528 | JniValueType args[4] = {{.E = env}, {.a = array}, {.p = carray}, {.r = mode}}; |
| 2529 | if (sc.Check(soa, true, "Eapr", args)) { |
| 2530 | if (soa.ForceCopy()) { |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 2531 | carray = GuardedCopy::ReleaseGuardedPACopy(__FUNCTION__, env, array, carray, mode); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2532 | } |
| 2533 | baseEnv(env)->ReleasePrimitiveArrayCritical(env, array, carray, mode); |
| 2534 | JniValueType result; |
| 2535 | result.V = nullptr; |
| 2536 | sc.Check(soa, false, "V", &result); |
| 2537 | } |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 2538 | } |
| 2539 | |
| 2540 | static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2541 | ScopedObjectAccess soa(env); |
| 2542 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 2543 | JniValueType args[3] = {{.E = env}, {.p = address}, {.J = capacity}}; |
| 2544 | if (sc.Check(soa, true, "EpJ", args)) { |
| 2545 | JniValueType result; |
| 2546 | // Note: the validity of address and capacity are checked in the base implementation. |
| 2547 | result.L = baseEnv(env)->NewDirectByteBuffer(env, address, capacity); |
| 2548 | if (sc.Check(soa, false, "L", &result)) { |
| 2549 | return result.L; |
| 2550 | } |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 2551 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2552 | return nullptr; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 2553 | } |
| 2554 | |
| 2555 | static void* GetDirectBufferAddress(JNIEnv* env, jobject buf) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2556 | ScopedObjectAccess soa(env); |
| 2557 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 2558 | JniValueType args[2] = {{.E = env}, {.L = buf}}; |
| 2559 | if (sc.Check(soa, true, "EL", args)) { |
| 2560 | JniValueType result; |
| 2561 | // Note: this is implemented in the base environment by a GetLongField which will sanity |
| 2562 | // check the type of buf in GetLongField above. |
| 2563 | result.p = baseEnv(env)->GetDirectBufferAddress(env, buf); |
| 2564 | if (sc.Check(soa, false, "p", &result)) { |
| 2565 | return const_cast<void*>(result.p); |
| 2566 | } |
| 2567 | } |
| 2568 | return nullptr; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 2569 | } |
| 2570 | |
| 2571 | static jlong GetDirectBufferCapacity(JNIEnv* env, jobject buf) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2572 | ScopedObjectAccess soa(env); |
| 2573 | ScopedCheck sc(kFlag_Default, __FUNCTION__); |
| 2574 | JniValueType args[2] = {{.E = env}, {.L = buf}}; |
| 2575 | if (sc.Check(soa, true, "EL", args)) { |
| 2576 | JniValueType result; |
| 2577 | // Note: this is implemented in the base environment by a GetIntField which will sanity |
| 2578 | // check the type of buf in GetIntField above. |
| 2579 | result.J = baseEnv(env)->GetDirectBufferCapacity(env, buf); |
| 2580 | if (sc.Check(soa, false, "J", &result)) { |
| 2581 | return result.J; |
| 2582 | } |
| 2583 | } |
| 2584 | return JNI_ERR; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | private: |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2588 | static JavaVMExt* GetJavaVMExt(JNIEnv* env) { |
| 2589 | return reinterpret_cast<JNIEnvExt*>(env)->vm; |
| 2590 | } |
| 2591 | |
| 2592 | static const JNINativeInterface* baseEnv(JNIEnv* env) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 2593 | return reinterpret_cast<JNIEnvExt*>(env)->unchecked_functions; |
| 2594 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2595 | |
| 2596 | static jobject NewRef(const char* function_name, JNIEnv* env, jobject obj, IndirectRefKind kind) { |
| 2597 | ScopedObjectAccess soa(env); |
| 2598 | ScopedCheck sc(kFlag_Default, function_name); |
| 2599 | JniValueType args[2] = {{.E = env}, {.L = obj}}; |
| 2600 | if (sc.Check(soa, true, "EL", args)) { |
| 2601 | JniValueType result; |
| 2602 | switch (kind) { |
| 2603 | case kGlobal: |
| 2604 | result.L = baseEnv(env)->NewGlobalRef(env, obj); |
| 2605 | break; |
| 2606 | case kLocal: |
| 2607 | result.L = baseEnv(env)->NewLocalRef(env, obj); |
| 2608 | break; |
| 2609 | case kWeakGlobal: |
| 2610 | result.L = baseEnv(env)->NewWeakGlobalRef(env, obj); |
| 2611 | break; |
| 2612 | default: |
| 2613 | LOG(FATAL) << "Unexpected reference kind: " << kind; |
| 2614 | } |
| 2615 | if (sc.Check(soa, false, "L", &result)) { |
| 2616 | DCHECK_EQ(IsSameObject(env, obj, result.L), JNI_TRUE); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2617 | DCHECK(sc.CheckReferenceKind(kind, soa.Self(), result.L)); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2618 | return result.L; |
| 2619 | } |
| 2620 | } |
| 2621 | return nullptr; |
| 2622 | } |
| 2623 | |
| 2624 | static void DeleteRef(const char* function_name, JNIEnv* env, jobject obj, IndirectRefKind kind) { |
| 2625 | ScopedObjectAccess soa(env); |
| 2626 | ScopedCheck sc(kFlag_ExcepOkay, function_name); |
| 2627 | JniValueType args[2] = {{.E = env}, {.L = obj}}; |
| 2628 | sc.Check(soa, true, "EL", args); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2629 | if (sc.CheckReferenceKind(kind, soa.Self(), obj)) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2630 | JniValueType result; |
| 2631 | switch (kind) { |
| 2632 | case kGlobal: |
| 2633 | baseEnv(env)->DeleteGlobalRef(env, obj); |
| 2634 | break; |
| 2635 | case kLocal: |
| 2636 | baseEnv(env)->DeleteLocalRef(env, obj); |
| 2637 | break; |
| 2638 | case kWeakGlobal: |
| 2639 | baseEnv(env)->DeleteWeakGlobalRef(env, obj); |
| 2640 | break; |
| 2641 | default: |
| 2642 | LOG(FATAL) << "Unexpected reference kind: " << kind; |
| 2643 | } |
| 2644 | result.V = nullptr; |
| 2645 | sc.Check(soa, false, "V", &result); |
| 2646 | } |
| 2647 | } |
| 2648 | |
| 2649 | static jmethodID GetMethodIDInternal(const char* function_name, JNIEnv* env, jclass c, |
| 2650 | const char* name, const char* sig, bool is_static) { |
| 2651 | ScopedObjectAccess soa(env); |
| 2652 | ScopedCheck sc(kFlag_Default, function_name); |
| 2653 | JniValueType args[4] = {{.E = env}, {.c = c}, {.u = name}, {.u = sig}}; |
| 2654 | if (sc.Check(soa, true, "Ecuu", args)) { |
| 2655 | JniValueType result; |
| 2656 | if (is_static) { |
| 2657 | result.m = baseEnv(env)->GetStaticMethodID(env, c, name, sig); |
| 2658 | } else { |
| 2659 | result.m = baseEnv(env)->GetMethodID(env, c, name, sig); |
| 2660 | } |
| 2661 | if (sc.Check(soa, false, "m", &result)) { |
| 2662 | return result.m; |
| 2663 | } |
| 2664 | } |
| 2665 | return nullptr; |
| 2666 | } |
| 2667 | |
| 2668 | static jfieldID GetFieldIDInternal(const char* function_name, JNIEnv* env, jclass c, |
| 2669 | const char* name, const char* sig, bool is_static) { |
| 2670 | ScopedObjectAccess soa(env); |
| 2671 | ScopedCheck sc(kFlag_Default, function_name); |
| 2672 | JniValueType args[4] = {{.E = env}, {.c = c}, {.u = name}, {.u = sig}}; |
| 2673 | if (sc.Check(soa, true, "Ecuu", args)) { |
| 2674 | JniValueType result; |
| 2675 | if (is_static) { |
| 2676 | result.f = baseEnv(env)->GetStaticFieldID(env, c, name, sig); |
| 2677 | } else { |
| 2678 | result.f = baseEnv(env)->GetFieldID(env, c, name, sig); |
| 2679 | } |
| 2680 | if (sc.Check(soa, false, "f", &result)) { |
| 2681 | return result.f; |
| 2682 | } |
| 2683 | } |
| 2684 | return nullptr; |
| 2685 | } |
| 2686 | |
| 2687 | static JniValueType GetField(const char* function_name, JNIEnv* env, jobject obj, jfieldID fid, |
| 2688 | bool is_static, Primitive::Type type) { |
| 2689 | ScopedObjectAccess soa(env); |
| 2690 | ScopedCheck sc(kFlag_Default, function_name); |
| 2691 | JniValueType args[3] = {{.E = env}, {.L = obj}, {.f = fid}}; |
| 2692 | JniValueType result; |
| 2693 | if (sc.Check(soa, true, is_static ? "Ecf" : "ELf", args) && |
| 2694 | sc.CheckFieldAccess(soa, obj, fid, is_static, type)) { |
| 2695 | const char* result_check = nullptr; |
| 2696 | switch (type) { |
| 2697 | case Primitive::kPrimNot: |
| 2698 | if (is_static) { |
| 2699 | result.L = baseEnv(env)->GetStaticObjectField(env, down_cast<jclass>(obj), fid); |
| 2700 | } else { |
| 2701 | result.L = baseEnv(env)->GetObjectField(env, obj, fid); |
| 2702 | } |
| 2703 | result_check = "L"; |
| 2704 | break; |
| 2705 | case Primitive::kPrimBoolean: |
| 2706 | if (is_static) { |
| 2707 | result.Z = baseEnv(env)->GetStaticBooleanField(env, down_cast<jclass>(obj), fid); |
| 2708 | } else { |
| 2709 | result.Z = baseEnv(env)->GetBooleanField(env, obj, fid); |
| 2710 | } |
| 2711 | result_check = "Z"; |
| 2712 | break; |
| 2713 | case Primitive::kPrimByte: |
| 2714 | if (is_static) { |
| 2715 | result.B = baseEnv(env)->GetStaticByteField(env, down_cast<jclass>(obj), fid); |
| 2716 | } else { |
| 2717 | result.B = baseEnv(env)->GetByteField(env, obj, fid); |
| 2718 | } |
| 2719 | result_check = "B"; |
| 2720 | break; |
| 2721 | case Primitive::kPrimChar: |
| 2722 | if (is_static) { |
| 2723 | result.C = baseEnv(env)->GetStaticCharField(env, down_cast<jclass>(obj), fid); |
| 2724 | } else { |
| 2725 | result.C = baseEnv(env)->GetCharField(env, obj, fid); |
| 2726 | } |
| 2727 | result_check = "C"; |
| 2728 | break; |
| 2729 | case Primitive::kPrimShort: |
| 2730 | if (is_static) { |
| 2731 | result.S = baseEnv(env)->GetStaticShortField(env, down_cast<jclass>(obj), fid); |
| 2732 | } else { |
| 2733 | result.S = baseEnv(env)->GetShortField(env, obj, fid); |
| 2734 | } |
| 2735 | result_check = "S"; |
| 2736 | break; |
| 2737 | case Primitive::kPrimInt: |
| 2738 | if (is_static) { |
| 2739 | result.I = baseEnv(env)->GetStaticIntField(env, down_cast<jclass>(obj), fid); |
| 2740 | } else { |
| 2741 | result.I = baseEnv(env)->GetIntField(env, obj, fid); |
| 2742 | } |
| 2743 | result_check = "I"; |
| 2744 | break; |
| 2745 | case Primitive::kPrimLong: |
| 2746 | if (is_static) { |
| 2747 | result.J = baseEnv(env)->GetStaticLongField(env, down_cast<jclass>(obj), fid); |
| 2748 | } else { |
| 2749 | result.J = baseEnv(env)->GetLongField(env, obj, fid); |
| 2750 | } |
| 2751 | result_check = "J"; |
| 2752 | break; |
| 2753 | case Primitive::kPrimFloat: |
| 2754 | if (is_static) { |
| 2755 | result.F = baseEnv(env)->GetStaticFloatField(env, down_cast<jclass>(obj), fid); |
| 2756 | } else { |
| 2757 | result.F = baseEnv(env)->GetFloatField(env, obj, fid); |
| 2758 | } |
| 2759 | result_check = "F"; |
| 2760 | break; |
| 2761 | case Primitive::kPrimDouble: |
| 2762 | if (is_static) { |
| 2763 | result.D = baseEnv(env)->GetStaticDoubleField(env, down_cast<jclass>(obj), fid); |
| 2764 | } else { |
| 2765 | result.D = baseEnv(env)->GetDoubleField(env, obj, fid); |
| 2766 | } |
| 2767 | result_check = "D"; |
| 2768 | break; |
| 2769 | case Primitive::kPrimVoid: |
| 2770 | LOG(FATAL) << "Unexpected type: " << type; |
| 2771 | break; |
| 2772 | } |
| 2773 | if (sc.Check(soa, false, result_check, &result)) { |
| 2774 | return result; |
| 2775 | } |
| 2776 | } |
| 2777 | result.J = 0; |
| 2778 | return result; |
| 2779 | } |
| 2780 | |
| 2781 | static void SetField(const char* function_name, JNIEnv* env, jobject obj, jfieldID fid, |
| 2782 | bool is_static, Primitive::Type type, JniValueType value) { |
| 2783 | ScopedObjectAccess soa(env); |
| 2784 | ScopedCheck sc(kFlag_Default, function_name); |
| 2785 | JniValueType args[4] = {{.E = env}, {.L = obj}, {.f = fid}, value}; |
| 2786 | char sig[5] = { 'E', is_static ? 'c' : 'L', 'f', |
| 2787 | type == Primitive::kPrimNot ? 'L' : Primitive::Descriptor(type)[0], '\0'}; |
| 2788 | if (sc.Check(soa, true, sig, args) && |
| 2789 | sc.CheckFieldAccess(soa, obj, fid, is_static, type)) { |
| 2790 | switch (type) { |
| 2791 | case Primitive::kPrimNot: |
| 2792 | if (is_static) { |
| 2793 | baseEnv(env)->SetStaticObjectField(env, down_cast<jclass>(obj), fid, value.L); |
| 2794 | } else { |
| 2795 | baseEnv(env)->SetObjectField(env, obj, fid, value.L); |
| 2796 | } |
| 2797 | break; |
| 2798 | case Primitive::kPrimBoolean: |
| 2799 | if (is_static) { |
| 2800 | baseEnv(env)->SetStaticBooleanField(env, down_cast<jclass>(obj), fid, value.Z); |
| 2801 | } else { |
| 2802 | baseEnv(env)->SetBooleanField(env, obj, fid, value.Z); |
| 2803 | } |
| 2804 | break; |
| 2805 | case Primitive::kPrimByte: |
| 2806 | if (is_static) { |
| 2807 | baseEnv(env)->SetStaticByteField(env, down_cast<jclass>(obj), fid, value.B); |
| 2808 | } else { |
| 2809 | baseEnv(env)->SetByteField(env, obj, fid, value.B); |
| 2810 | } |
| 2811 | break; |
| 2812 | case Primitive::kPrimChar: |
| 2813 | if (is_static) { |
| 2814 | baseEnv(env)->SetStaticCharField(env, down_cast<jclass>(obj), fid, value.C); |
| 2815 | } else { |
| 2816 | baseEnv(env)->SetCharField(env, obj, fid, value.C); |
| 2817 | } |
| 2818 | break; |
| 2819 | case Primitive::kPrimShort: |
| 2820 | if (is_static) { |
| 2821 | baseEnv(env)->SetStaticShortField(env, down_cast<jclass>(obj), fid, value.S); |
| 2822 | } else { |
| 2823 | baseEnv(env)->SetShortField(env, obj, fid, value.S); |
| 2824 | } |
| 2825 | break; |
| 2826 | case Primitive::kPrimInt: |
| 2827 | if (is_static) { |
| 2828 | baseEnv(env)->SetStaticIntField(env, down_cast<jclass>(obj), fid, value.I); |
| 2829 | } else { |
| 2830 | baseEnv(env)->SetIntField(env, obj, fid, value.I); |
| 2831 | } |
| 2832 | break; |
| 2833 | case Primitive::kPrimLong: |
| 2834 | if (is_static) { |
| 2835 | baseEnv(env)->SetStaticLongField(env, down_cast<jclass>(obj), fid, value.J); |
| 2836 | } else { |
| 2837 | baseEnv(env)->SetLongField(env, obj, fid, value.J); |
| 2838 | } |
| 2839 | break; |
| 2840 | case Primitive::kPrimFloat: |
| 2841 | if (is_static) { |
| 2842 | baseEnv(env)->SetStaticFloatField(env, down_cast<jclass>(obj), fid, value.F); |
| 2843 | } else { |
| 2844 | baseEnv(env)->SetFloatField(env, obj, fid, value.F); |
| 2845 | } |
| 2846 | break; |
| 2847 | case Primitive::kPrimDouble: |
| 2848 | if (is_static) { |
| 2849 | baseEnv(env)->SetStaticDoubleField(env, down_cast<jclass>(obj), fid, value.D); |
| 2850 | } else { |
| 2851 | baseEnv(env)->SetDoubleField(env, obj, fid, value.D); |
| 2852 | } |
| 2853 | break; |
| 2854 | case Primitive::kPrimVoid: |
| 2855 | LOG(FATAL) << "Unexpected type: " << type; |
| 2856 | break; |
| 2857 | } |
| 2858 | JniValueType result; |
| 2859 | result.V = nullptr; |
| 2860 | sc.Check(soa, false, "V", &result); |
| 2861 | } |
| 2862 | } |
| 2863 | |
| 2864 | static bool CheckCallArgs(ScopedObjectAccess& soa, ScopedCheck& sc, JNIEnv* env, jobject obj, |
Alex Light | 48ffe06 | 2015-08-19 15:23:23 -0700 | [diff] [blame] | 2865 | jclass c, jmethodID mid, InvokeType invoke, const VarArgs* vargs) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2866 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2867 | bool checked; |
| 2868 | switch (invoke) { |
| 2869 | case kVirtual: { |
| 2870 | DCHECK(c == nullptr); |
Alex Light | 48ffe06 | 2015-08-19 15:23:23 -0700 | [diff] [blame] | 2871 | JniValueType args[4] = {{.E = env}, {.L = obj}, {.m = mid}, {.va = vargs}}; |
| 2872 | checked = sc.Check(soa, true, "ELm.", args); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2873 | break; |
| 2874 | } |
| 2875 | case kDirect: { |
Alex Light | 48ffe06 | 2015-08-19 15:23:23 -0700 | [diff] [blame] | 2876 | JniValueType args[5] = {{.E = env}, {.L = obj}, {.c = c}, {.m = mid}, {.va = vargs}}; |
| 2877 | checked = sc.Check(soa, true, "ELcm.", args); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2878 | break; |
| 2879 | } |
| 2880 | case kStatic: { |
| 2881 | DCHECK(obj == nullptr); |
Alex Light | 48ffe06 | 2015-08-19 15:23:23 -0700 | [diff] [blame] | 2882 | JniValueType args[4] = {{.E = env}, {.c = c}, {.m = mid}, {.va = vargs}}; |
| 2883 | checked = sc.Check(soa, true, "Ecm.", args); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2884 | break; |
| 2885 | } |
| 2886 | default: |
| 2887 | LOG(FATAL) << "Unexpected invoke: " << invoke; |
| 2888 | checked = false; |
| 2889 | break; |
| 2890 | } |
| 2891 | return checked; |
| 2892 | } |
| 2893 | |
| 2894 | static JniValueType CallMethodA(const char* function_name, JNIEnv* env, jobject obj, jclass c, |
| 2895 | jmethodID mid, jvalue* vargs, Primitive::Type type, |
| 2896 | InvokeType invoke) { |
| 2897 | ScopedObjectAccess soa(env); |
| 2898 | ScopedCheck sc(kFlag_Default, function_name); |
| 2899 | JniValueType result; |
Alex Light | 48ffe06 | 2015-08-19 15:23:23 -0700 | [diff] [blame] | 2900 | VarArgs rest(mid, vargs); |
| 2901 | if (CheckCallArgs(soa, sc, env, obj, c, mid, invoke, &rest) && |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2902 | sc.CheckMethodAndSig(soa, obj, c, mid, type, invoke)) { |
| 2903 | const char* result_check; |
| 2904 | switch (type) { |
| 2905 | case Primitive::kPrimNot: |
| 2906 | result_check = "L"; |
| 2907 | switch (invoke) { |
| 2908 | case kVirtual: |
| 2909 | result.L = baseEnv(env)->CallObjectMethodA(env, obj, mid, vargs); |
| 2910 | break; |
| 2911 | case kDirect: |
| 2912 | result.L = baseEnv(env)->CallNonvirtualObjectMethodA(env, obj, c, mid, vargs); |
| 2913 | break; |
| 2914 | case kStatic: |
| 2915 | result.L = baseEnv(env)->CallStaticObjectMethodA(env, c, mid, vargs); |
| 2916 | break; |
| 2917 | default: |
| 2918 | break; |
| 2919 | } |
| 2920 | break; |
| 2921 | case Primitive::kPrimBoolean: |
| 2922 | result_check = "Z"; |
| 2923 | switch (invoke) { |
| 2924 | case kVirtual: |
| 2925 | result.Z = baseEnv(env)->CallBooleanMethodA(env, obj, mid, vargs); |
| 2926 | break; |
| 2927 | case kDirect: |
| 2928 | result.Z = baseEnv(env)->CallNonvirtualBooleanMethodA(env, obj, c, mid, vargs); |
| 2929 | break; |
| 2930 | case kStatic: |
| 2931 | result.Z = baseEnv(env)->CallStaticBooleanMethodA(env, c, mid, vargs); |
| 2932 | break; |
| 2933 | default: |
| 2934 | break; |
| 2935 | } |
| 2936 | break; |
| 2937 | case Primitive::kPrimByte: |
| 2938 | result_check = "B"; |
| 2939 | switch (invoke) { |
| 2940 | case kVirtual: |
| 2941 | result.B = baseEnv(env)->CallByteMethodA(env, obj, mid, vargs); |
| 2942 | break; |
| 2943 | case kDirect: |
| 2944 | result.B = baseEnv(env)->CallNonvirtualByteMethodA(env, obj, c, mid, vargs); |
| 2945 | break; |
| 2946 | case kStatic: |
| 2947 | result.B = baseEnv(env)->CallStaticByteMethodA(env, c, mid, vargs); |
| 2948 | break; |
| 2949 | default: |
| 2950 | break; |
| 2951 | } |
| 2952 | break; |
| 2953 | case Primitive::kPrimChar: |
| 2954 | result_check = "C"; |
| 2955 | switch (invoke) { |
| 2956 | case kVirtual: |
| 2957 | result.C = baseEnv(env)->CallCharMethodA(env, obj, mid, vargs); |
| 2958 | break; |
| 2959 | case kDirect: |
| 2960 | result.C = baseEnv(env)->CallNonvirtualCharMethodA(env, obj, c, mid, vargs); |
| 2961 | break; |
| 2962 | case kStatic: |
| 2963 | result.C = baseEnv(env)->CallStaticCharMethodA(env, c, mid, vargs); |
| 2964 | break; |
| 2965 | default: |
| 2966 | break; |
| 2967 | } |
| 2968 | break; |
| 2969 | case Primitive::kPrimShort: |
| 2970 | result_check = "S"; |
| 2971 | switch (invoke) { |
| 2972 | case kVirtual: |
| 2973 | result.S = baseEnv(env)->CallShortMethodA(env, obj, mid, vargs); |
| 2974 | break; |
| 2975 | case kDirect: |
| 2976 | result.S = baseEnv(env)->CallNonvirtualShortMethodA(env, obj, c, mid, vargs); |
| 2977 | break; |
| 2978 | case kStatic: |
| 2979 | result.S = baseEnv(env)->CallStaticShortMethodA(env, c, mid, vargs); |
| 2980 | break; |
| 2981 | default: |
| 2982 | break; |
| 2983 | } |
| 2984 | break; |
| 2985 | case Primitive::kPrimInt: |
| 2986 | result_check = "I"; |
| 2987 | switch (invoke) { |
| 2988 | case kVirtual: |
| 2989 | result.I = baseEnv(env)->CallIntMethodA(env, obj, mid, vargs); |
| 2990 | break; |
| 2991 | case kDirect: |
| 2992 | result.I = baseEnv(env)->CallNonvirtualIntMethodA(env, obj, c, mid, vargs); |
| 2993 | break; |
| 2994 | case kStatic: |
| 2995 | result.I = baseEnv(env)->CallStaticIntMethodA(env, c, mid, vargs); |
| 2996 | break; |
| 2997 | default: |
| 2998 | break; |
| 2999 | } |
| 3000 | break; |
| 3001 | case Primitive::kPrimLong: |
| 3002 | result_check = "J"; |
| 3003 | switch (invoke) { |
| 3004 | case kVirtual: |
| 3005 | result.J = baseEnv(env)->CallLongMethodA(env, obj, mid, vargs); |
| 3006 | break; |
| 3007 | case kDirect: |
| 3008 | result.J = baseEnv(env)->CallNonvirtualLongMethodA(env, obj, c, mid, vargs); |
| 3009 | break; |
| 3010 | case kStatic: |
| 3011 | result.J = baseEnv(env)->CallStaticLongMethodA(env, c, mid, vargs); |
| 3012 | break; |
| 3013 | default: |
| 3014 | break; |
| 3015 | } |
| 3016 | break; |
| 3017 | case Primitive::kPrimFloat: |
| 3018 | result_check = "F"; |
| 3019 | switch (invoke) { |
| 3020 | case kVirtual: |
| 3021 | result.F = baseEnv(env)->CallFloatMethodA(env, obj, mid, vargs); |
| 3022 | break; |
| 3023 | case kDirect: |
| 3024 | result.F = baseEnv(env)->CallNonvirtualFloatMethodA(env, obj, c, mid, vargs); |
| 3025 | break; |
| 3026 | case kStatic: |
| 3027 | result.F = baseEnv(env)->CallStaticFloatMethodA(env, c, mid, vargs); |
| 3028 | break; |
| 3029 | default: |
| 3030 | break; |
| 3031 | } |
| 3032 | break; |
| 3033 | case Primitive::kPrimDouble: |
| 3034 | result_check = "D"; |
| 3035 | switch (invoke) { |
| 3036 | case kVirtual: |
| 3037 | result.D = baseEnv(env)->CallDoubleMethodA(env, obj, mid, vargs); |
| 3038 | break; |
| 3039 | case kDirect: |
| 3040 | result.D = baseEnv(env)->CallNonvirtualDoubleMethodA(env, obj, c, mid, vargs); |
| 3041 | break; |
| 3042 | case kStatic: |
| 3043 | result.D = baseEnv(env)->CallStaticDoubleMethodA(env, c, mid, vargs); |
| 3044 | break; |
| 3045 | default: |
| 3046 | break; |
| 3047 | } |
| 3048 | break; |
| 3049 | case Primitive::kPrimVoid: |
| 3050 | result_check = "V"; |
| 3051 | result.V = nullptr; |
| 3052 | switch (invoke) { |
| 3053 | case kVirtual: |
| 3054 | baseEnv(env)->CallVoidMethodA(env, obj, mid, vargs); |
| 3055 | break; |
| 3056 | case kDirect: |
| 3057 | baseEnv(env)->CallNonvirtualVoidMethodA(env, obj, c, mid, vargs); |
| 3058 | break; |
| 3059 | case kStatic: |
| 3060 | baseEnv(env)->CallStaticVoidMethodA(env, c, mid, vargs); |
| 3061 | break; |
| 3062 | default: |
| 3063 | LOG(FATAL) << "Unexpected invoke: " << invoke; |
| 3064 | } |
| 3065 | break; |
| 3066 | default: |
| 3067 | LOG(FATAL) << "Unexpected return type: " << type; |
| 3068 | result_check = nullptr; |
| 3069 | } |
| 3070 | if (sc.Check(soa, false, result_check, &result)) { |
| 3071 | return result; |
| 3072 | } |
| 3073 | } |
| 3074 | result.J = 0; |
| 3075 | return result; |
| 3076 | } |
| 3077 | |
| 3078 | static JniValueType CallMethodV(const char* function_name, JNIEnv* env, jobject obj, jclass c, |
| 3079 | jmethodID mid, va_list vargs, Primitive::Type type, |
| 3080 | InvokeType invoke) { |
| 3081 | ScopedObjectAccess soa(env); |
| 3082 | ScopedCheck sc(kFlag_Default, function_name); |
| 3083 | JniValueType result; |
Alex Light | 48ffe06 | 2015-08-19 15:23:23 -0700 | [diff] [blame] | 3084 | VarArgs rest(mid, vargs); |
| 3085 | if (CheckCallArgs(soa, sc, env, obj, c, mid, invoke, &rest) && |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3086 | sc.CheckMethodAndSig(soa, obj, c, mid, type, invoke)) { |
| 3087 | const char* result_check; |
| 3088 | switch (type) { |
| 3089 | case Primitive::kPrimNot: |
| 3090 | result_check = "L"; |
| 3091 | switch (invoke) { |
| 3092 | case kVirtual: |
| 3093 | result.L = baseEnv(env)->CallObjectMethodV(env, obj, mid, vargs); |
| 3094 | break; |
| 3095 | case kDirect: |
| 3096 | result.L = baseEnv(env)->CallNonvirtualObjectMethodV(env, obj, c, mid, vargs); |
| 3097 | break; |
| 3098 | case kStatic: |
| 3099 | result.L = baseEnv(env)->CallStaticObjectMethodV(env, c, mid, vargs); |
| 3100 | break; |
| 3101 | default: |
| 3102 | LOG(FATAL) << "Unexpected invoke: " << invoke; |
| 3103 | } |
| 3104 | break; |
| 3105 | case Primitive::kPrimBoolean: |
| 3106 | result_check = "Z"; |
| 3107 | switch (invoke) { |
| 3108 | case kVirtual: |
| 3109 | result.Z = baseEnv(env)->CallBooleanMethodV(env, obj, mid, vargs); |
| 3110 | break; |
| 3111 | case kDirect: |
| 3112 | result.Z = baseEnv(env)->CallNonvirtualBooleanMethodV(env, obj, c, mid, vargs); |
| 3113 | break; |
| 3114 | case kStatic: |
| 3115 | result.Z = baseEnv(env)->CallStaticBooleanMethodV(env, c, mid, vargs); |
| 3116 | break; |
| 3117 | default: |
| 3118 | LOG(FATAL) << "Unexpected invoke: " << invoke; |
| 3119 | } |
| 3120 | break; |
| 3121 | case Primitive::kPrimByte: |
| 3122 | result_check = "B"; |
| 3123 | switch (invoke) { |
| 3124 | case kVirtual: |
| 3125 | result.B = baseEnv(env)->CallByteMethodV(env, obj, mid, vargs); |
| 3126 | break; |
| 3127 | case kDirect: |
| 3128 | result.B = baseEnv(env)->CallNonvirtualByteMethodV(env, obj, c, mid, vargs); |
| 3129 | break; |
| 3130 | case kStatic: |
| 3131 | result.B = baseEnv(env)->CallStaticByteMethodV(env, c, mid, vargs); |
| 3132 | break; |
| 3133 | default: |
| 3134 | LOG(FATAL) << "Unexpected invoke: " << invoke; |
| 3135 | } |
| 3136 | break; |
| 3137 | case Primitive::kPrimChar: |
| 3138 | result_check = "C"; |
| 3139 | switch (invoke) { |
| 3140 | case kVirtual: |
| 3141 | result.C = baseEnv(env)->CallCharMethodV(env, obj, mid, vargs); |
| 3142 | break; |
| 3143 | case kDirect: |
| 3144 | result.C = baseEnv(env)->CallNonvirtualCharMethodV(env, obj, c, mid, vargs); |
| 3145 | break; |
| 3146 | case kStatic: |
| 3147 | result.C = baseEnv(env)->CallStaticCharMethodV(env, c, mid, vargs); |
| 3148 | break; |
| 3149 | default: |
| 3150 | LOG(FATAL) << "Unexpected invoke: " << invoke; |
| 3151 | } |
| 3152 | break; |
| 3153 | case Primitive::kPrimShort: |
| 3154 | result_check = "S"; |
| 3155 | switch (invoke) { |
| 3156 | case kVirtual: |
| 3157 | result.S = baseEnv(env)->CallShortMethodV(env, obj, mid, vargs); |
| 3158 | break; |
| 3159 | case kDirect: |
| 3160 | result.S = baseEnv(env)->CallNonvirtualShortMethodV(env, obj, c, mid, vargs); |
| 3161 | break; |
| 3162 | case kStatic: |
| 3163 | result.S = baseEnv(env)->CallStaticShortMethodV(env, c, mid, vargs); |
| 3164 | break; |
| 3165 | default: |
| 3166 | LOG(FATAL) << "Unexpected invoke: " << invoke; |
| 3167 | } |
| 3168 | break; |
| 3169 | case Primitive::kPrimInt: |
| 3170 | result_check = "I"; |
| 3171 | switch (invoke) { |
| 3172 | case kVirtual: |
| 3173 | result.I = baseEnv(env)->CallIntMethodV(env, obj, mid, vargs); |
| 3174 | break; |
| 3175 | case kDirect: |
| 3176 | result.I = baseEnv(env)->CallNonvirtualIntMethodV(env, obj, c, mid, vargs); |
| 3177 | break; |
| 3178 | case kStatic: |
| 3179 | result.I = baseEnv(env)->CallStaticIntMethodV(env, c, mid, vargs); |
| 3180 | break; |
| 3181 | default: |
| 3182 | LOG(FATAL) << "Unexpected invoke: " << invoke; |
| 3183 | } |
| 3184 | break; |
| 3185 | case Primitive::kPrimLong: |
| 3186 | result_check = "J"; |
| 3187 | switch (invoke) { |
| 3188 | case kVirtual: |
| 3189 | result.J = baseEnv(env)->CallLongMethodV(env, obj, mid, vargs); |
| 3190 | break; |
| 3191 | case kDirect: |
| 3192 | result.J = baseEnv(env)->CallNonvirtualLongMethodV(env, obj, c, mid, vargs); |
| 3193 | break; |
| 3194 | case kStatic: |
| 3195 | result.J = baseEnv(env)->CallStaticLongMethodV(env, c, mid, vargs); |
| 3196 | break; |
| 3197 | default: |
| 3198 | LOG(FATAL) << "Unexpected invoke: " << invoke; |
| 3199 | } |
| 3200 | break; |
| 3201 | case Primitive::kPrimFloat: |
| 3202 | result_check = "F"; |
| 3203 | switch (invoke) { |
| 3204 | case kVirtual: |
| 3205 | result.F = baseEnv(env)->CallFloatMethodV(env, obj, mid, vargs); |
| 3206 | break; |
| 3207 | case kDirect: |
| 3208 | result.F = baseEnv(env)->CallNonvirtualFloatMethodV(env, obj, c, mid, vargs); |
| 3209 | break; |
| 3210 | case kStatic: |
| 3211 | result.F = baseEnv(env)->CallStaticFloatMethodV(env, c, mid, vargs); |
| 3212 | break; |
| 3213 | default: |
| 3214 | LOG(FATAL) << "Unexpected invoke: " << invoke; |
| 3215 | } |
| 3216 | break; |
| 3217 | case Primitive::kPrimDouble: |
| 3218 | result_check = "D"; |
| 3219 | switch (invoke) { |
| 3220 | case kVirtual: |
| 3221 | result.D = baseEnv(env)->CallDoubleMethodV(env, obj, mid, vargs); |
| 3222 | break; |
| 3223 | case kDirect: |
| 3224 | result.D = baseEnv(env)->CallNonvirtualDoubleMethodV(env, obj, c, mid, vargs); |
| 3225 | break; |
| 3226 | case kStatic: |
| 3227 | result.D = baseEnv(env)->CallStaticDoubleMethodV(env, c, mid, vargs); |
| 3228 | break; |
| 3229 | default: |
| 3230 | LOG(FATAL) << "Unexpected invoke: " << invoke; |
| 3231 | } |
| 3232 | break; |
| 3233 | case Primitive::kPrimVoid: |
| 3234 | result_check = "V"; |
| 3235 | result.V = nullptr; |
| 3236 | switch (invoke) { |
| 3237 | case kVirtual: |
| 3238 | baseEnv(env)->CallVoidMethodV(env, obj, mid, vargs); |
| 3239 | break; |
| 3240 | case kDirect: |
| 3241 | baseEnv(env)->CallNonvirtualVoidMethodV(env, obj, c, mid, vargs); |
| 3242 | break; |
| 3243 | case kStatic: |
| 3244 | baseEnv(env)->CallStaticVoidMethodV(env, c, mid, vargs); |
| 3245 | break; |
| 3246 | default: |
| 3247 | LOG(FATAL) << "Unexpected invoke: " << invoke; |
| 3248 | } |
| 3249 | break; |
| 3250 | default: |
| 3251 | LOG(FATAL) << "Unexpected return type: " << type; |
| 3252 | result_check = nullptr; |
| 3253 | } |
| 3254 | if (sc.Check(soa, false, result_check, &result)) { |
| 3255 | return result; |
| 3256 | } |
| 3257 | } |
| 3258 | result.J = 0; |
| 3259 | return result; |
| 3260 | } |
| 3261 | |
| 3262 | static const void* GetStringCharsInternal(const char* function_name, JNIEnv* env, jstring string, |
| 3263 | jboolean* is_copy, bool utf, bool critical) { |
| 3264 | ScopedObjectAccess soa(env); |
| 3265 | int flags = critical ? kFlag_CritGet : kFlag_CritOkay; |
| 3266 | ScopedCheck sc(flags, function_name); |
| 3267 | JniValueType args[3] = {{.E = env}, {.s = string}, {.p = is_copy}}; |
| 3268 | if (sc.Check(soa, true, "Esp", args)) { |
| 3269 | JniValueType result; |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 3270 | void* ptr; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3271 | if (utf) { |
| 3272 | CHECK(!critical); |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 3273 | ptr = const_cast<char*>(baseEnv(env)->GetStringUTFChars(env, string, is_copy)); |
| 3274 | result.u = reinterpret_cast<char*>(ptr); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3275 | } else { |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 3276 | ptr = const_cast<jchar*>(critical ? baseEnv(env)->GetStringCritical(env, string, is_copy) : |
| 3277 | baseEnv(env)->GetStringChars(env, string, is_copy)); |
| 3278 | result.p = ptr; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3279 | } |
| 3280 | // TODO: could we be smarter about not copying when local_is_copy? |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 3281 | if (ptr != nullptr && soa.ForceCopy()) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3282 | if (utf) { |
| 3283 | size_t length_in_bytes = strlen(result.u) + 1; |
| 3284 | result.u = |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 3285 | reinterpret_cast<const char*>(GuardedCopy::Create(ptr, length_in_bytes, false)); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3286 | } else { |
| 3287 | size_t length_in_bytes = baseEnv(env)->GetStringLength(env, string) * 2; |
| 3288 | result.p = |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 3289 | reinterpret_cast<const jchar*>(GuardedCopy::Create(ptr, length_in_bytes, false)); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3290 | } |
| 3291 | if (is_copy != nullptr) { |
| 3292 | *is_copy = JNI_TRUE; |
| 3293 | } |
| 3294 | } |
| 3295 | if (sc.Check(soa, false, utf ? "u" : "p", &result)) { |
| 3296 | return utf ? result.u : result.p; |
| 3297 | } |
| 3298 | } |
| 3299 | return nullptr; |
| 3300 | } |
| 3301 | |
| 3302 | static void ReleaseStringCharsInternal(const char* function_name, JNIEnv* env, jstring string, |
| 3303 | const void* chars, bool utf, bool critical) { |
| 3304 | ScopedObjectAccess soa(env); |
| 3305 | int flags = kFlag_ExcepOkay | kFlag_Release; |
| 3306 | if (critical) { |
| 3307 | flags |= kFlag_CritRelease; |
| 3308 | } |
| 3309 | ScopedCheck sc(flags, function_name); |
| 3310 | sc.CheckNonNull(chars); |
| 3311 | bool force_copy_ok = !soa.ForceCopy() || GuardedCopy::Check(function_name, chars, false); |
| 3312 | if (force_copy_ok && soa.ForceCopy()) { |
| 3313 | chars = reinterpret_cast<const jchar*>(GuardedCopy::Destroy(const_cast<void*>(chars))); |
| 3314 | } |
| 3315 | if (force_copy_ok) { |
| 3316 | JniValueType args[3] = {{.E = env}, {.s = string}, {.p = chars}}; |
| 3317 | if (sc.Check(soa, true, utf ? "Esu" : "Esp", args)) { |
| 3318 | if (utf) { |
| 3319 | CHECK(!critical); |
| 3320 | baseEnv(env)->ReleaseStringUTFChars(env, string, reinterpret_cast<const char*>(chars)); |
| 3321 | } else { |
| 3322 | if (critical) { |
| 3323 | baseEnv(env)->ReleaseStringCritical(env, string, reinterpret_cast<const jchar*>(chars)); |
| 3324 | } else { |
| 3325 | baseEnv(env)->ReleaseStringChars(env, string, reinterpret_cast<const jchar*>(chars)); |
| 3326 | } |
| 3327 | } |
| 3328 | JniValueType result; |
| 3329 | sc.Check(soa, false, "V", &result); |
| 3330 | } |
| 3331 | } |
| 3332 | } |
| 3333 | |
| 3334 | static jarray NewPrimitiveArray(const char* function_name, JNIEnv* env, jsize length, |
| 3335 | Primitive::Type type) { |
| 3336 | ScopedObjectAccess soa(env); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 3337 | ScopedCheck sc(kFlag_Default, function_name); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3338 | JniValueType args[2] = {{.E = env}, {.z = length}}; |
| 3339 | if (sc.Check(soa, true, "Ez", args)) { |
| 3340 | JniValueType result; |
| 3341 | switch (type) { |
| 3342 | case Primitive::kPrimBoolean: |
| 3343 | result.a = baseEnv(env)->NewBooleanArray(env, length); |
| 3344 | break; |
| 3345 | case Primitive::kPrimByte: |
| 3346 | result.a = baseEnv(env)->NewByteArray(env, length); |
| 3347 | break; |
| 3348 | case Primitive::kPrimChar: |
| 3349 | result.a = baseEnv(env)->NewCharArray(env, length); |
| 3350 | break; |
| 3351 | case Primitive::kPrimShort: |
| 3352 | result.a = baseEnv(env)->NewShortArray(env, length); |
| 3353 | break; |
| 3354 | case Primitive::kPrimInt: |
| 3355 | result.a = baseEnv(env)->NewIntArray(env, length); |
| 3356 | break; |
| 3357 | case Primitive::kPrimLong: |
| 3358 | result.a = baseEnv(env)->NewLongArray(env, length); |
| 3359 | break; |
| 3360 | case Primitive::kPrimFloat: |
| 3361 | result.a = baseEnv(env)->NewFloatArray(env, length); |
| 3362 | break; |
| 3363 | case Primitive::kPrimDouble: |
| 3364 | result.a = baseEnv(env)->NewDoubleArray(env, length); |
| 3365 | break; |
| 3366 | default: |
| 3367 | LOG(FATAL) << "Unexpected primitive type: " << type; |
| 3368 | } |
| 3369 | if (sc.Check(soa, false, "a", &result)) { |
| 3370 | return result.a; |
| 3371 | } |
| 3372 | } |
| 3373 | return nullptr; |
| 3374 | } |
| 3375 | |
| 3376 | static void* GetPrimitiveArrayElements(const char* function_name, Primitive::Type type, |
| 3377 | JNIEnv* env, jarray array, jboolean* is_copy) { |
| 3378 | ScopedObjectAccess soa(env); |
| 3379 | ScopedCheck sc(kFlag_Default, function_name); |
| 3380 | JniValueType args[3] = {{.E = env}, {.a = array}, {.p = is_copy}}; |
| 3381 | if (sc.Check(soa, true, "Eap", args) && sc.CheckPrimitiveArrayType(soa, array, type)) { |
| 3382 | JniValueType result; |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 3383 | void* ptr = nullptr; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3384 | switch (type) { |
| 3385 | case Primitive::kPrimBoolean: |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 3386 | ptr = baseEnv(env)->GetBooleanArrayElements(env, down_cast<jbooleanArray>(array), |
| 3387 | is_copy); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3388 | break; |
| 3389 | case Primitive::kPrimByte: |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 3390 | ptr = baseEnv(env)->GetByteArrayElements(env, down_cast<jbyteArray>(array), is_copy); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3391 | break; |
| 3392 | case Primitive::kPrimChar: |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 3393 | ptr = baseEnv(env)->GetCharArrayElements(env, down_cast<jcharArray>(array), is_copy); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3394 | break; |
| 3395 | case Primitive::kPrimShort: |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 3396 | ptr = baseEnv(env)->GetShortArrayElements(env, down_cast<jshortArray>(array), is_copy); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3397 | break; |
| 3398 | case Primitive::kPrimInt: |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 3399 | ptr = baseEnv(env)->GetIntArrayElements(env, down_cast<jintArray>(array), is_copy); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3400 | break; |
| 3401 | case Primitive::kPrimLong: |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 3402 | ptr = baseEnv(env)->GetLongArrayElements(env, down_cast<jlongArray>(array), is_copy); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3403 | break; |
| 3404 | case Primitive::kPrimFloat: |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 3405 | ptr = baseEnv(env)->GetFloatArrayElements(env, down_cast<jfloatArray>(array), is_copy); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3406 | break; |
| 3407 | case Primitive::kPrimDouble: |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 3408 | ptr = baseEnv(env)->GetDoubleArrayElements(env, down_cast<jdoubleArray>(array), is_copy); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3409 | break; |
| 3410 | default: |
| 3411 | LOG(FATAL) << "Unexpected primitive type: " << type; |
| 3412 | } |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 3413 | if (ptr != nullptr && soa.ForceCopy()) { |
| 3414 | ptr = GuardedCopy::CreateGuardedPACopy(env, array, is_copy, ptr); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3415 | if (is_copy != nullptr) { |
| 3416 | *is_copy = JNI_TRUE; |
| 3417 | } |
| 3418 | } |
Mathieu Chartier | b735bd9 | 2015-06-24 17:04:17 -0700 | [diff] [blame] | 3419 | result.p = ptr; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3420 | if (sc.Check(soa, false, "p", &result)) { |
| 3421 | return const_cast<void*>(result.p); |
| 3422 | } |
| 3423 | } |
| 3424 | return nullptr; |
| 3425 | } |
| 3426 | |
| 3427 | static void ReleasePrimitiveArrayElements(const char* function_name, Primitive::Type type, |
| 3428 | JNIEnv* env, jarray array, void* elems, jint mode) { |
| 3429 | ScopedObjectAccess soa(env); |
| 3430 | ScopedCheck sc(kFlag_ExcepOkay, function_name); |
| 3431 | if (sc.CheckNonNull(elems) && sc.CheckPrimitiveArrayType(soa, array, type)) { |
| 3432 | if (soa.ForceCopy()) { |
| 3433 | elems = GuardedCopy::ReleaseGuardedPACopy(function_name, env, array, elems, mode); |
| 3434 | } |
| 3435 | if (!soa.ForceCopy() || elems != nullptr) { |
| 3436 | JniValueType args[4] = {{.E = env}, {.a = array}, {.p = elems}, {.r = mode}}; |
| 3437 | if (sc.Check(soa, true, "Eapr", args)) { |
| 3438 | switch (type) { |
| 3439 | case Primitive::kPrimBoolean: |
| 3440 | baseEnv(env)->ReleaseBooleanArrayElements(env, down_cast<jbooleanArray>(array), |
| 3441 | reinterpret_cast<jboolean*>(elems), mode); |
| 3442 | break; |
| 3443 | case Primitive::kPrimByte: |
| 3444 | baseEnv(env)->ReleaseByteArrayElements(env, down_cast<jbyteArray>(array), |
| 3445 | reinterpret_cast<jbyte*>(elems), mode); |
| 3446 | break; |
| 3447 | case Primitive::kPrimChar: |
| 3448 | baseEnv(env)->ReleaseCharArrayElements(env, down_cast<jcharArray>(array), |
| 3449 | reinterpret_cast<jchar*>(elems), mode); |
| 3450 | break; |
| 3451 | case Primitive::kPrimShort: |
| 3452 | baseEnv(env)->ReleaseShortArrayElements(env, down_cast<jshortArray>(array), |
| 3453 | reinterpret_cast<jshort*>(elems), mode); |
| 3454 | break; |
| 3455 | case Primitive::kPrimInt: |
| 3456 | baseEnv(env)->ReleaseIntArrayElements(env, down_cast<jintArray>(array), |
| 3457 | reinterpret_cast<jint*>(elems), mode); |
| 3458 | break; |
| 3459 | case Primitive::kPrimLong: |
| 3460 | baseEnv(env)->ReleaseLongArrayElements(env, down_cast<jlongArray>(array), |
| 3461 | reinterpret_cast<jlong*>(elems), mode); |
| 3462 | break; |
| 3463 | case Primitive::kPrimFloat: |
| 3464 | baseEnv(env)->ReleaseFloatArrayElements(env, down_cast<jfloatArray>(array), |
| 3465 | reinterpret_cast<jfloat*>(elems), mode); |
| 3466 | break; |
| 3467 | case Primitive::kPrimDouble: |
| 3468 | baseEnv(env)->ReleaseDoubleArrayElements(env, down_cast<jdoubleArray>(array), |
| 3469 | reinterpret_cast<jdouble*>(elems), mode); |
| 3470 | break; |
| 3471 | default: |
| 3472 | LOG(FATAL) << "Unexpected primitive type: " << type; |
| 3473 | } |
| 3474 | JniValueType result; |
| 3475 | result.V = nullptr; |
| 3476 | sc.Check(soa, false, "V", &result); |
| 3477 | } |
| 3478 | } |
| 3479 | } |
| 3480 | } |
| 3481 | |
| 3482 | static void GetPrimitiveArrayRegion(const char* function_name, Primitive::Type type, JNIEnv* env, |
| 3483 | jarray array, jsize start, jsize len, void* buf) { |
| 3484 | ScopedObjectAccess soa(env); |
| 3485 | ScopedCheck sc(kFlag_Default, function_name); |
| 3486 | JniValueType args[5] = {{.E = env}, {.a = array}, {.z = start}, {.z = len}, {.p = buf}}; |
| 3487 | // Note: the start and len arguments are checked as 'I' rather than 'z' as invalid indices |
| 3488 | // result in ArrayIndexOutOfBoundsExceptions in the base implementation. |
| 3489 | if (sc.Check(soa, true, "EaIIp", args) && sc.CheckPrimitiveArrayType(soa, array, type)) { |
| 3490 | switch (type) { |
| 3491 | case Primitive::kPrimBoolean: |
| 3492 | baseEnv(env)->GetBooleanArrayRegion(env, down_cast<jbooleanArray>(array), start, len, |
| 3493 | reinterpret_cast<jboolean*>(buf)); |
| 3494 | break; |
| 3495 | case Primitive::kPrimByte: |
| 3496 | baseEnv(env)->GetByteArrayRegion(env, down_cast<jbyteArray>(array), start, len, |
| 3497 | reinterpret_cast<jbyte*>(buf)); |
| 3498 | break; |
| 3499 | case Primitive::kPrimChar: |
| 3500 | baseEnv(env)->GetCharArrayRegion(env, down_cast<jcharArray>(array), start, len, |
| 3501 | reinterpret_cast<jchar*>(buf)); |
| 3502 | break; |
| 3503 | case Primitive::kPrimShort: |
| 3504 | baseEnv(env)->GetShortArrayRegion(env, down_cast<jshortArray>(array), start, len, |
| 3505 | reinterpret_cast<jshort*>(buf)); |
| 3506 | break; |
| 3507 | case Primitive::kPrimInt: |
| 3508 | baseEnv(env)->GetIntArrayRegion(env, down_cast<jintArray>(array), start, len, |
| 3509 | reinterpret_cast<jint*>(buf)); |
| 3510 | break; |
| 3511 | case Primitive::kPrimLong: |
| 3512 | baseEnv(env)->GetLongArrayRegion(env, down_cast<jlongArray>(array), start, len, |
| 3513 | reinterpret_cast<jlong*>(buf)); |
| 3514 | break; |
| 3515 | case Primitive::kPrimFloat: |
| 3516 | baseEnv(env)->GetFloatArrayRegion(env, down_cast<jfloatArray>(array), start, len, |
| 3517 | reinterpret_cast<jfloat*>(buf)); |
| 3518 | break; |
| 3519 | case Primitive::kPrimDouble: |
| 3520 | baseEnv(env)->GetDoubleArrayRegion(env, down_cast<jdoubleArray>(array), start, len, |
| 3521 | reinterpret_cast<jdouble*>(buf)); |
| 3522 | break; |
| 3523 | default: |
| 3524 | LOG(FATAL) << "Unexpected primitive type: " << type; |
| 3525 | } |
| 3526 | JniValueType result; |
| 3527 | result.V = nullptr; |
| 3528 | sc.Check(soa, false, "V", &result); |
| 3529 | } |
| 3530 | } |
| 3531 | |
| 3532 | static void SetPrimitiveArrayRegion(const char* function_name, Primitive::Type type, JNIEnv* env, |
| 3533 | jarray array, jsize start, jsize len, const void* buf) { |
| 3534 | ScopedObjectAccess soa(env); |
| 3535 | ScopedCheck sc(kFlag_Default, function_name); |
| 3536 | JniValueType args[5] = {{.E = env}, {.a = array}, {.z = start}, {.z = len}, {.p = buf}}; |
| 3537 | // Note: the start and len arguments are checked as 'I' rather than 'z' as invalid indices |
| 3538 | // result in ArrayIndexOutOfBoundsExceptions in the base implementation. |
| 3539 | if (sc.Check(soa, true, "EaIIp", args) && sc.CheckPrimitiveArrayType(soa, array, type)) { |
| 3540 | switch (type) { |
| 3541 | case Primitive::kPrimBoolean: |
| 3542 | baseEnv(env)->SetBooleanArrayRegion(env, down_cast<jbooleanArray>(array), start, len, |
| 3543 | reinterpret_cast<const jboolean*>(buf)); |
| 3544 | break; |
| 3545 | case Primitive::kPrimByte: |
| 3546 | baseEnv(env)->SetByteArrayRegion(env, down_cast<jbyteArray>(array), start, len, |
| 3547 | reinterpret_cast<const jbyte*>(buf)); |
| 3548 | break; |
| 3549 | case Primitive::kPrimChar: |
| 3550 | baseEnv(env)->SetCharArrayRegion(env, down_cast<jcharArray>(array), start, len, |
| 3551 | reinterpret_cast<const jchar*>(buf)); |
| 3552 | break; |
| 3553 | case Primitive::kPrimShort: |
| 3554 | baseEnv(env)->SetShortArrayRegion(env, down_cast<jshortArray>(array), start, len, |
| 3555 | reinterpret_cast<const jshort*>(buf)); |
| 3556 | break; |
| 3557 | case Primitive::kPrimInt: |
| 3558 | baseEnv(env)->SetIntArrayRegion(env, down_cast<jintArray>(array), start, len, |
| 3559 | reinterpret_cast<const jint*>(buf)); |
| 3560 | break; |
| 3561 | case Primitive::kPrimLong: |
| 3562 | baseEnv(env)->SetLongArrayRegion(env, down_cast<jlongArray>(array), start, len, |
| 3563 | reinterpret_cast<const jlong*>(buf)); |
| 3564 | break; |
| 3565 | case Primitive::kPrimFloat: |
| 3566 | baseEnv(env)->SetFloatArrayRegion(env, down_cast<jfloatArray>(array), start, len, |
| 3567 | reinterpret_cast<const jfloat*>(buf)); |
| 3568 | break; |
| 3569 | case Primitive::kPrimDouble: |
| 3570 | baseEnv(env)->SetDoubleArrayRegion(env, down_cast<jdoubleArray>(array), start, len, |
| 3571 | reinterpret_cast<const jdouble*>(buf)); |
| 3572 | break; |
| 3573 | default: |
| 3574 | LOG(FATAL) << "Unexpected primitive type: " << type; |
| 3575 | } |
| 3576 | JniValueType result; |
| 3577 | result.V = nullptr; |
| 3578 | sc.Check(soa, false, "V", &result); |
| 3579 | } |
| 3580 | } |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 3581 | }; |
| 3582 | |
| 3583 | const JNINativeInterface gCheckNativeInterface = { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3584 | nullptr, // reserved0. |
| 3585 | nullptr, // reserved1. |
| 3586 | nullptr, // reserved2. |
| 3587 | nullptr, // reserved3. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 3588 | CheckJNI::GetVersion, |
| 3589 | CheckJNI::DefineClass, |
| 3590 | CheckJNI::FindClass, |
| 3591 | CheckJNI::FromReflectedMethod, |
| 3592 | CheckJNI::FromReflectedField, |
| 3593 | CheckJNI::ToReflectedMethod, |
| 3594 | CheckJNI::GetSuperclass, |
| 3595 | CheckJNI::IsAssignableFrom, |
| 3596 | CheckJNI::ToReflectedField, |
| 3597 | CheckJNI::Throw, |
| 3598 | CheckJNI::ThrowNew, |
| 3599 | CheckJNI::ExceptionOccurred, |
| 3600 | CheckJNI::ExceptionDescribe, |
| 3601 | CheckJNI::ExceptionClear, |
| 3602 | CheckJNI::FatalError, |
| 3603 | CheckJNI::PushLocalFrame, |
| 3604 | CheckJNI::PopLocalFrame, |
| 3605 | CheckJNI::NewGlobalRef, |
| 3606 | CheckJNI::DeleteGlobalRef, |
| 3607 | CheckJNI::DeleteLocalRef, |
| 3608 | CheckJNI::IsSameObject, |
| 3609 | CheckJNI::NewLocalRef, |
| 3610 | CheckJNI::EnsureLocalCapacity, |
| 3611 | CheckJNI::AllocObject, |
| 3612 | CheckJNI::NewObject, |
| 3613 | CheckJNI::NewObjectV, |
| 3614 | CheckJNI::NewObjectA, |
| 3615 | CheckJNI::GetObjectClass, |
| 3616 | CheckJNI::IsInstanceOf, |
| 3617 | CheckJNI::GetMethodID, |
| 3618 | CheckJNI::CallObjectMethod, |
| 3619 | CheckJNI::CallObjectMethodV, |
| 3620 | CheckJNI::CallObjectMethodA, |
| 3621 | CheckJNI::CallBooleanMethod, |
| 3622 | CheckJNI::CallBooleanMethodV, |
| 3623 | CheckJNI::CallBooleanMethodA, |
| 3624 | CheckJNI::CallByteMethod, |
| 3625 | CheckJNI::CallByteMethodV, |
| 3626 | CheckJNI::CallByteMethodA, |
| 3627 | CheckJNI::CallCharMethod, |
| 3628 | CheckJNI::CallCharMethodV, |
| 3629 | CheckJNI::CallCharMethodA, |
| 3630 | CheckJNI::CallShortMethod, |
| 3631 | CheckJNI::CallShortMethodV, |
| 3632 | CheckJNI::CallShortMethodA, |
| 3633 | CheckJNI::CallIntMethod, |
| 3634 | CheckJNI::CallIntMethodV, |
| 3635 | CheckJNI::CallIntMethodA, |
| 3636 | CheckJNI::CallLongMethod, |
| 3637 | CheckJNI::CallLongMethodV, |
| 3638 | CheckJNI::CallLongMethodA, |
| 3639 | CheckJNI::CallFloatMethod, |
| 3640 | CheckJNI::CallFloatMethodV, |
| 3641 | CheckJNI::CallFloatMethodA, |
| 3642 | CheckJNI::CallDoubleMethod, |
| 3643 | CheckJNI::CallDoubleMethodV, |
| 3644 | CheckJNI::CallDoubleMethodA, |
| 3645 | CheckJNI::CallVoidMethod, |
| 3646 | CheckJNI::CallVoidMethodV, |
| 3647 | CheckJNI::CallVoidMethodA, |
| 3648 | CheckJNI::CallNonvirtualObjectMethod, |
| 3649 | CheckJNI::CallNonvirtualObjectMethodV, |
| 3650 | CheckJNI::CallNonvirtualObjectMethodA, |
| 3651 | CheckJNI::CallNonvirtualBooleanMethod, |
| 3652 | CheckJNI::CallNonvirtualBooleanMethodV, |
| 3653 | CheckJNI::CallNonvirtualBooleanMethodA, |
| 3654 | CheckJNI::CallNonvirtualByteMethod, |
| 3655 | CheckJNI::CallNonvirtualByteMethodV, |
| 3656 | CheckJNI::CallNonvirtualByteMethodA, |
| 3657 | CheckJNI::CallNonvirtualCharMethod, |
| 3658 | CheckJNI::CallNonvirtualCharMethodV, |
| 3659 | CheckJNI::CallNonvirtualCharMethodA, |
| 3660 | CheckJNI::CallNonvirtualShortMethod, |
| 3661 | CheckJNI::CallNonvirtualShortMethodV, |
| 3662 | CheckJNI::CallNonvirtualShortMethodA, |
| 3663 | CheckJNI::CallNonvirtualIntMethod, |
| 3664 | CheckJNI::CallNonvirtualIntMethodV, |
| 3665 | CheckJNI::CallNonvirtualIntMethodA, |
| 3666 | CheckJNI::CallNonvirtualLongMethod, |
| 3667 | CheckJNI::CallNonvirtualLongMethodV, |
| 3668 | CheckJNI::CallNonvirtualLongMethodA, |
| 3669 | CheckJNI::CallNonvirtualFloatMethod, |
| 3670 | CheckJNI::CallNonvirtualFloatMethodV, |
| 3671 | CheckJNI::CallNonvirtualFloatMethodA, |
| 3672 | CheckJNI::CallNonvirtualDoubleMethod, |
| 3673 | CheckJNI::CallNonvirtualDoubleMethodV, |
| 3674 | CheckJNI::CallNonvirtualDoubleMethodA, |
| 3675 | CheckJNI::CallNonvirtualVoidMethod, |
| 3676 | CheckJNI::CallNonvirtualVoidMethodV, |
| 3677 | CheckJNI::CallNonvirtualVoidMethodA, |
| 3678 | CheckJNI::GetFieldID, |
| 3679 | CheckJNI::GetObjectField, |
| 3680 | CheckJNI::GetBooleanField, |
| 3681 | CheckJNI::GetByteField, |
| 3682 | CheckJNI::GetCharField, |
| 3683 | CheckJNI::GetShortField, |
| 3684 | CheckJNI::GetIntField, |
| 3685 | CheckJNI::GetLongField, |
| 3686 | CheckJNI::GetFloatField, |
| 3687 | CheckJNI::GetDoubleField, |
| 3688 | CheckJNI::SetObjectField, |
| 3689 | CheckJNI::SetBooleanField, |
| 3690 | CheckJNI::SetByteField, |
| 3691 | CheckJNI::SetCharField, |
| 3692 | CheckJNI::SetShortField, |
| 3693 | CheckJNI::SetIntField, |
| 3694 | CheckJNI::SetLongField, |
| 3695 | CheckJNI::SetFloatField, |
| 3696 | CheckJNI::SetDoubleField, |
| 3697 | CheckJNI::GetStaticMethodID, |
| 3698 | CheckJNI::CallStaticObjectMethod, |
| 3699 | CheckJNI::CallStaticObjectMethodV, |
| 3700 | CheckJNI::CallStaticObjectMethodA, |
| 3701 | CheckJNI::CallStaticBooleanMethod, |
| 3702 | CheckJNI::CallStaticBooleanMethodV, |
| 3703 | CheckJNI::CallStaticBooleanMethodA, |
| 3704 | CheckJNI::CallStaticByteMethod, |
| 3705 | CheckJNI::CallStaticByteMethodV, |
| 3706 | CheckJNI::CallStaticByteMethodA, |
| 3707 | CheckJNI::CallStaticCharMethod, |
| 3708 | CheckJNI::CallStaticCharMethodV, |
| 3709 | CheckJNI::CallStaticCharMethodA, |
| 3710 | CheckJNI::CallStaticShortMethod, |
| 3711 | CheckJNI::CallStaticShortMethodV, |
| 3712 | CheckJNI::CallStaticShortMethodA, |
| 3713 | CheckJNI::CallStaticIntMethod, |
| 3714 | CheckJNI::CallStaticIntMethodV, |
| 3715 | CheckJNI::CallStaticIntMethodA, |
| 3716 | CheckJNI::CallStaticLongMethod, |
| 3717 | CheckJNI::CallStaticLongMethodV, |
| 3718 | CheckJNI::CallStaticLongMethodA, |
| 3719 | CheckJNI::CallStaticFloatMethod, |
| 3720 | CheckJNI::CallStaticFloatMethodV, |
| 3721 | CheckJNI::CallStaticFloatMethodA, |
| 3722 | CheckJNI::CallStaticDoubleMethod, |
| 3723 | CheckJNI::CallStaticDoubleMethodV, |
| 3724 | CheckJNI::CallStaticDoubleMethodA, |
| 3725 | CheckJNI::CallStaticVoidMethod, |
| 3726 | CheckJNI::CallStaticVoidMethodV, |
| 3727 | CheckJNI::CallStaticVoidMethodA, |
| 3728 | CheckJNI::GetStaticFieldID, |
| 3729 | CheckJNI::GetStaticObjectField, |
| 3730 | CheckJNI::GetStaticBooleanField, |
| 3731 | CheckJNI::GetStaticByteField, |
| 3732 | CheckJNI::GetStaticCharField, |
| 3733 | CheckJNI::GetStaticShortField, |
| 3734 | CheckJNI::GetStaticIntField, |
| 3735 | CheckJNI::GetStaticLongField, |
| 3736 | CheckJNI::GetStaticFloatField, |
| 3737 | CheckJNI::GetStaticDoubleField, |
| 3738 | CheckJNI::SetStaticObjectField, |
| 3739 | CheckJNI::SetStaticBooleanField, |
| 3740 | CheckJNI::SetStaticByteField, |
| 3741 | CheckJNI::SetStaticCharField, |
| 3742 | CheckJNI::SetStaticShortField, |
| 3743 | CheckJNI::SetStaticIntField, |
| 3744 | CheckJNI::SetStaticLongField, |
| 3745 | CheckJNI::SetStaticFloatField, |
| 3746 | CheckJNI::SetStaticDoubleField, |
| 3747 | CheckJNI::NewString, |
| 3748 | CheckJNI::GetStringLength, |
| 3749 | CheckJNI::GetStringChars, |
| 3750 | CheckJNI::ReleaseStringChars, |
| 3751 | CheckJNI::NewStringUTF, |
| 3752 | CheckJNI::GetStringUTFLength, |
| 3753 | CheckJNI::GetStringUTFChars, |
| 3754 | CheckJNI::ReleaseStringUTFChars, |
| 3755 | CheckJNI::GetArrayLength, |
| 3756 | CheckJNI::NewObjectArray, |
| 3757 | CheckJNI::GetObjectArrayElement, |
| 3758 | CheckJNI::SetObjectArrayElement, |
| 3759 | CheckJNI::NewBooleanArray, |
| 3760 | CheckJNI::NewByteArray, |
| 3761 | CheckJNI::NewCharArray, |
| 3762 | CheckJNI::NewShortArray, |
| 3763 | CheckJNI::NewIntArray, |
| 3764 | CheckJNI::NewLongArray, |
| 3765 | CheckJNI::NewFloatArray, |
| 3766 | CheckJNI::NewDoubleArray, |
| 3767 | CheckJNI::GetBooleanArrayElements, |
| 3768 | CheckJNI::GetByteArrayElements, |
| 3769 | CheckJNI::GetCharArrayElements, |
| 3770 | CheckJNI::GetShortArrayElements, |
| 3771 | CheckJNI::GetIntArrayElements, |
| 3772 | CheckJNI::GetLongArrayElements, |
| 3773 | CheckJNI::GetFloatArrayElements, |
| 3774 | CheckJNI::GetDoubleArrayElements, |
| 3775 | CheckJNI::ReleaseBooleanArrayElements, |
| 3776 | CheckJNI::ReleaseByteArrayElements, |
| 3777 | CheckJNI::ReleaseCharArrayElements, |
| 3778 | CheckJNI::ReleaseShortArrayElements, |
| 3779 | CheckJNI::ReleaseIntArrayElements, |
| 3780 | CheckJNI::ReleaseLongArrayElements, |
| 3781 | CheckJNI::ReleaseFloatArrayElements, |
| 3782 | CheckJNI::ReleaseDoubleArrayElements, |
| 3783 | CheckJNI::GetBooleanArrayRegion, |
| 3784 | CheckJNI::GetByteArrayRegion, |
| 3785 | CheckJNI::GetCharArrayRegion, |
| 3786 | CheckJNI::GetShortArrayRegion, |
| 3787 | CheckJNI::GetIntArrayRegion, |
| 3788 | CheckJNI::GetLongArrayRegion, |
| 3789 | CheckJNI::GetFloatArrayRegion, |
| 3790 | CheckJNI::GetDoubleArrayRegion, |
| 3791 | CheckJNI::SetBooleanArrayRegion, |
| 3792 | CheckJNI::SetByteArrayRegion, |
| 3793 | CheckJNI::SetCharArrayRegion, |
| 3794 | CheckJNI::SetShortArrayRegion, |
| 3795 | CheckJNI::SetIntArrayRegion, |
| 3796 | CheckJNI::SetLongArrayRegion, |
| 3797 | CheckJNI::SetFloatArrayRegion, |
| 3798 | CheckJNI::SetDoubleArrayRegion, |
| 3799 | CheckJNI::RegisterNatives, |
| 3800 | CheckJNI::UnregisterNatives, |
| 3801 | CheckJNI::MonitorEnter, |
| 3802 | CheckJNI::MonitorExit, |
| 3803 | CheckJNI::GetJavaVM, |
| 3804 | CheckJNI::GetStringRegion, |
| 3805 | CheckJNI::GetStringUTFRegion, |
| 3806 | CheckJNI::GetPrimitiveArrayCritical, |
| 3807 | CheckJNI::ReleasePrimitiveArrayCritical, |
| 3808 | CheckJNI::GetStringCritical, |
| 3809 | CheckJNI::ReleaseStringCritical, |
| 3810 | CheckJNI::NewWeakGlobalRef, |
| 3811 | CheckJNI::DeleteWeakGlobalRef, |
| 3812 | CheckJNI::ExceptionCheck, |
| 3813 | CheckJNI::NewDirectByteBuffer, |
| 3814 | CheckJNI::GetDirectBufferAddress, |
| 3815 | CheckJNI::GetDirectBufferCapacity, |
| 3816 | CheckJNI::GetObjectRefType, |
| 3817 | }; |
| 3818 | |
| 3819 | const JNINativeInterface* GetCheckJniNativeInterface() { |
| 3820 | return &gCheckNativeInterface; |
| 3821 | } |
| 3822 | |
| 3823 | class CheckJII { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 3824 | public: |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 3825 | static jint DestroyJavaVM(JavaVM* vm) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3826 | ScopedCheck sc(kFlag_Invocation, __FUNCTION__, false); |
| 3827 | JniValueType args[1] = {{.v = vm}}; |
| 3828 | sc.CheckNonHeap(reinterpret_cast<JavaVMExt*>(vm), true, "v", args); |
| 3829 | JniValueType result; |
| 3830 | result.i = BaseVm(vm)->DestroyJavaVM(vm); |
Andreas Gampe | def194e | 2015-02-19 15:19:50 -0800 | [diff] [blame] | 3831 | // Use null to signal that the JavaVM isn't valid anymore. DestroyJavaVM deletes the runtime, |
| 3832 | // which will delete the JavaVMExt. |
| 3833 | sc.CheckNonHeap(nullptr, false, "i", &result); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3834 | return result.i; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 3835 | } |
| 3836 | |
| 3837 | static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3838 | ScopedCheck sc(kFlag_Invocation, __FUNCTION__); |
| 3839 | JniValueType args[3] = {{.v = vm}, {.p = p_env}, {.p = thr_args}}; |
| 3840 | sc.CheckNonHeap(reinterpret_cast<JavaVMExt*>(vm), true, "vpp", args); |
| 3841 | JniValueType result; |
| 3842 | result.i = BaseVm(vm)->AttachCurrentThread(vm, p_env, thr_args); |
| 3843 | sc.CheckNonHeap(reinterpret_cast<JavaVMExt*>(vm), false, "i", &result); |
| 3844 | return result.i; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 3845 | } |
| 3846 | |
| 3847 | static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3848 | ScopedCheck sc(kFlag_Invocation, __FUNCTION__); |
| 3849 | JniValueType args[3] = {{.v = vm}, {.p = p_env}, {.p = thr_args}}; |
| 3850 | sc.CheckNonHeap(reinterpret_cast<JavaVMExt*>(vm), true, "vpp", args); |
| 3851 | JniValueType result; |
| 3852 | result.i = BaseVm(vm)->AttachCurrentThreadAsDaemon(vm, p_env, thr_args); |
| 3853 | sc.CheckNonHeap(reinterpret_cast<JavaVMExt*>(vm), false, "i", &result); |
| 3854 | return result.i; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 3855 | } |
| 3856 | |
| 3857 | static jint DetachCurrentThread(JavaVM* vm) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3858 | ScopedCheck sc(kFlag_Invocation, __FUNCTION__); |
| 3859 | JniValueType args[1] = {{.v = vm}}; |
| 3860 | sc.CheckNonHeap(reinterpret_cast<JavaVMExt*>(vm), true, "v", args); |
| 3861 | JniValueType result; |
| 3862 | result.i = BaseVm(vm)->DetachCurrentThread(vm); |
| 3863 | sc.CheckNonHeap(reinterpret_cast<JavaVMExt*>(vm), false, "i", &result); |
| 3864 | return result.i; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 3865 | } |
| 3866 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3867 | static jint GetEnv(JavaVM* vm, void** p_env, jint version) { |
| 3868 | ScopedCheck sc(kFlag_Invocation, __FUNCTION__); |
| 3869 | JniValueType args[3] = {{.v = vm}, {.p = p_env}, {.I = version}}; |
| 3870 | sc.CheckNonHeap(reinterpret_cast<JavaVMExt*>(vm), true, "vpI", args); |
| 3871 | JniValueType result; |
| 3872 | result.i = BaseVm(vm)->GetEnv(vm, p_env, version); |
| 3873 | sc.CheckNonHeap(reinterpret_cast<JavaVMExt*>(vm), false, "i", &result); |
| 3874 | return result.i; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 3875 | } |
| 3876 | |
| 3877 | private: |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 3878 | static const JNIInvokeInterface* BaseVm(JavaVM* vm) { |
| 3879 | return reinterpret_cast<JavaVMExt*>(vm)->GetUncheckedFunctions(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 3880 | } |
| 3881 | }; |
| 3882 | |
| 3883 | const JNIInvokeInterface gCheckInvokeInterface = { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3884 | nullptr, // reserved0 |
| 3885 | nullptr, // reserved1 |
| 3886 | nullptr, // reserved2 |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 3887 | CheckJII::DestroyJavaVM, |
| 3888 | CheckJII::AttachCurrentThread, |
| 3889 | CheckJII::DetachCurrentThread, |
| 3890 | CheckJII::GetEnv, |
| 3891 | CheckJII::AttachCurrentThreadAsDaemon |
| 3892 | }; |
| 3893 | |
| 3894 | const JNIInvokeInterface* GetCheckJniInvokeInterface() { |
| 3895 | return &gCheckInvokeInterface; |
| 3896 | } |
| 3897 | |
| 3898 | } // namespace art |