blob: 5399dc52068c93b5f875fe22e0dd6b5d78cdb0e3 [file] [log] [blame]
Elliott Hughesa2501992011-08-26 19:39:54 -07001/*
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 Gampe277ccbd2014-11-03 21:36:10 -080017#include "check_jni.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070018
Andreas Gampeef4afe92015-07-27 21:03:25 -070019#include <iomanip>
Elliott Hughesa2501992011-08-26 19:39:54 -070020#include <sys/mman.h>
21#include <zlib.h>
22
Mathieu Chartierc7853442015-03-27 14:35:38 -070023#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070024#include "art_method-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "base/logging.h"
Ian Rogersc7dd2952014-10-21 23:31:19 -070026#include "base/to_str.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070027#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070029#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070030#include "gc/space/space.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070031#include "java_vm_ext.h"
Andreas Gampe277ccbd2014-11-03 21:36:10 -080032#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/object-inl.h"
35#include "mirror/object_array-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070036#include "mirror/string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/throwable.h"
Jeff Hao58df3272013-04-22 15:28:53 -070038#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070039#include "scoped_thread_state_change-inl.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070040#include "thread.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070041#include "well_known_classes.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070042
43namespace art {
44
Elliott Hughesa2501992011-08-26 19:39:54 -070045/*
46 * ===========================================================================
47 * JNI function helpers
48 * ===========================================================================
49 */
50
Elliott Hughes3f6635a2012-06-19 13:37:49 -070051// Flags passed into ScopedCheck.
Elliott Hughesa2501992011-08-26 19:39:54 -070052#define kFlag_Default 0x0000
53
Elliott Hughes3f6635a2012-06-19 13:37:49 -070054#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 Hughesa2501992011-08-26 19:39:54 -070059
Elliott Hughes3f6635a2012-06-19 13:37:49 -070060#define kFlag_ExcepBad 0x0000 // Raised exceptions are not allowed.
61#define kFlag_ExcepOkay 0x0004 // Raised exceptions are allowed.
Elliott Hughesa2501992011-08-26 19:39:54 -070062
Elliott Hughes3f6635a2012-06-19 13:37:49 -070063#define kFlag_Release 0x0010 // Are we in a non-critical release function?
64#define kFlag_NullableUtf 0x0020 // Are our UTF parameters nullable?
Elliott Hughesa2501992011-08-26 19:39:54 -070065
Elliott Hughes3f6635a2012-06-19 13:37:49 -070066#define kFlag_Invocation 0x8000 // Part of the invocation interface (JavaVM*).
Elliott Hughesa2501992011-08-26 19:39:54 -070067
Elliott Hughes485cac42011-12-09 17:49:35 -080068#define kFlag_ForceTrace 0x80000000 // Add this to a JNI function's flags if you want to trace every call.
Alex Light48ffe062015-08-19 15:23:23 -070069
70class VarArgs;
Ian Rogers68d8b422014-07-17 11:09:10 -070071/*
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 */
105union 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 Light48ffe062015-08-19 15:23:23 -0700131 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 */
140class 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 Hughesa0957642011-09-02 14:27:33 -0700241};
242
Elliott Hughesa2501992011-08-26 19:39:54 -0700243class ScopedCheck {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800244 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100245 ScopedCheck(int flags, const char* functionName, bool has_method = true)
Ian Rogers68d8b422014-07-17 11:09:10 -0700246 : function_name_(functionName), flags_(flags), indent_(0), has_method_(has_method) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700247 }
248
Ian Rogers68d8b422014-07-17 11:09:10 -0700249 ~ScopedCheck() {}
Elliott Hughesa2501992011-08-26 19:39:54 -0700250
Elliott Hughes81ff3182012-03-23 20:35:56 -0700251 // 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 Rogers68d8b422014-07-17 11:09:10 -0700255 bool CheckClassName(const char* class_name) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700256 if ((class_name == nullptr) || !IsValidJniClassName(class_name)) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700257 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 Hughesa2501992011-08-26 19:39:54 -0700261 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700262 return true;
Elliott Hughesa2501992011-08-26 19:39:54 -0700263 }
264
265 /*
266 * Verify that this instance field ID is valid for this object.
267 *
268 * Assumes "jobj" has already been validated.
269 */
Ian Rogers68d8b422014-07-17 11:09:10 -0700270 bool CheckInstanceFieldID(ScopedObjectAccess& soa, jobject java_object, jfieldID fid)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700271 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700272 ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(java_object);
Ian Rogers68d8b422014-07-17 11:09:10 -0700273 if (o == nullptr) {
274 AbortF("field operation on NULL object: %p", java_object);
275 return false;
276 }
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700277 if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(o.Ptr())) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700278 Runtime::Current()->GetHeap()->DumpSpaces(LOG_STREAM(ERROR));
Ian Rogers68d8b422014-07-17 11:09:10 -0700279 AbortF("field operation on invalid %s: %p",
Andreas Gampedc061d02016-10-24 13:19:37 -0700280 GetIndirectRefKindString(IndirectReferenceTable::GetIndirectRefKind(java_object)),
Ian Rogers68d8b422014-07-17 11:09:10 -0700281 java_object);
282 return false;
Elliott Hughesa2501992011-08-26 19:39:54 -0700283 }
284
Mathieu Chartierc7853442015-03-27 14:35:38 -0700285 ArtField* f = CheckFieldID(soa, fid);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800286 if (f == nullptr) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700287 return false;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700288 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800289 mirror::Class* c = o->GetClass();
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700290 if (c->FindInstanceField(f->GetName(), f->GetTypeDescriptor()) == nullptr) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700291 AbortF("jfieldID %s not valid for an object of class %s",
David Sehr709b0702016-10-13 09:12:37 -0700292 f->PrettyField().c_str(), o->PrettyTypeOf().c_str());
Ian Rogers68d8b422014-07-17 11:09:10 -0700293 return false;
Elliott Hughesa2501992011-08-26 19:39:54 -0700294 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700295 return true;
Elliott Hughesa2501992011-08-26 19:39:54 -0700296 }
297
298 /*
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700299 * Verify that the pointer value is non-null.
Elliott Hughesa2501992011-08-26 19:39:54 -0700300 */
Ian Rogers68d8b422014-07-17 11:09:10 -0700301 bool CheckNonNull(const void* ptr) {
302 if (UNLIKELY(ptr == nullptr)) {
303 AbortF("non-nullable argument was NULL");
304 return false;
Elliott Hughesa2501992011-08-26 19:39:54 -0700305 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700306 return true;
Elliott Hughesa2501992011-08-26 19:39:54 -0700307 }
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 Rogers68d8b422014-07-17 11:09:10 -0700313 bool CheckMethodAndSig(ScopedObjectAccess& soa, jobject jobj, jclass jc,
314 jmethodID mid, Primitive::Type type, InvokeType invoke)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700315 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700316 ArtMethod* m = CheckMethodID(soa, mid);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800317 if (m == nullptr) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700318 return false;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700319 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700320 if (type != Primitive::GetType(m->GetShorty()[0])) {
David Sehr709b0702016-10-13 09:12:37 -0700321 AbortF("the return type of %s does not match %s", function_name_, m->PrettyMethod().c_str());
Ian Rogers68d8b422014-07-17 11:09:10 -0700322 return false;
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700323 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700324 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 Sehr709b0702016-10-13 09:12:37 -0700328 m->PrettyMethod().c_str(), function_name_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700329 } else {
Ian Rogers68d8b422014-07-17 11:09:10 -0700330 AbortF("calling static method %s with %s",
David Sehr709b0702016-10-13 09:12:37 -0700331 m->PrettyMethod().c_str(), function_name_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700332 }
333 return false;
334 }
335 if (invoke != kVirtual) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700336 ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(jc);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700337 if (!m->GetDeclaringClass()->IsAssignableFrom(c)) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700338 AbortF("can't call %s %s with class %s", invoke == kStatic ? "static" : "nonvirtual",
David Sehr709b0702016-10-13 09:12:37 -0700339 m->PrettyMethod().c_str(), mirror::Class::PrettyClass(c).c_str());
Ian Rogers68d8b422014-07-17 11:09:10 -0700340 return false;
Elliott Hughesa2501992011-08-26 19:39:54 -0700341 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700342 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700343 if (invoke != kStatic) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700344 ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(jobj);
Mathieu Chartiera1e78fa2014-08-20 12:09:38 -0700345 if (o == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700346 AbortF("can't call %s on null object", m->PrettyMethod().c_str());
Mathieu Chartiera1e78fa2014-08-20 12:09:38 -0700347 return false;
348 } else if (!o->InstanceOf(m->GetDeclaringClass())) {
David Sehr709b0702016-10-13 09:12:37 -0700349 AbortF("can't call %s on instance of %s", m->PrettyMethod().c_str(),
350 o->PrettyTypeOf().c_str());
Ian Rogers68d8b422014-07-17 11:09:10 -0700351 return false;
352 }
353 }
354 return true;
Elliott Hughesa2501992011-08-26 19:39:54 -0700355 }
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 Rogers68d8b422014-07-17 11:09:10 -0700362 bool CheckStaticFieldID(ScopedObjectAccess& soa, jclass java_class, jfieldID fid)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700363 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700364 ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(java_class);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700365 ArtField* f = CheckFieldID(soa, fid);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800366 if (f == nullptr) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700367 return false;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700368 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700369 if (c != f->GetDeclaringClass()) {
David Sehr709b0702016-10-13 09:12:37 -0700370 AbortF("static jfieldID %p not valid for class %s", fid,
371 mirror::Class::PrettyClass(c).c_str());
Ian Rogers68d8b422014-07-17 11:09:10 -0700372 return false;
Elliott Hughesa2501992011-08-26 19:39:54 -0700373 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700374 return true;
Elliott Hughesa2501992011-08-26 19:39:54 -0700375 }
376
377 /*
Elliott Hughese84278b2012-03-22 10:06:53 -0700378 * Verify that "mid" is appropriate for "java_class".
Elliott Hughesa2501992011-08-26 19:39:54 -0700379 *
380 * A mismatch isn't dangerous, because the jmethodID defines the class. In
Elliott Hughese84278b2012-03-22 10:06:53 -0700381 * fact, java_class is unused in the implementation. It's best if we don't
Elliott Hughesa2501992011-08-26 19:39:54 -0700382 * allow bad code in the system though.
383 *
Elliott Hughese84278b2012-03-22 10:06:53 -0700384 * Instances of "java_class" must be instances of the method's declaring class.
Elliott Hughesa2501992011-08-26 19:39:54 -0700385 */
Ian Rogers68d8b422014-07-17 11:09:10 -0700386 bool CheckStaticMethod(ScopedObjectAccess& soa, jclass java_class, jmethodID mid)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700387 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700388 ArtMethod* m = CheckMethodID(soa, mid);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800389 if (m == nullptr) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700390 return false;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700391 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700392 ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(java_class);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700393 if (!m->GetDeclaringClass()->IsAssignableFrom(c)) {
David Sehr709b0702016-10-13 09:12:37 -0700394 AbortF("can't call static %s on class %s", m->PrettyMethod().c_str(),
395 mirror::Class::PrettyClass(c).c_str());
Ian Rogers68d8b422014-07-17 11:09:10 -0700396 return false;
Elliott Hughesa2501992011-08-26 19:39:54 -0700397 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700398 return true;
Elliott Hughesa2501992011-08-26 19:39:54 -0700399 }
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 Rogers68d8b422014-07-17 11:09:10 -0700408 bool CheckVirtualMethod(ScopedObjectAccess& soa, jobject java_object, jmethodID mid)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700409 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700410 ArtMethod* m = CheckMethodID(soa, mid);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800411 if (m == nullptr) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700412 return false;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700413 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700414 ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(java_object);
Mathieu Chartiera1e78fa2014-08-20 12:09:38 -0700415 if (o == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700416 AbortF("can't call %s on null object", m->PrettyMethod().c_str());
Mathieu Chartiera1e78fa2014-08-20 12:09:38 -0700417 return false;
418 } else if (!o->InstanceOf(m->GetDeclaringClass())) {
David Sehr709b0702016-10-13 09:12:37 -0700419 AbortF("can't call %s on instance of %s", m->PrettyMethod().c_str(),
420 o->PrettyTypeOf().c_str());
Ian Rogers68d8b422014-07-17 11:09:10 -0700421 return false;
Elliott Hughesa2501992011-08-26 19:39:54 -0700422 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700423 return true;
Elliott Hughesa2501992011-08-26 19:39:54 -0700424 }
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 Hughes78090d12011-10-07 14:31:47 -0700454 * u - const char* (Modified UTF-8)
Elliott Hughesa2501992011-08-26 19:39:54 -0700455 * z - jsize (for lengths; use i if negative values are okay)
456 * v - JavaVM*
457 * E - JNIEnv*
Alex Light48ffe062015-08-19 15:23:23 -0700458 * . - VarArgs* for Jni calls with variable length arguments
Elliott Hughesa2501992011-08-26 19:39:54 -0700459 *
460 * Use the kFlag_NullableUtf flag where 'u' field(s) are nullable.
461 */
Ian Rogers68d8b422014-07-17 11:09:10 -0700462 bool Check(ScopedObjectAccess& soa, bool entry, const char* fmt, JniValueType* args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700463 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700464 ArtMethod* traceMethod = nullptr;
Ian Rogers68d8b422014-07-17 11:09:10 -0700465 if (has_method_ && soa.Vm()->IsTracingEnabled()) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700466 // 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 Hughesa0957642011-09-02 14:27:33 -0700468 Thread* self = Thread::Current();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800469 if ((flags_ & kFlag_Invocation) == 0 || self != nullptr) {
470 traceMethod = self->GetCurrentMethod(nullptr);
Elliott Hughesa2501992011-08-26 19:39:54 -0700471 }
472 }
Elliott Hughesa0957642011-09-02 14:27:33 -0700473
Ian Rogersef7d42f2014-01-06 12:55:46 -0800474 if (((flags_ & kFlag_ForceTrace) != 0) ||
Ian Rogers68d8b422014-07-17 11:09:10 -0700475 (traceMethod != nullptr && soa.Vm()->ShouldTrace(traceMethod))) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700476 std::string msg;
Ian Rogers68d8b422014-07-17 11:09:10 -0700477 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 Hughesa2501992011-08-26 19:39:54 -0700480 StringAppendF(&msg, ", ");
481 }
482 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700483
Elliott Hughes485cac42011-12-09 17:49:35 -0800484 if ((flags_ & kFlag_ForceTrace) != 0) {
485 LOG(INFO) << "JNI: call to " << function_name_ << "(" << msg << ")";
486 } else if (entry) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700487 if (has_method_) {
David Sehr709b0702016-10-13 09:12:37 -0700488 std::string methodName(ArtMethod::PrettyMethod(traceMethod, false));
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700489 LOG(INFO) << "JNI: " << methodName << " -> " << function_name_ << "(" << msg << ")";
490 indent_ = methodName.size() + 1;
Elliott Hughesa2501992011-08-26 19:39:54 -0700491 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700492 LOG(INFO) << "JNI: -> " << function_name_ << "(" << msg << ")";
493 indent_ = 0;
Elliott Hughesa2501992011-08-26 19:39:54 -0700494 }
495 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700496 LOG(INFO) << StringPrintf("JNI: %*s<- %s returned %s", indent_, "", function_name_, msg.c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700497 }
498 }
499
500 // We always do the thorough checks on entry, and never on exit...
501 if (entry) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700502 for (size_t i = 0; fmt[i] != '\0'; ++i) {
503 if (!CheckPossibleHeapValue(soa, fmt[i], args[i])) {
504 return false;
Elliott Hughesa2501992011-08-26 19:39:54 -0700505 }
506 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700507 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700508 return true;
Elliott Hughesa2501992011-08-26 19:39:54 -0700509 }
510
Ian Rogers68d8b422014-07-17 11:09:10 -0700511 bool CheckNonHeap(JavaVMExt* vm, bool entry, const char* fmt, JniValueType* args) {
512 bool should_trace = (flags_ & kFlag_ForceTrace) != 0;
Andreas Gampedef194e2015-02-19 15:19:50 -0800513 if (!should_trace && vm != nullptr && vm->IsTracingEnabled()) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700514 // 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 Chartiere401d142015-04-22 13:56:20 -0700519 ArtMethod* traceMethod = self->GetCurrentMethod(nullptr);
Ian Rogers68d8b422014-07-17 11:09:10 -0700520 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 Chartiere401d142015-04-22 13:56:20 -0700538 ArtMethod* traceMethod = self->GetCurrentMethod(nullptr);
David Sehr709b0702016-10-13 09:12:37 -0700539 std::string methodName(ArtMethod::PrettyMethod(traceMethod, false));
Ian Rogers68d8b422014-07-17 11:09:10 -0700540 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 Gampebdf7f1c2016-08-30 16:38:47 -0700563 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700564 ObjPtr<mirror::Object> method = soa.Decode<mirror::Object>(jmethod);
Ian Rogers68d8b422014-07-17 11:09:10 -0700565 if (method == nullptr) {
566 AbortF("expected non-null method");
567 return false;
568 }
569 mirror::Class* c = method->GetClass();
Mathieu Chartier0795f232016-09-27 18:43:30 -0700570 if (soa.Decode<mirror::Class>(WellKnownClasses::java_lang_reflect_Method) != c &&
571 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_reflect_Constructor) != c) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700572 AbortF("expected java.lang.reflect.Method or "
573 "java.lang.reflect.Constructor but got object of type %s: %p",
David Sehr709b0702016-10-13 09:12:37 -0700574 method->PrettyTypeOf().c_str(), jmethod);
Ian Rogers68d8b422014-07-17 11:09:10 -0700575 return false;
576 }
577 return true;
578 }
579
580 bool CheckConstructor(ScopedObjectAccess& soa, jmethodID mid)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700581 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700582 ArtMethod* method = soa.DecodeMethod(mid);
Ian Rogers68d8b422014-07-17 11:09:10 -0700583 if (method == nullptr) {
584 AbortF("expected non-null constructor");
585 return false;
586 }
587 if (!method->IsConstructor() || method->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -0700588 AbortF("expected a constructor but %s: %p", method->PrettyMethod().c_str(), mid);
Ian Rogers68d8b422014-07-17 11:09:10 -0700589 return false;
590 }
591 return true;
592 }
593
594 bool CheckReflectedField(ScopedObjectAccess& soa, jobject jfield)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700595 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700596 ObjPtr<mirror::Object> field = soa.Decode<mirror::Object>(jfield);
Ian Rogers68d8b422014-07-17 11:09:10 -0700597 if (field == nullptr) {
598 AbortF("expected non-null java.lang.reflect.Field");
599 return false;
600 }
601 mirror::Class* c = field->GetClass();
Mathieu Chartier0795f232016-09-27 18:43:30 -0700602 if (soa.Decode<mirror::Class>(WellKnownClasses::java_lang_reflect_Field) != c) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700603 AbortF("expected java.lang.reflect.Field but got object of type %s: %p",
David Sehr709b0702016-10-13 09:12:37 -0700604 field->PrettyTypeOf().c_str(), jfield);
Ian Rogers68d8b422014-07-17 11:09:10 -0700605 return false;
606 }
607 return true;
608 }
609
610 bool CheckThrowable(ScopedObjectAccess& soa, jthrowable jobj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700611 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700612 ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(jobj);
Ian Rogers68d8b422014-07-17 11:09:10 -0700613 if (!obj->GetClass()->IsThrowableClass()) {
614 AbortF("expected java.lang.Throwable but got object of type "
David Sehr709b0702016-10-13 09:12:37 -0700615 "%s: %p", obj->PrettyTypeOf().c_str(), obj.Ptr());
Ian Rogers68d8b422014-07-17 11:09:10 -0700616 return false;
617 }
618 return true;
619 }
620
621 bool CheckThrowableClass(ScopedObjectAccess& soa, jclass jc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700622 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700623 ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(jc);
Ian Rogers68d8b422014-07-17 11:09:10 -0700624 if (!c->IsThrowableClass()) {
625 AbortF("expected java.lang.Throwable class but got object of "
David Sehr709b0702016-10-13 09:12:37 -0700626 "type %s: %p", c->PrettyDescriptor().c_str(), c.Ptr());
Ian Rogers68d8b422014-07-17 11:09:10 -0700627 return false;
628 }
629 return true;
630 }
631
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700632 bool CheckReferenceKind(IndirectRefKind expected_kind, Thread* self, jobject obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700633 IndirectRefKind found_kind;
634 if (expected_kind == kLocal) {
Andreas Gampedc061d02016-10-24 13:19:37 -0700635 found_kind = IndirectReferenceTable::GetIndirectRefKind(obj);
Ian Rogers68d8b422014-07-17 11:09:10 -0700636 if (found_kind == kHandleScopeOrInvalid && self->HandleScopeContains(obj)) {
637 found_kind = kLocal;
638 }
639 } else {
Andreas Gampedc061d02016-10-24 13:19:37 -0700640 found_kind = IndirectReferenceTable::GetIndirectRefKind(obj);
Ian Rogers68d8b422014-07-17 11:09:10 -0700641 }
642 if (obj != nullptr && found_kind != expected_kind) {
643 AbortF("expected reference of kind %s but found %s: %p",
Andreas Gampedc061d02016-10-24 13:19:37 -0700644 GetIndirectRefKindString(expected_kind),
645 GetIndirectRefKindString(IndirectReferenceTable::GetIndirectRefKind(obj)),
Ian Rogers68d8b422014-07-17 11:09:10 -0700646 obj);
647 return false;
648 }
649 return true;
650 }
651
652 bool CheckInstantiableNonArray(ScopedObjectAccess& soa, jclass jc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700653 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700654 ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(jc);
Ian Rogers68d8b422014-07-17 11:09:10 -0700655 if (!c->IsInstantiableNonArray()) {
David Sehr709b0702016-10-13 09:12:37 -0700656 AbortF("can't make objects of type %s: %p", c->PrettyDescriptor().c_str(), c.Ptr());
Ian Rogers68d8b422014-07-17 11:09:10 -0700657 return false;
658 }
659 return true;
660 }
661
662 bool CheckPrimitiveArrayType(ScopedObjectAccess& soa, jarray array, Primitive::Type type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700663 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700664 if (!CheckArray(soa, array)) {
665 return false;
666 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700667 ObjPtr<mirror::Array> a = soa.Decode<mirror::Array>(array);
Ian Rogers68d8b422014-07-17 11:09:10 -0700668 if (a->GetClass()->GetComponentType()->GetPrimitiveType() != type) {
669 AbortF("incompatible array type %s expected %s[]: %p",
David Sehr709b0702016-10-13 09:12:37 -0700670 a->GetClass()->PrettyDescriptor().c_str(), PrettyDescriptor(type).c_str(), array);
Ian Rogers68d8b422014-07-17 11:09:10 -0700671 return false;
672 }
673 return true;
674 }
675
676 bool CheckFieldAccess(ScopedObjectAccess& soa, jobject obj, jfieldID fid, bool is_static,
677 Primitive::Type type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700678 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700679 if (is_static && !CheckStaticFieldID(soa, down_cast<jclass>(obj), fid)) {
680 return false;
681 }
682 if (!is_static && !CheckInstanceFieldID(soa, obj, fid)) {
683 return false;
684 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700685 ArtField* field = soa.DecodeField(fid);
Ian Rogers68d8b422014-07-17 11:09:10 -0700686 DCHECK(field != nullptr); // Already checked by Check.
687 if (is_static != field->IsStatic()) {
688 AbortF("attempt to access %s field %s: %p",
David Sehr709b0702016-10-13 09:12:37 -0700689 field->IsStatic() ? "static" : "non-static", field->PrettyField().c_str(), fid);
Ian Rogers68d8b422014-07-17 11:09:10 -0700690 return false;
691 }
692 if (type != field->GetTypeAsPrimitiveType()) {
693 AbortF("attempt to access field %s of type %s with the wrong type %s: %p",
David Sehr709b0702016-10-13 09:12:37 -0700694 field->PrettyField().c_str(),
695 PrettyDescriptor(field->GetTypeDescriptor()).c_str(),
Ian Rogers68d8b422014-07-17 11:09:10 -0700696 PrettyDescriptor(type).c_str(), fid);
697 return false;
698 }
699 if (is_static) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700700 ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(obj);
Ian Rogers68d8b422014-07-17 11:09:10 -0700701 if (o == nullptr || !o->IsClass()) {
702 AbortF("attempt to access static field %s with a class argument of type %s: %p",
David Sehr709b0702016-10-13 09:12:37 -0700703 field->PrettyField().c_str(), o->PrettyTypeOf().c_str(), fid);
Ian Rogers68d8b422014-07-17 11:09:10 -0700704 return false;
705 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700706 ObjPtr<mirror::Class> c = o->AsClass();
707 if (c != field->GetDeclaringClass()) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700708 AbortF("attempt to access static field %s with an incompatible class argument of %s: %p",
David Sehr709b0702016-10-13 09:12:37 -0700709 field->PrettyField().c_str(), mirror::Class::PrettyDescriptor(c).c_str(), fid);
Ian Rogers68d8b422014-07-17 11:09:10 -0700710 return false;
711 }
712 } else {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700713 ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(obj);
Ian Rogers68d8b422014-07-17 11:09:10 -0700714 if (o == nullptr || !field->GetDeclaringClass()->IsAssignableFrom(o->GetClass())) {
715 AbortF("attempt to access field %s from an object argument of type %s: %p",
David Sehr709b0702016-10-13 09:12:37 -0700716 field->PrettyField().c_str(), o->PrettyTypeOf().c_str(), fid);
Ian Rogers68d8b422014-07-17 11:09:10 -0700717 return false;
718 }
719 }
720 return true;
721 }
722
723 private:
Elliott Hughesa92853e2012-02-07 16:09:27 -0800724 enum InstanceKind {
725 kClass,
Elliott Hughes0f3c5532012-03-30 14:51:51 -0700726 kDirectByteBuffer,
727 kObject,
728 kString,
729 kThrowable,
Elliott Hughesa92853e2012-02-07 16:09:27 -0800730 };
731
732 /*
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700733 * Verify that "jobj" is a valid non-null object reference, and points to
Elliott Hughesa92853e2012-02-07 16:09:27 -0800734 * an instance of expectedClass.
735 *
736 * Because we're looking at an object on the GC heap, we have to switch
737 * to "running" mode before doing the checks.
738 */
Ian Rogers68d8b422014-07-17 11:09:10 -0700739 bool CheckInstance(ScopedObjectAccess& soa, InstanceKind kind, jobject java_object, bool null_ok)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700740 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800741 const char* what = nullptr;
Elliott Hughesa92853e2012-02-07 16:09:27 -0800742 switch (kind) {
743 case kClass:
744 what = "jclass";
745 break;
746 case kDirectByteBuffer:
747 what = "direct ByteBuffer";
748 break;
749 case kObject:
750 what = "jobject";
751 break;
752 case kString:
753 what = "jstring";
754 break;
755 case kThrowable:
756 what = "jthrowable";
757 break;
758 default:
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700759 LOG(FATAL) << "Unknown kind " << static_cast<int>(kind);
Elliott Hughesa92853e2012-02-07 16:09:27 -0800760 }
761
Ian Rogersef7d42f2014-01-06 12:55:46 -0800762 if (java_object == nullptr) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700763 if (null_ok) {
764 return true;
765 } else {
766 AbortF("%s received NULL %s", function_name_, what);
767 return false;
768 }
Elliott Hughesa92853e2012-02-07 16:09:27 -0800769 }
770
Mathieu Chartier0795f232016-09-27 18:43:30 -0700771 ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(java_object);
Ian Rogersc0542af2014-09-03 16:16:56 -0700772 if (obj == nullptr) {
773 // Either java_object is invalid or is a cleared weak.
774 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
775 bool okay;
Andreas Gampedc061d02016-10-24 13:19:37 -0700776 if (IndirectReferenceTable::GetIndirectRefKind(ref) != kWeakGlobal) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700777 okay = false;
778 } else {
779 obj = soa.Vm()->DecodeWeakGlobal(soa.Self(), ref);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700780 okay = Runtime::Current()->IsClearedJniWeakGlobal(obj);
Ian Rogersc0542af2014-09-03 16:16:56 -0700781 }
782 if (!okay) {
783 AbortF("%s is an invalid %s: %p (%p)",
Andreas Gampedc061d02016-10-24 13:19:37 -0700784 what,
785 GetIndirectRefKindString(IndirectReferenceTable::GetIndirectRefKind(java_object)),
786 java_object,
787 obj.Ptr());
Ian Rogersc0542af2014-09-03 16:16:56 -0700788 return false;
789 }
790 }
791
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700792 if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(obj.Ptr())) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700793 Runtime::Current()->GetHeap()->DumpSpaces(LOG_STREAM(ERROR));
Ian Rogers68d8b422014-07-17 11:09:10 -0700794 AbortF("%s is an invalid %s: %p (%p)",
Andreas Gampedc061d02016-10-24 13:19:37 -0700795 what,
796 GetIndirectRefKindString(IndirectReferenceTable::GetIndirectRefKind(java_object)),
797 java_object,
798 obj.Ptr());
Elliott Hughesa92853e2012-02-07 16:09:27 -0800799 return false;
800 }
801
802 bool okay = true;
803 switch (kind) {
804 case kClass:
805 okay = obj->IsClass();
806 break;
807 case kDirectByteBuffer:
808 UNIMPLEMENTED(FATAL);
809 break;
810 case kString:
811 okay = obj->GetClass()->IsStringClass();
812 break;
813 case kThrowable:
814 okay = obj->GetClass()->IsThrowableClass();
815 break;
816 case kObject:
817 break;
818 }
819 if (!okay) {
David Sehr709b0702016-10-13 09:12:37 -0700820 AbortF("%s has wrong type: %s", what, mirror::Object::PrettyTypeOf(obj).c_str());
Elliott Hughesa92853e2012-02-07 16:09:27 -0800821 return false;
822 }
823
824 return true;
825 }
826
Ian Rogers68d8b422014-07-17 11:09:10 -0700827 /*
828 * Verify that the "mode" argument passed to a primitive array Release
829 * function is one of the valid values.
830 */
831 bool CheckReleaseMode(jint mode) {
832 if (mode != 0 && mode != JNI_COMMIT && mode != JNI_ABORT) {
833 AbortF("unknown value for release mode: %d", mode);
834 return false;
835 }
836 return true;
Elliott Hughesa2501992011-08-26 19:39:54 -0700837 }
838
Ian Rogers68d8b422014-07-17 11:09:10 -0700839 bool CheckPossibleHeapValue(ScopedObjectAccess& soa, char fmt, JniValueType arg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700840 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700841 switch (fmt) {
842 case 'a': // jarray
843 return CheckArray(soa, arg.a);
844 case 'c': // jclass
845 return CheckInstance(soa, kClass, arg.c, false);
846 case 'f': // jfieldID
847 return CheckFieldID(soa, arg.f) != nullptr;
848 case 'm': // jmethodID
849 return CheckMethodID(soa, arg.m) != nullptr;
850 case 'r': // release int
851 return CheckReleaseMode(arg.r);
852 case 's': // jstring
853 return CheckInstance(soa, kString, arg.s, false);
854 case 't': // jthrowable
855 return CheckInstance(soa, kThrowable, arg.t, false);
856 case 'E': // JNIEnv*
857 return CheckThread(arg.E);
858 case 'L': // jobject
859 return CheckInstance(soa, kObject, arg.L, true);
Alex Light48ffe062015-08-19 15:23:23 -0700860 case '.': // A VarArgs list
861 return CheckVarArgs(soa, arg.va);
Ian Rogers68d8b422014-07-17 11:09:10 -0700862 default:
863 return CheckNonHeapValue(fmt, arg);
864 }
865 }
866
Alex Light48ffe062015-08-19 15:23:23 -0700867 bool CheckVarArgs(ScopedObjectAccess& soa, const VarArgs* args_p)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700868 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Light48ffe062015-08-19 15:23:23 -0700869 CHECK(args_p != nullptr);
870 VarArgs args(args_p->Clone());
871 ArtMethod* m = CheckMethodID(soa, args.GetMethodID());
872 if (m == nullptr) {
873 return false;
874 }
875 uint32_t len = 0;
876 const char* shorty = m->GetShorty(&len);
877 // Skip the return type
878 CHECK_GE(len, 1u);
879 len--;
880 shorty++;
881 for (uint32_t i = 0; i < len; i++) {
882 if (!CheckPossibleHeapValue(soa, shorty[i], args.GetValue(shorty[i]))) {
883 return false;
884 }
885 }
886 return true;
887 }
888
Ian Rogers68d8b422014-07-17 11:09:10 -0700889 bool CheckNonHeapValue(char fmt, JniValueType arg) {
890 switch (fmt) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700891 case 'p': // TODO: pointer - null or readable?
892 case 'v': // JavaVM*
893 case 'B': // jbyte
894 case 'C': // jchar
895 case 'D': // jdouble
896 case 'F': // jfloat
897 case 'I': // jint
898 case 'J': // jlong
899 case 'S': // jshort
900 break; // Ignored.
901 case 'b': // jboolean, why two? Fall-through.
902 case 'Z':
903 return CheckBoolean(arg.Z);
904 case 'u': // utf8
905 if ((flags_ & kFlag_Release) != 0) {
906 return CheckNonNull(arg.u);
907 } else {
908 bool nullable = ((flags_ & kFlag_NullableUtf) != 0);
909 return CheckUtfString(arg.u, nullable);
910 }
911 case 'w': // jobjectRefType
912 switch (arg.w) {
913 case JNIInvalidRefType:
914 case JNILocalRefType:
915 case JNIGlobalRefType:
916 case JNIWeakGlobalRefType:
917 break;
918 default:
919 AbortF("Unknown reference type");
920 return false;
921 }
922 break;
923 case 'z': // jsize
924 return CheckLengthPositive(arg.z);
925 default:
926 AbortF("unknown format specifier: '%c'", fmt);
927 return false;
928 }
929 return true;
930 }
931
932 void TracePossibleHeapValue(ScopedObjectAccess& soa, bool entry, char fmt, JniValueType arg,
933 std::string* msg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700934 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700935 switch (fmt) {
936 case 'L': // jobject fall-through.
937 case 'a': // jarray fall-through.
938 case 's': // jstring fall-through.
939 case 't': // jthrowable fall-through.
940 if (arg.L == nullptr) {
941 *msg += "NULL";
942 } else {
943 StringAppendF(msg, "%p", arg.L);
944 }
945 break;
946 case 'c': { // jclass
947 jclass jc = arg.c;
Mathieu Chartier0795f232016-09-27 18:43:30 -0700948 ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(jc);
Ian Rogers68d8b422014-07-17 11:09:10 -0700949 if (c == nullptr) {
950 *msg += "NULL";
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700951 } else if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(c.Ptr())) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700952 StringAppendF(msg, "INVALID POINTER:%p", jc);
953 } else if (!c->IsClass()) {
David Sehr709b0702016-10-13 09:12:37 -0700954 *msg += "INVALID NON-CLASS OBJECT OF TYPE:" + c->PrettyTypeOf();
Ian Rogers68d8b422014-07-17 11:09:10 -0700955 } else {
David Sehr709b0702016-10-13 09:12:37 -0700956 *msg += c->PrettyClass();
Ian Rogers68d8b422014-07-17 11:09:10 -0700957 if (!entry) {
958 StringAppendF(msg, " (%p)", jc);
959 }
960 }
961 break;
962 }
963 case 'f': { // jfieldID
964 jfieldID fid = arg.f;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700965 ArtField* f = soa.DecodeField(fid);
David Sehr709b0702016-10-13 09:12:37 -0700966 *msg += ArtField::PrettyField(f);
Ian Rogers68d8b422014-07-17 11:09:10 -0700967 if (!entry) {
968 StringAppendF(msg, " (%p)", fid);
969 }
970 break;
971 }
972 case 'm': { // jmethodID
973 jmethodID mid = arg.m;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700974 ArtMethod* m = soa.DecodeMethod(mid);
David Sehr709b0702016-10-13 09:12:37 -0700975 *msg += ArtMethod::PrettyMethod(m);
Ian Rogers68d8b422014-07-17 11:09:10 -0700976 if (!entry) {
977 StringAppendF(msg, " (%p)", mid);
978 }
979 break;
980 }
Alex Light48ffe062015-08-19 15:23:23 -0700981 case '.': {
982 const VarArgs* va = arg.va;
983 VarArgs args(va->Clone());
984 ArtMethod* m = soa.DecodeMethod(args.GetMethodID());
985 uint32_t len;
986 const char* shorty = m->GetShorty(&len);
987 CHECK_GE(len, 1u);
988 // Skip past return value.
989 len--;
990 shorty++;
991 // Remove the previous ', ' from the message.
992 msg->erase(msg->length() - 2);
993 for (uint32_t i = 0; i < len; i++) {
994 *msg += ", ";
995 TracePossibleHeapValue(soa, entry, shorty[i], args.GetValue(shorty[i]), msg);
996 }
997 break;
998 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700999 default:
1000 TraceNonHeapValue(fmt, arg, msg);
1001 break;
1002 }
1003 }
1004
1005 void TraceNonHeapValue(char fmt, JniValueType arg, std::string* msg) {
1006 switch (fmt) {
1007 case 'B': // jbyte
1008 if (arg.B >= 0 && arg.B < 10) {
1009 StringAppendF(msg, "%d", arg.B);
1010 } else {
1011 StringAppendF(msg, "%#x (%d)", arg.B, arg.B);
1012 }
1013 break;
1014 case 'C': // jchar
1015 if (arg.C < 0x7f && arg.C >= ' ') {
1016 StringAppendF(msg, "U+%x ('%c')", arg.C, arg.C);
1017 } else {
1018 StringAppendF(msg, "U+%x", arg.C);
1019 }
1020 break;
1021 case 'F': // jfloat
1022 StringAppendF(msg, "%g", arg.F);
1023 break;
1024 case 'D': // jdouble
1025 StringAppendF(msg, "%g", arg.D);
1026 break;
1027 case 'S': // jshort
1028 StringAppendF(msg, "%d", arg.S);
1029 break;
1030 case 'i': // jint - fall-through.
1031 case 'I': // jint
1032 StringAppendF(msg, "%d", arg.I);
1033 break;
1034 case 'J': // jlong
1035 StringAppendF(msg, "%" PRId64, arg.J);
1036 break;
1037 case 'Z': // jboolean
1038 case 'b': // jboolean (JNI-style)
1039 *msg += arg.b == JNI_TRUE ? "true" : "false";
1040 break;
1041 case 'V': // void
1042 DCHECK(arg.V == nullptr);
1043 *msg += "void";
1044 break;
1045 case 'v': // JavaVM*
1046 StringAppendF(msg, "(JavaVM*)%p", arg.v);
1047 break;
1048 case 'E':
1049 StringAppendF(msg, "(JNIEnv*)%p", arg.E);
1050 break;
1051 case 'z': // non-negative jsize
1052 // You might expect jsize to be size_t, but it's not; it's the same as jint.
1053 // We only treat this specially so we can do the non-negative check.
1054 // TODO: maybe this wasn't worth it?
1055 StringAppendF(msg, "%d", arg.z);
1056 break;
1057 case 'p': // void* ("pointer")
1058 if (arg.p == nullptr) {
1059 *msg += "NULL";
1060 } else {
1061 StringAppendF(msg, "(void*) %p", arg.p);
1062 }
1063 break;
1064 case 'r': { // jint (release mode)
1065 jint releaseMode = arg.r;
1066 if (releaseMode == 0) {
1067 *msg += "0";
1068 } else if (releaseMode == JNI_ABORT) {
1069 *msg += "JNI_ABORT";
1070 } else if (releaseMode == JNI_COMMIT) {
1071 *msg += "JNI_COMMIT";
1072 } else {
1073 StringAppendF(msg, "invalid release mode %d", releaseMode);
1074 }
1075 break;
1076 }
1077 case 'u': // const char* (Modified UTF-8)
1078 if (arg.u == nullptr) {
1079 *msg += "NULL";
1080 } else {
1081 StringAppendF(msg, "\"%s\"", arg.u);
1082 }
1083 break;
1084 case 'w': // jobjectRefType
1085 switch (arg.w) {
1086 case JNIInvalidRefType:
1087 *msg += "invalid reference type";
1088 break;
1089 case JNILocalRefType:
1090 *msg += "local ref type";
1091 break;
1092 case JNIGlobalRefType:
1093 *msg += "global ref type";
1094 break;
1095 case JNIWeakGlobalRefType:
1096 *msg += "weak global ref type";
1097 break;
1098 default:
1099 *msg += "unknown ref type";
1100 break;
1101 }
1102 break;
Ian Rogers68d8b422014-07-17 11:09:10 -07001103 default:
1104 LOG(FATAL) << function_name_ << ": unknown trace format specifier: '" << fmt << "'";
1105 }
1106 }
Elliott Hughesa2501992011-08-26 19:39:54 -07001107 /*
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001108 * Verify that "array" is non-null and points to an Array object.
Elliott Hughesa2501992011-08-26 19:39:54 -07001109 *
1110 * Since we're dealing with objects, switch to "running" mode.
1111 */
Ian Rogers68d8b422014-07-17 11:09:10 -07001112 bool CheckArray(ScopedObjectAccess& soa, jarray java_array)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001113 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001114 if (UNLIKELY(java_array == nullptr)) {
1115 AbortF("jarray was NULL");
1116 return false;
Elliott Hughesa2501992011-08-26 19:39:54 -07001117 }
1118
Mathieu Chartier0795f232016-09-27 18:43:30 -07001119 ObjPtr<mirror::Array> a = soa.Decode<mirror::Array>(java_array);
Mathieu Chartier9d156d52016-10-06 17:44:26 -07001120 if (UNLIKELY(!Runtime::Current()->GetHeap()->IsValidObjectAddress(a.Ptr()))) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -07001121 Runtime::Current()->GetHeap()->DumpSpaces(LOG_STREAM(ERROR));
Ian Rogers68d8b422014-07-17 11:09:10 -07001122 AbortF("jarray is an invalid %s: %p (%p)",
Andreas Gampedc061d02016-10-24 13:19:37 -07001123 GetIndirectRefKindString(IndirectReferenceTable::GetIndirectRefKind(java_array)),
1124 java_array,
1125 a.Ptr());
Ian Rogers68d8b422014-07-17 11:09:10 -07001126 return false;
Elliott Hughesa2501992011-08-26 19:39:54 -07001127 } else if (!a->IsArrayInstance()) {
David Sehr709b0702016-10-13 09:12:37 -07001128 AbortF("jarray argument has non-array type: %s", a->PrettyTypeOf().c_str());
Ian Rogers68d8b422014-07-17 11:09:10 -07001129 return false;
Elliott Hughesa2501992011-08-26 19:39:54 -07001130 }
Ian Rogers68d8b422014-07-17 11:09:10 -07001131 return true;
Elliott Hughesa2501992011-08-26 19:39:54 -07001132 }
1133
Ian Rogers68d8b422014-07-17 11:09:10 -07001134 bool CheckBoolean(jboolean z) {
1135 if (z != JNI_TRUE && z != JNI_FALSE) {
1136 AbortF("unexpected jboolean value: %d", z);
1137 return false;
1138 }
1139 return true;
1140 }
1141
1142 bool CheckLengthPositive(jsize length) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001143 if (length < 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001144 AbortF("negative jsize: %d", length);
1145 return false;
Elliott Hughesa2501992011-08-26 19:39:54 -07001146 }
Ian Rogers68d8b422014-07-17 11:09:10 -07001147 return true;
Elliott Hughesa2501992011-08-26 19:39:54 -07001148 }
1149
Mathieu Chartierc7853442015-03-27 14:35:38 -07001150 ArtField* CheckFieldID(ScopedObjectAccess& soa, jfieldID fid)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001151 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001152 if (fid == nullptr) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001153 AbortF("jfieldID was NULL");
Ian Rogersef7d42f2014-01-06 12:55:46 -08001154 return nullptr;
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001155 }
Mathieu Chartierc7853442015-03-27 14:35:38 -07001156 ArtField* f = soa.DecodeField(fid);
1157 // TODO: Better check here.
Mathieu Chartier9d156d52016-10-06 17:44:26 -07001158 if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(f->GetDeclaringClass().Ptr())) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -07001159 Runtime::Current()->GetHeap()->DumpSpaces(LOG_STREAM(ERROR));
Ian Rogers68d8b422014-07-17 11:09:10 -07001160 AbortF("invalid jfieldID: %p", fid);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001161 return nullptr;
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001162 }
1163 return f;
1164 }
1165
Mathieu Chartiere401d142015-04-22 13:56:20 -07001166 ArtMethod* CheckMethodID(ScopedObjectAccess& soa, jmethodID mid)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001167 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001168 if (mid == nullptr) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001169 AbortF("jmethodID was NULL");
Ian Rogersef7d42f2014-01-06 12:55:46 -08001170 return nullptr;
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001171 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001172 ArtMethod* m = soa.DecodeMethod(mid);
1173 // TODO: Better check here.
1174 if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(m->GetDeclaringClass())) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -07001175 Runtime::Current()->GetHeap()->DumpSpaces(LOG_STREAM(ERROR));
Ian Rogers68d8b422014-07-17 11:09:10 -07001176 AbortF("invalid jmethodID: %p", mid);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001177 return nullptr;
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001178 }
1179 return m;
1180 }
1181
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001182 bool CheckThread(JNIEnv* env) REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001183 Thread* self = Thread::Current();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001184 if (self == nullptr) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001185 AbortF("a thread (tid %d) is making JNI calls without being attached", GetTid());
1186 return false;
Elliott Hughesa2501992011-08-26 19:39:54 -07001187 }
1188
Daniel Erat35e827a2016-05-10 18:07:18 -06001189 // Get the current thread's JNIEnv by going through our TLS pointer.
Elliott Hughesa2501992011-08-26 19:39:54 -07001190 JNIEnvExt* threadEnv = self->GetJniEnv();
1191
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001192 // Verify that the current thread is (a) attached and (b) associated with
1193 // this particular instance of JNIEnv.
Ian Rogers68d8b422014-07-17 11:09:10 -07001194 if (env != threadEnv) {
Daniel Erat35e827a2016-05-10 18:07:18 -06001195 // Get the thread owning the JNIEnv that's being used.
1196 Thread* envThread = reinterpret_cast<JNIEnvExt*>(env)->self;
Ian Rogers68d8b422014-07-17 11:09:10 -07001197 AbortF("thread %s using JNIEnv* from thread %s",
Daniel Erat35e827a2016-05-10 18:07:18 -06001198 ToStr<Thread>(*self).c_str(), ToStr<Thread>(*envThread).c_str());
Ian Rogers68d8b422014-07-17 11:09:10 -07001199 return false;
Elliott Hughesa2501992011-08-26 19:39:54 -07001200 }
1201
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001202 // Verify that, if this thread previously made a critical "get" call, we
1203 // do the corresponding "release" call before we try anything else.
Ian Rogers68d8b422014-07-17 11:09:10 -07001204 switch (flags_ & kFlag_CritMask) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001205 case kFlag_CritOkay: // okay to call this method
1206 break;
1207 case kFlag_CritBad: // not okay to call
1208 if (threadEnv->critical) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001209 AbortF("thread %s using JNI after critical get",
1210 ToStr<Thread>(*self).c_str());
1211 return false;
Elliott Hughesa2501992011-08-26 19:39:54 -07001212 }
1213 break;
1214 case kFlag_CritGet: // this is a "get" call
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001215 // Don't check here; we allow nested gets.
Elliott Hughesa2501992011-08-26 19:39:54 -07001216 threadEnv->critical++;
1217 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001218 case kFlag_CritRelease: // this is a "release" call
Elliott Hughesa2501992011-08-26 19:39:54 -07001219 threadEnv->critical--;
1220 if (threadEnv->critical < 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001221 AbortF("thread %s called too many critical releases",
1222 ToStr<Thread>(*self).c_str());
1223 return false;
Elliott Hughesa2501992011-08-26 19:39:54 -07001224 }
1225 break;
1226 default:
Ian Rogers68d8b422014-07-17 11:09:10 -07001227 LOG(FATAL) << "Bad flags (internal error): " << flags_;
Elliott Hughesa2501992011-08-26 19:39:54 -07001228 }
1229
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001230 // Verify that, if an exception has been raised, the native code doesn't
1231 // make any JNI calls other than the Exception* methods.
Ian Rogers68d8b422014-07-17 11:09:10 -07001232 if ((flags_ & kFlag_ExcepOkay) == 0 && self->IsExceptionPending()) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +00001233 mirror::Throwable* exception = self->GetException();
1234 AbortF("JNI %s called with pending exception %s",
1235 function_name_,
1236 exception->Dump().c_str());
Ian Rogers68d8b422014-07-17 11:09:10 -07001237 return false;
Elliott Hughesa2501992011-08-26 19:39:54 -07001238 }
Ian Rogers68d8b422014-07-17 11:09:10 -07001239 return true;
Elliott Hughesa2501992011-08-26 19:39:54 -07001240 }
1241
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001242 // Verifies that "bytes" points to valid Modified UTF-8 data.
Ian Rogers68d8b422014-07-17 11:09:10 -07001243 bool CheckUtfString(const char* bytes, bool nullable) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001244 if (bytes == nullptr) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001245 if (!nullable) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001246 AbortF("non-nullable const char* was NULL");
1247 return false;
Elliott Hughesa2501992011-08-26 19:39:54 -07001248 }
Ian Rogers68d8b422014-07-17 11:09:10 -07001249 return true;
Elliott Hughesa2501992011-08-26 19:39:54 -07001250 }
1251
Ian Rogersef7d42f2014-01-06 12:55:46 -08001252 const char* errorKind = nullptr;
Andreas Gampeef4afe92015-07-27 21:03:25 -07001253 const uint8_t* utf8 = CheckUtfBytes(bytes, &errorKind);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001254 if (errorKind != nullptr) {
Andreas Gampeef4afe92015-07-27 21:03:25 -07001255 // This is an expensive loop that will resize often, but this isn't supposed to hit in
1256 // practice anyways.
1257 std::ostringstream oss;
1258 oss << std::hex;
1259 const uint8_t* tmp = reinterpret_cast<const uint8_t*>(bytes);
1260 while (*tmp != 0) {
1261 if (tmp == utf8) {
1262 oss << "<";
1263 }
1264 oss << "0x" << std::setfill('0') << std::setw(2) << static_cast<uint32_t>(*tmp);
1265 if (tmp == utf8) {
1266 oss << '>';
1267 }
1268 tmp++;
1269 if (*tmp != 0) {
1270 oss << ' ';
1271 }
1272 }
1273
Ian Rogers68d8b422014-07-17 11:09:10 -07001274 AbortF("input is not valid Modified UTF-8: illegal %s byte %#x\n"
Andreas Gampeef4afe92015-07-27 21:03:25 -07001275 " string: '%s'\n input: '%s'", errorKind, *utf8, bytes, oss.str().c_str());
Ian Rogers68d8b422014-07-17 11:09:10 -07001276 return false;
Elliott Hughesa2501992011-08-26 19:39:54 -07001277 }
Ian Rogers68d8b422014-07-17 11:09:10 -07001278 return true;
Elliott Hughesa2501992011-08-26 19:39:54 -07001279 }
1280
Narayan Kamatha5afcfc2015-01-29 20:06:46 +00001281 // Checks whether |bytes| is valid modified UTF-8. We also accept 4 byte UTF
1282 // sequences in place of encoded surrogate pairs.
Andreas Gampeef4afe92015-07-27 21:03:25 -07001283 static const uint8_t* CheckUtfBytes(const char* bytes, const char** errorKind) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001284 while (*bytes != '\0') {
Andreas Gampeef4afe92015-07-27 21:03:25 -07001285 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>(bytes++);
Elliott Hughesa2501992011-08-26 19:39:54 -07001286 // Switch on the high four bits.
Andreas Gampeef4afe92015-07-27 21:03:25 -07001287 switch (*utf8 >> 4) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001288 case 0x00:
1289 case 0x01:
1290 case 0x02:
1291 case 0x03:
1292 case 0x04:
1293 case 0x05:
1294 case 0x06:
1295 case 0x07:
1296 // Bit pattern 0xxx. No need for any extra bytes.
1297 break;
1298 case 0x08:
1299 case 0x09:
1300 case 0x0a:
1301 case 0x0b:
Narayan Kamatha5afcfc2015-01-29 20:06:46 +00001302 // Bit patterns 10xx, which are illegal start bytes.
Elliott Hughesa2501992011-08-26 19:39:54 -07001303 *errorKind = "start";
1304 return utf8;
Narayan Kamatha5afcfc2015-01-29 20:06:46 +00001305 case 0x0f:
1306 // Bit pattern 1111, which might be the start of a 4 byte sequence.
Andreas Gampeef4afe92015-07-27 21:03:25 -07001307 if ((*utf8 & 0x08) == 0) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +00001308 // Bit pattern 1111 0xxx, which is the start of a 4 byte sequence.
1309 // We consume one continuation byte here, and fall through to consume two more.
Andreas Gampeef4afe92015-07-27 21:03:25 -07001310 utf8 = reinterpret_cast<const uint8_t*>(bytes++);
1311 if ((*utf8 & 0xc0) != 0x80) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +00001312 *errorKind = "continuation";
1313 return utf8;
1314 }
1315 } else {
1316 *errorKind = "start";
1317 return utf8;
1318 }
1319
1320 // Fall through to the cases below to consume two more continuation bytes.
1321 FALLTHROUGH_INTENDED;
Elliott Hughesa2501992011-08-26 19:39:54 -07001322 case 0x0e:
1323 // Bit pattern 1110, so there are two additional bytes.
Andreas Gampeef4afe92015-07-27 21:03:25 -07001324 utf8 = reinterpret_cast<const uint8_t*>(bytes++);
1325 if ((*utf8 & 0xc0) != 0x80) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001326 *errorKind = "continuation";
1327 return utf8;
1328 }
Narayan Kamatha5afcfc2015-01-29 20:06:46 +00001329
1330 // Fall through to consume one more continuation byte.
1331 FALLTHROUGH_INTENDED;
Elliott Hughesa2501992011-08-26 19:39:54 -07001332 case 0x0c:
1333 case 0x0d:
1334 // Bit pattern 110x, so there is one additional byte.
Andreas Gampeef4afe92015-07-27 21:03:25 -07001335 utf8 = reinterpret_cast<const uint8_t*>(bytes++);
1336 if ((*utf8 & 0xc0) != 0x80) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001337 *errorKind = "continuation";
1338 return utf8;
1339 }
1340 break;
1341 }
1342 }
1343 return 0;
1344 }
1345
Ian Rogers68d8b422014-07-17 11:09:10 -07001346 void AbortF(const char* fmt, ...) __attribute__((__format__(__printf__, 2, 3))) {
1347 va_list args;
1348 va_start(args, fmt);
1349 Runtime::Current()->GetJavaVM()->JniAbortV(function_name_, fmt, args);
1350 va_end(args);
1351 }
1352
1353 // The name of the JNI function being checked.
1354 const char* const function_name_;
1355
1356 const int flags_;
Elliott Hughes92cb4982011-12-16 16:57:28 -08001357 int indent_;
Elliott Hughesa2501992011-08-26 19:39:54 -07001358
Ian Rogers68d8b422014-07-17 11:09:10 -07001359 const bool has_method_;
1360
Elliott Hughesa2501992011-08-26 19:39:54 -07001361 DISALLOW_COPY_AND_ASSIGN(ScopedCheck);
1362};
1363
Elliott Hughesa2501992011-08-26 19:39:54 -07001364/*
1365 * ===========================================================================
1366 * Guarded arrays
1367 * ===========================================================================
1368 */
1369
Elliott Hughesa2501992011-08-26 19:39:54 -07001370/* this gets tucked in at the start of the buffer; struct size must be even */
Ian Rogers68d8b422014-07-17 11:09:10 -07001371class GuardedCopy {
1372 public:
Elliott Hughesa2501992011-08-26 19:39:54 -07001373 /*
1374 * Create an over-sized buffer to hold the contents of "buf". Copy it in,
1375 * filling in the area around it with guard data.
Elliott Hughesa2501992011-08-26 19:39:54 -07001376 */
Mathieu Chartierb735bd92015-06-24 17:04:17 -07001377 static void* Create(void* original_buf, size_t len, bool mod_okay) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001378 const size_t new_len = LengthIncludingRedZones(len);
1379 uint8_t* const new_buf = DebugAlloc(new_len);
Elliott Hughesa2501992011-08-26 19:39:54 -07001380
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001381 // If modification is not expected, grab a checksum.
Elliott Hughesa2501992011-08-26 19:39:54 -07001382 uLong adler = 0;
Ian Rogers68d8b422014-07-17 11:09:10 -07001383 if (!mod_okay) {
1384 adler = adler32(adler32(0L, Z_NULL, 0), reinterpret_cast<const Bytef*>(original_buf), len);
Elliott Hughesa2501992011-08-26 19:39:54 -07001385 }
1386
Ian Rogers68d8b422014-07-17 11:09:10 -07001387 GuardedCopy* copy = new (new_buf) GuardedCopy(original_buf, len, adler);
Elliott Hughesa2501992011-08-26 19:39:54 -07001388
Ian Rogers68d8b422014-07-17 11:09:10 -07001389 // Fill begin region with canary pattern.
1390 const size_t kStartCanaryLength = (GuardedCopy::kRedZoneSize / 2) - sizeof(GuardedCopy);
1391 for (size_t i = 0, j = 0; i < kStartCanaryLength; ++i) {
1392 const_cast<char*>(copy->StartRedZone())[i] = kCanary[j];
1393 if (kCanary[j] == '\0') {
1394 j = 0;
tony.ys_liu068361a2015-07-14 16:25:16 +08001395 } else {
1396 j++;
Ian Rogers68d8b422014-07-17 11:09:10 -07001397 }
1398 }
1399
1400 // Copy the data in; note "len" could be zero.
1401 memcpy(const_cast<uint8_t*>(copy->BufferWithinRedZones()), original_buf, len);
1402
1403 // Fill end region with canary pattern.
1404 for (size_t i = 0, j = 0; i < kEndCanaryLength; ++i) {
1405 const_cast<char*>(copy->EndRedZone())[i] = kCanary[j];
1406 if (kCanary[j] == '\0') {
1407 j = 0;
tony.ys_liu068361a2015-07-14 16:25:16 +08001408 } else {
1409 j++;
Ian Rogers68d8b422014-07-17 11:09:10 -07001410 }
1411 }
1412
1413 return const_cast<uint8_t*>(copy->BufferWithinRedZones());
Elliott Hughesa2501992011-08-26 19:39:54 -07001414 }
1415
1416 /*
Ian Rogers68d8b422014-07-17 11:09:10 -07001417 * Create a guarded copy of a primitive array. Modifications to the copied
1418 * data are allowed. Returns a pointer to the copied data.
1419 */
Mathieu Chartierb735bd92015-06-24 17:04:17 -07001420 static void* CreateGuardedPACopy(JNIEnv* env, const jarray java_array, jboolean* is_copy,
1421 void* original_ptr) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001422 ScopedObjectAccess soa(env);
1423
Mathieu Chartier0795f232016-09-27 18:43:30 -07001424 ObjPtr<mirror::Array> a = soa.Decode<mirror::Array>(java_array);
Ian Rogers68d8b422014-07-17 11:09:10 -07001425 size_t component_size = a->GetClass()->GetComponentSize();
1426 size_t byte_count = a->GetLength() * component_size;
Mathieu Chartierb735bd92015-06-24 17:04:17 -07001427 void* result = Create(original_ptr, byte_count, true);
Ian Rogers68d8b422014-07-17 11:09:10 -07001428 if (is_copy != nullptr) {
1429 *is_copy = JNI_TRUE;
1430 }
1431 return result;
1432 }
1433
1434 /*
1435 * Perform the array "release" operation, which may or may not copy data
1436 * back into the managed heap, and may or may not release the underlying storage.
1437 */
Mathieu Chartierb735bd92015-06-24 17:04:17 -07001438 static void* ReleaseGuardedPACopy(const char* function_name, JNIEnv* env,
1439 jarray java_array ATTRIBUTE_UNUSED, void* embedded_buf,
1440 int mode) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001441 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -07001442 if (!GuardedCopy::Check(function_name, embedded_buf, true)) {
1443 return nullptr;
1444 }
Mathieu Chartierb735bd92015-06-24 17:04:17 -07001445 GuardedCopy* const copy = FromEmbedded(embedded_buf);
1446 void* original_ptr = copy->original_ptr_;
Ian Rogers68d8b422014-07-17 11:09:10 -07001447 if (mode != JNI_ABORT) {
Mathieu Chartierb735bd92015-06-24 17:04:17 -07001448 memcpy(original_ptr, embedded_buf, copy->original_length_);
Ian Rogers68d8b422014-07-17 11:09:10 -07001449 }
1450 if (mode != JNI_COMMIT) {
Mathieu Chartierb735bd92015-06-24 17:04:17 -07001451 Destroy(embedded_buf);
Ian Rogers68d8b422014-07-17 11:09:10 -07001452 }
Mathieu Chartierb735bd92015-06-24 17:04:17 -07001453 return original_ptr;
Ian Rogers68d8b422014-07-17 11:09:10 -07001454 }
1455
1456
1457 /*
Elliott Hughesa2501992011-08-26 19:39:54 -07001458 * Free up the guard buffer, scrub it, and return the original pointer.
1459 */
Ian Rogers68d8b422014-07-17 11:09:10 -07001460 static void* Destroy(void* embedded_buf) {
1461 GuardedCopy* copy = FromEmbedded(embedded_buf);
1462 void* original_ptr = const_cast<void*>(copy->original_ptr_);
1463 size_t len = LengthIncludingRedZones(copy->original_length_);
1464 DebugFree(copy, len);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001465 return original_ptr;
Elliott Hughesa2501992011-08-26 19:39:54 -07001466 }
1467
1468 /*
1469 * Verify the guard area and, if "modOkay" is false, that the data itself
1470 * has not been altered.
1471 *
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001472 * The caller has already checked that "dataBuf" is non-null.
Elliott Hughesa2501992011-08-26 19:39:54 -07001473 */
Ian Rogers68d8b422014-07-17 11:09:10 -07001474 static bool Check(const char* function_name, const void* embedded_buf, bool mod_okay) {
1475 const GuardedCopy* copy = FromEmbedded(embedded_buf);
1476 return copy->CheckHeader(function_name, mod_okay) && copy->CheckRedZones(function_name);
Elliott Hughesa2501992011-08-26 19:39:54 -07001477 }
1478
1479 private:
Mathieu Chartierb735bd92015-06-24 17:04:17 -07001480 GuardedCopy(void* original_buf, size_t len, uLong adler) :
Ian Rogers68d8b422014-07-17 11:09:10 -07001481 magic_(kGuardMagic), adler_(adler), original_ptr_(original_buf), original_length_(len) {
1482 }
1483
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001484 static uint8_t* DebugAlloc(size_t len) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001485 void* result = mmap(nullptr, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
Elliott Hughesa2501992011-08-26 19:39:54 -07001486 if (result == MAP_FAILED) {
1487 PLOG(FATAL) << "GuardedCopy::create mmap(" << len << ") failed";
1488 }
1489 return reinterpret_cast<uint8_t*>(result);
1490 }
1491
Ian Rogers68d8b422014-07-17 11:09:10 -07001492 static void DebugFree(void* buf, size_t len) {
1493 if (munmap(buf, len) != 0) {
1494 PLOG(FATAL) << "munmap(" << buf << ", " << len << ") failed";
Elliott Hughesa2501992011-08-26 19:39:54 -07001495 }
1496 }
1497
Ian Rogers68d8b422014-07-17 11:09:10 -07001498 static size_t LengthIncludingRedZones(size_t len) {
1499 return len + kRedZoneSize;
Elliott Hughesa2501992011-08-26 19:39:54 -07001500 }
1501
Ian Rogers68d8b422014-07-17 11:09:10 -07001502 // Get the GuardedCopy from the interior pointer.
1503 static GuardedCopy* FromEmbedded(void* embedded_buf) {
1504 return reinterpret_cast<GuardedCopy*>(
1505 reinterpret_cast<uint8_t*>(embedded_buf) - (kRedZoneSize / 2));
Elliott Hughesa2501992011-08-26 19:39:54 -07001506 }
1507
Ian Rogers68d8b422014-07-17 11:09:10 -07001508 static const GuardedCopy* FromEmbedded(const void* embedded_buf) {
1509 return reinterpret_cast<const GuardedCopy*>(
1510 reinterpret_cast<const uint8_t*>(embedded_buf) - (kRedZoneSize / 2));
Elliott Hughesa2501992011-08-26 19:39:54 -07001511 }
Ian Rogers68d8b422014-07-17 11:09:10 -07001512
1513 static void AbortF(const char* jni_function_name, const char* fmt, ...) {
1514 va_list args;
1515 va_start(args, fmt);
1516 Runtime::Current()->GetJavaVM()->JniAbortV(jni_function_name, fmt, args);
1517 va_end(args);
1518 }
1519
1520 bool CheckHeader(const char* function_name, bool mod_okay) const {
1521 static const uint32_t kMagicCmp = kGuardMagic;
1522
1523 // Before we do anything with "pExtra", check the magic number. We
1524 // do the check with memcmp rather than "==" in case the pointer is
1525 // unaligned. If it points to completely bogus memory we're going
1526 // to crash, but there's no easy way around that.
1527 if (UNLIKELY(memcmp(&magic_, &kMagicCmp, 4) != 0)) {
1528 uint8_t buf[4];
1529 memcpy(buf, &magic_, 4);
1530 AbortF(function_name,
1531 "guard magic does not match (found 0x%02x%02x%02x%02x) -- incorrect data pointer %p?",
1532 buf[3], buf[2], buf[1], buf[0], this); // Assumes little-endian.
1533 return false;
1534 }
1535
1536 // If modification is not expected, verify checksum. Strictly speaking this is wrong: if we
1537 // told the client that we made a copy, there's no reason they can't alter the buffer.
1538 if (!mod_okay) {
1539 uLong computed_adler =
1540 adler32(adler32(0L, Z_NULL, 0), BufferWithinRedZones(), original_length_);
1541 if (computed_adler != adler_) {
1542 AbortF(function_name, "buffer modified (0x%08lx vs 0x%08lx) at address %p",
1543 computed_adler, adler_, this);
1544 return false;
1545 }
1546 }
1547 return true;
1548 }
1549
1550 bool CheckRedZones(const char* function_name) const {
1551 // Check the begin red zone.
1552 const size_t kStartCanaryLength = (GuardedCopy::kRedZoneSize / 2) - sizeof(GuardedCopy);
1553 for (size_t i = 0, j = 0; i < kStartCanaryLength; ++i) {
1554 if (UNLIKELY(StartRedZone()[i] != kCanary[j])) {
1555 AbortF(function_name, "guard pattern before buffer disturbed at %p +%zd", this, i);
1556 return false;
1557 }
1558 if (kCanary[j] == '\0') {
1559 j = 0;
tony.ys_liu068361a2015-07-14 16:25:16 +08001560 } else {
1561 j++;
Ian Rogers68d8b422014-07-17 11:09:10 -07001562 }
1563 }
1564
1565 // Check end region.
1566 for (size_t i = 0, j = 0; i < kEndCanaryLength; ++i) {
1567 if (UNLIKELY(EndRedZone()[i] != kCanary[j])) {
1568 size_t offset_from_buffer_start =
1569 &(EndRedZone()[i]) - &(StartRedZone()[kStartCanaryLength]);
1570 AbortF(function_name, "guard pattern after buffer disturbed at %p +%zd", this,
1571 offset_from_buffer_start);
1572 return false;
1573 }
1574 if (kCanary[j] == '\0') {
1575 j = 0;
tony.ys_liu068361a2015-07-14 16:25:16 +08001576 } else {
1577 j++;
Ian Rogers68d8b422014-07-17 11:09:10 -07001578 }
1579 }
1580 return true;
1581 }
1582
1583 // Location that canary value will be written before the guarded region.
1584 const char* StartRedZone() const {
1585 const uint8_t* buf = reinterpret_cast<const uint8_t*>(this);
1586 return reinterpret_cast<const char*>(buf + sizeof(GuardedCopy));
1587 }
1588
1589 // Return the interior embedded buffer.
1590 const uint8_t* BufferWithinRedZones() const {
1591 const uint8_t* embedded_buf = reinterpret_cast<const uint8_t*>(this) + (kRedZoneSize / 2);
1592 return embedded_buf;
1593 }
1594
1595 // Location that canary value will be written after the guarded region.
1596 const char* EndRedZone() const {
1597 const uint8_t* buf = reinterpret_cast<const uint8_t*>(this);
1598 size_t buf_len = LengthIncludingRedZones(original_length_);
1599 return reinterpret_cast<const char*>(buf + (buf_len - (kRedZoneSize / 2)));
1600 }
1601
1602 static constexpr size_t kRedZoneSize = 512;
1603 static constexpr size_t kEndCanaryLength = kRedZoneSize / 2;
1604
1605 // Value written before and after the guarded array.
1606 static const char* const kCanary;
1607
1608 static constexpr uint32_t kGuardMagic = 0xffd5aa96;
1609
1610 const uint32_t magic_;
1611 const uLong adler_;
Mathieu Chartierb735bd92015-06-24 17:04:17 -07001612 void* const original_ptr_;
Ian Rogers68d8b422014-07-17 11:09:10 -07001613 const size_t original_length_;
Elliott Hughesa2501992011-08-26 19:39:54 -07001614};
Ian Rogers68d8b422014-07-17 11:09:10 -07001615const char* const GuardedCopy::kCanary = "JNI BUFFER RED ZONE";
Elliott Hughesa2501992011-08-26 19:39:54 -07001616
1617/*
1618 * ===========================================================================
1619 * JNI functions
1620 * ===========================================================================
1621 */
1622
1623class CheckJNI {
1624 public:
1625 static jint GetVersion(JNIEnv* env) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001626 ScopedObjectAccess soa(env);
1627 ScopedCheck sc(kFlag_Default, __FUNCTION__);
1628 JniValueType args[1] = {{.E = env }};
1629 if (sc.Check(soa, true, "E", args)) {
1630 JniValueType result;
1631 result.I = baseEnv(env)->GetVersion(env);
1632 if (sc.Check(soa, false, "I", &result)) {
1633 return result.I;
1634 }
1635 }
1636 return JNI_ERR;
Elliott Hughesa2501992011-08-26 19:39:54 -07001637 }
1638
Ian Rogers68d8b422014-07-17 11:09:10 -07001639 static jint GetJavaVM(JNIEnv *env, JavaVM **vm) {
1640 ScopedObjectAccess soa(env);
1641 ScopedCheck sc(kFlag_Default, __FUNCTION__);
1642 JniValueType args[2] = {{.E = env }, {.p = vm}};
1643 if (sc.Check(soa, true, "Ep", args)) {
1644 JniValueType result;
1645 result.i = baseEnv(env)->GetJavaVM(env, vm);
1646 if (sc.Check(soa, false, "i", &result)) {
1647 return result.i;
1648 }
1649 }
1650 return JNI_ERR;
1651 }
1652
1653 static jint RegisterNatives(JNIEnv* env, jclass c, const JNINativeMethod* methods, jint nMethods) {
1654 ScopedObjectAccess soa(env);
1655 ScopedCheck sc(kFlag_Default, __FUNCTION__);
1656 JniValueType args[4] = {{.E = env }, {.c = c}, {.p = methods}, {.I = nMethods}};
1657 if (sc.Check(soa, true, "EcpI", args)) {
1658 JniValueType result;
1659 result.i = baseEnv(env)->RegisterNatives(env, c, methods, nMethods);
1660 if (sc.Check(soa, false, "i", &result)) {
1661 return result.i;
1662 }
1663 }
1664 return JNI_ERR;
1665 }
1666
1667 static jint UnregisterNatives(JNIEnv* env, jclass c) {
1668 ScopedObjectAccess soa(env);
1669 ScopedCheck sc(kFlag_Default, __FUNCTION__);
1670 JniValueType args[2] = {{.E = env }, {.c = c}};
1671 if (sc.Check(soa, true, "Ec", args)) {
1672 JniValueType result;
1673 result.i = baseEnv(env)->UnregisterNatives(env, c);
1674 if (sc.Check(soa, false, "i", &result)) {
1675 return result.i;
1676 }
1677 }
1678 return JNI_ERR;
1679 }
1680
1681 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject obj) {
Ian Rogersc0542af2014-09-03 16:16:56 -07001682 // Note: we use "EL" here but "Ep" has been used in the past on the basis that we'd like to
1683 // know the object is invalid. The spec says that passing invalid objects or even ones that
1684 // are deleted isn't supported.
Ian Rogers68d8b422014-07-17 11:09:10 -07001685 ScopedObjectAccess soa(env);
1686 ScopedCheck sc(kFlag_Default, __FUNCTION__);
Ian Rogersc0542af2014-09-03 16:16:56 -07001687 JniValueType args[2] = {{.E = env }, {.L = obj}};
1688 if (sc.Check(soa, true, "EL", args)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001689 JniValueType result;
1690 result.w = baseEnv(env)->GetObjectRefType(env, obj);
1691 if (sc.Check(soa, false, "w", &result)) {
1692 return result.w;
1693 }
1694 }
1695 return JNIInvalidRefType;
1696 }
1697
1698 static jclass DefineClass(JNIEnv* env, const char* name, jobject loader, const jbyte* buf,
1699 jsize bufLen) {
1700 ScopedObjectAccess soa(env);
1701 ScopedCheck sc(kFlag_Default, __FUNCTION__);
1702 JniValueType args[5] = {{.E = env}, {.u = name}, {.L = loader}, {.p = buf}, {.z = bufLen}};
1703 if (sc.Check(soa, true, "EuLpz", args) && sc.CheckClassName(name)) {
1704 JniValueType result;
1705 result.c = baseEnv(env)->DefineClass(env, name, loader, buf, bufLen);
1706 if (sc.Check(soa, false, "c", &result)) {
1707 return result.c;
1708 }
1709 }
1710 return nullptr;
Elliott Hughesa2501992011-08-26 19:39:54 -07001711 }
1712
1713 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001714 ScopedObjectAccess soa(env);
1715 ScopedCheck sc(kFlag_Default, __FUNCTION__);
1716 JniValueType args[2] = {{.E = env}, {.u = name}};
1717 if (sc.Check(soa, true, "Eu", args) && sc.CheckClassName(name)) {
1718 JniValueType result;
1719 result.c = baseEnv(env)->FindClass(env, name);
1720 if (sc.Check(soa, false, "c", &result)) {
1721 return result.c;
1722 }
1723 }
1724 return nullptr;
Elliott Hughesa2501992011-08-26 19:39:54 -07001725 }
1726
Elliott Hughese84278b2012-03-22 10:06:53 -07001727 static jclass GetSuperclass(JNIEnv* env, jclass c) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001728 ScopedObjectAccess soa(env);
1729 ScopedCheck sc(kFlag_Default, __FUNCTION__);
1730 JniValueType args[2] = {{.E = env}, {.c = c}};
1731 if (sc.Check(soa, true, "Ec", args)) {
1732 JniValueType result;
1733 result.c = baseEnv(env)->GetSuperclass(env, c);
1734 if (sc.Check(soa, false, "c", &result)) {
1735 return result.c;
1736 }
1737 }
1738 return nullptr;
Elliott Hughesa2501992011-08-26 19:39:54 -07001739 }
1740
Elliott Hughese84278b2012-03-22 10:06:53 -07001741 static jboolean IsAssignableFrom(JNIEnv* env, jclass c1, jclass c2) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001742 ScopedObjectAccess soa(env);
1743 ScopedCheck sc(kFlag_Default, __FUNCTION__);
1744 JniValueType args[3] = {{.E = env}, {.c = c1}, {.c = c2}};
1745 if (sc.Check(soa, true, "Ecc", args)) {
1746 JniValueType result;
1747 result.b = baseEnv(env)->IsAssignableFrom(env, c1, c2);
1748 if (sc.Check(soa, false, "b", &result)) {
1749 return result.b;
1750 }
1751 }
1752 return JNI_FALSE;
Elliott Hughesa2501992011-08-26 19:39:54 -07001753 }
1754
1755 static jmethodID FromReflectedMethod(JNIEnv* env, jobject method) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001756 ScopedObjectAccess soa(env);
1757 ScopedCheck sc(kFlag_Default, __FUNCTION__);
1758 JniValueType args[2] = {{.E = env}, {.L = method}};
1759 if (sc.Check(soa, true, "EL", args) && sc.CheckReflectedMethod(soa, method)) {
1760 JniValueType result;
1761 result.m = baseEnv(env)->FromReflectedMethod(env, method);
1762 if (sc.Check(soa, false, "m", &result)) {
1763 return result.m;
1764 }
1765 }
1766 return nullptr;
Elliott Hughesa2501992011-08-26 19:39:54 -07001767 }
1768
1769 static jfieldID FromReflectedField(JNIEnv* env, jobject field) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001770 ScopedObjectAccess soa(env);
1771 ScopedCheck sc(kFlag_Default, __FUNCTION__);
1772 JniValueType args[2] = {{.E = env}, {.L = field}};
1773 if (sc.Check(soa, true, "EL", args) && sc.CheckReflectedField(soa, field)) {
1774 JniValueType result;
1775 result.f = baseEnv(env)->FromReflectedField(env, field);
1776 if (sc.Check(soa, false, "f", &result)) {
1777 return result.f;
1778 }
1779 }
1780 return nullptr;
Elliott Hughesa2501992011-08-26 19:39:54 -07001781 }
1782
1783 static jobject ToReflectedMethod(JNIEnv* env, jclass cls, jmethodID mid, jboolean isStatic) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001784 ScopedObjectAccess soa(env);
1785 ScopedCheck sc(kFlag_Default, __FUNCTION__);
1786 JniValueType args[4] = {{.E = env}, {.c = cls}, {.m = mid}, {.b = isStatic}};
1787 if (sc.Check(soa, true, "Ecmb", args)) {
1788 JniValueType result;
1789 result.L = baseEnv(env)->ToReflectedMethod(env, cls, mid, isStatic);
1790 if (sc.Check(soa, false, "L", &result) && (result.L != nullptr)) {
1791 DCHECK(sc.CheckReflectedMethod(soa, result.L));
1792 return result.L;
1793 }
1794 }
1795 return nullptr;
Elliott Hughesa2501992011-08-26 19:39:54 -07001796 }
1797
1798 static jobject ToReflectedField(JNIEnv* env, jclass cls, jfieldID fid, jboolean isStatic) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001799 ScopedObjectAccess soa(env);
1800 ScopedCheck sc(kFlag_Default, __FUNCTION__);
1801 JniValueType args[4] = {{.E = env}, {.c = cls}, {.f = fid}, {.b = isStatic}};
1802 if (sc.Check(soa, true, "Ecfb", args)) {
1803 JniValueType result;
1804 result.L = baseEnv(env)->ToReflectedField(env, cls, fid, isStatic);
1805 if (sc.Check(soa, false, "L", &result) && (result.L != nullptr)) {
1806 DCHECK(sc.CheckReflectedField(soa, result.L));
1807 return result.L;
1808 }
1809 }
1810 return nullptr;
Elliott Hughesa2501992011-08-26 19:39:54 -07001811 }
1812
1813 static jint Throw(JNIEnv* env, jthrowable obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001814 ScopedObjectAccess soa(env);
1815 ScopedCheck sc(kFlag_Default, __FUNCTION__);
1816 JniValueType args[2] = {{.E = env}, {.t = obj}};
1817 if (sc.Check(soa, true, "Et", args) && sc.CheckThrowable(soa, obj)) {
1818 JniValueType result;
1819 result.i = baseEnv(env)->Throw(env, obj);
1820 if (sc.Check(soa, false, "i", &result)) {
1821 return result.i;
1822 }
1823 }
1824 return JNI_ERR;
Elliott Hughesa2501992011-08-26 19:39:54 -07001825 }
1826
Elliott Hughese84278b2012-03-22 10:06:53 -07001827 static jint ThrowNew(JNIEnv* env, jclass c, const char* message) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001828 ScopedObjectAccess soa(env);
1829 ScopedCheck sc(kFlag_NullableUtf, __FUNCTION__);
Christopher Ferrisc50358b2015-04-17 13:26:09 -07001830 JniValueType args[3] = {{.E = env}, {.c = c}, {.u = message}};
Ian Rogers68d8b422014-07-17 11:09:10 -07001831 if (sc.Check(soa, true, "Ecu", args) && sc.CheckThrowableClass(soa, c)) {
1832 JniValueType result;
1833 result.i = baseEnv(env)->ThrowNew(env, c, message);
1834 if (sc.Check(soa, false, "i", &result)) {
1835 return result.i;
1836 }
1837 }
1838 return JNI_ERR;
Elliott Hughesa2501992011-08-26 19:39:54 -07001839 }
1840
1841 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001842 ScopedObjectAccess soa(env);
1843 ScopedCheck sc(kFlag_ExcepOkay, __FUNCTION__);
1844 JniValueType args[1] = {{.E = env}};
1845 if (sc.Check(soa, true, "E", args)) {
1846 JniValueType result;
1847 result.t = baseEnv(env)->ExceptionOccurred(env);
1848 if (sc.Check(soa, false, "t", &result)) {
1849 return result.t;
1850 }
1851 }
1852 return nullptr;
Elliott Hughesa2501992011-08-26 19:39:54 -07001853 }
1854
1855 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001856 ScopedObjectAccess soa(env);
1857 ScopedCheck sc(kFlag_ExcepOkay, __FUNCTION__);
1858 JniValueType args[1] = {{.E = env}};
1859 if (sc.Check(soa, true, "E", args)) {
1860 JniValueType result;
1861 baseEnv(env)->ExceptionDescribe(env);
1862 result.V = nullptr;
1863 sc.Check(soa, false, "V", &result);
1864 }
Elliott Hughesa2501992011-08-26 19:39:54 -07001865 }
1866
1867 static void ExceptionClear(JNIEnv* env) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001868 ScopedObjectAccess soa(env);
1869 ScopedCheck sc(kFlag_ExcepOkay, __FUNCTION__);
1870 JniValueType args[1] = {{.E = env}};
1871 if (sc.Check(soa, true, "E", args)) {
1872 JniValueType result;
1873 baseEnv(env)->ExceptionClear(env);
1874 result.V = nullptr;
1875 sc.Check(soa, false, "V", &result);
1876 }
1877 }
1878
1879 static jboolean ExceptionCheck(JNIEnv* env) {
1880 ScopedObjectAccess soa(env);
1881 ScopedCheck sc(kFlag_CritOkay | kFlag_ExcepOkay, __FUNCTION__);
1882 JniValueType args[1] = {{.E = env}};
1883 if (sc.Check(soa, true, "E", args)) {
1884 JniValueType result;
1885 result.b = baseEnv(env)->ExceptionCheck(env);
1886 if (sc.Check(soa, false, "b", &result)) {
1887 return result.b;
1888 }
1889 }
1890 return JNI_FALSE;
Elliott Hughesa2501992011-08-26 19:39:54 -07001891 }
1892
1893 static void FatalError(JNIEnv* env, const char* msg) {
Elliott Hughesc4378df2013-06-14 17:05:13 -07001894 // The JNI specification doesn't say it's okay to call FatalError with a pending exception,
1895 // but you're about to abort anyway, and it's quite likely that you have a pending exception,
1896 // and it's not unimaginable that you don't know that you do. So we allow it.
Ian Rogers68d8b422014-07-17 11:09:10 -07001897 ScopedObjectAccess soa(env);
1898 ScopedCheck sc(kFlag_ExcepOkay | kFlag_NullableUtf, __FUNCTION__);
1899 JniValueType args[2] = {{.E = env}, {.u = msg}};
1900 if (sc.Check(soa, true, "Eu", args)) {
1901 JniValueType result;
1902 baseEnv(env)->FatalError(env, msg);
1903 // Unreachable.
1904 result.V = nullptr;
1905 sc.Check(soa, false, "V", &result);
1906 }
Elliott Hughesa2501992011-08-26 19:39:54 -07001907 }
1908
1909 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001910 ScopedObjectAccess soa(env);
1911 ScopedCheck sc(kFlag_ExcepOkay, __FUNCTION__);
1912 JniValueType args[2] = {{.E = env}, {.I = capacity}};
1913 if (sc.Check(soa, true, "EI", args)) {
1914 JniValueType result;
1915 result.i = baseEnv(env)->PushLocalFrame(env, capacity);
1916 if (sc.Check(soa, false, "i", &result)) {
1917 return result.i;
1918 }
1919 }
1920 return JNI_ERR;
Elliott Hughesa2501992011-08-26 19:39:54 -07001921 }
1922
1923 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001924 ScopedObjectAccess soa(env);
1925 ScopedCheck sc(kFlag_ExcepOkay, __FUNCTION__);
1926 JniValueType args[2] = {{.E = env}, {.L = res}};
1927 if (sc.Check(soa, true, "EL", args)) {
1928 JniValueType result;
1929 result.L = baseEnv(env)->PopLocalFrame(env, res);
1930 sc.Check(soa, false, "L", &result);
1931 return result.L;
1932 }
1933 return nullptr;
Elliott Hughesa2501992011-08-26 19:39:54 -07001934 }
1935
1936 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001937 return NewRef(__FUNCTION__, env, obj, kGlobal);
Elliott Hughesa2501992011-08-26 19:39:54 -07001938 }
1939
Ian Rogers68d8b422014-07-17 11:09:10 -07001940 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
1941 return NewRef(__FUNCTION__, env, obj, kLocal);
Elliott Hughesa2501992011-08-26 19:39:54 -07001942 }
1943
1944 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001945 return NewRef(__FUNCTION__, env, obj, kWeakGlobal);
Elliott Hughesa2501992011-08-26 19:39:54 -07001946 }
1947
Ian Rogers68d8b422014-07-17 11:09:10 -07001948 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
1949 DeleteRef(__FUNCTION__, env, obj, kGlobal);
Elliott Hughesa2501992011-08-26 19:39:54 -07001950 }
1951
Ian Rogers68d8b422014-07-17 11:09:10 -07001952 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
1953 DeleteRef(__FUNCTION__, env, obj, kWeakGlobal);
1954 }
1955
1956 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
1957 DeleteRef(__FUNCTION__, env, obj, kLocal);
1958 }
1959
1960 static jint EnsureLocalCapacity(JNIEnv *env, jint capacity) {
1961 ScopedObjectAccess soa(env);
1962 ScopedCheck sc(kFlag_Default, __FUNCTION__);
1963 JniValueType args[2] = {{.E = env}, {.I = capacity}};
1964 if (sc.Check(soa, true, "EI", args)) {
1965 JniValueType result;
1966 result.i = baseEnv(env)->EnsureLocalCapacity(env, capacity);
1967 if (sc.Check(soa, false, "i", &result)) {
1968 return result.i;
1969 }
1970 }
1971 return JNI_ERR;
1972 }
1973
1974 static jboolean IsSameObject(JNIEnv* env, jobject ref1, jobject ref2) {
1975 ScopedObjectAccess soa(env);
1976 ScopedCheck sc(kFlag_Default, __FUNCTION__);
1977 JniValueType args[3] = {{.E = env}, {.L = ref1}, {.L = ref2}};
1978 if (sc.Check(soa, true, "ELL", args)) {
1979 JniValueType result;
1980 result.b = baseEnv(env)->IsSameObject(env, ref1, ref2);
1981 if (sc.Check(soa, false, "b", &result)) {
1982 return result.b;
1983 }
1984 }
1985 return JNI_FALSE;
1986 }
1987
1988 static jobject AllocObject(JNIEnv* env, jclass c) {
1989 ScopedObjectAccess soa(env);
1990 ScopedCheck sc(kFlag_Default, __FUNCTION__);
1991 JniValueType args[2] = {{.E = env}, {.c = c}};
1992 if (sc.Check(soa, true, "Ec", args) && sc.CheckInstantiableNonArray(soa, c)) {
1993 JniValueType result;
1994 result.L = baseEnv(env)->AllocObject(env, c);
1995 if (sc.Check(soa, false, "L", &result)) {
1996 return result.L;
1997 }
1998 }
1999 return nullptr;
2000 }
2001
2002 static jobject NewObjectV(JNIEnv* env, jclass c, jmethodID mid, va_list vargs) {
2003 ScopedObjectAccess soa(env);
2004 ScopedCheck sc(kFlag_Default, __FUNCTION__);
Alex Light48ffe062015-08-19 15:23:23 -07002005 VarArgs rest(mid, vargs);
2006 JniValueType args[4] = {{.E = env}, {.c = c}, {.m = mid}, {.va = &rest}};
2007 if (sc.Check(soa, true, "Ecm.", args) && sc.CheckInstantiableNonArray(soa, c) &&
Ian Rogers68d8b422014-07-17 11:09:10 -07002008 sc.CheckConstructor(soa, mid)) {
2009 JniValueType result;
2010 result.L = baseEnv(env)->NewObjectV(env, c, mid, vargs);
2011 if (sc.Check(soa, false, "L", &result)) {
2012 return result.L;
2013 }
2014 }
2015 return nullptr;
2016 }
2017
2018 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
2019 va_list args;
2020 va_start(args, mid);
2021 jobject result = NewObjectV(env, c, mid, args);
2022 va_end(args);
2023 return result;
2024 }
2025
2026 static jobject NewObjectA(JNIEnv* env, jclass c, jmethodID mid, jvalue* vargs) {
2027 ScopedObjectAccess soa(env);
2028 ScopedCheck sc(kFlag_Default, __FUNCTION__);
Alex Light48ffe062015-08-19 15:23:23 -07002029 VarArgs rest(mid, vargs);
2030 JniValueType args[4] = {{.E = env}, {.c = c}, {.m = mid}, {.va = &rest}};
2031 if (sc.Check(soa, true, "Ecm.", args) && sc.CheckInstantiableNonArray(soa, c) &&
Ian Rogers68d8b422014-07-17 11:09:10 -07002032 sc.CheckConstructor(soa, mid)) {
2033 JniValueType result;
2034 result.L = baseEnv(env)->NewObjectA(env, c, mid, vargs);
2035 if (sc.Check(soa, false, "L", &result)) {
2036 return result.L;
2037 }
2038 }
2039 return nullptr;
2040 }
2041
2042 static jclass GetObjectClass(JNIEnv* env, jobject obj) {
2043 ScopedObjectAccess soa(env);
2044 ScopedCheck sc(kFlag_Default, __FUNCTION__);
2045 JniValueType args[2] = {{.E = env}, {.L = obj}};
2046 if (sc.Check(soa, true, "EL", args)) {
2047 JniValueType result;
2048 result.c = baseEnv(env)->GetObjectClass(env, obj);
2049 if (sc.Check(soa, false, "c", &result)) {
2050 return result.c;
2051 }
2052 }
2053 return nullptr;
2054 }
2055
2056 static jboolean IsInstanceOf(JNIEnv* env, jobject obj, jclass c) {
2057 ScopedObjectAccess soa(env);
2058 ScopedCheck sc(kFlag_Default, __FUNCTION__);
2059 JniValueType args[3] = {{.E = env}, {.L = obj}, {.c = c}};
2060 if (sc.Check(soa, true, "ELc", args)) {
2061 JniValueType result;
2062 result.b = baseEnv(env)->IsInstanceOf(env, obj, c);
2063 if (sc.Check(soa, false, "b", &result)) {
2064 return result.b;
2065 }
2066 }
2067 return JNI_FALSE;
2068 }
2069
2070 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
2071 return GetMethodIDInternal(__FUNCTION__, env, c, name, sig, false);
2072 }
2073
2074 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
2075 return GetMethodIDInternal(__FUNCTION__, env, c, name, sig, true);
2076 }
2077
2078 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
2079 return GetFieldIDInternal(__FUNCTION__, env, c, name, sig, false);
2080 }
2081
2082 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
2083 return GetFieldIDInternal(__FUNCTION__, env, c, name, sig, true);
2084 }
2085
2086#define FIELD_ACCESSORS(jtype, name, ptype, shorty) \
2087 static jtype GetStatic##name##Field(JNIEnv* env, jclass c, jfieldID fid) { \
2088 return GetField(__FUNCTION__, env, c, fid, true, ptype).shorty; \
2089 } \
2090 \
2091 static jtype Get##name##Field(JNIEnv* env, jobject obj, jfieldID fid) { \
2092 return GetField(__FUNCTION__, env, obj, fid, false, ptype).shorty; \
2093 } \
2094 \
2095 static void SetStatic##name##Field(JNIEnv* env, jclass c, jfieldID fid, jtype v) { \
2096 JniValueType value; \
2097 value.shorty = v; \
2098 SetField(__FUNCTION__, env, c, fid, true, ptype, value); \
2099 } \
2100 \
2101 static void Set##name##Field(JNIEnv* env, jobject obj, jfieldID fid, jtype v) { \
2102 JniValueType value; \
2103 value.shorty = v; \
2104 SetField(__FUNCTION__, env, obj, fid, false, ptype, value); \
2105 }
2106
2107 FIELD_ACCESSORS(jobject, Object, Primitive::kPrimNot, L)
2108 FIELD_ACCESSORS(jboolean, Boolean, Primitive::kPrimBoolean, Z)
2109 FIELD_ACCESSORS(jbyte, Byte, Primitive::kPrimByte, B)
2110 FIELD_ACCESSORS(jchar, Char, Primitive::kPrimChar, C)
2111 FIELD_ACCESSORS(jshort, Short, Primitive::kPrimShort, S)
2112 FIELD_ACCESSORS(jint, Int, Primitive::kPrimInt, I)
2113 FIELD_ACCESSORS(jlong, Long, Primitive::kPrimLong, J)
2114 FIELD_ACCESSORS(jfloat, Float, Primitive::kPrimFloat, F)
2115 FIELD_ACCESSORS(jdouble, Double, Primitive::kPrimDouble, D)
2116#undef FIELD_ACCESSORS
2117
2118 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* vargs) {
2119 CallMethodA(__FUNCTION__, env, obj, nullptr, mid, vargs, Primitive::kPrimVoid, kVirtual);
2120 }
2121
2122 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass c, jmethodID mid,
2123 jvalue* vargs) {
2124 CallMethodA(__FUNCTION__, env, obj, c, mid, vargs, Primitive::kPrimVoid, kDirect);
2125 }
2126
2127 static void CallStaticVoidMethodA(JNIEnv* env, jclass c, jmethodID mid, jvalue* vargs) {
Marcus Oakland51be0a72014-09-24 15:23:06 +01002128 CallMethodA(__FUNCTION__, env, nullptr, c, mid, vargs, Primitive::kPrimVoid, kStatic);
Ian Rogers68d8b422014-07-17 11:09:10 -07002129 }
2130
2131 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list vargs) {
2132 CallMethodV(__FUNCTION__, env, obj, nullptr, mid, vargs, Primitive::kPrimVoid, kVirtual);
2133 }
2134
2135 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass c, jmethodID mid,
2136 va_list vargs) {
2137 CallMethodV(__FUNCTION__, env, obj, c, mid, vargs, Primitive::kPrimVoid, kDirect);
2138 }
2139
2140 static void CallStaticVoidMethodV(JNIEnv* env, jclass c, jmethodID mid, va_list vargs) {
2141 CallMethodV(__FUNCTION__, env, nullptr, c, mid, vargs, Primitive::kPrimVoid, kStatic);
2142 }
2143
2144 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
2145 va_list vargs;
2146 va_start(vargs, mid);
2147 CallMethodV(__FUNCTION__, env, obj, nullptr, mid, vargs, Primitive::kPrimVoid, kVirtual);
2148 va_end(vargs);
2149 }
2150
2151 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass c, jmethodID mid, ...) {
2152 va_list vargs;
2153 va_start(vargs, mid);
2154 CallMethodV(__FUNCTION__, env, obj, c, mid, vargs, Primitive::kPrimVoid, kDirect);
2155 va_end(vargs);
2156 }
2157
2158 static void CallStaticVoidMethod(JNIEnv* env, jclass c, jmethodID mid, ...) {
2159 va_list vargs;
2160 va_start(vargs, mid);
2161 CallMethodV(__FUNCTION__, env, nullptr, c, mid, vargs, Primitive::kPrimVoid, kStatic);
2162 va_end(vargs);
2163 }
2164
2165#define CALL(rtype, name, ptype, shorty) \
2166 static rtype Call##name##MethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* vargs) { \
2167 return CallMethodA(__FUNCTION__, env, obj, nullptr, mid, vargs, ptype, kVirtual).shorty; \
2168 } \
2169 \
2170 static rtype CallNonvirtual##name##MethodA(JNIEnv* env, jobject obj, jclass c, jmethodID mid, \
2171 jvalue* vargs) { \
2172 return CallMethodA(__FUNCTION__, env, obj, c, mid, vargs, ptype, kDirect).shorty; \
2173 } \
2174 \
2175 static rtype CallStatic##name##MethodA(JNIEnv* env, jclass c, jmethodID mid, jvalue* vargs) { \
2176 return CallMethodA(__FUNCTION__, env, nullptr, c, mid, vargs, ptype, kStatic).shorty; \
2177 } \
2178 \
2179 static rtype Call##name##MethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list vargs) { \
2180 return CallMethodV(__FUNCTION__, env, obj, nullptr, mid, vargs, ptype, kVirtual).shorty; \
2181 } \
2182 \
2183 static rtype CallNonvirtual##name##MethodV(JNIEnv* env, jobject obj, jclass c, jmethodID mid, \
2184 va_list vargs) { \
2185 return CallMethodV(__FUNCTION__, env, obj, c, mid, vargs, ptype, kDirect).shorty; \
2186 } \
2187 \
2188 static rtype CallStatic##name##MethodV(JNIEnv* env, jclass c, jmethodID mid, va_list vargs) { \
2189 return CallMethodV(__FUNCTION__, env, nullptr, c, mid, vargs, ptype, kStatic).shorty; \
2190 } \
2191 \
2192 static rtype Call##name##Method(JNIEnv* env, jobject obj, jmethodID mid, ...) { \
2193 va_list vargs; \
2194 va_start(vargs, mid); \
2195 rtype result = \
2196 CallMethodV(__FUNCTION__, env, obj, nullptr, mid, vargs, ptype, kVirtual).shorty; \
2197 va_end(vargs); \
2198 return result; \
2199 } \
2200 \
2201 static rtype CallNonvirtual##name##Method(JNIEnv* env, jobject obj, jclass c, jmethodID mid, \
2202 ...) { \
2203 va_list vargs; \
2204 va_start(vargs, mid); \
2205 rtype result = \
2206 CallMethodV(__FUNCTION__, env, obj, c, mid, vargs, ptype, kDirect).shorty; \
2207 va_end(vargs); \
2208 return result; \
2209 } \
2210 \
2211 static rtype CallStatic##name##Method(JNIEnv* env, jclass c, jmethodID mid, ...) { \
2212 va_list vargs; \
2213 va_start(vargs, mid); \
2214 rtype result = \
2215 CallMethodV(__FUNCTION__, env, nullptr, c, mid, vargs, ptype, kStatic).shorty; \
2216 va_end(vargs); \
2217 return result; \
2218 }
2219
2220 CALL(jobject, Object, Primitive::kPrimNot, L)
2221 CALL(jboolean, Boolean, Primitive::kPrimBoolean, Z)
2222 CALL(jbyte, Byte, Primitive::kPrimByte, B)
2223 CALL(jchar, Char, Primitive::kPrimChar, C)
2224 CALL(jshort, Short, Primitive::kPrimShort, S)
2225 CALL(jint, Int, Primitive::kPrimInt, I)
2226 CALL(jlong, Long, Primitive::kPrimLong, J)
2227 CALL(jfloat, Float, Primitive::kPrimFloat, F)
2228 CALL(jdouble, Double, Primitive::kPrimDouble, D)
2229#undef CALL
2230
2231 static jstring NewString(JNIEnv* env, const jchar* unicode_chars, jsize len) {
2232 ScopedObjectAccess soa(env);
2233 ScopedCheck sc(kFlag_Default, __FUNCTION__);
2234 JniValueType args[3] = {{.E = env}, {.p = unicode_chars}, {.z = len}};
2235 if (sc.Check(soa, true, "Epz", args)) {
2236 JniValueType result;
2237 result.s = baseEnv(env)->NewString(env, unicode_chars, len);
2238 if (sc.Check(soa, false, "s", &result)) {
2239 return result.s;
2240 }
2241 }
2242 return nullptr;
2243 }
2244
2245 static jstring NewStringUTF(JNIEnv* env, const char* chars) {
2246 ScopedObjectAccess soa(env);
2247 ScopedCheck sc(kFlag_NullableUtf, __FUNCTION__);
2248 JniValueType args[2] = {{.E = env}, {.u = chars}};
2249 if (sc.Check(soa, true, "Eu", args)) {
2250 JniValueType result;
2251 // TODO: stale? show pointer and truncate string.
2252 result.s = baseEnv(env)->NewStringUTF(env, chars);
2253 if (sc.Check(soa, false, "s", &result)) {
2254 return result.s;
2255 }
2256 }
2257 return nullptr;
2258 }
2259
2260 static jsize GetStringLength(JNIEnv* env, jstring string) {
2261 ScopedObjectAccess soa(env);
2262 ScopedCheck sc(kFlag_CritOkay, __FUNCTION__);
2263 JniValueType args[2] = {{.E = env}, {.s = string}};
2264 if (sc.Check(soa, true, "Es", args)) {
2265 JniValueType result;
2266 result.z = baseEnv(env)->GetStringLength(env, string);
2267 if (sc.Check(soa, false, "z", &result)) {
2268 return result.z;
2269 }
2270 }
2271 return JNI_ERR;
2272 }
2273
2274 static jsize GetStringUTFLength(JNIEnv* env, jstring string) {
2275 ScopedObjectAccess soa(env);
2276 ScopedCheck sc(kFlag_CritOkay, __FUNCTION__);
2277 JniValueType args[2] = {{.E = env}, {.s = string}};
2278 if (sc.Check(soa, true, "Es", args)) {
2279 JniValueType result;
2280 result.z = baseEnv(env)->GetStringUTFLength(env, string);
2281 if (sc.Check(soa, false, "z", &result)) {
2282 return result.z;
2283 }
2284 }
2285 return JNI_ERR;
2286 }
2287
2288 static const jchar* GetStringChars(JNIEnv* env, jstring string, jboolean* is_copy) {
2289 return reinterpret_cast<const jchar*>(GetStringCharsInternal(__FUNCTION__, env, string,
2290 is_copy, false, false));
2291 }
2292
2293 static const char* GetStringUTFChars(JNIEnv* env, jstring string, jboolean* is_copy) {
2294 return reinterpret_cast<const char*>(GetStringCharsInternal(__FUNCTION__, env, string,
2295 is_copy, true, false));
2296 }
2297
2298 static const jchar* GetStringCritical(JNIEnv* env, jstring string, jboolean* is_copy) {
2299 return reinterpret_cast<const jchar*>(GetStringCharsInternal(__FUNCTION__, env, string,
2300 is_copy, false, true));
2301 }
2302
2303 static void ReleaseStringChars(JNIEnv* env, jstring string, const jchar* chars) {
2304 ReleaseStringCharsInternal(__FUNCTION__, env, string, chars, false, false);
2305 }
2306
2307 static void ReleaseStringUTFChars(JNIEnv* env, jstring string, const char* utf) {
2308 ReleaseStringCharsInternal(__FUNCTION__, env, string, utf, true, false);
2309 }
2310
2311 static void ReleaseStringCritical(JNIEnv* env, jstring string, const jchar* chars) {
2312 ReleaseStringCharsInternal(__FUNCTION__, env, string, chars, false, true);
2313 }
2314
2315 static void GetStringRegion(JNIEnv* env, jstring string, jsize start, jsize len, jchar* buf) {
2316 ScopedObjectAccess soa(env);
2317 ScopedCheck sc(kFlag_CritOkay, __FUNCTION__);
2318 JniValueType args[5] = {{.E = env}, {.s = string}, {.z = start}, {.z = len}, {.p = buf}};
2319 // Note: the start and len arguments are checked as 'I' rather than 'z' as invalid indices
2320 // result in ArrayIndexOutOfBoundsExceptions in the base implementation.
2321 if (sc.Check(soa, true, "EsIIp", args)) {
2322 baseEnv(env)->GetStringRegion(env, string, start, len, buf);
2323 JniValueType result;
2324 result.V = nullptr;
2325 sc.Check(soa, false, "V", &result);
2326 }
2327 }
2328
2329 static void GetStringUTFRegion(JNIEnv* env, jstring string, jsize start, jsize len, char* buf) {
2330 ScopedObjectAccess soa(env);
2331 ScopedCheck sc(kFlag_CritOkay, __FUNCTION__);
2332 JniValueType args[5] = {{.E = env}, {.s = string}, {.z = start}, {.z = len}, {.p = buf}};
2333 // Note: the start and len arguments are checked as 'I' rather than 'z' as invalid indices
2334 // result in ArrayIndexOutOfBoundsExceptions in the base implementation.
2335 if (sc.Check(soa, true, "EsIIp", args)) {
2336 baseEnv(env)->GetStringUTFRegion(env, string, start, len, buf);
2337 JniValueType result;
2338 result.V = nullptr;
2339 sc.Check(soa, false, "V", &result);
2340 }
2341 }
2342
2343 static jsize GetArrayLength(JNIEnv* env, jarray array) {
2344 ScopedObjectAccess soa(env);
2345 ScopedCheck sc(kFlag_CritOkay, __FUNCTION__);
2346 JniValueType args[2] = {{.E = env}, {.a = array}};
2347 if (sc.Check(soa, true, "Ea", args)) {
2348 JniValueType result;
2349 result.z = baseEnv(env)->GetArrayLength(env, array);
2350 if (sc.Check(soa, false, "z", &result)) {
2351 return result.z;
2352 }
2353 }
2354 return JNI_ERR;
2355 }
2356
2357 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_class,
2358 jobject initial_element) {
2359 ScopedObjectAccess soa(env);
2360 ScopedCheck sc(kFlag_Default, __FUNCTION__);
2361 JniValueType args[4] =
2362 {{.E = env}, {.z = length}, {.c = element_class}, {.L = initial_element}};
2363 if (sc.Check(soa, true, "EzcL", args)) {
2364 JniValueType result;
2365 // Note: assignability tests of initial_element are done in the base implementation.
2366 result.a = baseEnv(env)->NewObjectArray(env, length, element_class, initial_element);
2367 if (sc.Check(soa, false, "a", &result)) {
2368 return down_cast<jobjectArray>(result.a);
2369 }
2370 }
2371 return nullptr;
2372 }
2373
2374 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index) {
2375 ScopedObjectAccess soa(env);
2376 ScopedCheck sc(kFlag_Default, __FUNCTION__);
2377 JniValueType args[3] = {{.E = env}, {.a = array}, {.z = index}};
2378 if (sc.Check(soa, true, "Eaz", args)) {
2379 JniValueType result;
2380 result.L = baseEnv(env)->GetObjectArrayElement(env, array, index);
2381 if (sc.Check(soa, false, "L", &result)) {
2382 return result.L;
2383 }
2384 }
2385 return nullptr;
2386 }
2387
2388 static void SetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index, jobject value) {
2389 ScopedObjectAccess soa(env);
2390 ScopedCheck sc(kFlag_Default, __FUNCTION__);
2391 JniValueType args[4] = {{.E = env}, {.a = array}, {.z = index}, {.L = value}};
2392 // Note: the index arguments is checked as 'I' rather than 'z' as invalid indices result in
2393 // ArrayIndexOutOfBoundsExceptions in the base implementation. Similarly invalid stores result
2394 // in ArrayStoreExceptions.
2395 if (sc.Check(soa, true, "EaIL", args)) {
2396 baseEnv(env)->SetObjectArrayElement(env, array, index, value);
2397 JniValueType result;
2398 result.V = nullptr;
2399 sc.Check(soa, false, "V", &result);
2400 }
2401 }
2402
2403 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
2404 return down_cast<jbooleanArray>(NewPrimitiveArray(__FUNCTION__, env, length,
2405 Primitive::kPrimBoolean));
2406 }
2407
2408 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
2409 return down_cast<jbyteArray>(NewPrimitiveArray(__FUNCTION__, env, length,
2410 Primitive::kPrimByte));
2411 }
2412
2413 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
2414 return down_cast<jcharArray>(NewPrimitiveArray(__FUNCTION__, env, length,
2415 Primitive::kPrimChar));
2416 }
2417
2418 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
2419 return down_cast<jshortArray>(NewPrimitiveArray(__FUNCTION__, env, length,
2420 Primitive::kPrimShort));
2421 }
2422
2423 static jintArray NewIntArray(JNIEnv* env, jsize length) {
2424 return down_cast<jintArray>(NewPrimitiveArray(__FUNCTION__, env, length, Primitive::kPrimInt));
2425 }
2426
2427 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
2428 return down_cast<jlongArray>(NewPrimitiveArray(__FUNCTION__, env, length,
2429 Primitive::kPrimLong));
2430 }
2431
2432 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
2433 return down_cast<jfloatArray>(NewPrimitiveArray(__FUNCTION__, env, length,
2434 Primitive::kPrimFloat));
2435 }
2436
2437 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
2438 return down_cast<jdoubleArray>(NewPrimitiveArray(__FUNCTION__, env, length,
2439 Primitive::kPrimDouble));
2440 }
2441
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -07002442// NOLINT added to avoid wrong warning/fix from clang-tidy.
Ian Rogers68d8b422014-07-17 11:09:10 -07002443#define PRIMITIVE_ARRAY_FUNCTIONS(ctype, name, ptype) \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -07002444 static ctype* Get##name##ArrayElements(JNIEnv* env, ctype##Array array, jboolean* is_copy) { /* NOLINT */ \
2445 return reinterpret_cast<ctype*>( /* NOLINT */ \
Ian Rogers68d8b422014-07-17 11:09:10 -07002446 GetPrimitiveArrayElements(__FUNCTION__, ptype, env, array, is_copy)); \
2447 } \
2448 \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -07002449 static void Release##name##ArrayElements(JNIEnv* env, ctype##Array array, ctype* elems, /* NOLINT */ \
Ian Rogers68d8b422014-07-17 11:09:10 -07002450 jint mode) { \
2451 ReleasePrimitiveArrayElements(__FUNCTION__, ptype, env, array, elems, mode); \
2452 } \
2453 \
2454 static void Get##name##ArrayRegion(JNIEnv* env, ctype##Array array, jsize start, jsize len, \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -07002455 ctype* buf) { /* NOLINT */ \
Ian Rogers68d8b422014-07-17 11:09:10 -07002456 GetPrimitiveArrayRegion(__FUNCTION__, ptype, env, array, start, len, buf); \
2457 } \
2458 \
2459 static void Set##name##ArrayRegion(JNIEnv* env, ctype##Array array, jsize start, jsize len, \
2460 const ctype* buf) { \
2461 SetPrimitiveArrayRegion(__FUNCTION__, ptype, env, array, start, len, buf); \
2462 }
2463
2464 PRIMITIVE_ARRAY_FUNCTIONS(jboolean, Boolean, Primitive::kPrimBoolean)
2465 PRIMITIVE_ARRAY_FUNCTIONS(jbyte, Byte, Primitive::kPrimByte)
2466 PRIMITIVE_ARRAY_FUNCTIONS(jchar, Char, Primitive::kPrimChar)
2467 PRIMITIVE_ARRAY_FUNCTIONS(jshort, Short, Primitive::kPrimShort)
2468 PRIMITIVE_ARRAY_FUNCTIONS(jint, Int, Primitive::kPrimInt)
2469 PRIMITIVE_ARRAY_FUNCTIONS(jlong, Long, Primitive::kPrimLong)
2470 PRIMITIVE_ARRAY_FUNCTIONS(jfloat, Float, Primitive::kPrimFloat)
2471 PRIMITIVE_ARRAY_FUNCTIONS(jdouble, Double, Primitive::kPrimDouble)
2472#undef PRIMITIVE_ARRAY_FUNCTIONS
2473
2474 static jint MonitorEnter(JNIEnv* env, jobject obj) {
2475 ScopedObjectAccess soa(env);
2476 ScopedCheck sc(kFlag_Default, __FUNCTION__);
2477 JniValueType args[2] = {{.E = env}, {.L = obj}};
2478 if (sc.Check(soa, true, "EL", args)) {
Andreas Gampe5f4a09a2015-09-28 13:16:33 -07002479 if (obj != nullptr) {
2480 down_cast<JNIEnvExt*>(env)->RecordMonitorEnter(obj);
2481 }
Ian Rogers68d8b422014-07-17 11:09:10 -07002482 JniValueType result;
2483 result.i = baseEnv(env)->MonitorEnter(env, obj);
2484 if (sc.Check(soa, false, "i", &result)) {
2485 return result.i;
2486 }
2487 }
2488 return JNI_ERR;
2489 }
2490
2491 static jint MonitorExit(JNIEnv* env, jobject obj) {
2492 ScopedObjectAccess soa(env);
2493 ScopedCheck sc(kFlag_ExcepOkay, __FUNCTION__);
2494 JniValueType args[2] = {{.E = env}, {.L = obj}};
2495 if (sc.Check(soa, true, "EL", args)) {
Andreas Gampe5f4a09a2015-09-28 13:16:33 -07002496 if (obj != nullptr) {
2497 down_cast<JNIEnvExt*>(env)->CheckMonitorRelease(obj);
2498 }
Ian Rogers68d8b422014-07-17 11:09:10 -07002499 JniValueType result;
2500 result.i = baseEnv(env)->MonitorExit(env, obj);
2501 if (sc.Check(soa, false, "i", &result)) {
2502 return result.i;
2503 }
2504 }
2505 return JNI_ERR;
2506 }
2507
2508 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* is_copy) {
2509 ScopedObjectAccess soa(env);
2510 ScopedCheck sc(kFlag_CritGet, __FUNCTION__);
2511 JniValueType args[3] = {{.E = env}, {.a = array}, {.p = is_copy}};
2512 if (sc.Check(soa, true, "Eap", args)) {
2513 JniValueType result;
Mathieu Chartierb735bd92015-06-24 17:04:17 -07002514 void* ptr = baseEnv(env)->GetPrimitiveArrayCritical(env, array, is_copy);
2515 if (ptr != nullptr && soa.ForceCopy()) {
2516 ptr = GuardedCopy::CreateGuardedPACopy(env, array, is_copy, ptr);
Ian Rogers68d8b422014-07-17 11:09:10 -07002517 }
Mathieu Chartierb735bd92015-06-24 17:04:17 -07002518 result.p = ptr;
Ian Rogers68d8b422014-07-17 11:09:10 -07002519 if (sc.Check(soa, false, "p", &result)) {
2520 return const_cast<void*>(result.p);
2521 }
2522 }
2523 return nullptr;
2524 }
2525
2526 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* carray, jint mode) {
2527 ScopedObjectAccess soa(env);
2528 ScopedCheck sc(kFlag_CritRelease | kFlag_ExcepOkay, __FUNCTION__);
2529 sc.CheckNonNull(carray);
2530 JniValueType args[4] = {{.E = env}, {.a = array}, {.p = carray}, {.r = mode}};
2531 if (sc.Check(soa, true, "Eapr", args)) {
2532 if (soa.ForceCopy()) {
Mathieu Chartierb735bd92015-06-24 17:04:17 -07002533 carray = GuardedCopy::ReleaseGuardedPACopy(__FUNCTION__, env, array, carray, mode);
Ian Rogers68d8b422014-07-17 11:09:10 -07002534 }
2535 baseEnv(env)->ReleasePrimitiveArrayCritical(env, array, carray, mode);
2536 JniValueType result;
2537 result.V = nullptr;
2538 sc.Check(soa, false, "V", &result);
2539 }
Elliott Hughesa2501992011-08-26 19:39:54 -07002540 }
2541
2542 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002543 ScopedObjectAccess soa(env);
2544 ScopedCheck sc(kFlag_Default, __FUNCTION__);
2545 JniValueType args[3] = {{.E = env}, {.p = address}, {.J = capacity}};
2546 if (sc.Check(soa, true, "EpJ", args)) {
2547 JniValueType result;
2548 // Note: the validity of address and capacity are checked in the base implementation.
2549 result.L = baseEnv(env)->NewDirectByteBuffer(env, address, capacity);
2550 if (sc.Check(soa, false, "L", &result)) {
2551 return result.L;
2552 }
Elliott Hughesa2501992011-08-26 19:39:54 -07002553 }
Ian Rogers68d8b422014-07-17 11:09:10 -07002554 return nullptr;
Elliott Hughesa2501992011-08-26 19:39:54 -07002555 }
2556
2557 static void* GetDirectBufferAddress(JNIEnv* env, jobject buf) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002558 ScopedObjectAccess soa(env);
2559 ScopedCheck sc(kFlag_Default, __FUNCTION__);
2560 JniValueType args[2] = {{.E = env}, {.L = buf}};
2561 if (sc.Check(soa, true, "EL", args)) {
2562 JniValueType result;
2563 // Note: this is implemented in the base environment by a GetLongField which will sanity
2564 // check the type of buf in GetLongField above.
2565 result.p = baseEnv(env)->GetDirectBufferAddress(env, buf);
2566 if (sc.Check(soa, false, "p", &result)) {
2567 return const_cast<void*>(result.p);
2568 }
2569 }
2570 return nullptr;
Elliott Hughesa2501992011-08-26 19:39:54 -07002571 }
2572
2573 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject buf) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002574 ScopedObjectAccess soa(env);
2575 ScopedCheck sc(kFlag_Default, __FUNCTION__);
2576 JniValueType args[2] = {{.E = env}, {.L = buf}};
2577 if (sc.Check(soa, true, "EL", args)) {
2578 JniValueType result;
2579 // Note: this is implemented in the base environment by a GetIntField which will sanity
2580 // check the type of buf in GetIntField above.
2581 result.J = baseEnv(env)->GetDirectBufferCapacity(env, buf);
2582 if (sc.Check(soa, false, "J", &result)) {
2583 return result.J;
2584 }
2585 }
2586 return JNI_ERR;
Elliott Hughesa2501992011-08-26 19:39:54 -07002587 }
2588
2589 private:
Ian Rogers68d8b422014-07-17 11:09:10 -07002590 static JavaVMExt* GetJavaVMExt(JNIEnv* env) {
2591 return reinterpret_cast<JNIEnvExt*>(env)->vm;
2592 }
2593
2594 static const JNINativeInterface* baseEnv(JNIEnv* env) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002595 return reinterpret_cast<JNIEnvExt*>(env)->unchecked_functions;
2596 }
Ian Rogers68d8b422014-07-17 11:09:10 -07002597
2598 static jobject NewRef(const char* function_name, JNIEnv* env, jobject obj, IndirectRefKind kind) {
2599 ScopedObjectAccess soa(env);
2600 ScopedCheck sc(kFlag_Default, function_name);
2601 JniValueType args[2] = {{.E = env}, {.L = obj}};
2602 if (sc.Check(soa, true, "EL", args)) {
2603 JniValueType result;
2604 switch (kind) {
2605 case kGlobal:
2606 result.L = baseEnv(env)->NewGlobalRef(env, obj);
2607 break;
2608 case kLocal:
2609 result.L = baseEnv(env)->NewLocalRef(env, obj);
2610 break;
2611 case kWeakGlobal:
2612 result.L = baseEnv(env)->NewWeakGlobalRef(env, obj);
2613 break;
2614 default:
2615 LOG(FATAL) << "Unexpected reference kind: " << kind;
2616 }
2617 if (sc.Check(soa, false, "L", &result)) {
2618 DCHECK_EQ(IsSameObject(env, obj, result.L), JNI_TRUE);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002619 DCHECK(sc.CheckReferenceKind(kind, soa.Self(), result.L));
Ian Rogers68d8b422014-07-17 11:09:10 -07002620 return result.L;
2621 }
2622 }
2623 return nullptr;
2624 }
2625
2626 static void DeleteRef(const char* function_name, JNIEnv* env, jobject obj, IndirectRefKind kind) {
2627 ScopedObjectAccess soa(env);
2628 ScopedCheck sc(kFlag_ExcepOkay, function_name);
2629 JniValueType args[2] = {{.E = env}, {.L = obj}};
2630 sc.Check(soa, true, "EL", args);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002631 if (sc.CheckReferenceKind(kind, soa.Self(), obj)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002632 JniValueType result;
2633 switch (kind) {
2634 case kGlobal:
2635 baseEnv(env)->DeleteGlobalRef(env, obj);
2636 break;
2637 case kLocal:
2638 baseEnv(env)->DeleteLocalRef(env, obj);
2639 break;
2640 case kWeakGlobal:
2641 baseEnv(env)->DeleteWeakGlobalRef(env, obj);
2642 break;
2643 default:
2644 LOG(FATAL) << "Unexpected reference kind: " << kind;
2645 }
2646 result.V = nullptr;
2647 sc.Check(soa, false, "V", &result);
2648 }
2649 }
2650
2651 static jmethodID GetMethodIDInternal(const char* function_name, JNIEnv* env, jclass c,
2652 const char* name, const char* sig, bool is_static) {
2653 ScopedObjectAccess soa(env);
2654 ScopedCheck sc(kFlag_Default, function_name);
2655 JniValueType args[4] = {{.E = env}, {.c = c}, {.u = name}, {.u = sig}};
2656 if (sc.Check(soa, true, "Ecuu", args)) {
2657 JniValueType result;
2658 if (is_static) {
2659 result.m = baseEnv(env)->GetStaticMethodID(env, c, name, sig);
2660 } else {
2661 result.m = baseEnv(env)->GetMethodID(env, c, name, sig);
2662 }
2663 if (sc.Check(soa, false, "m", &result)) {
2664 return result.m;
2665 }
2666 }
2667 return nullptr;
2668 }
2669
2670 static jfieldID GetFieldIDInternal(const char* function_name, JNIEnv* env, jclass c,
2671 const char* name, const char* sig, bool is_static) {
2672 ScopedObjectAccess soa(env);
2673 ScopedCheck sc(kFlag_Default, function_name);
2674 JniValueType args[4] = {{.E = env}, {.c = c}, {.u = name}, {.u = sig}};
2675 if (sc.Check(soa, true, "Ecuu", args)) {
2676 JniValueType result;
2677 if (is_static) {
2678 result.f = baseEnv(env)->GetStaticFieldID(env, c, name, sig);
2679 } else {
2680 result.f = baseEnv(env)->GetFieldID(env, c, name, sig);
2681 }
2682 if (sc.Check(soa, false, "f", &result)) {
2683 return result.f;
2684 }
2685 }
2686 return nullptr;
2687 }
2688
2689 static JniValueType GetField(const char* function_name, JNIEnv* env, jobject obj, jfieldID fid,
2690 bool is_static, Primitive::Type type) {
2691 ScopedObjectAccess soa(env);
2692 ScopedCheck sc(kFlag_Default, function_name);
2693 JniValueType args[3] = {{.E = env}, {.L = obj}, {.f = fid}};
2694 JniValueType result;
2695 if (sc.Check(soa, true, is_static ? "Ecf" : "ELf", args) &&
2696 sc.CheckFieldAccess(soa, obj, fid, is_static, type)) {
2697 const char* result_check = nullptr;
2698 switch (type) {
2699 case Primitive::kPrimNot:
2700 if (is_static) {
2701 result.L = baseEnv(env)->GetStaticObjectField(env, down_cast<jclass>(obj), fid);
2702 } else {
2703 result.L = baseEnv(env)->GetObjectField(env, obj, fid);
2704 }
2705 result_check = "L";
2706 break;
2707 case Primitive::kPrimBoolean:
2708 if (is_static) {
2709 result.Z = baseEnv(env)->GetStaticBooleanField(env, down_cast<jclass>(obj), fid);
2710 } else {
2711 result.Z = baseEnv(env)->GetBooleanField(env, obj, fid);
2712 }
2713 result_check = "Z";
2714 break;
2715 case Primitive::kPrimByte:
2716 if (is_static) {
2717 result.B = baseEnv(env)->GetStaticByteField(env, down_cast<jclass>(obj), fid);
2718 } else {
2719 result.B = baseEnv(env)->GetByteField(env, obj, fid);
2720 }
2721 result_check = "B";
2722 break;
2723 case Primitive::kPrimChar:
2724 if (is_static) {
2725 result.C = baseEnv(env)->GetStaticCharField(env, down_cast<jclass>(obj), fid);
2726 } else {
2727 result.C = baseEnv(env)->GetCharField(env, obj, fid);
2728 }
2729 result_check = "C";
2730 break;
2731 case Primitive::kPrimShort:
2732 if (is_static) {
2733 result.S = baseEnv(env)->GetStaticShortField(env, down_cast<jclass>(obj), fid);
2734 } else {
2735 result.S = baseEnv(env)->GetShortField(env, obj, fid);
2736 }
2737 result_check = "S";
2738 break;
2739 case Primitive::kPrimInt:
2740 if (is_static) {
2741 result.I = baseEnv(env)->GetStaticIntField(env, down_cast<jclass>(obj), fid);
2742 } else {
2743 result.I = baseEnv(env)->GetIntField(env, obj, fid);
2744 }
2745 result_check = "I";
2746 break;
2747 case Primitive::kPrimLong:
2748 if (is_static) {
2749 result.J = baseEnv(env)->GetStaticLongField(env, down_cast<jclass>(obj), fid);
2750 } else {
2751 result.J = baseEnv(env)->GetLongField(env, obj, fid);
2752 }
2753 result_check = "J";
2754 break;
2755 case Primitive::kPrimFloat:
2756 if (is_static) {
2757 result.F = baseEnv(env)->GetStaticFloatField(env, down_cast<jclass>(obj), fid);
2758 } else {
2759 result.F = baseEnv(env)->GetFloatField(env, obj, fid);
2760 }
2761 result_check = "F";
2762 break;
2763 case Primitive::kPrimDouble:
2764 if (is_static) {
2765 result.D = baseEnv(env)->GetStaticDoubleField(env, down_cast<jclass>(obj), fid);
2766 } else {
2767 result.D = baseEnv(env)->GetDoubleField(env, obj, fid);
2768 }
2769 result_check = "D";
2770 break;
2771 case Primitive::kPrimVoid:
2772 LOG(FATAL) << "Unexpected type: " << type;
2773 break;
2774 }
2775 if (sc.Check(soa, false, result_check, &result)) {
2776 return result;
2777 }
2778 }
2779 result.J = 0;
2780 return result;
2781 }
2782
2783 static void SetField(const char* function_name, JNIEnv* env, jobject obj, jfieldID fid,
2784 bool is_static, Primitive::Type type, JniValueType value) {
2785 ScopedObjectAccess soa(env);
2786 ScopedCheck sc(kFlag_Default, function_name);
2787 JniValueType args[4] = {{.E = env}, {.L = obj}, {.f = fid}, value};
2788 char sig[5] = { 'E', is_static ? 'c' : 'L', 'f',
2789 type == Primitive::kPrimNot ? 'L' : Primitive::Descriptor(type)[0], '\0'};
2790 if (sc.Check(soa, true, sig, args) &&
2791 sc.CheckFieldAccess(soa, obj, fid, is_static, type)) {
2792 switch (type) {
2793 case Primitive::kPrimNot:
2794 if (is_static) {
2795 baseEnv(env)->SetStaticObjectField(env, down_cast<jclass>(obj), fid, value.L);
2796 } else {
2797 baseEnv(env)->SetObjectField(env, obj, fid, value.L);
2798 }
2799 break;
2800 case Primitive::kPrimBoolean:
2801 if (is_static) {
2802 baseEnv(env)->SetStaticBooleanField(env, down_cast<jclass>(obj), fid, value.Z);
2803 } else {
2804 baseEnv(env)->SetBooleanField(env, obj, fid, value.Z);
2805 }
2806 break;
2807 case Primitive::kPrimByte:
2808 if (is_static) {
2809 baseEnv(env)->SetStaticByteField(env, down_cast<jclass>(obj), fid, value.B);
2810 } else {
2811 baseEnv(env)->SetByteField(env, obj, fid, value.B);
2812 }
2813 break;
2814 case Primitive::kPrimChar:
2815 if (is_static) {
2816 baseEnv(env)->SetStaticCharField(env, down_cast<jclass>(obj), fid, value.C);
2817 } else {
2818 baseEnv(env)->SetCharField(env, obj, fid, value.C);
2819 }
2820 break;
2821 case Primitive::kPrimShort:
2822 if (is_static) {
2823 baseEnv(env)->SetStaticShortField(env, down_cast<jclass>(obj), fid, value.S);
2824 } else {
2825 baseEnv(env)->SetShortField(env, obj, fid, value.S);
2826 }
2827 break;
2828 case Primitive::kPrimInt:
2829 if (is_static) {
2830 baseEnv(env)->SetStaticIntField(env, down_cast<jclass>(obj), fid, value.I);
2831 } else {
2832 baseEnv(env)->SetIntField(env, obj, fid, value.I);
2833 }
2834 break;
2835 case Primitive::kPrimLong:
2836 if (is_static) {
2837 baseEnv(env)->SetStaticLongField(env, down_cast<jclass>(obj), fid, value.J);
2838 } else {
2839 baseEnv(env)->SetLongField(env, obj, fid, value.J);
2840 }
2841 break;
2842 case Primitive::kPrimFloat:
2843 if (is_static) {
2844 baseEnv(env)->SetStaticFloatField(env, down_cast<jclass>(obj), fid, value.F);
2845 } else {
2846 baseEnv(env)->SetFloatField(env, obj, fid, value.F);
2847 }
2848 break;
2849 case Primitive::kPrimDouble:
2850 if (is_static) {
2851 baseEnv(env)->SetStaticDoubleField(env, down_cast<jclass>(obj), fid, value.D);
2852 } else {
2853 baseEnv(env)->SetDoubleField(env, obj, fid, value.D);
2854 }
2855 break;
2856 case Primitive::kPrimVoid:
2857 LOG(FATAL) << "Unexpected type: " << type;
2858 break;
2859 }
2860 JniValueType result;
2861 result.V = nullptr;
2862 sc.Check(soa, false, "V", &result);
2863 }
2864 }
2865
2866 static bool CheckCallArgs(ScopedObjectAccess& soa, ScopedCheck& sc, JNIEnv* env, jobject obj,
Alex Light48ffe062015-08-19 15:23:23 -07002867 jclass c, jmethodID mid, InvokeType invoke, const VarArgs* vargs)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002868 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002869 bool checked;
2870 switch (invoke) {
2871 case kVirtual: {
2872 DCHECK(c == nullptr);
Alex Light48ffe062015-08-19 15:23:23 -07002873 JniValueType args[4] = {{.E = env}, {.L = obj}, {.m = mid}, {.va = vargs}};
2874 checked = sc.Check(soa, true, "ELm.", args);
Ian Rogers68d8b422014-07-17 11:09:10 -07002875 break;
2876 }
2877 case kDirect: {
Alex Light48ffe062015-08-19 15:23:23 -07002878 JniValueType args[5] = {{.E = env}, {.L = obj}, {.c = c}, {.m = mid}, {.va = vargs}};
2879 checked = sc.Check(soa, true, "ELcm.", args);
Ian Rogers68d8b422014-07-17 11:09:10 -07002880 break;
2881 }
2882 case kStatic: {
2883 DCHECK(obj == nullptr);
Alex Light48ffe062015-08-19 15:23:23 -07002884 JniValueType args[4] = {{.E = env}, {.c = c}, {.m = mid}, {.va = vargs}};
2885 checked = sc.Check(soa, true, "Ecm.", args);
Ian Rogers68d8b422014-07-17 11:09:10 -07002886 break;
2887 }
2888 default:
2889 LOG(FATAL) << "Unexpected invoke: " << invoke;
2890 checked = false;
2891 break;
2892 }
2893 return checked;
2894 }
2895
2896 static JniValueType CallMethodA(const char* function_name, JNIEnv* env, jobject obj, jclass c,
2897 jmethodID mid, jvalue* vargs, Primitive::Type type,
2898 InvokeType invoke) {
2899 ScopedObjectAccess soa(env);
2900 ScopedCheck sc(kFlag_Default, function_name);
2901 JniValueType result;
Alex Light48ffe062015-08-19 15:23:23 -07002902 VarArgs rest(mid, vargs);
2903 if (CheckCallArgs(soa, sc, env, obj, c, mid, invoke, &rest) &&
Ian Rogers68d8b422014-07-17 11:09:10 -07002904 sc.CheckMethodAndSig(soa, obj, c, mid, type, invoke)) {
2905 const char* result_check;
2906 switch (type) {
2907 case Primitive::kPrimNot:
2908 result_check = "L";
2909 switch (invoke) {
2910 case kVirtual:
2911 result.L = baseEnv(env)->CallObjectMethodA(env, obj, mid, vargs);
2912 break;
2913 case kDirect:
2914 result.L = baseEnv(env)->CallNonvirtualObjectMethodA(env, obj, c, mid, vargs);
2915 break;
2916 case kStatic:
2917 result.L = baseEnv(env)->CallStaticObjectMethodA(env, c, mid, vargs);
2918 break;
2919 default:
2920 break;
2921 }
2922 break;
2923 case Primitive::kPrimBoolean:
2924 result_check = "Z";
2925 switch (invoke) {
2926 case kVirtual:
2927 result.Z = baseEnv(env)->CallBooleanMethodA(env, obj, mid, vargs);
2928 break;
2929 case kDirect:
2930 result.Z = baseEnv(env)->CallNonvirtualBooleanMethodA(env, obj, c, mid, vargs);
2931 break;
2932 case kStatic:
2933 result.Z = baseEnv(env)->CallStaticBooleanMethodA(env, c, mid, vargs);
2934 break;
2935 default:
2936 break;
2937 }
2938 break;
2939 case Primitive::kPrimByte:
2940 result_check = "B";
2941 switch (invoke) {
2942 case kVirtual:
2943 result.B = baseEnv(env)->CallByteMethodA(env, obj, mid, vargs);
2944 break;
2945 case kDirect:
2946 result.B = baseEnv(env)->CallNonvirtualByteMethodA(env, obj, c, mid, vargs);
2947 break;
2948 case kStatic:
2949 result.B = baseEnv(env)->CallStaticByteMethodA(env, c, mid, vargs);
2950 break;
2951 default:
2952 break;
2953 }
2954 break;
2955 case Primitive::kPrimChar:
2956 result_check = "C";
2957 switch (invoke) {
2958 case kVirtual:
2959 result.C = baseEnv(env)->CallCharMethodA(env, obj, mid, vargs);
2960 break;
2961 case kDirect:
2962 result.C = baseEnv(env)->CallNonvirtualCharMethodA(env, obj, c, mid, vargs);
2963 break;
2964 case kStatic:
2965 result.C = baseEnv(env)->CallStaticCharMethodA(env, c, mid, vargs);
2966 break;
2967 default:
2968 break;
2969 }
2970 break;
2971 case Primitive::kPrimShort:
2972 result_check = "S";
2973 switch (invoke) {
2974 case kVirtual:
2975 result.S = baseEnv(env)->CallShortMethodA(env, obj, mid, vargs);
2976 break;
2977 case kDirect:
2978 result.S = baseEnv(env)->CallNonvirtualShortMethodA(env, obj, c, mid, vargs);
2979 break;
2980 case kStatic:
2981 result.S = baseEnv(env)->CallStaticShortMethodA(env, c, mid, vargs);
2982 break;
2983 default:
2984 break;
2985 }
2986 break;
2987 case Primitive::kPrimInt:
2988 result_check = "I";
2989 switch (invoke) {
2990 case kVirtual:
2991 result.I = baseEnv(env)->CallIntMethodA(env, obj, mid, vargs);
2992 break;
2993 case kDirect:
2994 result.I = baseEnv(env)->CallNonvirtualIntMethodA(env, obj, c, mid, vargs);
2995 break;
2996 case kStatic:
2997 result.I = baseEnv(env)->CallStaticIntMethodA(env, c, mid, vargs);
2998 break;
2999 default:
3000 break;
3001 }
3002 break;
3003 case Primitive::kPrimLong:
3004 result_check = "J";
3005 switch (invoke) {
3006 case kVirtual:
3007 result.J = baseEnv(env)->CallLongMethodA(env, obj, mid, vargs);
3008 break;
3009 case kDirect:
3010 result.J = baseEnv(env)->CallNonvirtualLongMethodA(env, obj, c, mid, vargs);
3011 break;
3012 case kStatic:
3013 result.J = baseEnv(env)->CallStaticLongMethodA(env, c, mid, vargs);
3014 break;
3015 default:
3016 break;
3017 }
3018 break;
3019 case Primitive::kPrimFloat:
3020 result_check = "F";
3021 switch (invoke) {
3022 case kVirtual:
3023 result.F = baseEnv(env)->CallFloatMethodA(env, obj, mid, vargs);
3024 break;
3025 case kDirect:
3026 result.F = baseEnv(env)->CallNonvirtualFloatMethodA(env, obj, c, mid, vargs);
3027 break;
3028 case kStatic:
3029 result.F = baseEnv(env)->CallStaticFloatMethodA(env, c, mid, vargs);
3030 break;
3031 default:
3032 break;
3033 }
3034 break;
3035 case Primitive::kPrimDouble:
3036 result_check = "D";
3037 switch (invoke) {
3038 case kVirtual:
3039 result.D = baseEnv(env)->CallDoubleMethodA(env, obj, mid, vargs);
3040 break;
3041 case kDirect:
3042 result.D = baseEnv(env)->CallNonvirtualDoubleMethodA(env, obj, c, mid, vargs);
3043 break;
3044 case kStatic:
3045 result.D = baseEnv(env)->CallStaticDoubleMethodA(env, c, mid, vargs);
3046 break;
3047 default:
3048 break;
3049 }
3050 break;
3051 case Primitive::kPrimVoid:
3052 result_check = "V";
3053 result.V = nullptr;
3054 switch (invoke) {
3055 case kVirtual:
3056 baseEnv(env)->CallVoidMethodA(env, obj, mid, vargs);
3057 break;
3058 case kDirect:
3059 baseEnv(env)->CallNonvirtualVoidMethodA(env, obj, c, mid, vargs);
3060 break;
3061 case kStatic:
3062 baseEnv(env)->CallStaticVoidMethodA(env, c, mid, vargs);
3063 break;
3064 default:
3065 LOG(FATAL) << "Unexpected invoke: " << invoke;
3066 }
3067 break;
3068 default:
3069 LOG(FATAL) << "Unexpected return type: " << type;
3070 result_check = nullptr;
3071 }
3072 if (sc.Check(soa, false, result_check, &result)) {
3073 return result;
3074 }
3075 }
3076 result.J = 0;
3077 return result;
3078 }
3079
3080 static JniValueType CallMethodV(const char* function_name, JNIEnv* env, jobject obj, jclass c,
3081 jmethodID mid, va_list vargs, Primitive::Type type,
3082 InvokeType invoke) {
3083 ScopedObjectAccess soa(env);
3084 ScopedCheck sc(kFlag_Default, function_name);
3085 JniValueType result;
Alex Light48ffe062015-08-19 15:23:23 -07003086 VarArgs rest(mid, vargs);
3087 if (CheckCallArgs(soa, sc, env, obj, c, mid, invoke, &rest) &&
Ian Rogers68d8b422014-07-17 11:09:10 -07003088 sc.CheckMethodAndSig(soa, obj, c, mid, type, invoke)) {
3089 const char* result_check;
3090 switch (type) {
3091 case Primitive::kPrimNot:
3092 result_check = "L";
3093 switch (invoke) {
3094 case kVirtual:
3095 result.L = baseEnv(env)->CallObjectMethodV(env, obj, mid, vargs);
3096 break;
3097 case kDirect:
3098 result.L = baseEnv(env)->CallNonvirtualObjectMethodV(env, obj, c, mid, vargs);
3099 break;
3100 case kStatic:
3101 result.L = baseEnv(env)->CallStaticObjectMethodV(env, c, mid, vargs);
3102 break;
3103 default:
3104 LOG(FATAL) << "Unexpected invoke: " << invoke;
3105 }
3106 break;
3107 case Primitive::kPrimBoolean:
3108 result_check = "Z";
3109 switch (invoke) {
3110 case kVirtual:
3111 result.Z = baseEnv(env)->CallBooleanMethodV(env, obj, mid, vargs);
3112 break;
3113 case kDirect:
3114 result.Z = baseEnv(env)->CallNonvirtualBooleanMethodV(env, obj, c, mid, vargs);
3115 break;
3116 case kStatic:
3117 result.Z = baseEnv(env)->CallStaticBooleanMethodV(env, c, mid, vargs);
3118 break;
3119 default:
3120 LOG(FATAL) << "Unexpected invoke: " << invoke;
3121 }
3122 break;
3123 case Primitive::kPrimByte:
3124 result_check = "B";
3125 switch (invoke) {
3126 case kVirtual:
3127 result.B = baseEnv(env)->CallByteMethodV(env, obj, mid, vargs);
3128 break;
3129 case kDirect:
3130 result.B = baseEnv(env)->CallNonvirtualByteMethodV(env, obj, c, mid, vargs);
3131 break;
3132 case kStatic:
3133 result.B = baseEnv(env)->CallStaticByteMethodV(env, c, mid, vargs);
3134 break;
3135 default:
3136 LOG(FATAL) << "Unexpected invoke: " << invoke;
3137 }
3138 break;
3139 case Primitive::kPrimChar:
3140 result_check = "C";
3141 switch (invoke) {
3142 case kVirtual:
3143 result.C = baseEnv(env)->CallCharMethodV(env, obj, mid, vargs);
3144 break;
3145 case kDirect:
3146 result.C = baseEnv(env)->CallNonvirtualCharMethodV(env, obj, c, mid, vargs);
3147 break;
3148 case kStatic:
3149 result.C = baseEnv(env)->CallStaticCharMethodV(env, c, mid, vargs);
3150 break;
3151 default:
3152 LOG(FATAL) << "Unexpected invoke: " << invoke;
3153 }
3154 break;
3155 case Primitive::kPrimShort:
3156 result_check = "S";
3157 switch (invoke) {
3158 case kVirtual:
3159 result.S = baseEnv(env)->CallShortMethodV(env, obj, mid, vargs);
3160 break;
3161 case kDirect:
3162 result.S = baseEnv(env)->CallNonvirtualShortMethodV(env, obj, c, mid, vargs);
3163 break;
3164 case kStatic:
3165 result.S = baseEnv(env)->CallStaticShortMethodV(env, c, mid, vargs);
3166 break;
3167 default:
3168 LOG(FATAL) << "Unexpected invoke: " << invoke;
3169 }
3170 break;
3171 case Primitive::kPrimInt:
3172 result_check = "I";
3173 switch (invoke) {
3174 case kVirtual:
3175 result.I = baseEnv(env)->CallIntMethodV(env, obj, mid, vargs);
3176 break;
3177 case kDirect:
3178 result.I = baseEnv(env)->CallNonvirtualIntMethodV(env, obj, c, mid, vargs);
3179 break;
3180 case kStatic:
3181 result.I = baseEnv(env)->CallStaticIntMethodV(env, c, mid, vargs);
3182 break;
3183 default:
3184 LOG(FATAL) << "Unexpected invoke: " << invoke;
3185 }
3186 break;
3187 case Primitive::kPrimLong:
3188 result_check = "J";
3189 switch (invoke) {
3190 case kVirtual:
3191 result.J = baseEnv(env)->CallLongMethodV(env, obj, mid, vargs);
3192 break;
3193 case kDirect:
3194 result.J = baseEnv(env)->CallNonvirtualLongMethodV(env, obj, c, mid, vargs);
3195 break;
3196 case kStatic:
3197 result.J = baseEnv(env)->CallStaticLongMethodV(env, c, mid, vargs);
3198 break;
3199 default:
3200 LOG(FATAL) << "Unexpected invoke: " << invoke;
3201 }
3202 break;
3203 case Primitive::kPrimFloat:
3204 result_check = "F";
3205 switch (invoke) {
3206 case kVirtual:
3207 result.F = baseEnv(env)->CallFloatMethodV(env, obj, mid, vargs);
3208 break;
3209 case kDirect:
3210 result.F = baseEnv(env)->CallNonvirtualFloatMethodV(env, obj, c, mid, vargs);
3211 break;
3212 case kStatic:
3213 result.F = baseEnv(env)->CallStaticFloatMethodV(env, c, mid, vargs);
3214 break;
3215 default:
3216 LOG(FATAL) << "Unexpected invoke: " << invoke;
3217 }
3218 break;
3219 case Primitive::kPrimDouble:
3220 result_check = "D";
3221 switch (invoke) {
3222 case kVirtual:
3223 result.D = baseEnv(env)->CallDoubleMethodV(env, obj, mid, vargs);
3224 break;
3225 case kDirect:
3226 result.D = baseEnv(env)->CallNonvirtualDoubleMethodV(env, obj, c, mid, vargs);
3227 break;
3228 case kStatic:
3229 result.D = baseEnv(env)->CallStaticDoubleMethodV(env, c, mid, vargs);
3230 break;
3231 default:
3232 LOG(FATAL) << "Unexpected invoke: " << invoke;
3233 }
3234 break;
3235 case Primitive::kPrimVoid:
3236 result_check = "V";
3237 result.V = nullptr;
3238 switch (invoke) {
3239 case kVirtual:
3240 baseEnv(env)->CallVoidMethodV(env, obj, mid, vargs);
3241 break;
3242 case kDirect:
3243 baseEnv(env)->CallNonvirtualVoidMethodV(env, obj, c, mid, vargs);
3244 break;
3245 case kStatic:
3246 baseEnv(env)->CallStaticVoidMethodV(env, c, mid, vargs);
3247 break;
3248 default:
3249 LOG(FATAL) << "Unexpected invoke: " << invoke;
3250 }
3251 break;
3252 default:
3253 LOG(FATAL) << "Unexpected return type: " << type;
3254 result_check = nullptr;
3255 }
3256 if (sc.Check(soa, false, result_check, &result)) {
3257 return result;
3258 }
3259 }
3260 result.J = 0;
3261 return result;
3262 }
3263
3264 static const void* GetStringCharsInternal(const char* function_name, JNIEnv* env, jstring string,
3265 jboolean* is_copy, bool utf, bool critical) {
3266 ScopedObjectAccess soa(env);
3267 int flags = critical ? kFlag_CritGet : kFlag_CritOkay;
3268 ScopedCheck sc(flags, function_name);
3269 JniValueType args[3] = {{.E = env}, {.s = string}, {.p = is_copy}};
3270 if (sc.Check(soa, true, "Esp", args)) {
3271 JniValueType result;
Mathieu Chartierb735bd92015-06-24 17:04:17 -07003272 void* ptr;
Ian Rogers68d8b422014-07-17 11:09:10 -07003273 if (utf) {
3274 CHECK(!critical);
Mathieu Chartierb735bd92015-06-24 17:04:17 -07003275 ptr = const_cast<char*>(baseEnv(env)->GetStringUTFChars(env, string, is_copy));
3276 result.u = reinterpret_cast<char*>(ptr);
Ian Rogers68d8b422014-07-17 11:09:10 -07003277 } else {
Mathieu Chartierb735bd92015-06-24 17:04:17 -07003278 ptr = const_cast<jchar*>(critical ? baseEnv(env)->GetStringCritical(env, string, is_copy) :
3279 baseEnv(env)->GetStringChars(env, string, is_copy));
3280 result.p = ptr;
Ian Rogers68d8b422014-07-17 11:09:10 -07003281 }
3282 // TODO: could we be smarter about not copying when local_is_copy?
Mathieu Chartierb735bd92015-06-24 17:04:17 -07003283 if (ptr != nullptr && soa.ForceCopy()) {
Ian Rogers68d8b422014-07-17 11:09:10 -07003284 if (utf) {
3285 size_t length_in_bytes = strlen(result.u) + 1;
3286 result.u =
Mathieu Chartierb735bd92015-06-24 17:04:17 -07003287 reinterpret_cast<const char*>(GuardedCopy::Create(ptr, length_in_bytes, false));
Ian Rogers68d8b422014-07-17 11:09:10 -07003288 } else {
3289 size_t length_in_bytes = baseEnv(env)->GetStringLength(env, string) * 2;
3290 result.p =
Mathieu Chartierb735bd92015-06-24 17:04:17 -07003291 reinterpret_cast<const jchar*>(GuardedCopy::Create(ptr, length_in_bytes, false));
Ian Rogers68d8b422014-07-17 11:09:10 -07003292 }
3293 if (is_copy != nullptr) {
3294 *is_copy = JNI_TRUE;
3295 }
3296 }
3297 if (sc.Check(soa, false, utf ? "u" : "p", &result)) {
3298 return utf ? result.u : result.p;
3299 }
3300 }
3301 return nullptr;
3302 }
3303
3304 static void ReleaseStringCharsInternal(const char* function_name, JNIEnv* env, jstring string,
3305 const void* chars, bool utf, bool critical) {
3306 ScopedObjectAccess soa(env);
3307 int flags = kFlag_ExcepOkay | kFlag_Release;
3308 if (critical) {
3309 flags |= kFlag_CritRelease;
3310 }
3311 ScopedCheck sc(flags, function_name);
3312 sc.CheckNonNull(chars);
3313 bool force_copy_ok = !soa.ForceCopy() || GuardedCopy::Check(function_name, chars, false);
3314 if (force_copy_ok && soa.ForceCopy()) {
3315 chars = reinterpret_cast<const jchar*>(GuardedCopy::Destroy(const_cast<void*>(chars)));
3316 }
3317 if (force_copy_ok) {
3318 JniValueType args[3] = {{.E = env}, {.s = string}, {.p = chars}};
3319 if (sc.Check(soa, true, utf ? "Esu" : "Esp", args)) {
3320 if (utf) {
3321 CHECK(!critical);
3322 baseEnv(env)->ReleaseStringUTFChars(env, string, reinterpret_cast<const char*>(chars));
3323 } else {
3324 if (critical) {
3325 baseEnv(env)->ReleaseStringCritical(env, string, reinterpret_cast<const jchar*>(chars));
3326 } else {
3327 baseEnv(env)->ReleaseStringChars(env, string, reinterpret_cast<const jchar*>(chars));
3328 }
3329 }
3330 JniValueType result;
3331 sc.Check(soa, false, "V", &result);
3332 }
3333 }
3334 }
3335
3336 static jarray NewPrimitiveArray(const char* function_name, JNIEnv* env, jsize length,
3337 Primitive::Type type) {
3338 ScopedObjectAccess soa(env);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003339 ScopedCheck sc(kFlag_Default, function_name);
Ian Rogers68d8b422014-07-17 11:09:10 -07003340 JniValueType args[2] = {{.E = env}, {.z = length}};
3341 if (sc.Check(soa, true, "Ez", args)) {
3342 JniValueType result;
3343 switch (type) {
3344 case Primitive::kPrimBoolean:
3345 result.a = baseEnv(env)->NewBooleanArray(env, length);
3346 break;
3347 case Primitive::kPrimByte:
3348 result.a = baseEnv(env)->NewByteArray(env, length);
3349 break;
3350 case Primitive::kPrimChar:
3351 result.a = baseEnv(env)->NewCharArray(env, length);
3352 break;
3353 case Primitive::kPrimShort:
3354 result.a = baseEnv(env)->NewShortArray(env, length);
3355 break;
3356 case Primitive::kPrimInt:
3357 result.a = baseEnv(env)->NewIntArray(env, length);
3358 break;
3359 case Primitive::kPrimLong:
3360 result.a = baseEnv(env)->NewLongArray(env, length);
3361 break;
3362 case Primitive::kPrimFloat:
3363 result.a = baseEnv(env)->NewFloatArray(env, length);
3364 break;
3365 case Primitive::kPrimDouble:
3366 result.a = baseEnv(env)->NewDoubleArray(env, length);
3367 break;
3368 default:
3369 LOG(FATAL) << "Unexpected primitive type: " << type;
3370 }
3371 if (sc.Check(soa, false, "a", &result)) {
3372 return result.a;
3373 }
3374 }
3375 return nullptr;
3376 }
3377
3378 static void* GetPrimitiveArrayElements(const char* function_name, Primitive::Type type,
3379 JNIEnv* env, jarray array, jboolean* is_copy) {
3380 ScopedObjectAccess soa(env);
3381 ScopedCheck sc(kFlag_Default, function_name);
3382 JniValueType args[3] = {{.E = env}, {.a = array}, {.p = is_copy}};
3383 if (sc.Check(soa, true, "Eap", args) && sc.CheckPrimitiveArrayType(soa, array, type)) {
3384 JniValueType result;
Mathieu Chartierb735bd92015-06-24 17:04:17 -07003385 void* ptr = nullptr;
Ian Rogers68d8b422014-07-17 11:09:10 -07003386 switch (type) {
3387 case Primitive::kPrimBoolean:
Mathieu Chartierb735bd92015-06-24 17:04:17 -07003388 ptr = baseEnv(env)->GetBooleanArrayElements(env, down_cast<jbooleanArray>(array),
3389 is_copy);
Ian Rogers68d8b422014-07-17 11:09:10 -07003390 break;
3391 case Primitive::kPrimByte:
Mathieu Chartierb735bd92015-06-24 17:04:17 -07003392 ptr = baseEnv(env)->GetByteArrayElements(env, down_cast<jbyteArray>(array), is_copy);
Ian Rogers68d8b422014-07-17 11:09:10 -07003393 break;
3394 case Primitive::kPrimChar:
Mathieu Chartierb735bd92015-06-24 17:04:17 -07003395 ptr = baseEnv(env)->GetCharArrayElements(env, down_cast<jcharArray>(array), is_copy);
Ian Rogers68d8b422014-07-17 11:09:10 -07003396 break;
3397 case Primitive::kPrimShort:
Mathieu Chartierb735bd92015-06-24 17:04:17 -07003398 ptr = baseEnv(env)->GetShortArrayElements(env, down_cast<jshortArray>(array), is_copy);
Ian Rogers68d8b422014-07-17 11:09:10 -07003399 break;
3400 case Primitive::kPrimInt:
Mathieu Chartierb735bd92015-06-24 17:04:17 -07003401 ptr = baseEnv(env)->GetIntArrayElements(env, down_cast<jintArray>(array), is_copy);
Ian Rogers68d8b422014-07-17 11:09:10 -07003402 break;
3403 case Primitive::kPrimLong:
Mathieu Chartierb735bd92015-06-24 17:04:17 -07003404 ptr = baseEnv(env)->GetLongArrayElements(env, down_cast<jlongArray>(array), is_copy);
Ian Rogers68d8b422014-07-17 11:09:10 -07003405 break;
3406 case Primitive::kPrimFloat:
Mathieu Chartierb735bd92015-06-24 17:04:17 -07003407 ptr = baseEnv(env)->GetFloatArrayElements(env, down_cast<jfloatArray>(array), is_copy);
Ian Rogers68d8b422014-07-17 11:09:10 -07003408 break;
3409 case Primitive::kPrimDouble:
Mathieu Chartierb735bd92015-06-24 17:04:17 -07003410 ptr = baseEnv(env)->GetDoubleArrayElements(env, down_cast<jdoubleArray>(array), is_copy);
Ian Rogers68d8b422014-07-17 11:09:10 -07003411 break;
3412 default:
3413 LOG(FATAL) << "Unexpected primitive type: " << type;
3414 }
Mathieu Chartierb735bd92015-06-24 17:04:17 -07003415 if (ptr != nullptr && soa.ForceCopy()) {
3416 ptr = GuardedCopy::CreateGuardedPACopy(env, array, is_copy, ptr);
Ian Rogers68d8b422014-07-17 11:09:10 -07003417 if (is_copy != nullptr) {
3418 *is_copy = JNI_TRUE;
3419 }
3420 }
Mathieu Chartierb735bd92015-06-24 17:04:17 -07003421 result.p = ptr;
Ian Rogers68d8b422014-07-17 11:09:10 -07003422 if (sc.Check(soa, false, "p", &result)) {
3423 return const_cast<void*>(result.p);
3424 }
3425 }
3426 return nullptr;
3427 }
3428
3429 static void ReleasePrimitiveArrayElements(const char* function_name, Primitive::Type type,
3430 JNIEnv* env, jarray array, void* elems, jint mode) {
3431 ScopedObjectAccess soa(env);
3432 ScopedCheck sc(kFlag_ExcepOkay, function_name);
3433 if (sc.CheckNonNull(elems) && sc.CheckPrimitiveArrayType(soa, array, type)) {
3434 if (soa.ForceCopy()) {
3435 elems = GuardedCopy::ReleaseGuardedPACopy(function_name, env, array, elems, mode);
3436 }
3437 if (!soa.ForceCopy() || elems != nullptr) {
3438 JniValueType args[4] = {{.E = env}, {.a = array}, {.p = elems}, {.r = mode}};
3439 if (sc.Check(soa, true, "Eapr", args)) {
3440 switch (type) {
3441 case Primitive::kPrimBoolean:
3442 baseEnv(env)->ReleaseBooleanArrayElements(env, down_cast<jbooleanArray>(array),
3443 reinterpret_cast<jboolean*>(elems), mode);
3444 break;
3445 case Primitive::kPrimByte:
3446 baseEnv(env)->ReleaseByteArrayElements(env, down_cast<jbyteArray>(array),
3447 reinterpret_cast<jbyte*>(elems), mode);
3448 break;
3449 case Primitive::kPrimChar:
3450 baseEnv(env)->ReleaseCharArrayElements(env, down_cast<jcharArray>(array),
3451 reinterpret_cast<jchar*>(elems), mode);
3452 break;
3453 case Primitive::kPrimShort:
3454 baseEnv(env)->ReleaseShortArrayElements(env, down_cast<jshortArray>(array),
3455 reinterpret_cast<jshort*>(elems), mode);
3456 break;
3457 case Primitive::kPrimInt:
3458 baseEnv(env)->ReleaseIntArrayElements(env, down_cast<jintArray>(array),
3459 reinterpret_cast<jint*>(elems), mode);
3460 break;
3461 case Primitive::kPrimLong:
3462 baseEnv(env)->ReleaseLongArrayElements(env, down_cast<jlongArray>(array),
3463 reinterpret_cast<jlong*>(elems), mode);
3464 break;
3465 case Primitive::kPrimFloat:
3466 baseEnv(env)->ReleaseFloatArrayElements(env, down_cast<jfloatArray>(array),
3467 reinterpret_cast<jfloat*>(elems), mode);
3468 break;
3469 case Primitive::kPrimDouble:
3470 baseEnv(env)->ReleaseDoubleArrayElements(env, down_cast<jdoubleArray>(array),
3471 reinterpret_cast<jdouble*>(elems), mode);
3472 break;
3473 default:
3474 LOG(FATAL) << "Unexpected primitive type: " << type;
3475 }
3476 JniValueType result;
3477 result.V = nullptr;
3478 sc.Check(soa, false, "V", &result);
3479 }
3480 }
3481 }
3482 }
3483
3484 static void GetPrimitiveArrayRegion(const char* function_name, Primitive::Type type, JNIEnv* env,
3485 jarray array, jsize start, jsize len, void* buf) {
3486 ScopedObjectAccess soa(env);
3487 ScopedCheck sc(kFlag_Default, function_name);
3488 JniValueType args[5] = {{.E = env}, {.a = array}, {.z = start}, {.z = len}, {.p = buf}};
3489 // Note: the start and len arguments are checked as 'I' rather than 'z' as invalid indices
3490 // result in ArrayIndexOutOfBoundsExceptions in the base implementation.
3491 if (sc.Check(soa, true, "EaIIp", args) && sc.CheckPrimitiveArrayType(soa, array, type)) {
3492 switch (type) {
3493 case Primitive::kPrimBoolean:
3494 baseEnv(env)->GetBooleanArrayRegion(env, down_cast<jbooleanArray>(array), start, len,
3495 reinterpret_cast<jboolean*>(buf));
3496 break;
3497 case Primitive::kPrimByte:
3498 baseEnv(env)->GetByteArrayRegion(env, down_cast<jbyteArray>(array), start, len,
3499 reinterpret_cast<jbyte*>(buf));
3500 break;
3501 case Primitive::kPrimChar:
3502 baseEnv(env)->GetCharArrayRegion(env, down_cast<jcharArray>(array), start, len,
3503 reinterpret_cast<jchar*>(buf));
3504 break;
3505 case Primitive::kPrimShort:
3506 baseEnv(env)->GetShortArrayRegion(env, down_cast<jshortArray>(array), start, len,
3507 reinterpret_cast<jshort*>(buf));
3508 break;
3509 case Primitive::kPrimInt:
3510 baseEnv(env)->GetIntArrayRegion(env, down_cast<jintArray>(array), start, len,
3511 reinterpret_cast<jint*>(buf));
3512 break;
3513 case Primitive::kPrimLong:
3514 baseEnv(env)->GetLongArrayRegion(env, down_cast<jlongArray>(array), start, len,
3515 reinterpret_cast<jlong*>(buf));
3516 break;
3517 case Primitive::kPrimFloat:
3518 baseEnv(env)->GetFloatArrayRegion(env, down_cast<jfloatArray>(array), start, len,
3519 reinterpret_cast<jfloat*>(buf));
3520 break;
3521 case Primitive::kPrimDouble:
3522 baseEnv(env)->GetDoubleArrayRegion(env, down_cast<jdoubleArray>(array), start, len,
3523 reinterpret_cast<jdouble*>(buf));
3524 break;
3525 default:
3526 LOG(FATAL) << "Unexpected primitive type: " << type;
3527 }
3528 JniValueType result;
3529 result.V = nullptr;
3530 sc.Check(soa, false, "V", &result);
3531 }
3532 }
3533
3534 static void SetPrimitiveArrayRegion(const char* function_name, Primitive::Type type, JNIEnv* env,
3535 jarray array, jsize start, jsize len, const void* buf) {
3536 ScopedObjectAccess soa(env);
3537 ScopedCheck sc(kFlag_Default, function_name);
3538 JniValueType args[5] = {{.E = env}, {.a = array}, {.z = start}, {.z = len}, {.p = buf}};
3539 // Note: the start and len arguments are checked as 'I' rather than 'z' as invalid indices
3540 // result in ArrayIndexOutOfBoundsExceptions in the base implementation.
3541 if (sc.Check(soa, true, "EaIIp", args) && sc.CheckPrimitiveArrayType(soa, array, type)) {
3542 switch (type) {
3543 case Primitive::kPrimBoolean:
3544 baseEnv(env)->SetBooleanArrayRegion(env, down_cast<jbooleanArray>(array), start, len,
3545 reinterpret_cast<const jboolean*>(buf));
3546 break;
3547 case Primitive::kPrimByte:
3548 baseEnv(env)->SetByteArrayRegion(env, down_cast<jbyteArray>(array), start, len,
3549 reinterpret_cast<const jbyte*>(buf));
3550 break;
3551 case Primitive::kPrimChar:
3552 baseEnv(env)->SetCharArrayRegion(env, down_cast<jcharArray>(array), start, len,
3553 reinterpret_cast<const jchar*>(buf));
3554 break;
3555 case Primitive::kPrimShort:
3556 baseEnv(env)->SetShortArrayRegion(env, down_cast<jshortArray>(array), start, len,
3557 reinterpret_cast<const jshort*>(buf));
3558 break;
3559 case Primitive::kPrimInt:
3560 baseEnv(env)->SetIntArrayRegion(env, down_cast<jintArray>(array), start, len,
3561 reinterpret_cast<const jint*>(buf));
3562 break;
3563 case Primitive::kPrimLong:
3564 baseEnv(env)->SetLongArrayRegion(env, down_cast<jlongArray>(array), start, len,
3565 reinterpret_cast<const jlong*>(buf));
3566 break;
3567 case Primitive::kPrimFloat:
3568 baseEnv(env)->SetFloatArrayRegion(env, down_cast<jfloatArray>(array), start, len,
3569 reinterpret_cast<const jfloat*>(buf));
3570 break;
3571 case Primitive::kPrimDouble:
3572 baseEnv(env)->SetDoubleArrayRegion(env, down_cast<jdoubleArray>(array), start, len,
3573 reinterpret_cast<const jdouble*>(buf));
3574 break;
3575 default:
3576 LOG(FATAL) << "Unexpected primitive type: " << type;
3577 }
3578 JniValueType result;
3579 result.V = nullptr;
3580 sc.Check(soa, false, "V", &result);
3581 }
3582 }
Elliott Hughesa2501992011-08-26 19:39:54 -07003583};
3584
3585const JNINativeInterface gCheckNativeInterface = {
Ian Rogersef7d42f2014-01-06 12:55:46 -08003586 nullptr, // reserved0.
3587 nullptr, // reserved1.
3588 nullptr, // reserved2.
3589 nullptr, // reserved3.
Elliott Hughesa2501992011-08-26 19:39:54 -07003590 CheckJNI::GetVersion,
3591 CheckJNI::DefineClass,
3592 CheckJNI::FindClass,
3593 CheckJNI::FromReflectedMethod,
3594 CheckJNI::FromReflectedField,
3595 CheckJNI::ToReflectedMethod,
3596 CheckJNI::GetSuperclass,
3597 CheckJNI::IsAssignableFrom,
3598 CheckJNI::ToReflectedField,
3599 CheckJNI::Throw,
3600 CheckJNI::ThrowNew,
3601 CheckJNI::ExceptionOccurred,
3602 CheckJNI::ExceptionDescribe,
3603 CheckJNI::ExceptionClear,
3604 CheckJNI::FatalError,
3605 CheckJNI::PushLocalFrame,
3606 CheckJNI::PopLocalFrame,
3607 CheckJNI::NewGlobalRef,
3608 CheckJNI::DeleteGlobalRef,
3609 CheckJNI::DeleteLocalRef,
3610 CheckJNI::IsSameObject,
3611 CheckJNI::NewLocalRef,
3612 CheckJNI::EnsureLocalCapacity,
3613 CheckJNI::AllocObject,
3614 CheckJNI::NewObject,
3615 CheckJNI::NewObjectV,
3616 CheckJNI::NewObjectA,
3617 CheckJNI::GetObjectClass,
3618 CheckJNI::IsInstanceOf,
3619 CheckJNI::GetMethodID,
3620 CheckJNI::CallObjectMethod,
3621 CheckJNI::CallObjectMethodV,
3622 CheckJNI::CallObjectMethodA,
3623 CheckJNI::CallBooleanMethod,
3624 CheckJNI::CallBooleanMethodV,
3625 CheckJNI::CallBooleanMethodA,
3626 CheckJNI::CallByteMethod,
3627 CheckJNI::CallByteMethodV,
3628 CheckJNI::CallByteMethodA,
3629 CheckJNI::CallCharMethod,
3630 CheckJNI::CallCharMethodV,
3631 CheckJNI::CallCharMethodA,
3632 CheckJNI::CallShortMethod,
3633 CheckJNI::CallShortMethodV,
3634 CheckJNI::CallShortMethodA,
3635 CheckJNI::CallIntMethod,
3636 CheckJNI::CallIntMethodV,
3637 CheckJNI::CallIntMethodA,
3638 CheckJNI::CallLongMethod,
3639 CheckJNI::CallLongMethodV,
3640 CheckJNI::CallLongMethodA,
3641 CheckJNI::CallFloatMethod,
3642 CheckJNI::CallFloatMethodV,
3643 CheckJNI::CallFloatMethodA,
3644 CheckJNI::CallDoubleMethod,
3645 CheckJNI::CallDoubleMethodV,
3646 CheckJNI::CallDoubleMethodA,
3647 CheckJNI::CallVoidMethod,
3648 CheckJNI::CallVoidMethodV,
3649 CheckJNI::CallVoidMethodA,
3650 CheckJNI::CallNonvirtualObjectMethod,
3651 CheckJNI::CallNonvirtualObjectMethodV,
3652 CheckJNI::CallNonvirtualObjectMethodA,
3653 CheckJNI::CallNonvirtualBooleanMethod,
3654 CheckJNI::CallNonvirtualBooleanMethodV,
3655 CheckJNI::CallNonvirtualBooleanMethodA,
3656 CheckJNI::CallNonvirtualByteMethod,
3657 CheckJNI::CallNonvirtualByteMethodV,
3658 CheckJNI::CallNonvirtualByteMethodA,
3659 CheckJNI::CallNonvirtualCharMethod,
3660 CheckJNI::CallNonvirtualCharMethodV,
3661 CheckJNI::CallNonvirtualCharMethodA,
3662 CheckJNI::CallNonvirtualShortMethod,
3663 CheckJNI::CallNonvirtualShortMethodV,
3664 CheckJNI::CallNonvirtualShortMethodA,
3665 CheckJNI::CallNonvirtualIntMethod,
3666 CheckJNI::CallNonvirtualIntMethodV,
3667 CheckJNI::CallNonvirtualIntMethodA,
3668 CheckJNI::CallNonvirtualLongMethod,
3669 CheckJNI::CallNonvirtualLongMethodV,
3670 CheckJNI::CallNonvirtualLongMethodA,
3671 CheckJNI::CallNonvirtualFloatMethod,
3672 CheckJNI::CallNonvirtualFloatMethodV,
3673 CheckJNI::CallNonvirtualFloatMethodA,
3674 CheckJNI::CallNonvirtualDoubleMethod,
3675 CheckJNI::CallNonvirtualDoubleMethodV,
3676 CheckJNI::CallNonvirtualDoubleMethodA,
3677 CheckJNI::CallNonvirtualVoidMethod,
3678 CheckJNI::CallNonvirtualVoidMethodV,
3679 CheckJNI::CallNonvirtualVoidMethodA,
3680 CheckJNI::GetFieldID,
3681 CheckJNI::GetObjectField,
3682 CheckJNI::GetBooleanField,
3683 CheckJNI::GetByteField,
3684 CheckJNI::GetCharField,
3685 CheckJNI::GetShortField,
3686 CheckJNI::GetIntField,
3687 CheckJNI::GetLongField,
3688 CheckJNI::GetFloatField,
3689 CheckJNI::GetDoubleField,
3690 CheckJNI::SetObjectField,
3691 CheckJNI::SetBooleanField,
3692 CheckJNI::SetByteField,
3693 CheckJNI::SetCharField,
3694 CheckJNI::SetShortField,
3695 CheckJNI::SetIntField,
3696 CheckJNI::SetLongField,
3697 CheckJNI::SetFloatField,
3698 CheckJNI::SetDoubleField,
3699 CheckJNI::GetStaticMethodID,
3700 CheckJNI::CallStaticObjectMethod,
3701 CheckJNI::CallStaticObjectMethodV,
3702 CheckJNI::CallStaticObjectMethodA,
3703 CheckJNI::CallStaticBooleanMethod,
3704 CheckJNI::CallStaticBooleanMethodV,
3705 CheckJNI::CallStaticBooleanMethodA,
3706 CheckJNI::CallStaticByteMethod,
3707 CheckJNI::CallStaticByteMethodV,
3708 CheckJNI::CallStaticByteMethodA,
3709 CheckJNI::CallStaticCharMethod,
3710 CheckJNI::CallStaticCharMethodV,
3711 CheckJNI::CallStaticCharMethodA,
3712 CheckJNI::CallStaticShortMethod,
3713 CheckJNI::CallStaticShortMethodV,
3714 CheckJNI::CallStaticShortMethodA,
3715 CheckJNI::CallStaticIntMethod,
3716 CheckJNI::CallStaticIntMethodV,
3717 CheckJNI::CallStaticIntMethodA,
3718 CheckJNI::CallStaticLongMethod,
3719 CheckJNI::CallStaticLongMethodV,
3720 CheckJNI::CallStaticLongMethodA,
3721 CheckJNI::CallStaticFloatMethod,
3722 CheckJNI::CallStaticFloatMethodV,
3723 CheckJNI::CallStaticFloatMethodA,
3724 CheckJNI::CallStaticDoubleMethod,
3725 CheckJNI::CallStaticDoubleMethodV,
3726 CheckJNI::CallStaticDoubleMethodA,
3727 CheckJNI::CallStaticVoidMethod,
3728 CheckJNI::CallStaticVoidMethodV,
3729 CheckJNI::CallStaticVoidMethodA,
3730 CheckJNI::GetStaticFieldID,
3731 CheckJNI::GetStaticObjectField,
3732 CheckJNI::GetStaticBooleanField,
3733 CheckJNI::GetStaticByteField,
3734 CheckJNI::GetStaticCharField,
3735 CheckJNI::GetStaticShortField,
3736 CheckJNI::GetStaticIntField,
3737 CheckJNI::GetStaticLongField,
3738 CheckJNI::GetStaticFloatField,
3739 CheckJNI::GetStaticDoubleField,
3740 CheckJNI::SetStaticObjectField,
3741 CheckJNI::SetStaticBooleanField,
3742 CheckJNI::SetStaticByteField,
3743 CheckJNI::SetStaticCharField,
3744 CheckJNI::SetStaticShortField,
3745 CheckJNI::SetStaticIntField,
3746 CheckJNI::SetStaticLongField,
3747 CheckJNI::SetStaticFloatField,
3748 CheckJNI::SetStaticDoubleField,
3749 CheckJNI::NewString,
3750 CheckJNI::GetStringLength,
3751 CheckJNI::GetStringChars,
3752 CheckJNI::ReleaseStringChars,
3753 CheckJNI::NewStringUTF,
3754 CheckJNI::GetStringUTFLength,
3755 CheckJNI::GetStringUTFChars,
3756 CheckJNI::ReleaseStringUTFChars,
3757 CheckJNI::GetArrayLength,
3758 CheckJNI::NewObjectArray,
3759 CheckJNI::GetObjectArrayElement,
3760 CheckJNI::SetObjectArrayElement,
3761 CheckJNI::NewBooleanArray,
3762 CheckJNI::NewByteArray,
3763 CheckJNI::NewCharArray,
3764 CheckJNI::NewShortArray,
3765 CheckJNI::NewIntArray,
3766 CheckJNI::NewLongArray,
3767 CheckJNI::NewFloatArray,
3768 CheckJNI::NewDoubleArray,
3769 CheckJNI::GetBooleanArrayElements,
3770 CheckJNI::GetByteArrayElements,
3771 CheckJNI::GetCharArrayElements,
3772 CheckJNI::GetShortArrayElements,
3773 CheckJNI::GetIntArrayElements,
3774 CheckJNI::GetLongArrayElements,
3775 CheckJNI::GetFloatArrayElements,
3776 CheckJNI::GetDoubleArrayElements,
3777 CheckJNI::ReleaseBooleanArrayElements,
3778 CheckJNI::ReleaseByteArrayElements,
3779 CheckJNI::ReleaseCharArrayElements,
3780 CheckJNI::ReleaseShortArrayElements,
3781 CheckJNI::ReleaseIntArrayElements,
3782 CheckJNI::ReleaseLongArrayElements,
3783 CheckJNI::ReleaseFloatArrayElements,
3784 CheckJNI::ReleaseDoubleArrayElements,
3785 CheckJNI::GetBooleanArrayRegion,
3786 CheckJNI::GetByteArrayRegion,
3787 CheckJNI::GetCharArrayRegion,
3788 CheckJNI::GetShortArrayRegion,
3789 CheckJNI::GetIntArrayRegion,
3790 CheckJNI::GetLongArrayRegion,
3791 CheckJNI::GetFloatArrayRegion,
3792 CheckJNI::GetDoubleArrayRegion,
3793 CheckJNI::SetBooleanArrayRegion,
3794 CheckJNI::SetByteArrayRegion,
3795 CheckJNI::SetCharArrayRegion,
3796 CheckJNI::SetShortArrayRegion,
3797 CheckJNI::SetIntArrayRegion,
3798 CheckJNI::SetLongArrayRegion,
3799 CheckJNI::SetFloatArrayRegion,
3800 CheckJNI::SetDoubleArrayRegion,
3801 CheckJNI::RegisterNatives,
3802 CheckJNI::UnregisterNatives,
3803 CheckJNI::MonitorEnter,
3804 CheckJNI::MonitorExit,
3805 CheckJNI::GetJavaVM,
3806 CheckJNI::GetStringRegion,
3807 CheckJNI::GetStringUTFRegion,
3808 CheckJNI::GetPrimitiveArrayCritical,
3809 CheckJNI::ReleasePrimitiveArrayCritical,
3810 CheckJNI::GetStringCritical,
3811 CheckJNI::ReleaseStringCritical,
3812 CheckJNI::NewWeakGlobalRef,
3813 CheckJNI::DeleteWeakGlobalRef,
3814 CheckJNI::ExceptionCheck,
3815 CheckJNI::NewDirectByteBuffer,
3816 CheckJNI::GetDirectBufferAddress,
3817 CheckJNI::GetDirectBufferCapacity,
3818 CheckJNI::GetObjectRefType,
3819};
3820
3821const JNINativeInterface* GetCheckJniNativeInterface() {
3822 return &gCheckNativeInterface;
3823}
3824
3825class CheckJII {
Elliott Hughesba8eee12012-01-24 20:25:24 -08003826 public:
Elliott Hughesa2501992011-08-26 19:39:54 -07003827 static jint DestroyJavaVM(JavaVM* vm) {
Ian Rogers68d8b422014-07-17 11:09:10 -07003828 ScopedCheck sc(kFlag_Invocation, __FUNCTION__, false);
3829 JniValueType args[1] = {{.v = vm}};
3830 sc.CheckNonHeap(reinterpret_cast<JavaVMExt*>(vm), true, "v", args);
3831 JniValueType result;
3832 result.i = BaseVm(vm)->DestroyJavaVM(vm);
Andreas Gampedef194e2015-02-19 15:19:50 -08003833 // Use null to signal that the JavaVM isn't valid anymore. DestroyJavaVM deletes the runtime,
3834 // which will delete the JavaVMExt.
3835 sc.CheckNonHeap(nullptr, false, "i", &result);
Ian Rogers68d8b422014-07-17 11:09:10 -07003836 return result.i;
Elliott Hughesa2501992011-08-26 19:39:54 -07003837 }
3838
3839 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Ian Rogers68d8b422014-07-17 11:09:10 -07003840 ScopedCheck sc(kFlag_Invocation, __FUNCTION__);
3841 JniValueType args[3] = {{.v = vm}, {.p = p_env}, {.p = thr_args}};
3842 sc.CheckNonHeap(reinterpret_cast<JavaVMExt*>(vm), true, "vpp", args);
3843 JniValueType result;
3844 result.i = BaseVm(vm)->AttachCurrentThread(vm, p_env, thr_args);
3845 sc.CheckNonHeap(reinterpret_cast<JavaVMExt*>(vm), false, "i", &result);
3846 return result.i;
Elliott Hughesa2501992011-08-26 19:39:54 -07003847 }
3848
3849 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Ian Rogers68d8b422014-07-17 11:09:10 -07003850 ScopedCheck sc(kFlag_Invocation, __FUNCTION__);
3851 JniValueType args[3] = {{.v = vm}, {.p = p_env}, {.p = thr_args}};
3852 sc.CheckNonHeap(reinterpret_cast<JavaVMExt*>(vm), true, "vpp", args);
3853 JniValueType result;
3854 result.i = BaseVm(vm)->AttachCurrentThreadAsDaemon(vm, p_env, thr_args);
3855 sc.CheckNonHeap(reinterpret_cast<JavaVMExt*>(vm), false, "i", &result);
3856 return result.i;
Elliott Hughesa2501992011-08-26 19:39:54 -07003857 }
3858
3859 static jint DetachCurrentThread(JavaVM* vm) {
Ian Rogers68d8b422014-07-17 11:09:10 -07003860 ScopedCheck sc(kFlag_Invocation, __FUNCTION__);
3861 JniValueType args[1] = {{.v = vm}};
3862 sc.CheckNonHeap(reinterpret_cast<JavaVMExt*>(vm), true, "v", args);
3863 JniValueType result;
3864 result.i = BaseVm(vm)->DetachCurrentThread(vm);
3865 sc.CheckNonHeap(reinterpret_cast<JavaVMExt*>(vm), false, "i", &result);
3866 return result.i;
Elliott Hughesa2501992011-08-26 19:39:54 -07003867 }
3868
Ian Rogers68d8b422014-07-17 11:09:10 -07003869 static jint GetEnv(JavaVM* vm, void** p_env, jint version) {
3870 ScopedCheck sc(kFlag_Invocation, __FUNCTION__);
3871 JniValueType args[3] = {{.v = vm}, {.p = p_env}, {.I = version}};
3872 sc.CheckNonHeap(reinterpret_cast<JavaVMExt*>(vm), true, "vpI", args);
3873 JniValueType result;
3874 result.i = BaseVm(vm)->GetEnv(vm, p_env, version);
3875 sc.CheckNonHeap(reinterpret_cast<JavaVMExt*>(vm), false, "i", &result);
3876 return result.i;
Elliott Hughesa2501992011-08-26 19:39:54 -07003877 }
3878
3879 private:
Ian Rogers68d8b422014-07-17 11:09:10 -07003880 static const JNIInvokeInterface* BaseVm(JavaVM* vm) {
3881 return reinterpret_cast<JavaVMExt*>(vm)->GetUncheckedFunctions();
Elliott Hughesa2501992011-08-26 19:39:54 -07003882 }
3883};
3884
3885const JNIInvokeInterface gCheckInvokeInterface = {
Ian Rogersef7d42f2014-01-06 12:55:46 -08003886 nullptr, // reserved0
3887 nullptr, // reserved1
3888 nullptr, // reserved2
Elliott Hughesa2501992011-08-26 19:39:54 -07003889 CheckJII::DestroyJavaVM,
3890 CheckJII::AttachCurrentThread,
3891 CheckJII::DetachCurrentThread,
3892 CheckJII::GetEnv,
3893 CheckJII::AttachCurrentThreadAsDaemon
3894};
3895
3896const JNIInvokeInterface* GetCheckJniInvokeInterface() {
3897 return &gCheckInvokeInterface;
3898}
3899
3900} // namespace art