• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "jni_internal.h"
18 
19 #include <cstdarg>
20 #include <log/log.h>
21 #include <memory>
22 #include <utility>
23 
24 #include "art_field-inl.h"
25 #include "art_method-inl.h"
26 #include "base/allocator.h"
27 #include "base/atomic.h"
28 #include "base/casts.h"
29 #include "base/enums.h"
30 #include "base/file_utils.h"
31 #include "base/logging.h"  // For VLOG.
32 #include "base/mutex.h"
33 #include "base/safe_map.h"
34 #include "base/stl_util.h"
35 #include "class_linker-inl.h"
36 #include "class_root.h"
37 #include "dex/dex_file-inl.h"
38 #include "dex/utf-inl.h"
39 #include "fault_handler.h"
40 #include "hidden_api.h"
41 #include "gc/accounting/card_table-inl.h"
42 #include "gc_root.h"
43 #include "indirect_reference_table-inl.h"
44 #include "interpreter/interpreter.h"
45 #include "java_vm_ext.h"
46 #include "jni_env_ext.h"
47 #include "jvalue-inl.h"
48 #include "mirror/class-alloc-inl.h"
49 #include "mirror/class-inl.h"
50 #include "mirror/class_loader.h"
51 #include "mirror/dex_cache-inl.h"
52 #include "mirror/field-inl.h"
53 #include "mirror/method.h"
54 #include "mirror/object-inl.h"
55 #include "mirror/object_array-alloc-inl.h"
56 #include "mirror/object_array-inl.h"
57 #include "mirror/string-alloc-inl.h"
58 #include "mirror/string-inl.h"
59 #include "mirror/throwable.h"
60 #include "nativehelper/scoped_local_ref.h"
61 #include "parsed_options.h"
62 #include "reflection.h"
63 #include "runtime.h"
64 #include "scoped_thread_state_change-inl.h"
65 #include "thread.h"
66 #include "well_known_classes.h"
67 
68 namespace art {
69 
70 namespace {
71 
72 // Frees the given va_list upon destruction.
73 // This also guards the returns from inside of the CHECK_NON_NULL_ARGUMENTs.
74 struct ScopedVAArgs {
ScopedVAArgsart::__anon4d235f140111::ScopedVAArgs75   explicit ScopedVAArgs(va_list* args): args(args) {}
76   ScopedVAArgs(const ScopedVAArgs&) = delete;
77   ScopedVAArgs(ScopedVAArgs&&) = delete;
~ScopedVAArgsart::__anon4d235f140111::ScopedVAArgs78   ~ScopedVAArgs() { va_end(*args); }
79 
80  private:
81   va_list* args;
82 };
83 
84 constexpr char kBadUtf8ReplacementChar[] = "?";
85 
86 // This is a modified version of CountModifiedUtf8Chars() from utf.cc
87 // with extra checks and different output options.
88 //
89 // The `good` functor can process valid characters.
90 // The `bad` functor is called when we find an invalid character and
91 // returns true to abort processing, or false to continue processing.
92 //
93 // When aborted, VisitModifiedUtf8Chars() returns 0, otherwise the
94 // number of UTF-16 chars.
95 template <typename GoodFunc, typename BadFunc>
VisitModifiedUtf8Chars(const char * utf8,size_t byte_count,GoodFunc good,BadFunc bad)96 size_t VisitModifiedUtf8Chars(const char* utf8, size_t byte_count, GoodFunc good, BadFunc bad) {
97   DCHECK_LE(byte_count, strlen(utf8));
98   size_t len = 0;
99   const char* end = utf8 + byte_count;
100   while (utf8 != end) {
101     int ic = *utf8;
102     if (LIKELY((ic & 0x80) == 0)) {
103       // One-byte encoding.
104       good(utf8, 1u);
105       utf8 += 1u;
106       len += 1u;
107       continue;
108     }
109     auto is_ascii = [utf8]() {
110       const char* ptr = utf8;  // Make a copy that can be modified by GetUtf16FromUtf8().
111       return mirror::String::IsASCII(dchecked_integral_cast<uint16_t>(GetUtf16FromUtf8(&ptr)));
112     };
113     // Note: Neither CountModifiedUtf8Chars() nor GetUtf16FromUtf8() checks whether
114     // the bit 0x40 is correctly set in the leading byte of a multi-byte sequence.
115     if ((ic & 0x20) == 0) {
116       // Two-byte encoding.
117       if (static_cast<size_t>(end - utf8) < 2u) {
118         return bad() ? 0u : len + 1u;  // Reached end of sequence
119       }
120       if (mirror::kUseStringCompression && is_ascii()) {
121         if (bad()) {
122           return 0u;
123         }
124       } else {
125         good(utf8, 2u);
126       }
127       utf8 += 2u;
128       len += 1u;
129       continue;
130     }
131     if ((ic & 0x10) == 0) {
132       // Three-byte encoding.
133       if (static_cast<size_t>(end - utf8) < 3u) {
134         return bad() ? 0u : len + 1u;  // Reached end of sequence
135       }
136       if (mirror::kUseStringCompression && is_ascii()) {
137         if (bad()) {
138           return 0u;
139         }
140       } else {
141         good(utf8, 3u);
142       }
143       utf8 += 3u;
144       len += 1u;
145       continue;
146     }
147 
148     // Four-byte encoding: needs to be converted into a surrogate pair.
149     // The decoded chars are never ASCII.
150     if (static_cast<size_t>(end - utf8) < 4u) {
151       return bad() ? 0u : len + 1u;  // Reached end of sequence
152     }
153     good(utf8, 4u);
154     utf8 += 4u;
155     len += 2u;
156   }
157   return len;
158 }
159 
160 }  // namespace
161 
162 // Consider turning this on when there is errors which could be related to JNI array copies such as
163 // things not rendering correctly. E.g. b/16858794
164 static constexpr bool kWarnJniAbort = false;
165 
166 template<typename T>
ShouldDenyAccessToMember(T * member,Thread * self)167 ALWAYS_INLINE static bool ShouldDenyAccessToMember(T* member, Thread* self)
168     REQUIRES_SHARED(Locks::mutator_lock_) {
169   return hiddenapi::ShouldDenyAccessToMember(
170       member,
171       [&]() REQUIRES_SHARED(Locks::mutator_lock_) {
172         // Construct AccessContext from the first calling class on stack.
173         // If the calling class cannot be determined, e.g. unattached threads,
174         // we conservatively assume the caller is trusted.
175         ObjPtr<mirror::Class> caller = GetCallingClass(self, /* num_frames */ 1);
176         return caller.IsNull() ? hiddenapi::AccessContext(/* is_trusted= */ true)
177                                : hiddenapi::AccessContext(caller);
178       },
179       hiddenapi::AccessMethod::kJNI);
180 }
181 
182 // Helpers to call instrumentation functions for fields. These take jobjects so we don't need to set
183 // up handles for the rare case where these actually do something. Once these functions return it is
184 // possible there will be a pending exception if the instrumentation happens to throw one.
NotifySetObjectField(ArtField * field,jobject obj,jobject jval)185 static void NotifySetObjectField(ArtField* field, jobject obj, jobject jval)
186     REQUIRES_SHARED(Locks::mutator_lock_) {
187   DCHECK_EQ(field->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
188   instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
189   if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
190     Thread* self = Thread::Current();
191     ArtMethod* cur_method = self->GetCurrentMethod(/*dex_pc=*/ nullptr,
192                                                    /*check_suspended=*/ true,
193                                                    /*abort_on_error=*/ false);
194 
195     if (cur_method == nullptr) {
196       // Set/Get Fields can be issued without a method during runtime startup/teardown. Ignore all
197       // of these changes.
198       return;
199     }
200     DCHECK(cur_method->IsNative());
201     JValue val;
202     val.SetL(self->DecodeJObject(jval));
203     instrumentation->FieldWriteEvent(self,
204                                      self->DecodeJObject(obj),
205                                      cur_method,
206                                      0,  // dex_pc is always 0 since this is a native method.
207                                      field,
208                                      val);
209   }
210 }
211 
NotifySetPrimitiveField(ArtField * field,jobject obj,JValue val)212 static void NotifySetPrimitiveField(ArtField* field, jobject obj, JValue val)
213     REQUIRES_SHARED(Locks::mutator_lock_) {
214   DCHECK_NE(field->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
215   instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
216   if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
217     Thread* self = Thread::Current();
218     ArtMethod* cur_method = self->GetCurrentMethod(/*dex_pc=*/ nullptr,
219                                                    /*check_suspended=*/ true,
220                                                    /*abort_on_error=*/ false);
221 
222     if (cur_method == nullptr) {
223       // Set/Get Fields can be issued without a method during runtime startup/teardown. Ignore all
224       // of these changes.
225       return;
226     }
227     DCHECK(cur_method->IsNative());
228     instrumentation->FieldWriteEvent(self,
229                                      self->DecodeJObject(obj),
230                                      cur_method,
231                                      0,  // dex_pc is always 0 since this is a native method.
232                                      field,
233                                      val);
234   }
235 }
236 
NotifyGetField(ArtField * field,jobject obj)237 static void NotifyGetField(ArtField* field, jobject obj)
238     REQUIRES_SHARED(Locks::mutator_lock_) {
239   instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
240   if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
241     Thread* self = Thread::Current();
242     ArtMethod* cur_method = self->GetCurrentMethod(/*dex_pc=*/ nullptr,
243                                                    /*check_suspended=*/ true,
244                                                    /*abort_on_error=*/ false);
245 
246     if (cur_method == nullptr) {
247       // Set/Get Fields can be issued without a method during runtime startup/teardown. Ignore all
248       // of these changes.
249       return;
250     }
251     DCHECK(cur_method->IsNative());
252     instrumentation->FieldReadEvent(self,
253                                     self->DecodeJObject(obj),
254                                     cur_method,
255                                     0,  // dex_pc is always 0 since this is a native method.
256                                     field);
257   }
258 }
259 
260 // Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
261 // separated with slashes but aren't wrapped with "L;" like regular descriptors
262 // (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
263 // exception; there the "L;" must be present ("[La/b/C;"). Historically we've
264 // supported names with dots too (such as "a.b.C").
NormalizeJniClassDescriptor(const char * name)265 static std::string NormalizeJniClassDescriptor(const char* name) {
266   std::string result;
267   // Add the missing "L;" if necessary.
268   if (name[0] == '[') {
269     result = name;
270   } else {
271     result += 'L';
272     result += name;
273     result += ';';
274   }
275   // Rewrite '.' as '/' for backwards compatibility.
276   if (result.find('.') != std::string::npos) {
277     LOG(WARNING) << "Call to JNI FindClass with dots in name: "
278                  << "\"" << name << "\"";
279     std::replace(result.begin(), result.end(), '.', '/');
280   }
281   return result;
282 }
283 
ReportInvalidJNINativeMethod(const ScopedObjectAccess & soa,ObjPtr<mirror::Class> c,const char * kind,jint idx)284 static void ReportInvalidJNINativeMethod(const ScopedObjectAccess& soa,
285                                          ObjPtr<mirror::Class> c,
286                                          const char* kind,
287                                          jint idx)
288     REQUIRES_SHARED(Locks::mutator_lock_) {
289   LOG(ERROR)
290       << "Failed to register native method in " << c->PrettyDescriptor()
291       << " in " << c->GetDexCache()->GetLocation()->ToModifiedUtf8()
292       << ": " << kind << " is null at index " << idx;
293   soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
294                                  "%s is null at index %d",
295                                  kind,
296                                  idx);
297 }
298 
299 template<bool kEnableIndexIds>
FindMethodID(ScopedObjectAccess & soa,jclass jni_class,const char * name,const char * sig,bool is_static)300 static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
301                               const char* name, const char* sig, bool is_static)
302     REQUIRES_SHARED(Locks::mutator_lock_) {
303   return jni::EncodeArtMethod<kEnableIndexIds>(FindMethodJNI(soa, jni_class, name, sig, is_static));
304 }
305 
306 template<bool kEnableIndexIds>
GetClassLoader(const ScopedObjectAccess & soa)307 static ObjPtr<mirror::ClassLoader> GetClassLoader(const ScopedObjectAccess& soa)
308     REQUIRES_SHARED(Locks::mutator_lock_) {
309   ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
310   // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
311   if (method ==
312       jni::DecodeArtMethod<kEnableIndexIds>(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
313     return soa.Decode<mirror::ClassLoader>(soa.Self()->GetClassLoaderOverride());
314   }
315   // If we have a method, use its ClassLoader for context.
316   if (method != nullptr) {
317     return method->GetDeclaringClass()->GetClassLoader();
318   }
319   // We don't have a method, so try to use the system ClassLoader.
320   ObjPtr<mirror::ClassLoader> class_loader =
321       soa.Decode<mirror::ClassLoader>(Runtime::Current()->GetSystemClassLoader());
322   if (class_loader != nullptr) {
323     return class_loader;
324   }
325   // See if the override ClassLoader is set for gtests.
326   class_loader = soa.Decode<mirror::ClassLoader>(soa.Self()->GetClassLoaderOverride());
327   if (class_loader != nullptr) {
328     // If so, CommonCompilerTest should have marked the runtime as a compiler not compiling an
329     // image.
330     CHECK(Runtime::Current()->IsAotCompiler());
331     CHECK(!Runtime::Current()->IsCompilingBootImage());
332     return class_loader;
333   }
334   // Use the BOOTCLASSPATH.
335   return nullptr;
336 }
337 
338 template<bool kEnableIndexIds>
FindFieldID(const ScopedObjectAccess & soa,jclass jni_class,const char * name,const char * sig,bool is_static)339 static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
340                             const char* sig, bool is_static)
341     REQUIRES_SHARED(Locks::mutator_lock_) {
342   return jni::EncodeArtField<kEnableIndexIds>(FindFieldJNI(soa, jni_class, name, sig, is_static));
343 }
344 
ThrowAIOOBE(ScopedObjectAccess & soa,ObjPtr<mirror::Array> array,jsize start,jsize length,const char * identifier)345 static void ThrowAIOOBE(ScopedObjectAccess& soa,
346                         ObjPtr<mirror::Array> array,
347                         jsize start,
348                         jsize length,
349                         const char* identifier)
350     REQUIRES_SHARED(Locks::mutator_lock_) {
351   std::string type(array->PrettyTypeOf());
352   soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
353                                  "%s offset=%d length=%d %s.length=%d",
354                                  type.c_str(), start, length, identifier, array->GetLength());
355 }
356 
ThrowSIOOBE(ScopedObjectAccess & soa,jsize start,jsize length,jsize array_length)357 static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
358                         jsize array_length)
359     REQUIRES_SHARED(Locks::mutator_lock_) {
360   soa.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
361                                  "offset=%d length=%d string.length()=%d", start, length,
362                                  array_length);
363 }
364 
ThrowNoSuchMethodError(const ScopedObjectAccess & soa,ObjPtr<mirror::Class> c,const char * name,const char * sig,const char * kind)365 static void ThrowNoSuchMethodError(const ScopedObjectAccess& soa,
366                                    ObjPtr<mirror::Class> c,
367                                    const char* name,
368                                    const char* sig,
369                                    const char* kind)
370     REQUIRES_SHARED(Locks::mutator_lock_) {
371   std::string temp;
372   soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
373                                  "no %s method \"%s.%s%s\"",
374                                  kind,
375                                  c->GetDescriptor(&temp),
376                                  name,
377                                  sig);
378 }
379 
EnsureInitialized(Thread * self,ObjPtr<mirror::Class> klass)380 static ObjPtr<mirror::Class> EnsureInitialized(Thread* self, ObjPtr<mirror::Class> klass)
381     REQUIRES_SHARED(Locks::mutator_lock_) {
382   if (LIKELY(klass->IsInitialized())) {
383     return klass;
384   }
385   StackHandleScope<1> hs(self);
386   Handle<mirror::Class> h_klass(hs.NewHandle(klass));
387   if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
388     return nullptr;
389   }
390   return h_klass.Get();
391 }
392 
FindMethodJNI(const ScopedObjectAccess & soa,jclass jni_class,const char * name,const char * sig,bool is_static)393 ArtMethod* FindMethodJNI(const ScopedObjectAccess& soa,
394                          jclass jni_class,
395                          const char* name,
396                          const char* sig,
397                          bool is_static) {
398   ObjPtr<mirror::Class> c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class>(jni_class));
399   if (c == nullptr) {
400     return nullptr;
401   }
402   ArtMethod* method = nullptr;
403   auto pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
404   if (c->IsInterface()) {
405     method = c->FindInterfaceMethod(name, sig, pointer_size);
406   } else {
407     method = c->FindClassMethod(name, sig, pointer_size);
408   }
409   if (method != nullptr && ShouldDenyAccessToMember(method, soa.Self())) {
410     method = nullptr;
411   }
412   if (method == nullptr || method->IsStatic() != is_static) {
413     ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
414     return nullptr;
415   }
416   return method;
417 }
418 
FindFieldJNI(const ScopedObjectAccess & soa,jclass jni_class,const char * name,const char * sig,bool is_static)419 ArtField* FindFieldJNI(const ScopedObjectAccess& soa,
420                        jclass jni_class,
421                        const char* name,
422                        const char* sig,
423                        bool is_static) {
424   StackHandleScope<2> hs(soa.Self());
425   Handle<mirror::Class> c(
426       hs.NewHandle(EnsureInitialized(soa.Self(), soa.Decode<mirror::Class>(jni_class))));
427   if (c == nullptr) {
428     return nullptr;
429   }
430   ArtField* field = nullptr;
431   ObjPtr<mirror::Class> field_type;
432   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
433   if (UNLIKELY(sig[0] == '\0')) {
434     DCHECK(field == nullptr);
435   } else if (sig[1] != '\0') {
436     Handle<mirror::ClassLoader> class_loader(hs.NewHandle(c->GetClassLoader()));
437     field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
438   } else {
439     field_type = class_linker->FindPrimitiveClass(*sig);
440   }
441   if (field_type == nullptr) {
442     // Failed to find type from the signature of the field.
443     DCHECK(sig[0] == '\0' || soa.Self()->IsExceptionPending());
444     StackHandleScope<1> hs2(soa.Self());
445     Handle<mirror::Throwable> cause(hs2.NewHandle(soa.Self()->GetException()));
446     soa.Self()->ClearException();
447     std::string temp;
448     soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
449                                    "no type \"%s\" found and so no field \"%s\" "
450                                    "could be found in class \"%s\" or its superclasses", sig, name,
451                                    c->GetDescriptor(&temp));
452     if (cause != nullptr) {
453       soa.Self()->GetException()->SetCause(cause.Get());
454     }
455     return nullptr;
456   }
457   std::string temp;
458   if (is_static) {
459     field = mirror::Class::FindStaticField(
460         soa.Self(), c.Get(), name, field_type->GetDescriptor(&temp));
461   } else {
462     field = c->FindInstanceField(name, field_type->GetDescriptor(&temp));
463   }
464   if (field != nullptr && ShouldDenyAccessToMember(field, soa.Self())) {
465     field = nullptr;
466   }
467   if (field == nullptr) {
468     soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
469                                    "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
470                                    sig, name, c->GetDescriptor(&temp));
471     return nullptr;
472   }
473   return field;
474 }
475 
ThrowNewException(JNIEnv * env,jclass exception_class,const char * msg,jobject cause)476 int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
477     REQUIRES(!Locks::mutator_lock_) {
478   // Turn the const char* into a java.lang.String.
479   ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
480   if (msg != nullptr && s.get() == nullptr) {
481     return JNI_ERR;
482   }
483 
484   // Choose an appropriate constructor and set up the arguments.
485   jvalue args[2];
486   const char* signature;
487   if (msg == nullptr && cause == nullptr) {
488     signature = "()V";
489   } else if (msg != nullptr && cause == nullptr) {
490     signature = "(Ljava/lang/String;)V";
491     args[0].l = s.get();
492   } else if (msg == nullptr && cause != nullptr) {
493     signature = "(Ljava/lang/Throwable;)V";
494     args[0].l = cause;
495   } else {
496     signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
497     args[0].l = s.get();
498     args[1].l = cause;
499   }
500   jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
501   if (mid == nullptr) {
502     ScopedObjectAccess soa(env);
503     LOG(ERROR) << "No <init>" << signature << " in "
504         << mirror::Class::PrettyClass(soa.Decode<mirror::Class>(exception_class));
505     return JNI_ERR;
506   }
507 
508   ScopedLocalRef<jthrowable> exception(
509       env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
510   if (exception.get() == nullptr) {
511     return JNI_ERR;
512   }
513   ScopedObjectAccess soa(env);
514   soa.Self()->SetException(soa.Decode<mirror::Throwable>(exception.get()));
515   return JNI_OK;
516 }
517 
JavaVmExtFromEnv(JNIEnv * env)518 static JavaVMExt* JavaVmExtFromEnv(JNIEnv* env) {
519   return reinterpret_cast<JNIEnvExt*>(env)->GetVm();
520 }
521 
522 #define CHECK_NON_NULL_ARGUMENT(value) \
523     CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, nullptr)
524 
525 #define CHECK_NON_NULL_ARGUMENT_RETURN_VOID(value) \
526     CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, )
527 
528 #define CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(value) \
529     CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, 0)
530 
531 #define CHECK_NON_NULL_ARGUMENT_RETURN(value, return_val) \
532     CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, return_val)
533 
534 #define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value, return_val) \
535   if (UNLIKELY((value) == nullptr)) { \
536     JavaVmExtFromEnv(env)->JniAbort(name, #value " == null"); \
537     return return_val; \
538   }
539 
540 #define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
541   if (UNLIKELY((length) != 0 && (value) == nullptr)) { \
542     JavaVmExtFromEnv(env)->JniAbort(__FUNCTION__, #value " == null"); \
543     return; \
544   }
545 
546 template <bool kNative>
FindMethod(ObjPtr<mirror::Class> c,std::string_view name,std::string_view sig)547 static ArtMethod* FindMethod(ObjPtr<mirror::Class> c,
548                              std::string_view name,
549                              std::string_view sig)
550     REQUIRES_SHARED(Locks::mutator_lock_) {
551   auto pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
552   for (auto& method : c->GetMethods(pointer_size)) {
553     if (kNative == method.IsNative() && name == method.GetName() && method.GetSignature() == sig) {
554       return &method;
555     }
556   }
557   return nullptr;
558 }
559 
560 template <bool kEnableIndexIds>
561 class JNI {
562  public:
GetVersion(JNIEnv *)563   static jint GetVersion(JNIEnv*) {
564     return JNI_VERSION_1_6;
565   }
566 
DefineClass(JNIEnv *,const char *,jobject,const jbyte *,jsize)567   static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
568     LOG(WARNING) << "JNI DefineClass is not supported";
569     return nullptr;
570   }
571 
FindClass(JNIEnv * env,const char * name)572   static jclass FindClass(JNIEnv* env, const char* name) {
573     CHECK_NON_NULL_ARGUMENT(name);
574     Runtime* runtime = Runtime::Current();
575     ClassLinker* class_linker = runtime->GetClassLinker();
576     std::string descriptor(NormalizeJniClassDescriptor(name));
577     ScopedObjectAccess soa(env);
578     ObjPtr<mirror::Class> c = nullptr;
579     if (runtime->IsStarted()) {
580       StackHandleScope<1> hs(soa.Self());
581       Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader<kEnableIndexIds>(soa)));
582       c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
583     } else {
584       c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
585     }
586     return soa.AddLocalReference<jclass>(c);
587   }
588 
FromReflectedMethod(JNIEnv * env,jobject jlr_method)589   static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
590     CHECK_NON_NULL_ARGUMENT(jlr_method);
591     ScopedObjectAccess soa(env);
592     return jni::EncodeArtMethod<kEnableIndexIds>(ArtMethod::FromReflectedMethod(soa, jlr_method));
593   }
594 
FromReflectedField(JNIEnv * env,jobject jlr_field)595   static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
596     CHECK_NON_NULL_ARGUMENT(jlr_field);
597     ScopedObjectAccess soa(env);
598     ObjPtr<mirror::Object> obj_field = soa.Decode<mirror::Object>(jlr_field);
599     if (obj_field->GetClass() != GetClassRoot<mirror::Field>()) {
600       // Not even a java.lang.reflect.Field, return null. TODO, is this check necessary?
601       return nullptr;
602     }
603     ObjPtr<mirror::Field> field = ObjPtr<mirror::Field>::DownCast(obj_field);
604     return jni::EncodeArtField<kEnableIndexIds>(field->GetArtField());
605   }
606 
ToReflectedMethod(JNIEnv * env,jclass,jmethodID mid,jboolean)607   static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
608     CHECK_NON_NULL_ARGUMENT(mid);
609     ScopedObjectAccess soa(env);
610     ArtMethod* m = jni::DecodeArtMethod(mid);
611     ObjPtr<mirror::Executable> method;
612     DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
613     DCHECK(!Runtime::Current()->IsActiveTransaction());
614     if (m->IsConstructor()) {
615       method = mirror::Constructor::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), m);
616     } else {
617       method = mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), m);
618     }
619     return soa.AddLocalReference<jobject>(method);
620   }
621 
ToReflectedField(JNIEnv * env,jclass,jfieldID fid,jboolean)622   static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
623     CHECK_NON_NULL_ARGUMENT(fid);
624     ScopedObjectAccess soa(env);
625     ArtField* f = jni::DecodeArtField(fid);
626     return soa.AddLocalReference<jobject>(
627         mirror::Field::CreateFromArtField<kRuntimePointerSize>(soa.Self(), f, true));
628   }
629 
GetObjectClass(JNIEnv * env,jobject java_object)630   static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
631     CHECK_NON_NULL_ARGUMENT(java_object);
632     ScopedObjectAccess soa(env);
633     ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(java_object);
634     return soa.AddLocalReference<jclass>(o->GetClass());
635   }
636 
GetSuperclass(JNIEnv * env,jclass java_class)637   static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
638     CHECK_NON_NULL_ARGUMENT(java_class);
639     ScopedObjectAccess soa(env);
640     ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(java_class);
641     return soa.AddLocalReference<jclass>(c->IsInterface() ? nullptr : c->GetSuperClass());
642   }
643 
644   // Note: java_class1 should be safely castable to java_class2, and
645   // not the other way around.
IsAssignableFrom(JNIEnv * env,jclass java_class1,jclass java_class2)646   static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
647     CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
648     CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
649     ScopedObjectAccess soa(env);
650     ObjPtr<mirror::Class> c1 = soa.Decode<mirror::Class>(java_class1);
651     ObjPtr<mirror::Class> c2 = soa.Decode<mirror::Class>(java_class2);
652     return c2->IsAssignableFrom(c1) ? JNI_TRUE : JNI_FALSE;
653   }
654 
IsInstanceOf(JNIEnv * env,jobject jobj,jclass java_class)655   static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
656     CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
657     if (jobj == nullptr) {
658       // Note: JNI is different from regular Java instanceof in this respect
659       return JNI_TRUE;
660     } else {
661       ScopedObjectAccess soa(env);
662       ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(jobj);
663       ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(java_class);
664       return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
665     }
666   }
667 
Throw(JNIEnv * env,jthrowable java_exception)668   static jint Throw(JNIEnv* env, jthrowable java_exception) {
669     ScopedObjectAccess soa(env);
670     ObjPtr<mirror::Throwable> exception = soa.Decode<mirror::Throwable>(java_exception);
671     if (exception == nullptr) {
672       return JNI_ERR;
673     }
674     soa.Self()->SetException(exception);
675     return JNI_OK;
676   }
677 
ThrowNew(JNIEnv * env,jclass c,const char * msg)678   static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
679     CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
680     return ThrowNewException(env, c, msg, nullptr);
681   }
682 
ExceptionCheck(JNIEnv * env)683   static jboolean ExceptionCheck(JNIEnv* env) {
684     return static_cast<JNIEnvExt*>(env)->self_->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
685   }
686 
ExceptionClear(JNIEnv * env)687   static void ExceptionClear(JNIEnv* env) {
688     ScopedObjectAccess soa(env);
689     soa.Self()->ClearException();
690   }
691 
ExceptionDescribe(JNIEnv * env)692   static void ExceptionDescribe(JNIEnv* env) {
693     ScopedObjectAccess soa(env);
694 
695     // If we have no exception to describe, pass through.
696     if (!soa.Self()->GetException()) {
697       return;
698     }
699 
700     StackHandleScope<1> hs(soa.Self());
701     Handle<mirror::Throwable> old_exception(
702         hs.NewHandle<mirror::Throwable>(soa.Self()->GetException()));
703     soa.Self()->ClearException();
704     ScopedLocalRef<jthrowable> exception(env,
705                                          soa.AddLocalReference<jthrowable>(old_exception.Get()));
706     ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
707     jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
708     if (mid == nullptr) {
709       LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
710                    << mirror::Object::PrettyTypeOf(old_exception.Get());
711     } else {
712       env->CallVoidMethod(exception.get(), mid);
713       if (soa.Self()->IsExceptionPending()) {
714         LOG(WARNING) << "JNI WARNING: " << mirror::Object::PrettyTypeOf(soa.Self()->GetException())
715                      << " thrown while calling printStackTrace";
716         soa.Self()->ClearException();
717       }
718     }
719     soa.Self()->SetException(old_exception.Get());
720   }
721 
ExceptionOccurred(JNIEnv * env)722   static jthrowable ExceptionOccurred(JNIEnv* env) {
723     ScopedObjectAccess soa(env);
724     ObjPtr<mirror::Object> exception = soa.Self()->GetException();
725     return soa.AddLocalReference<jthrowable>(exception);
726   }
727 
FatalError(JNIEnv *,const char * msg)728   static void FatalError(JNIEnv*, const char* msg) {
729     LOG(FATAL) << "JNI FatalError called: " << msg;
730   }
731 
PushLocalFrame(JNIEnv * env,jint capacity)732   static jint PushLocalFrame(JNIEnv* env, jint capacity) {
733     // TODO: SOA may not be necessary but I do it to please lock annotations.
734     ScopedObjectAccess soa(env);
735     if (EnsureLocalCapacityInternal(soa, capacity, "PushLocalFrame") != JNI_OK) {
736       return JNI_ERR;
737     }
738     down_cast<JNIEnvExt*>(env)->PushFrame(capacity);
739     return JNI_OK;
740   }
741 
PopLocalFrame(JNIEnv * env,jobject java_survivor)742   static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
743     ScopedObjectAccess soa(env);
744     ObjPtr<mirror::Object> survivor = soa.Decode<mirror::Object>(java_survivor);
745     soa.Env()->PopFrame();
746     return soa.AddLocalReference<jobject>(survivor);
747   }
748 
EnsureLocalCapacity(JNIEnv * env,jint desired_capacity)749   static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
750     // TODO: SOA may not be necessary but I do it to please lock annotations.
751     ScopedObjectAccess soa(env);
752     return EnsureLocalCapacityInternal(soa, desired_capacity, "EnsureLocalCapacity");
753   }
754 
NewGlobalRef(JNIEnv * env,jobject obj)755   static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
756     ScopedObjectAccess soa(env);
757     ObjPtr<mirror::Object> decoded_obj = soa.Decode<mirror::Object>(obj);
758     return soa.Vm()->AddGlobalRef(soa.Self(), decoded_obj);
759   }
760 
DeleteGlobalRef(JNIEnv * env,jobject obj)761   static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
762     JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->GetVm();
763     Thread* self = down_cast<JNIEnvExt*>(env)->self_;
764     vm->DeleteGlobalRef(self, obj);
765   }
766 
NewWeakGlobalRef(JNIEnv * env,jobject obj)767   static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
768     ScopedObjectAccess soa(env);
769     ObjPtr<mirror::Object> decoded_obj = soa.Decode<mirror::Object>(obj);
770     return soa.Vm()->AddWeakGlobalRef(soa.Self(), decoded_obj);
771   }
772 
DeleteWeakGlobalRef(JNIEnv * env,jweak obj)773   static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
774     JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->GetVm();
775     Thread* self = down_cast<JNIEnvExt*>(env)->self_;
776     vm->DeleteWeakGlobalRef(self, obj);
777   }
778 
NewLocalRef(JNIEnv * env,jobject obj)779   static jobject NewLocalRef(JNIEnv* env, jobject obj) {
780     ScopedObjectAccess soa(env);
781     ObjPtr<mirror::Object> decoded_obj = soa.Decode<mirror::Object>(obj);
782     // Check for null after decoding the object to handle cleared weak globals.
783     if (decoded_obj == nullptr) {
784       return nullptr;
785     }
786     return soa.AddLocalReference<jobject>(decoded_obj);
787   }
788 
DeleteLocalRef(JNIEnv * env,jobject obj)789   static void DeleteLocalRef(JNIEnv* env, jobject obj) {
790     if (obj == nullptr) {
791       return;
792     }
793     // SOA is only necessary to have exclusion between GC root marking and removing.
794     // We don't want to have the GC attempt to mark a null root if we just removed
795     // it. b/22119403
796     ScopedObjectAccess soa(env);
797     auto* ext_env = down_cast<JNIEnvExt*>(env);
798     if (!ext_env->locals_.Remove(ext_env->local_ref_cookie_, obj)) {
799       // Attempting to delete a local reference that is not in the
800       // topmost local reference frame is a no-op.  DeleteLocalRef returns
801       // void and doesn't throw any exceptions, but we should probably
802       // complain about it so the user will notice that things aren't
803       // going quite the way they expect.
804       LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
805                    << "failed to find entry";
806     }
807   }
808 
IsSameObject(JNIEnv * env,jobject obj1,jobject obj2)809   static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
810     if (obj1 == obj2) {
811       return JNI_TRUE;
812     } else {
813       ScopedObjectAccess soa(env);
814       return (soa.Decode<mirror::Object>(obj1) == soa.Decode<mirror::Object>(obj2))
815               ? JNI_TRUE : JNI_FALSE;
816     }
817   }
818 
AllocObject(JNIEnv * env,jclass java_class)819   static jobject AllocObject(JNIEnv* env, jclass java_class) {
820     CHECK_NON_NULL_ARGUMENT(java_class);
821     ScopedObjectAccess soa(env);
822     ObjPtr<mirror::Class> c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class>(java_class));
823     if (c == nullptr) {
824       return nullptr;
825     }
826     if (c->IsStringClass()) {
827       gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
828       return soa.AddLocalReference<jobject>(
829           mirror::String::AllocEmptyString(soa.Self(), allocator_type));
830     }
831     return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
832   }
833 
NewObject(JNIEnv * env,jclass java_class,jmethodID mid,...)834   static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
835     va_list args;
836     va_start(args, mid);
837     ScopedVAArgs free_args_later(&args);
838     CHECK_NON_NULL_ARGUMENT(java_class);
839     CHECK_NON_NULL_ARGUMENT(mid);
840     jobject result = NewObjectV(env, java_class, mid, args);
841     return result;
842   }
843 
NewObjectV(JNIEnv * env,jclass java_class,jmethodID mid,va_list args)844   static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
845     CHECK_NON_NULL_ARGUMENT(java_class);
846     CHECK_NON_NULL_ARGUMENT(mid);
847     ScopedObjectAccess soa(env);
848     ObjPtr<mirror::Class> c = EnsureInitialized(soa.Self(),
849                                                 soa.Decode<mirror::Class>(java_class));
850     if (c == nullptr) {
851       return nullptr;
852     }
853     if (c->IsStringClass()) {
854       // Replace calls to String.<init> with equivalent StringFactory call.
855       jmethodID sf_mid = jni::EncodeArtMethod<kEnableIndexIds>(
856           WellKnownClasses::StringInitToStringFactory(jni::DecodeArtMethod(mid)));
857       return CallStaticObjectMethodV(env, WellKnownClasses::java_lang_StringFactory, sf_mid, args);
858     }
859     ObjPtr<mirror::Object> result = c->AllocObject(soa.Self());
860     if (result == nullptr) {
861       return nullptr;
862     }
863     jobject local_result = soa.AddLocalReference<jobject>(result);
864     CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
865     if (soa.Self()->IsExceptionPending()) {
866       return nullptr;
867     }
868     return local_result;
869   }
870 
NewObjectA(JNIEnv * env,jclass java_class,jmethodID mid,const jvalue * args)871   static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, const jvalue* args) {
872     CHECK_NON_NULL_ARGUMENT(java_class);
873     CHECK_NON_NULL_ARGUMENT(mid);
874     ScopedObjectAccess soa(env);
875     ObjPtr<mirror::Class> c = EnsureInitialized(soa.Self(),
876                                                 soa.Decode<mirror::Class>(java_class));
877     if (c == nullptr) {
878       return nullptr;
879     }
880     if (c->IsStringClass()) {
881       // Replace calls to String.<init> with equivalent StringFactory call.
882       jmethodID sf_mid = jni::EncodeArtMethod<kEnableIndexIds>(
883           WellKnownClasses::StringInitToStringFactory(jni::DecodeArtMethod(mid)));
884       return CallStaticObjectMethodA(env, WellKnownClasses::java_lang_StringFactory, sf_mid, args);
885     }
886     ObjPtr<mirror::Object> result = c->AllocObject(soa.Self());
887     if (result == nullptr) {
888       return nullptr;
889     }
890     jobject local_result = soa.AddLocalReference<jobjectArray>(result);
891     CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
892     if (soa.Self()->IsExceptionPending()) {
893       return nullptr;
894     }
895     return local_result;
896   }
897 
GetMethodID(JNIEnv * env,jclass java_class,const char * name,const char * sig)898   static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
899     CHECK_NON_NULL_ARGUMENT(java_class);
900     CHECK_NON_NULL_ARGUMENT(name);
901     CHECK_NON_NULL_ARGUMENT(sig);
902     ScopedObjectAccess soa(env);
903     return FindMethodID<kEnableIndexIds>(soa, java_class, name, sig, false);
904   }
905 
GetStaticMethodID(JNIEnv * env,jclass java_class,const char * name,const char * sig)906   static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
907                                      const char* sig) {
908     CHECK_NON_NULL_ARGUMENT(java_class);
909     CHECK_NON_NULL_ARGUMENT(name);
910     CHECK_NON_NULL_ARGUMENT(sig);
911     ScopedObjectAccess soa(env);
912     return FindMethodID<kEnableIndexIds>(soa, java_class, name, sig, true);
913   }
914 
CallObjectMethod(JNIEnv * env,jobject obj,jmethodID mid,...)915   static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
916     va_list ap;
917     va_start(ap, mid);
918     ScopedVAArgs free_args_later(&ap);
919     CHECK_NON_NULL_ARGUMENT(obj);
920     CHECK_NON_NULL_ARGUMENT(mid);
921     ScopedObjectAccess soa(env);
922     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
923     return soa.AddLocalReference<jobject>(result.GetL());
924   }
925 
CallObjectMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)926   static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
927     CHECK_NON_NULL_ARGUMENT(obj);
928     CHECK_NON_NULL_ARGUMENT(mid);
929     ScopedObjectAccess soa(env);
930     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
931     return soa.AddLocalReference<jobject>(result.GetL());
932   }
933 
CallObjectMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)934   static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
935     CHECK_NON_NULL_ARGUMENT(obj);
936     CHECK_NON_NULL_ARGUMENT(mid);
937     ScopedObjectAccess soa(env);
938     JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
939     return soa.AddLocalReference<jobject>(result.GetL());
940   }
941 
CallBooleanMethod(JNIEnv * env,jobject obj,jmethodID mid,...)942   static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
943     va_list ap;
944     va_start(ap, mid);
945     ScopedVAArgs free_args_later(&ap);
946     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
947     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
948     ScopedObjectAccess soa(env);
949     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
950     return result.GetZ();
951   }
952 
CallBooleanMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)953   static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
954     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
955     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
956     ScopedObjectAccess soa(env);
957     return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
958   }
959 
CallBooleanMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)960   static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
961     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
962     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
963     ScopedObjectAccess soa(env);
964     return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
965   }
966 
CallByteMethod(JNIEnv * env,jobject obj,jmethodID mid,...)967   static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
968     va_list ap;
969     va_start(ap, mid);
970     ScopedVAArgs free_args_later(&ap);
971     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
972     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
973     ScopedObjectAccess soa(env);
974     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
975     return result.GetB();
976   }
977 
CallByteMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)978   static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
979     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
980     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
981     ScopedObjectAccess soa(env);
982     return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
983   }
984 
CallByteMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)985   static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
986     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
987     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
988     ScopedObjectAccess soa(env);
989     return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
990   }
991 
CallCharMethod(JNIEnv * env,jobject obj,jmethodID mid,...)992   static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
993     va_list ap;
994     va_start(ap, mid);
995     ScopedVAArgs free_args_later(&ap);
996     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
997     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
998     ScopedObjectAccess soa(env);
999     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
1000     return result.GetC();
1001   }
1002 
CallCharMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)1003   static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
1004     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1005     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1006     ScopedObjectAccess soa(env);
1007     return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
1008   }
1009 
CallCharMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)1010   static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
1011     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1012     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1013     ScopedObjectAccess soa(env);
1014     return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
1015   }
1016 
CallDoubleMethod(JNIEnv * env,jobject obj,jmethodID mid,...)1017   static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
1018     va_list ap;
1019     va_start(ap, mid);
1020     ScopedVAArgs free_args_later(&ap);
1021     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1022     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1023     ScopedObjectAccess soa(env);
1024     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
1025     return result.GetD();
1026   }
1027 
CallDoubleMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)1028   static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
1029     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1030     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1031     ScopedObjectAccess soa(env);
1032     return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
1033   }
1034 
CallDoubleMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)1035   static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
1036     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1037     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1038     ScopedObjectAccess soa(env);
1039     return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
1040   }
1041 
CallFloatMethod(JNIEnv * env,jobject obj,jmethodID mid,...)1042   static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
1043     va_list ap;
1044     va_start(ap, mid);
1045     ScopedVAArgs free_args_later(&ap);
1046     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1047     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1048     ScopedObjectAccess soa(env);
1049     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
1050     return result.GetF();
1051   }
1052 
CallFloatMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)1053   static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
1054     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1055     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1056     ScopedObjectAccess soa(env);
1057     return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
1058   }
1059 
CallFloatMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)1060   static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
1061     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1062     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1063     ScopedObjectAccess soa(env);
1064     return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
1065   }
1066 
CallIntMethod(JNIEnv * env,jobject obj,jmethodID mid,...)1067   static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
1068     va_list ap;
1069     va_start(ap, mid);
1070     ScopedVAArgs free_args_later(&ap);
1071     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1072     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1073     ScopedObjectAccess soa(env);
1074     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
1075     return result.GetI();
1076   }
1077 
CallIntMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)1078   static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
1079     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1080     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1081     ScopedObjectAccess soa(env);
1082     return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
1083   }
1084 
CallIntMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)1085   static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
1086     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1087     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1088     ScopedObjectAccess soa(env);
1089     return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
1090   }
1091 
CallLongMethod(JNIEnv * env,jobject obj,jmethodID mid,...)1092   static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
1093     va_list ap;
1094     va_start(ap, mid);
1095     ScopedVAArgs free_args_later(&ap);
1096     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1097     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1098     ScopedObjectAccess soa(env);
1099     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
1100     return result.GetJ();
1101   }
1102 
CallLongMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)1103   static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
1104     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1105     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1106     ScopedObjectAccess soa(env);
1107     return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
1108   }
1109 
CallLongMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)1110   static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
1111     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1112     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1113     ScopedObjectAccess soa(env);
1114     return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
1115   }
1116 
CallShortMethod(JNIEnv * env,jobject obj,jmethodID mid,...)1117   static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
1118     va_list ap;
1119     va_start(ap, mid);
1120     ScopedVAArgs free_args_later(&ap);
1121     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1122     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1123     ScopedObjectAccess soa(env);
1124     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
1125     return result.GetS();
1126   }
1127 
CallShortMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)1128   static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
1129     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1130     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1131     ScopedObjectAccess soa(env);
1132     return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
1133   }
1134 
CallShortMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)1135   static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
1136     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1137     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1138     ScopedObjectAccess soa(env);
1139     return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
1140   }
1141 
CallVoidMethod(JNIEnv * env,jobject obj,jmethodID mid,...)1142   static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
1143     va_list ap;
1144     va_start(ap, mid);
1145     ScopedVAArgs free_args_later(&ap);
1146     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1147     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1148     ScopedObjectAccess soa(env);
1149     InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
1150   }
1151 
CallVoidMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)1152   static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
1153     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1154     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1155     ScopedObjectAccess soa(env);
1156     InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
1157   }
1158 
CallVoidMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)1159   static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
1160     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1161     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1162     ScopedObjectAccess soa(env);
1163     InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
1164   }
1165 
CallNonvirtualObjectMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1166   static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1167     va_list ap;
1168     va_start(ap, mid);
1169     ScopedVAArgs free_args_later(&ap);
1170     CHECK_NON_NULL_ARGUMENT(obj);
1171     CHECK_NON_NULL_ARGUMENT(mid);
1172     ScopedObjectAccess soa(env);
1173     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1174     jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
1175     return local_result;
1176   }
1177 
CallNonvirtualObjectMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1178   static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1179                                              va_list args) {
1180     CHECK_NON_NULL_ARGUMENT(obj);
1181     CHECK_NON_NULL_ARGUMENT(mid);
1182     ScopedObjectAccess soa(env);
1183     JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1184     return soa.AddLocalReference<jobject>(result.GetL());
1185   }
1186 
CallNonvirtualObjectMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1187   static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1188                                              const jvalue* args) {
1189     CHECK_NON_NULL_ARGUMENT(obj);
1190     CHECK_NON_NULL_ARGUMENT(mid);
1191     ScopedObjectAccess soa(env);
1192     JValue result(InvokeWithJValues(soa, obj, mid, args));
1193     return soa.AddLocalReference<jobject>(result.GetL());
1194   }
1195 
CallNonvirtualBooleanMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1196   static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1197                                               ...) {
1198     va_list ap;
1199     va_start(ap, mid);
1200     ScopedVAArgs free_args_later(&ap);
1201     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1202     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1203     ScopedObjectAccess soa(env);
1204     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1205     return result.GetZ();
1206   }
1207 
CallNonvirtualBooleanMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1208   static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1209                                                va_list args) {
1210     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1211     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1212     ScopedObjectAccess soa(env);
1213     return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
1214   }
1215 
CallNonvirtualBooleanMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1216   static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1217                                                const jvalue* args) {
1218     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1219     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1220     ScopedObjectAccess soa(env);
1221     return InvokeWithJValues(soa, obj, mid, args).GetZ();
1222   }
1223 
CallNonvirtualByteMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1224   static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1225     va_list ap;
1226     va_start(ap, mid);
1227     ScopedVAArgs free_args_later(&ap);
1228     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1229     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1230     ScopedObjectAccess soa(env);
1231     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1232     return result.GetB();
1233   }
1234 
CallNonvirtualByteMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1235   static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1236                                          va_list args) {
1237     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1238     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1239     ScopedObjectAccess soa(env);
1240     return InvokeWithVarArgs(soa, obj, mid, args).GetB();
1241   }
1242 
CallNonvirtualByteMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1243   static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1244                                          const jvalue* args) {
1245     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1246     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1247     ScopedObjectAccess soa(env);
1248     return InvokeWithJValues(soa, obj, mid, args).GetB();
1249   }
1250 
CallNonvirtualCharMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1251   static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1252     va_list ap;
1253     va_start(ap, mid);
1254     ScopedVAArgs free_args_later(&ap);
1255     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1256     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1257     ScopedObjectAccess soa(env);
1258     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1259     return result.GetC();
1260   }
1261 
CallNonvirtualCharMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1262   static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1263                                          va_list args) {
1264     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1265     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1266     ScopedObjectAccess soa(env);
1267     return InvokeWithVarArgs(soa, obj, mid, args).GetC();
1268   }
1269 
CallNonvirtualCharMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1270   static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1271                                          const jvalue* args) {
1272     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1273     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1274     ScopedObjectAccess soa(env);
1275     return InvokeWithJValues(soa, obj, mid, args).GetC();
1276   }
1277 
CallNonvirtualShortMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1278   static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1279     va_list ap;
1280     va_start(ap, mid);
1281     ScopedVAArgs free_args_later(&ap);
1282     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1283     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1284     ScopedObjectAccess soa(env);
1285     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1286     return result.GetS();
1287   }
1288 
CallNonvirtualShortMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1289   static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1290                                            va_list args) {
1291     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1292     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1293     ScopedObjectAccess soa(env);
1294     return InvokeWithVarArgs(soa, obj, mid, args).GetS();
1295   }
1296 
CallNonvirtualShortMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1297   static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1298                                            const jvalue* args) {
1299     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1300     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1301     ScopedObjectAccess soa(env);
1302     return InvokeWithJValues(soa, obj, mid, args).GetS();
1303   }
1304 
CallNonvirtualIntMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1305   static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1306     va_list ap;
1307     va_start(ap, mid);
1308     ScopedVAArgs free_args_later(&ap);
1309     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1310     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1311     ScopedObjectAccess soa(env);
1312     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1313     return result.GetI();
1314   }
1315 
CallNonvirtualIntMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1316   static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1317                                        va_list args) {
1318     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1319     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1320     ScopedObjectAccess soa(env);
1321     return InvokeWithVarArgs(soa, obj, mid, args).GetI();
1322   }
1323 
CallNonvirtualIntMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1324   static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1325                                        const jvalue* args) {
1326     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1327     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1328     ScopedObjectAccess soa(env);
1329     return InvokeWithJValues(soa, obj, mid, args).GetI();
1330   }
1331 
CallNonvirtualLongMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1332   static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1333     va_list ap;
1334     va_start(ap, mid);
1335     ScopedVAArgs free_args_later(&ap);
1336     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1337     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1338     ScopedObjectAccess soa(env);
1339     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1340     return result.GetJ();
1341   }
1342 
CallNonvirtualLongMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1343   static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1344                                          va_list args) {
1345     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1346     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1347     ScopedObjectAccess soa(env);
1348     return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
1349   }
1350 
CallNonvirtualLongMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1351   static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1352                                          const jvalue* args) {
1353     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1354     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1355     ScopedObjectAccess soa(env);
1356     return InvokeWithJValues(soa, obj, mid, args).GetJ();
1357   }
1358 
CallNonvirtualFloatMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1359   static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1360     va_list ap;
1361     va_start(ap, mid);
1362     ScopedVAArgs free_args_later(&ap);
1363     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1364     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1365     ScopedObjectAccess soa(env);
1366     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1367     return result.GetF();
1368   }
1369 
CallNonvirtualFloatMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1370   static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1371                                            va_list args) {
1372     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1373     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1374     ScopedObjectAccess soa(env);
1375     return InvokeWithVarArgs(soa, obj, mid, args).GetF();
1376   }
1377 
CallNonvirtualFloatMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1378   static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1379                                            const jvalue* args) {
1380     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1381     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1382     ScopedObjectAccess soa(env);
1383     return InvokeWithJValues(soa, obj, mid, args).GetF();
1384   }
1385 
CallNonvirtualDoubleMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1386   static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1387     va_list ap;
1388     va_start(ap, mid);
1389     ScopedVAArgs free_args_later(&ap);
1390     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1391     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1392     ScopedObjectAccess soa(env);
1393     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1394     return result.GetD();
1395   }
1396 
CallNonvirtualDoubleMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1397   static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1398                                              va_list args) {
1399     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1400     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1401     ScopedObjectAccess soa(env);
1402     return InvokeWithVarArgs(soa, obj, mid, args).GetD();
1403   }
1404 
CallNonvirtualDoubleMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1405   static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1406                                              const jvalue* args) {
1407     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1408     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1409     ScopedObjectAccess soa(env);
1410     return InvokeWithJValues(soa, obj, mid, args).GetD();
1411   }
1412 
CallNonvirtualVoidMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1413   static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1414     va_list ap;
1415     va_start(ap, mid);
1416     ScopedVAArgs free_args_later(&ap);
1417     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1418     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1419     ScopedObjectAccess soa(env);
1420     InvokeWithVarArgs(soa, obj, mid, ap);
1421   }
1422 
CallNonvirtualVoidMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1423   static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1424                                         va_list args) {
1425     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1426     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1427     ScopedObjectAccess soa(env);
1428     InvokeWithVarArgs(soa, obj, mid, args);
1429   }
1430 
CallNonvirtualVoidMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1431   static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1432                                         const jvalue* args) {
1433     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1434     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1435     ScopedObjectAccess soa(env);
1436     InvokeWithJValues(soa, obj, mid, args);
1437   }
1438 
GetFieldID(JNIEnv * env,jclass java_class,const char * name,const char * sig)1439   static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
1440     CHECK_NON_NULL_ARGUMENT(java_class);
1441     CHECK_NON_NULL_ARGUMENT(name);
1442     CHECK_NON_NULL_ARGUMENT(sig);
1443     ScopedObjectAccess soa(env);
1444     return FindFieldID<kEnableIndexIds>(soa, java_class, name, sig, false);
1445   }
1446 
GetStaticFieldID(JNIEnv * env,jclass java_class,const char * name,const char * sig)1447   static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1448                                    const char* sig) {
1449     CHECK_NON_NULL_ARGUMENT(java_class);
1450     CHECK_NON_NULL_ARGUMENT(name);
1451     CHECK_NON_NULL_ARGUMENT(sig);
1452     ScopedObjectAccess soa(env);
1453     return FindFieldID<kEnableIndexIds>(soa, java_class, name, sig, true);
1454   }
1455 
GetObjectField(JNIEnv * env,jobject obj,jfieldID fid)1456   static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
1457     CHECK_NON_NULL_ARGUMENT(obj);
1458     CHECK_NON_NULL_ARGUMENT(fid);
1459     ScopedObjectAccess soa(env);
1460     ArtField* f = jni::DecodeArtField<kEnableIndexIds>(fid);
1461     NotifyGetField(f, obj);
1462     ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(obj);
1463     return soa.AddLocalReference<jobject>(f->GetObject(o));
1464   }
1465 
GetStaticObjectField(JNIEnv * env,jclass,jfieldID fid)1466   static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
1467     CHECK_NON_NULL_ARGUMENT(fid);
1468     ScopedObjectAccess soa(env);
1469     ArtField* f = jni::DecodeArtField<kEnableIndexIds>(fid);
1470     NotifyGetField(f, nullptr);
1471     return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
1472   }
1473 
SetObjectField(JNIEnv * env,jobject java_object,jfieldID fid,jobject java_value)1474   static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
1475     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1476     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
1477     ScopedObjectAccess soa(env);
1478     ArtField* f = jni::DecodeArtField<kEnableIndexIds>(fid);
1479     NotifySetObjectField(f, java_object, java_value);
1480     ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(java_object);
1481     ObjPtr<mirror::Object> v = soa.Decode<mirror::Object>(java_value);
1482     f->SetObject<false>(o, v);
1483   }
1484 
SetStaticObjectField(JNIEnv * env,jclass,jfieldID fid,jobject java_value)1485   static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
1486     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
1487     ScopedObjectAccess soa(env);
1488     ArtField* f = jni::DecodeArtField<kEnableIndexIds>(fid);
1489     NotifySetObjectField(f, nullptr, java_value);
1490     ObjPtr<mirror::Object> v = soa.Decode<mirror::Object>(java_value);
1491     f->SetObject<false>(f->GetDeclaringClass(), v);
1492   }
1493 
1494 #define GET_PRIMITIVE_FIELD(fn, instance) \
1495   CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1496   CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
1497   ScopedObjectAccess soa(env); \
1498   ArtField* f = jni::DecodeArtField<kEnableIndexIds>(fid); \
1499   NotifyGetField(f, instance); \
1500   ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(instance); \
1501   return f->Get ##fn (o)
1502 
1503 #define GET_STATIC_PRIMITIVE_FIELD(fn) \
1504   CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
1505   ScopedObjectAccess soa(env); \
1506   ArtField* f = jni::DecodeArtField<kEnableIndexIds>(fid); \
1507   NotifyGetField(f, nullptr); \
1508   return f->Get ##fn (f->GetDeclaringClass())
1509 
1510 #define SET_PRIMITIVE_FIELD(fn, instance, value) \
1511   CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1512   CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
1513   ScopedObjectAccess soa(env); \
1514   ArtField* f = jni::DecodeArtField<kEnableIndexIds>(fid); \
1515   NotifySetPrimitiveField(f, instance, JValue::FromPrimitive<decltype(value)>(value)); \
1516   ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(instance); \
1517   f->Set ##fn <false>(o, value)
1518 
1519 #define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
1520   CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
1521   ScopedObjectAccess soa(env); \
1522   ArtField* f = jni::DecodeArtField<kEnableIndexIds>(fid); \
1523   NotifySetPrimitiveField(f, nullptr, JValue::FromPrimitive<decltype(value)>(value)); \
1524   f->Set ##fn <false>(f->GetDeclaringClass(), value)
1525 
GetBooleanField(JNIEnv * env,jobject obj,jfieldID fid)1526   static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1527     GET_PRIMITIVE_FIELD(Boolean, obj);
1528   }
1529 
GetByteField(JNIEnv * env,jobject obj,jfieldID fid)1530   static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1531     GET_PRIMITIVE_FIELD(Byte, obj);
1532   }
1533 
GetCharField(JNIEnv * env,jobject obj,jfieldID fid)1534   static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1535     GET_PRIMITIVE_FIELD(Char, obj);
1536   }
1537 
GetShortField(JNIEnv * env,jobject obj,jfieldID fid)1538   static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1539     GET_PRIMITIVE_FIELD(Short, obj);
1540   }
1541 
GetIntField(JNIEnv * env,jobject obj,jfieldID fid)1542   static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1543     GET_PRIMITIVE_FIELD(Int, obj);
1544   }
1545 
GetLongField(JNIEnv * env,jobject obj,jfieldID fid)1546   static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1547     GET_PRIMITIVE_FIELD(Long, obj);
1548   }
1549 
GetFloatField(JNIEnv * env,jobject obj,jfieldID fid)1550   static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1551     GET_PRIMITIVE_FIELD(Float, obj);
1552   }
1553 
GetDoubleField(JNIEnv * env,jobject obj,jfieldID fid)1554   static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1555     GET_PRIMITIVE_FIELD(Double, obj);
1556   }
1557 
GetStaticBooleanField(JNIEnv * env,jclass,jfieldID fid)1558   static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
1559     GET_STATIC_PRIMITIVE_FIELD(Boolean);
1560   }
1561 
GetStaticByteField(JNIEnv * env,jclass,jfieldID fid)1562   static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
1563     GET_STATIC_PRIMITIVE_FIELD(Byte);
1564   }
1565 
GetStaticCharField(JNIEnv * env,jclass,jfieldID fid)1566   static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
1567     GET_STATIC_PRIMITIVE_FIELD(Char);
1568   }
1569 
GetStaticShortField(JNIEnv * env,jclass,jfieldID fid)1570   static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
1571     GET_STATIC_PRIMITIVE_FIELD(Short);
1572   }
1573 
GetStaticIntField(JNIEnv * env,jclass,jfieldID fid)1574   static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
1575     GET_STATIC_PRIMITIVE_FIELD(Int);
1576   }
1577 
GetStaticLongField(JNIEnv * env,jclass,jfieldID fid)1578   static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
1579     GET_STATIC_PRIMITIVE_FIELD(Long);
1580   }
1581 
GetStaticFloatField(JNIEnv * env,jclass,jfieldID fid)1582   static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
1583     GET_STATIC_PRIMITIVE_FIELD(Float);
1584   }
1585 
GetStaticDoubleField(JNIEnv * env,jclass,jfieldID fid)1586   static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
1587     GET_STATIC_PRIMITIVE_FIELD(Double);
1588   }
1589 
SetBooleanField(JNIEnv * env,jobject obj,jfieldID fid,jboolean v)1590   static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1591     SET_PRIMITIVE_FIELD(Boolean, obj, v);
1592   }
1593 
SetByteField(JNIEnv * env,jobject obj,jfieldID fid,jbyte v)1594   static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1595     SET_PRIMITIVE_FIELD(Byte, obj, v);
1596   }
1597 
SetCharField(JNIEnv * env,jobject obj,jfieldID fid,jchar v)1598   static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1599     SET_PRIMITIVE_FIELD(Char, obj, v);
1600   }
1601 
SetFloatField(JNIEnv * env,jobject obj,jfieldID fid,jfloat v)1602   static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1603     SET_PRIMITIVE_FIELD(Float, obj, v);
1604   }
1605 
SetDoubleField(JNIEnv * env,jobject obj,jfieldID fid,jdouble v)1606   static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1607     SET_PRIMITIVE_FIELD(Double, obj, v);
1608   }
1609 
SetIntField(JNIEnv * env,jobject obj,jfieldID fid,jint v)1610   static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1611     SET_PRIMITIVE_FIELD(Int, obj, v);
1612   }
1613 
SetLongField(JNIEnv * env,jobject obj,jfieldID fid,jlong v)1614   static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1615     SET_PRIMITIVE_FIELD(Long, obj, v);
1616   }
1617 
SetShortField(JNIEnv * env,jobject obj,jfieldID fid,jshort v)1618   static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1619     SET_PRIMITIVE_FIELD(Short, obj, v);
1620   }
1621 
SetStaticBooleanField(JNIEnv * env,jclass,jfieldID fid,jboolean v)1622   static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1623     SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
1624   }
1625 
SetStaticByteField(JNIEnv * env,jclass,jfieldID fid,jbyte v)1626   static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1627     SET_STATIC_PRIMITIVE_FIELD(Byte, v);
1628   }
1629 
SetStaticCharField(JNIEnv * env,jclass,jfieldID fid,jchar v)1630   static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1631     SET_STATIC_PRIMITIVE_FIELD(Char, v);
1632   }
1633 
SetStaticFloatField(JNIEnv * env,jclass,jfieldID fid,jfloat v)1634   static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1635     SET_STATIC_PRIMITIVE_FIELD(Float, v);
1636   }
1637 
SetStaticDoubleField(JNIEnv * env,jclass,jfieldID fid,jdouble v)1638   static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1639     SET_STATIC_PRIMITIVE_FIELD(Double, v);
1640   }
1641 
SetStaticIntField(JNIEnv * env,jclass,jfieldID fid,jint v)1642   static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1643     SET_STATIC_PRIMITIVE_FIELD(Int, v);
1644   }
1645 
SetStaticLongField(JNIEnv * env,jclass,jfieldID fid,jlong v)1646   static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1647     SET_STATIC_PRIMITIVE_FIELD(Long, v);
1648   }
1649 
SetStaticShortField(JNIEnv * env,jclass,jfieldID fid,jshort v)1650   static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1651     SET_STATIC_PRIMITIVE_FIELD(Short, v);
1652   }
1653 
CallStaticObjectMethod(JNIEnv * env,jclass,jmethodID mid,...)1654   static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1655     va_list ap;
1656     va_start(ap, mid);
1657     ScopedVAArgs free_args_later(&ap);
1658     CHECK_NON_NULL_ARGUMENT(mid);
1659     ScopedObjectAccess soa(env);
1660     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1661     jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
1662     return local_result;
1663   }
1664 
CallStaticObjectMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1665   static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1666     CHECK_NON_NULL_ARGUMENT(mid);
1667     ScopedObjectAccess soa(env);
1668     JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
1669     return soa.AddLocalReference<jobject>(result.GetL());
1670   }
1671 
CallStaticObjectMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1672   static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1673     CHECK_NON_NULL_ARGUMENT(mid);
1674     ScopedObjectAccess soa(env);
1675     JValue result(InvokeWithJValues(soa, nullptr, mid, args));
1676     return soa.AddLocalReference<jobject>(result.GetL());
1677   }
1678 
CallStaticBooleanMethod(JNIEnv * env,jclass,jmethodID mid,...)1679   static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1680     va_list ap;
1681     va_start(ap, mid);
1682     ScopedVAArgs free_args_later(&ap);
1683     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1684     ScopedObjectAccess soa(env);
1685     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1686     return result.GetZ();
1687   }
1688 
CallStaticBooleanMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1689   static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1690     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1691     ScopedObjectAccess soa(env);
1692     return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
1693   }
1694 
CallStaticBooleanMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1695   static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1696     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1697     ScopedObjectAccess soa(env);
1698     return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
1699   }
1700 
CallStaticByteMethod(JNIEnv * env,jclass,jmethodID mid,...)1701   static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1702     va_list ap;
1703     va_start(ap, mid);
1704     ScopedVAArgs free_args_later(&ap);
1705     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1706     ScopedObjectAccess soa(env);
1707     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1708     return result.GetB();
1709   }
1710 
CallStaticByteMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1711   static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1712     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1713     ScopedObjectAccess soa(env);
1714     return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
1715   }
1716 
CallStaticByteMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1717   static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1718     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1719     ScopedObjectAccess soa(env);
1720     return InvokeWithJValues(soa, nullptr, mid, args).GetB();
1721   }
1722 
CallStaticCharMethod(JNIEnv * env,jclass,jmethodID mid,...)1723   static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1724     va_list ap;
1725     va_start(ap, mid);
1726     ScopedVAArgs free_args_later(&ap);
1727     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1728     ScopedObjectAccess soa(env);
1729     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1730     return result.GetC();
1731   }
1732 
CallStaticCharMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1733   static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1734     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1735     ScopedObjectAccess soa(env);
1736     return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
1737   }
1738 
CallStaticCharMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1739   static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1740     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1741     ScopedObjectAccess soa(env);
1742     return InvokeWithJValues(soa, nullptr, mid, args).GetC();
1743   }
1744 
CallStaticShortMethod(JNIEnv * env,jclass,jmethodID mid,...)1745   static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1746     va_list ap;
1747     va_start(ap, mid);
1748     ScopedVAArgs free_args_later(&ap);
1749     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1750     ScopedObjectAccess soa(env);
1751     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1752     return result.GetS();
1753   }
1754 
CallStaticShortMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1755   static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1756     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1757     ScopedObjectAccess soa(env);
1758     return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
1759   }
1760 
CallStaticShortMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1761   static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1762     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1763     ScopedObjectAccess soa(env);
1764     return InvokeWithJValues(soa, nullptr, mid, args).GetS();
1765   }
1766 
CallStaticIntMethod(JNIEnv * env,jclass,jmethodID mid,...)1767   static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1768     va_list ap;
1769     va_start(ap, mid);
1770     ScopedVAArgs free_args_later(&ap);
1771     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1772     ScopedObjectAccess soa(env);
1773     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1774     return result.GetI();
1775   }
1776 
CallStaticIntMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1777   static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1778     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1779     ScopedObjectAccess soa(env);
1780     return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
1781   }
1782 
CallStaticIntMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1783   static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1784     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1785     ScopedObjectAccess soa(env);
1786     return InvokeWithJValues(soa, nullptr, mid, args).GetI();
1787   }
1788 
CallStaticLongMethod(JNIEnv * env,jclass,jmethodID mid,...)1789   static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1790     va_list ap;
1791     va_start(ap, mid);
1792     ScopedVAArgs free_args_later(&ap);
1793     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1794     ScopedObjectAccess soa(env);
1795     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1796     return result.GetJ();
1797   }
1798 
CallStaticLongMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1799   static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1800     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1801     ScopedObjectAccess soa(env);
1802     return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
1803   }
1804 
CallStaticLongMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1805   static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1806     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1807     ScopedObjectAccess soa(env);
1808     return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
1809   }
1810 
CallStaticFloatMethod(JNIEnv * env,jclass,jmethodID mid,...)1811   static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1812     va_list ap;
1813     va_start(ap, mid);
1814     ScopedVAArgs free_args_later(&ap);
1815     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1816     ScopedObjectAccess soa(env);
1817     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1818     return result.GetF();
1819   }
1820 
CallStaticFloatMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1821   static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1822     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1823     ScopedObjectAccess soa(env);
1824     return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
1825   }
1826 
CallStaticFloatMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1827   static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1828     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1829     ScopedObjectAccess soa(env);
1830     return InvokeWithJValues(soa, nullptr, mid, args).GetF();
1831   }
1832 
CallStaticDoubleMethod(JNIEnv * env,jclass,jmethodID mid,...)1833   static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1834     va_list ap;
1835     va_start(ap, mid);
1836     ScopedVAArgs free_args_later(&ap);
1837     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1838     ScopedObjectAccess soa(env);
1839     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1840     return result.GetD();
1841   }
1842 
CallStaticDoubleMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1843   static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1844     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1845     ScopedObjectAccess soa(env);
1846     return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
1847   }
1848 
CallStaticDoubleMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1849   static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1850     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1851     ScopedObjectAccess soa(env);
1852     return InvokeWithJValues(soa, nullptr, mid, args).GetD();
1853   }
1854 
CallStaticVoidMethod(JNIEnv * env,jclass,jmethodID mid,...)1855   static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1856     va_list ap;
1857     va_start(ap, mid);
1858     ScopedVAArgs free_args_later(&ap);
1859     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1860     ScopedObjectAccess soa(env);
1861     InvokeWithVarArgs(soa, nullptr, mid, ap);
1862   }
1863 
CallStaticVoidMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1864   static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1865     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1866     ScopedObjectAccess soa(env);
1867     InvokeWithVarArgs(soa, nullptr, mid, args);
1868   }
1869 
CallStaticVoidMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1870   static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1871     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1872     ScopedObjectAccess soa(env);
1873     InvokeWithJValues(soa, nullptr, mid, args);
1874   }
1875 
NewString(JNIEnv * env,const jchar * chars,jsize char_count)1876   static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
1877     if (UNLIKELY(char_count < 0)) {
1878       JavaVmExtFromEnv(env)->JniAbortF("NewString", "char_count < 0: %d", char_count);
1879       return nullptr;
1880     }
1881     if (UNLIKELY(chars == nullptr && char_count > 0)) {
1882       JavaVmExtFromEnv(env)->JniAbortF("NewString", "chars == null && char_count > 0");
1883       return nullptr;
1884     }
1885     ScopedObjectAccess soa(env);
1886     ObjPtr<mirror::String> result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
1887     return soa.AddLocalReference<jstring>(result);
1888   }
1889 
NewStringUTF(JNIEnv * env,const char * utf)1890   static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1891     if (utf == nullptr) {
1892       return nullptr;
1893     }
1894 
1895     // The input may come from an untrusted source, so we need to validate it.
1896     // We do not perform full validation, only as much as necessary to avoid reading
1897     // beyond the terminating null character or breaking string compression invariants.
1898     // CheckJNI performs stronger validation.
1899     size_t utf8_length = strlen(utf);
1900     if (UNLIKELY(utf8_length > static_cast<uint32_t>(std::numeric_limits<int32_t>::max()))) {
1901       // Converting the utf8_length to int32_t for String::AllocFromModifiedUtf8() would
1902       // overflow. Throw OOME eagerly to avoid 2GiB allocation when trying to replace
1903       // invalid sequences (even if such replacements could reduce the size below 2GiB).
1904       std::string error =
1905           android::base::StringPrintf("NewStringUTF input is 2 GiB or more: %zu", utf8_length);
1906       ScopedObjectAccess soa(env);
1907       soa.Self()->ThrowOutOfMemoryError(error.c_str());
1908       return nullptr;
1909     }
1910     std::optional<std::string> replacement_utf;
1911     size_t utf16_length = VisitModifiedUtf8Chars(
1912         utf,
1913         utf8_length,
1914         /*good=*/ [](const char* ptr ATTRIBUTE_UNUSED, size_t length ATTRIBUTE_UNUSED) {},
1915         /*bad=*/ []() { return true; });  // Abort processing and return 0 for bad characters.
1916     if (UNLIKELY(utf8_length != 0u && utf16_length == 0u)) {
1917       // VisitModifiedUtf8Chars() aborted for a bad character.
1918       android_errorWriteLog(0x534e4554, "172655291");  // Report to SafetyNet.
1919       // Report the error to logcat but avoid too much spam.
1920       static const uint64_t kMinDelay = UINT64_C(10000000000);  // 10s
1921       static std::atomic<uint64_t> prev_bad_input_time(UINT64_C(0));
1922       uint64_t prev_time = prev_bad_input_time.load(std::memory_order_relaxed);
1923       uint64_t now = NanoTime();
1924       if ((prev_time == 0u || now - prev_time >= kMinDelay) &&
1925           prev_bad_input_time.compare_exchange_strong(prev_time, now, std::memory_order_relaxed)) {
1926         LOG(ERROR) << "Invalid UTF-8 input to JNI::NewStringUTF()";
1927       }
1928       // Copy the input to the `replacement_utf` and replace bad characters.
1929       replacement_utf.emplace();
1930       replacement_utf->reserve(utf8_length);
1931       utf16_length = VisitModifiedUtf8Chars(
1932           utf,
1933           utf8_length,
1934           /*good=*/ [&](const char* ptr, size_t length) {
1935             replacement_utf->append(ptr, length);
1936           },
1937           /*bad=*/ [&]() {
1938             replacement_utf->append(kBadUtf8ReplacementChar, sizeof(kBadUtf8ReplacementChar) - 1u);
1939             return false;  // Continue processing.
1940           });
1941       utf = replacement_utf->c_str();
1942       utf8_length = replacement_utf->length();
1943     }
1944     DCHECK_LE(utf16_length, utf8_length);
1945     DCHECK_LE(utf8_length, static_cast<uint32_t>(std::numeric_limits<int32_t>::max()));
1946 
1947     ScopedObjectAccess soa(env);
1948     ObjPtr<mirror::String> result =
1949         mirror::String::AllocFromModifiedUtf8(soa.Self(), utf16_length, utf, utf8_length);
1950     return soa.AddLocalReference<jstring>(result);
1951   }
1952 
GetStringLength(JNIEnv * env,jstring java_string)1953   static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1954     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
1955     ScopedObjectAccess soa(env);
1956     return soa.Decode<mirror::String>(java_string)->GetLength();
1957   }
1958 
GetStringUTFLength(JNIEnv * env,jstring java_string)1959   static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1960     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
1961     ScopedObjectAccess soa(env);
1962     return soa.Decode<mirror::String>(java_string)->GetUtfLength();
1963   }
1964 
GetStringRegion(JNIEnv * env,jstring java_string,jsize start,jsize length,jchar * buf)1965   static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1966                               jchar* buf) {
1967     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1968     ScopedObjectAccess soa(env);
1969     ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
1970     if (start < 0 || length < 0 || length > s->GetLength() - start) {
1971       ThrowSIOOBE(soa, start, length, s->GetLength());
1972     } else {
1973       CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
1974       if (s->IsCompressed()) {
1975         for (int i = 0; i < length; ++i) {
1976           buf[i] = static_cast<jchar>(s->CharAt(start+i));
1977         }
1978       } else {
1979         const jchar* chars = static_cast<jchar*>(s->GetValue());
1980         memcpy(buf, chars + start, length * sizeof(jchar));
1981       }
1982     }
1983   }
1984 
GetStringUTFRegion(JNIEnv * env,jstring java_string,jsize start,jsize length,char * buf)1985   static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1986                                  char* buf) {
1987     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1988     ScopedObjectAccess soa(env);
1989     ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
1990     if (start < 0 || length < 0 || length > s->GetLength() - start) {
1991       ThrowSIOOBE(soa, start, length, s->GetLength());
1992     } else {
1993       CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
1994       if (s->IsCompressed()) {
1995         for (int i = 0; i < length; ++i) {
1996           buf[i] = s->CharAt(start+i);
1997         }
1998       } else {
1999         const jchar* chars = s->GetValue();
2000         size_t bytes = CountUtf8Bytes(chars + start, length);
2001         ConvertUtf16ToModifiedUtf8(buf, bytes, chars + start, length);
2002       }
2003     }
2004   }
2005 
GetStringChars(JNIEnv * env,jstring java_string,jboolean * is_copy)2006   static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
2007     CHECK_NON_NULL_ARGUMENT(java_string);
2008     ScopedObjectAccess soa(env);
2009     ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
2010     gc::Heap* heap = Runtime::Current()->GetHeap();
2011     if (heap->IsMovableObject(s) || s->IsCompressed()) {
2012       jchar* chars = new jchar[s->GetLength()];
2013       if (s->IsCompressed()) {
2014         int32_t length = s->GetLength();
2015         for (int i = 0; i < length; ++i) {
2016           chars[i] = s->CharAt(i);
2017         }
2018       } else {
2019         memcpy(chars, s->GetValue(), sizeof(jchar) * s->GetLength());
2020       }
2021       if (is_copy != nullptr) {
2022         *is_copy = JNI_TRUE;
2023       }
2024       return chars;
2025     }
2026     if (is_copy != nullptr) {
2027       *is_copy = JNI_FALSE;
2028     }
2029     return static_cast<jchar*>(s->GetValue());
2030   }
2031 
ReleaseStringChars(JNIEnv * env,jstring java_string,const jchar * chars)2032   static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
2033     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
2034     ScopedObjectAccess soa(env);
2035     ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
2036     if (s->IsCompressed() || (s->IsCompressed() == false && chars != s->GetValue())) {
2037       delete[] chars;
2038     }
2039   }
2040 
GetStringCritical(JNIEnv * env,jstring java_string,jboolean * is_copy)2041   static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
2042     CHECK_NON_NULL_ARGUMENT(java_string);
2043     ScopedObjectAccess soa(env);
2044     ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
2045     gc::Heap* heap = Runtime::Current()->GetHeap();
2046     if (heap->IsMovableObject(s)) {
2047       StackHandleScope<1> hs(soa.Self());
2048       HandleWrapperObjPtr<mirror::String> h(hs.NewHandleWrapper(&s));
2049       if (!kUseReadBarrier) {
2050         heap->IncrementDisableMovingGC(soa.Self());
2051       } else {
2052         // For the CC collector, we only need to wait for the thread flip rather than the whole GC
2053         // to occur thanks to the to-space invariant.
2054         heap->IncrementDisableThreadFlip(soa.Self());
2055       }
2056     }
2057     if (s->IsCompressed()) {
2058       if (is_copy != nullptr) {
2059         *is_copy = JNI_TRUE;
2060       }
2061       int32_t length = s->GetLength();
2062       jchar* chars = new jchar[length];
2063       for (int i = 0; i < length; ++i) {
2064         chars[i] = s->CharAt(i);
2065       }
2066       return chars;
2067     } else {
2068       if (is_copy != nullptr) {
2069         *is_copy = JNI_FALSE;
2070       }
2071       return static_cast<jchar*>(s->GetValue());
2072     }
2073   }
2074 
ReleaseStringCritical(JNIEnv * env,jstring java_string,const jchar * chars)2075   static void ReleaseStringCritical(JNIEnv* env,
2076                                     jstring java_string,
2077                                     const jchar* chars) {
2078     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
2079     ScopedObjectAccess soa(env);
2080     gc::Heap* heap = Runtime::Current()->GetHeap();
2081     ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
2082     if (heap->IsMovableObject(s)) {
2083       if (!kUseReadBarrier) {
2084         heap->DecrementDisableMovingGC(soa.Self());
2085       } else {
2086         heap->DecrementDisableThreadFlip(soa.Self());
2087       }
2088     }
2089     if (s->IsCompressed() || (s->IsCompressed() == false && s->GetValue() != chars)) {
2090       delete[] chars;
2091     }
2092   }
2093 
GetStringUTFChars(JNIEnv * env,jstring java_string,jboolean * is_copy)2094   static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
2095     if (java_string == nullptr) {
2096       return nullptr;
2097     }
2098     if (is_copy != nullptr) {
2099       *is_copy = JNI_TRUE;
2100     }
2101     ScopedObjectAccess soa(env);
2102     ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
2103     size_t byte_count = s->GetUtfLength();
2104     char* bytes = new char[byte_count + 1];
2105     CHECK(bytes != nullptr);  // bionic aborts anyway.
2106     if (s->IsCompressed()) {
2107       for (size_t i = 0; i < byte_count; ++i) {
2108         bytes[i] = s->CharAt(i);
2109       }
2110     } else {
2111       const uint16_t* chars = s->GetValue();
2112       ConvertUtf16ToModifiedUtf8(bytes, byte_count, chars, s->GetLength());
2113     }
2114     bytes[byte_count] = '\0';
2115     return bytes;
2116   }
2117 
ReleaseStringUTFChars(JNIEnv *,jstring,const char * chars)2118   static void ReleaseStringUTFChars(JNIEnv*, jstring, const char* chars) {
2119     delete[] chars;
2120   }
2121 
GetArrayLength(JNIEnv * env,jarray java_array)2122   static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
2123     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
2124     ScopedObjectAccess soa(env);
2125     ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(java_array);
2126     if (UNLIKELY(!obj->IsArrayInstance())) {
2127       soa.Vm()->JniAbortF("GetArrayLength", "not an array: %s", obj->PrettyTypeOf().c_str());
2128       return 0;
2129     }
2130     ObjPtr<mirror::Array> array = obj->AsArray();
2131     return array->GetLength();
2132   }
2133 
GetObjectArrayElement(JNIEnv * env,jobjectArray java_array,jsize index)2134   static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
2135     CHECK_NON_NULL_ARGUMENT(java_array);
2136     ScopedObjectAccess soa(env);
2137     ObjPtr<mirror::ObjectArray<mirror::Object>> array =
2138         soa.Decode<mirror::ObjectArray<mirror::Object>>(java_array);
2139     return soa.AddLocalReference<jobject>(array->Get(index));
2140   }
2141 
SetObjectArrayElement(JNIEnv * env,jobjectArray java_array,jsize index,jobject java_value)2142   static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2143                                     jobject java_value) {
2144     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2145     ScopedObjectAccess soa(env);
2146     ObjPtr<mirror::ObjectArray<mirror::Object>> array =
2147         soa.Decode<mirror::ObjectArray<mirror::Object>>(java_array);
2148     ObjPtr<mirror::Object> value = soa.Decode<mirror::Object>(java_value);
2149     array->Set<false>(index, value);
2150   }
2151 
NewBooleanArray(JNIEnv * env,jsize length)2152   static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
2153     return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
2154   }
2155 
NewByteArray(JNIEnv * env,jsize length)2156   static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
2157     return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
2158   }
2159 
NewCharArray(JNIEnv * env,jsize length)2160   static jcharArray NewCharArray(JNIEnv* env, jsize length) {
2161     return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
2162   }
2163 
NewDoubleArray(JNIEnv * env,jsize length)2164   static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
2165     return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
2166   }
2167 
NewFloatArray(JNIEnv * env,jsize length)2168   static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
2169     return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
2170   }
2171 
NewIntArray(JNIEnv * env,jsize length)2172   static jintArray NewIntArray(JNIEnv* env, jsize length) {
2173     return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
2174   }
2175 
NewLongArray(JNIEnv * env,jsize length)2176   static jlongArray NewLongArray(JNIEnv* env, jsize length) {
2177     return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
2178   }
2179 
NewObjectArray(JNIEnv * env,jsize length,jclass element_jclass,jobject initial_element)2180   static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2181                                      jobject initial_element) {
2182     if (UNLIKELY(length < 0)) {
2183       JavaVmExtFromEnv(env)->JniAbortF("NewObjectArray", "negative array length: %d", length);
2184       return nullptr;
2185     }
2186     CHECK_NON_NULL_ARGUMENT(element_jclass);
2187 
2188     // Compute the array class corresponding to the given element class.
2189     ScopedObjectAccess soa(env);
2190     ObjPtr<mirror::Class> array_class;
2191     {
2192       ObjPtr<mirror::Class> element_class = soa.Decode<mirror::Class>(element_jclass);
2193       if (UNLIKELY(element_class->IsPrimitive())) {
2194         soa.Vm()->JniAbortF("NewObjectArray",
2195                             "not an object type: %s",
2196                             element_class->PrettyDescriptor().c_str());
2197         return nullptr;
2198       }
2199       ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2200       array_class = class_linker->FindArrayClass(soa.Self(), element_class);
2201       if (UNLIKELY(array_class == nullptr)) {
2202         return nullptr;
2203       }
2204     }
2205 
2206     // Allocate and initialize if necessary.
2207     ObjPtr<mirror::ObjectArray<mirror::Object>> result =
2208         mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
2209     if (result != nullptr && initial_element != nullptr) {
2210       ObjPtr<mirror::Object> initial_object = soa.Decode<mirror::Object>(initial_element);
2211       if (initial_object != nullptr) {
2212         ObjPtr<mirror::Class> element_class = result->GetClass()->GetComponentType();
2213         if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2214           soa.Vm()->JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with "
2215                               "element type of '%s'",
2216                               mirror::Class::PrettyDescriptor(initial_object->GetClass()).c_str(),
2217                               element_class->PrettyDescriptor().c_str());
2218           return nullptr;
2219         } else {
2220           for (jsize i = 0; i < length; ++i) {
2221             result->SetWithoutChecks<false>(i, initial_object);
2222           }
2223         }
2224       }
2225     }
2226     return soa.AddLocalReference<jobjectArray>(result);
2227   }
2228 
NewShortArray(JNIEnv * env,jsize length)2229   static jshortArray NewShortArray(JNIEnv* env, jsize length) {
2230     return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
2231   }
2232 
GetPrimitiveArrayCritical(JNIEnv * env,jarray java_array,jboolean * is_copy)2233   static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
2234     CHECK_NON_NULL_ARGUMENT(java_array);
2235     ScopedObjectAccess soa(env);
2236     ObjPtr<mirror::Array> array = soa.Decode<mirror::Array>(java_array);
2237     if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2238       soa.Vm()->JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
2239                           array->GetClass()->PrettyDescriptor().c_str());
2240       return nullptr;
2241     }
2242     gc::Heap* heap = Runtime::Current()->GetHeap();
2243     if (heap->IsMovableObject(array)) {
2244       if (!kUseReadBarrier) {
2245         heap->IncrementDisableMovingGC(soa.Self());
2246       } else {
2247         // For the CC collector, we only need to wait for the thread flip rather than the whole GC
2248         // to occur thanks to the to-space invariant.
2249         heap->IncrementDisableThreadFlip(soa.Self());
2250       }
2251       // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
2252       array = soa.Decode<mirror::Array>(java_array);
2253     }
2254     if (is_copy != nullptr) {
2255       *is_copy = JNI_FALSE;
2256     }
2257     return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
2258   }
2259 
ReleasePrimitiveArrayCritical(JNIEnv * env,jarray java_array,void * elements,jint mode)2260   static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
2261                                             jint mode) {
2262     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2263     ScopedObjectAccess soa(env);
2264     ObjPtr<mirror::Array> array = soa.Decode<mirror::Array>(java_array);
2265     if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2266       soa.Vm()->JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
2267                           array->GetClass()->PrettyDescriptor().c_str());
2268       return;
2269     }
2270     const size_t component_size = array->GetClass()->GetComponentSize();
2271     ReleasePrimitiveArray(soa, array, component_size, elements, mode);
2272   }
2273 
GetBooleanArrayElements(JNIEnv * env,jbooleanArray array,jboolean * is_copy)2274   static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
2275     return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
2276   }
2277 
GetByteArrayElements(JNIEnv * env,jbyteArray array,jboolean * is_copy)2278   static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
2279     return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
2280   }
2281 
GetCharArrayElements(JNIEnv * env,jcharArray array,jboolean * is_copy)2282   static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
2283     return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
2284   }
2285 
GetDoubleArrayElements(JNIEnv * env,jdoubleArray array,jboolean * is_copy)2286   static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
2287     return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
2288   }
2289 
GetFloatArrayElements(JNIEnv * env,jfloatArray array,jboolean * is_copy)2290   static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
2291     return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
2292   }
2293 
GetIntArrayElements(JNIEnv * env,jintArray array,jboolean * is_copy)2294   static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
2295     return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
2296   }
2297 
GetLongArrayElements(JNIEnv * env,jlongArray array,jboolean * is_copy)2298   static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
2299     return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
2300   }
2301 
GetShortArrayElements(JNIEnv * env,jshortArray array,jboolean * is_copy)2302   static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
2303     return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
2304   }
2305 
ReleaseBooleanArrayElements(JNIEnv * env,jbooleanArray array,jboolean * elements,jint mode)2306   static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2307                                           jint mode) {
2308     ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
2309                                                                          mode);
2310   }
2311 
ReleaseByteArrayElements(JNIEnv * env,jbyteArray array,jbyte * elements,jint mode)2312   static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
2313     ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
2314   }
2315 
ReleaseCharArrayElements(JNIEnv * env,jcharArray array,jchar * elements,jint mode)2316   static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
2317     ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
2318   }
2319 
ReleaseDoubleArrayElements(JNIEnv * env,jdoubleArray array,jdouble * elements,jint mode)2320   static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2321                                          jint mode) {
2322     ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
2323   }
2324 
ReleaseFloatArrayElements(JNIEnv * env,jfloatArray array,jfloat * elements,jint mode)2325   static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2326                                         jint mode) {
2327     ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
2328   }
2329 
ReleaseIntArrayElements(JNIEnv * env,jintArray array,jint * elements,jint mode)2330   static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
2331     ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
2332   }
2333 
ReleaseLongArrayElements(JNIEnv * env,jlongArray array,jlong * elements,jint mode)2334   static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
2335     ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
2336   }
2337 
ReleaseShortArrayElements(JNIEnv * env,jshortArray array,jshort * elements,jint mode)2338   static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2339                                         jint mode) {
2340     ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
2341   }
2342 
GetBooleanArrayRegion(JNIEnv * env,jbooleanArray array,jsize start,jsize length,jboolean * buf)2343   static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2344                                     jboolean* buf) {
2345     GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
2346                                                                            length, buf);
2347   }
2348 
GetByteArrayRegion(JNIEnv * env,jbyteArray array,jsize start,jsize length,jbyte * buf)2349   static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2350                                  jbyte* buf) {
2351     GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
2352   }
2353 
GetCharArrayRegion(JNIEnv * env,jcharArray array,jsize start,jsize length,jchar * buf)2354   static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2355                                  jchar* buf) {
2356     GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
2357   }
2358 
GetDoubleArrayRegion(JNIEnv * env,jdoubleArray array,jsize start,jsize length,jdouble * buf)2359   static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2360                                    jdouble* buf) {
2361     GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
2362                                                                         buf);
2363   }
2364 
GetFloatArrayRegion(JNIEnv * env,jfloatArray array,jsize start,jsize length,jfloat * buf)2365   static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2366                                   jfloat* buf) {
2367     GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
2368                                                                      buf);
2369   }
2370 
GetIntArrayRegion(JNIEnv * env,jintArray array,jsize start,jsize length,jint * buf)2371   static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2372                                 jint* buf) {
2373     GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
2374   }
2375 
GetLongArrayRegion(JNIEnv * env,jlongArray array,jsize start,jsize length,jlong * buf)2376   static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2377                                  jlong* buf) {
2378     GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
2379   }
2380 
GetShortArrayRegion(JNIEnv * env,jshortArray array,jsize start,jsize length,jshort * buf)2381   static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2382                                   jshort* buf) {
2383     GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
2384                                                                      buf);
2385   }
2386 
SetBooleanArrayRegion(JNIEnv * env,jbooleanArray array,jsize start,jsize length,const jboolean * buf)2387   static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2388                                     const jboolean* buf) {
2389     SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
2390                                                                            length, buf);
2391   }
2392 
SetByteArrayRegion(JNIEnv * env,jbyteArray array,jsize start,jsize length,const jbyte * buf)2393   static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2394                                  const jbyte* buf) {
2395     SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
2396   }
2397 
SetCharArrayRegion(JNIEnv * env,jcharArray array,jsize start,jsize length,const jchar * buf)2398   static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2399                                  const jchar* buf) {
2400     SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
2401   }
2402 
SetDoubleArrayRegion(JNIEnv * env,jdoubleArray array,jsize start,jsize length,const jdouble * buf)2403   static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2404                                    const jdouble* buf) {
2405     SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
2406                                                                         buf);
2407   }
2408 
SetFloatArrayRegion(JNIEnv * env,jfloatArray array,jsize start,jsize length,const jfloat * buf)2409   static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2410                                   const jfloat* buf) {
2411     SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
2412                                                                      buf);
2413   }
2414 
SetIntArrayRegion(JNIEnv * env,jintArray array,jsize start,jsize length,const jint * buf)2415   static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2416                                 const jint* buf) {
2417     SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
2418   }
2419 
SetLongArrayRegion(JNIEnv * env,jlongArray array,jsize start,jsize length,const jlong * buf)2420   static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2421                                  const jlong* buf) {
2422     SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
2423   }
2424 
SetShortArrayRegion(JNIEnv * env,jshortArray array,jsize start,jsize length,const jshort * buf)2425   static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2426                                   const jshort* buf) {
2427     SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
2428                                                                      buf);
2429   }
2430 
RegisterNatives(JNIEnv * env,jclass java_class,const JNINativeMethod * methods,jint method_count)2431   static jint RegisterNatives(JNIEnv* env,
2432                               jclass java_class,
2433                               const JNINativeMethod* methods,
2434                               jint method_count) {
2435     if (UNLIKELY(method_count < 0)) {
2436       JavaVmExtFromEnv(env)->JniAbortF("RegisterNatives", "negative method count: %d",
2437                                        method_count);
2438       return JNI_ERR;  // Not reached except in unit tests.
2439     }
2440     CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
2441     ScopedObjectAccess soa(env);
2442     StackHandleScope<1> hs(soa.Self());
2443     Handle<mirror::Class> c = hs.NewHandle(soa.Decode<mirror::Class>(java_class));
2444     if (UNLIKELY(method_count == 0)) {
2445       LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2446           << c->PrettyDescriptor();
2447       return JNI_OK;
2448     }
2449     CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
2450     for (jint i = 0; i < method_count; ++i) {
2451       const char* name = methods[i].name;
2452       const char* sig = methods[i].signature;
2453       const void* fnPtr = methods[i].fnPtr;
2454       if (UNLIKELY(name == nullptr)) {
2455         ReportInvalidJNINativeMethod(soa, c.Get(), "method name", i);
2456         return JNI_ERR;
2457       } else if (UNLIKELY(sig == nullptr)) {
2458         ReportInvalidJNINativeMethod(soa, c.Get(), "method signature", i);
2459         return JNI_ERR;
2460       } else if (UNLIKELY(fnPtr == nullptr)) {
2461         ReportInvalidJNINativeMethod(soa, c.Get(), "native function", i);
2462         return JNI_ERR;
2463       }
2464       bool is_fast = false;
2465       // Notes about fast JNI calls:
2466       //
2467       // On a normal JNI call, the calling thread usually transitions
2468       // from the kRunnable state to the kNative state. But if the
2469       // called native function needs to access any Java object, it
2470       // will have to transition back to the kRunnable state.
2471       //
2472       // There is a cost to this double transition. For a JNI call
2473       // that should be quick, this cost may dominate the call cost.
2474       //
2475       // On a fast JNI call, the calling thread avoids this double
2476       // transition by not transitioning from kRunnable to kNative and
2477       // stays in the kRunnable state.
2478       //
2479       // There are risks to using a fast JNI call because it can delay
2480       // a response to a thread suspension request which is typically
2481       // used for a GC root scanning, etc. If a fast JNI call takes a
2482       // long time, it could cause longer thread suspension latency
2483       // and GC pauses.
2484       //
2485       // Thus, fast JNI should be used with care. It should be used
2486       // for a JNI call that takes a short amount of time (eg. no
2487       // long-running loop) and does not block (eg. no locks, I/O,
2488       // etc.)
2489       //
2490       // A '!' prefix in the signature in the JNINativeMethod
2491       // indicates that it's a fast JNI call and the runtime omits the
2492       // thread state transition from kRunnable to kNative at the
2493       // entry.
2494       if (*sig == '!') {
2495         is_fast = true;
2496         ++sig;
2497       }
2498 
2499       // Note: the right order is to try to find the method locally
2500       // first, either as a direct or a virtual method. Then move to
2501       // the parent.
2502       ArtMethod* m = nullptr;
2503       bool warn_on_going_to_parent = down_cast<JNIEnvExt*>(env)->GetVm()->IsCheckJniEnabled();
2504       for (ObjPtr<mirror::Class> current_class = c.Get();
2505            current_class != nullptr;
2506            current_class = current_class->GetSuperClass()) {
2507         // Search first only comparing methods which are native.
2508         m = FindMethod<true>(current_class, name, sig);
2509         if (m != nullptr) {
2510           break;
2511         }
2512 
2513         // Search again comparing to all methods, to find non-native methods that match.
2514         m = FindMethod<false>(current_class, name, sig);
2515         if (m != nullptr) {
2516           break;
2517         }
2518 
2519         if (warn_on_going_to_parent) {
2520           LOG(WARNING) << "CheckJNI: method to register \"" << name << "\" not in the given class. "
2521                        << "This is slow, consider changing your RegisterNatives calls.";
2522           warn_on_going_to_parent = false;
2523         }
2524       }
2525 
2526       if (m == nullptr) {
2527         c->DumpClass(LOG_STREAM(ERROR), mirror::Class::kDumpClassFullDetail);
2528         LOG(ERROR)
2529             << "Failed to register native method "
2530             << c->PrettyDescriptor() << "." << name << sig << " in "
2531             << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
2532         ThrowNoSuchMethodError(soa, c.Get(), name, sig, "static or non-static");
2533         return JNI_ERR;
2534       } else if (!m->IsNative()) {
2535         LOG(ERROR)
2536             << "Failed to register non-native method "
2537             << c->PrettyDescriptor() << "." << name << sig
2538             << " as native";
2539         ThrowNoSuchMethodError(soa, c.Get(), name, sig, "native");
2540         return JNI_ERR;
2541       }
2542 
2543       VLOG(jni) << "[Registering JNI native method " << m->PrettyMethod() << "]";
2544 
2545       if (UNLIKELY(is_fast)) {
2546         // There are a few reasons to switch:
2547         // 1) We don't support !bang JNI anymore, it will turn to a hard error later.
2548         // 2) @FastNative is actually faster. At least 1.5x faster than !bang JNI.
2549         //    and switching is super easy, remove ! in C code, add annotation in .java code.
2550         // 3) Good chance of hitting DCHECK failures in ScopedFastNativeObjectAccess
2551         //    since that checks for presence of @FastNative and not for ! in the descriptor.
2552         LOG(WARNING) << "!bang JNI is deprecated. Switch to @FastNative for " << m->PrettyMethod();
2553         is_fast = false;
2554         // TODO: make this a hard register error in the future.
2555       }
2556 
2557       const void* final_function_ptr = m->RegisterNative(fnPtr);
2558       UNUSED(final_function_ptr);
2559     }
2560     return JNI_OK;
2561   }
2562 
UnregisterNatives(JNIEnv * env,jclass java_class)2563   static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
2564     CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
2565     ScopedObjectAccess soa(env);
2566     ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(java_class);
2567 
2568     VLOG(jni) << "[Unregistering JNI native methods for " << mirror::Class::PrettyClass(c) << "]";
2569 
2570     size_t unregistered_count = 0;
2571     auto pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2572     for (auto& m : c->GetMethods(pointer_size)) {
2573       if (m.IsNative()) {
2574         m.UnregisterNative();
2575         unregistered_count++;
2576       }
2577     }
2578 
2579     if (unregistered_count == 0) {
2580       LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2581           << mirror::Class::PrettyDescriptor(c) << "' that contains no native methods";
2582     }
2583     return JNI_OK;
2584   }
2585 
MonitorEnter(JNIEnv * env,jobject java_object)2586   static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
2587     CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
2588     ScopedObjectAccess soa(env);
2589     ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(java_object);
2590     o = o->MonitorEnter(soa.Self());
2591     if (soa.Self()->HoldsLock(o)) {
2592       soa.Env()->monitors_.Add(o);
2593     }
2594     if (soa.Self()->IsExceptionPending()) {
2595       return JNI_ERR;
2596     }
2597     return JNI_OK;
2598   }
2599 
MonitorExit(JNIEnv * env,jobject java_object)2600   static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
2601     CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
2602     ScopedObjectAccess soa(env);
2603     ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(java_object);
2604     bool remove_mon = soa.Self()->HoldsLock(o);
2605     o->MonitorExit(soa.Self());
2606     if (remove_mon) {
2607       soa.Env()->monitors_.Remove(o);
2608     }
2609     if (soa.Self()->IsExceptionPending()) {
2610       return JNI_ERR;
2611     }
2612     return JNI_OK;
2613   }
2614 
GetJavaVM(JNIEnv * env,JavaVM ** vm)2615   static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2616     CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
2617     Runtime* runtime = Runtime::Current();
2618     if (runtime != nullptr) {
2619       *vm = runtime->GetJavaVM();
2620     } else {
2621       *vm = nullptr;
2622     }
2623     return (*vm != nullptr) ? JNI_OK : JNI_ERR;
2624   }
2625 
NewDirectByteBuffer(JNIEnv * env,void * address,jlong capacity)2626   static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2627     if (capacity < 0) {
2628       JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64,
2629                                        capacity);
2630       return nullptr;
2631     }
2632     if (address == nullptr && capacity != 0) {
2633       JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2634                                        "non-zero capacity for nullptr pointer: %" PRId64, capacity);
2635       return nullptr;
2636     }
2637 
2638     // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
2639     if (capacity > INT_MAX) {
2640       JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2641                                        "buffer capacity greater than maximum jint: %" PRId64,
2642                                        capacity);
2643       return nullptr;
2644     }
2645     jlong address_arg = reinterpret_cast<jlong>(address);
2646     jint capacity_arg = static_cast<jint>(capacity);
2647 
2648     jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2649                                     WellKnownClasses::java_nio_DirectByteBuffer_init,
2650                                     address_arg, capacity_arg);
2651     return static_cast<JNIEnvExt*>(env)->self_->IsExceptionPending() ? nullptr : result;
2652   }
2653 
GetDirectBufferAddress(JNIEnv * env,jobject java_buffer)2654   static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
2655     return reinterpret_cast<void*>(env->GetLongField(
2656         java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
2657   }
2658 
GetDirectBufferCapacity(JNIEnv * env,jobject java_buffer)2659   static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
2660     return static_cast<jlong>(env->GetIntField(
2661         java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
2662   }
2663 
GetObjectRefType(JNIEnv * env ATTRIBUTE_UNUSED,jobject java_object)2664   static jobjectRefType GetObjectRefType(JNIEnv* env ATTRIBUTE_UNUSED, jobject java_object) {
2665     if (java_object == nullptr) {
2666       return JNIInvalidRefType;
2667     }
2668 
2669     // Do we definitely know what kind of reference this is?
2670     IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2671     IndirectRefKind kind = IndirectReferenceTable::GetIndirectRefKind(ref);
2672     switch (kind) {
2673     case kLocal:
2674       return JNILocalRefType;
2675     case kGlobal:
2676       return JNIGlobalRefType;
2677     case kWeakGlobal:
2678       return JNIWeakGlobalRefType;
2679     case kHandleScopeOrInvalid:
2680       // Assume value is in a handle scope.
2681       return JNILocalRefType;
2682     }
2683     LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2684     UNREACHABLE();
2685   }
2686 
2687  private:
EnsureLocalCapacityInternal(ScopedObjectAccess & soa,jint desired_capacity,const char * caller)2688   static jint EnsureLocalCapacityInternal(ScopedObjectAccess& soa, jint desired_capacity,
2689                                           const char* caller)
2690       REQUIRES_SHARED(Locks::mutator_lock_) {
2691     if (desired_capacity < 0) {
2692       LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2693       return JNI_ERR;
2694     }
2695 
2696     std::string error_msg;
2697     if (!soa.Env()->locals_.EnsureFreeCapacity(static_cast<size_t>(desired_capacity), &error_msg)) {
2698       std::string caller_error = android::base::StringPrintf("%s: %s", caller, error_msg.c_str());
2699       soa.Self()->ThrowOutOfMemoryError(caller_error.c_str());
2700       return JNI_ERR;
2701     }
2702     return JNI_OK;
2703   }
2704 
2705   template<typename JniT, typename ArtT>
NewPrimitiveArray(JNIEnv * env,jsize length)2706   static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
2707     ScopedObjectAccess soa(env);
2708     if (UNLIKELY(length < 0)) {
2709       soa.Vm()->JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
2710       return nullptr;
2711     }
2712     ObjPtr<ArtT> result = ArtT::Alloc(soa.Self(), length);
2713     return soa.AddLocalReference<JniT>(result);
2714   }
2715 
2716   template <typename JArrayT, typename ElementT, typename ArtArrayT>
DecodeAndCheckArrayType(ScopedObjectAccess & soa,JArrayT java_array,const char * fn_name,const char * operation)2717   static ObjPtr<ArtArrayT> DecodeAndCheckArrayType(ScopedObjectAccess& soa,
2718                                                    JArrayT java_array,
2719                                                    const char* fn_name,
2720                                                    const char* operation)
2721       REQUIRES_SHARED(Locks::mutator_lock_) {
2722     ObjPtr<ArtArrayT> array = soa.Decode<ArtArrayT>(java_array);
2723     ObjPtr<mirror::Class> expected_array_class = GetClassRoot<ArtArrayT>();
2724     if (UNLIKELY(expected_array_class != array->GetClass())) {
2725       soa.Vm()->JniAbortF(fn_name,
2726                           "attempt to %s %s primitive array elements with an object of type %s",
2727                           operation,
2728                           mirror::Class::PrettyDescriptor(
2729                               expected_array_class->GetComponentType()).c_str(),
2730                           mirror::Class::PrettyDescriptor(array->GetClass()).c_str());
2731       return nullptr;
2732     }
2733     DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2734     return array;
2735   }
2736 
2737   template <typename ArrayT, typename ElementT, typename ArtArrayT>
GetPrimitiveArray(JNIEnv * env,ArrayT java_array,jboolean * is_copy)2738   static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2739     CHECK_NON_NULL_ARGUMENT(java_array);
2740     ScopedObjectAccess soa(env);
2741     ObjPtr<ArtArrayT> array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(
2742         soa, java_array, "GetArrayElements", "get");
2743     if (UNLIKELY(array == nullptr)) {
2744       return nullptr;
2745     }
2746     // Only make a copy if necessary.
2747     if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2748       if (is_copy != nullptr) {
2749         *is_copy = JNI_TRUE;
2750       }
2751       const size_t component_size = sizeof(ElementT);
2752       size_t size = array->GetLength() * component_size;
2753       void* data = new uint64_t[RoundUp(size, 8) / 8];
2754       memcpy(data, array->GetData(), size);
2755       return reinterpret_cast<ElementT*>(data);
2756     } else {
2757       if (is_copy != nullptr) {
2758         *is_copy = JNI_FALSE;
2759       }
2760       return reinterpret_cast<ElementT*>(array->GetData());
2761     }
2762   }
2763 
2764   template <typename ArrayT, typename ElementT, typename ArtArrayT>
ReleasePrimitiveArray(JNIEnv * env,ArrayT java_array,ElementT * elements,jint mode)2765   static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
2766     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2767     ScopedObjectAccess soa(env);
2768     ObjPtr<ArtArrayT> array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(
2769         soa, java_array, "ReleaseArrayElements", "release");
2770     if (array == nullptr) {
2771       return;
2772     }
2773     ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2774   }
2775 
ReleasePrimitiveArray(ScopedObjectAccess & soa,ObjPtr<mirror::Array> array,size_t component_size,void * elements,jint mode)2776   static void ReleasePrimitiveArray(ScopedObjectAccess& soa,
2777                                     ObjPtr<mirror::Array> array,
2778                                     size_t component_size,
2779                                     void* elements,
2780                                     jint mode)
2781       REQUIRES_SHARED(Locks::mutator_lock_) {
2782     void* array_data = array->GetRawData(component_size, 0);
2783     gc::Heap* heap = Runtime::Current()->GetHeap();
2784     bool is_copy = array_data != elements;
2785     size_t bytes = array->GetLength() * component_size;
2786     if (is_copy) {
2787       // Sanity check: If elements is not the same as the java array's data, it better not be a
2788       // heap address. TODO: This might be slow to check, may be worth keeping track of which
2789       // copies we make?
2790       if (heap->IsNonDiscontinuousSpaceHeapAddress(elements)) {
2791         soa.Vm()->JniAbortF("ReleaseArrayElements",
2792                             "invalid element pointer %p, array elements are %p",
2793                             reinterpret_cast<void*>(elements), array_data);
2794         return;
2795       }
2796       if (mode != JNI_ABORT) {
2797         memcpy(array_data, elements, bytes);
2798       } else if (kWarnJniAbort && memcmp(array_data, elements, bytes) != 0) {
2799         // Warn if we have JNI_ABORT and the arrays don't match since this is usually an error.
2800         LOG(WARNING) << "Possible incorrect JNI_ABORT in Release*ArrayElements";
2801         soa.Self()->DumpJavaStack(LOG_STREAM(WARNING));
2802       }
2803     }
2804     if (mode != JNI_COMMIT) {
2805       if (is_copy) {
2806         delete[] reinterpret_cast<uint64_t*>(elements);
2807       } else if (heap->IsMovableObject(array)) {
2808         // Non copy to a movable object must means that we had disabled the moving GC.
2809         if (!kUseReadBarrier) {
2810           heap->DecrementDisableMovingGC(soa.Self());
2811         } else {
2812           heap->DecrementDisableThreadFlip(soa.Self());
2813         }
2814       }
2815     }
2816   }
2817 
2818   template <typename JArrayT, typename ElementT, typename ArtArrayT>
GetPrimitiveArrayRegion(JNIEnv * env,JArrayT java_array,jsize start,jsize length,ElementT * buf)2819   static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2820                                       jsize start, jsize length, ElementT* buf) {
2821     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2822     ScopedObjectAccess soa(env);
2823     ObjPtr<ArtArrayT> array = DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(
2824         soa, java_array, "GetPrimitiveArrayRegion", "get region of");
2825     if (array != nullptr) {
2826       if (start < 0 || length < 0 || length > array->GetLength() - start) {
2827         ThrowAIOOBE(soa, array, start, length, "src");
2828       } else {
2829         CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2830         ElementT* data = array->GetData();
2831         memcpy(buf, data + start, length * sizeof(ElementT));
2832       }
2833     }
2834   }
2835 
2836   template <typename JArrayT, typename ElementT, typename ArtArrayT>
SetPrimitiveArrayRegion(JNIEnv * env,JArrayT java_array,jsize start,jsize length,const ElementT * buf)2837   static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2838                                       jsize start, jsize length, const ElementT* buf) {
2839     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2840     ScopedObjectAccess soa(env);
2841     ObjPtr<ArtArrayT> array = DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(
2842         soa, java_array, "SetPrimitiveArrayRegion", "set region of");
2843     if (array != nullptr) {
2844       if (start < 0 || length < 0 || length > array->GetLength() - start) {
2845         ThrowAIOOBE(soa, array, start, length, "dst");
2846       } else {
2847         CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2848         ElementT* data = array->GetData();
2849         memcpy(data + start, buf, length * sizeof(ElementT));
2850       }
2851     }
2852   }
2853 };
2854 
2855 template<bool kEnableIndexIds>
2856 struct JniNativeInterfaceFunctions {
2857   using JNIImpl = JNI<kEnableIndexIds>;
2858   static constexpr JNINativeInterface gJniNativeInterface = {
2859     nullptr,  // reserved0.
2860     nullptr,  // reserved1.
2861     nullptr,  // reserved2.
2862     nullptr,  // reserved3.
2863     JNIImpl::GetVersion,
2864     JNIImpl::DefineClass,
2865     JNIImpl::FindClass,
2866     JNIImpl::FromReflectedMethod,
2867     JNIImpl::FromReflectedField,
2868     JNIImpl::ToReflectedMethod,
2869     JNIImpl::GetSuperclass,
2870     JNIImpl::IsAssignableFrom,
2871     JNIImpl::ToReflectedField,
2872     JNIImpl::Throw,
2873     JNIImpl::ThrowNew,
2874     JNIImpl::ExceptionOccurred,
2875     JNIImpl::ExceptionDescribe,
2876     JNIImpl::ExceptionClear,
2877     JNIImpl::FatalError,
2878     JNIImpl::PushLocalFrame,
2879     JNIImpl::PopLocalFrame,
2880     JNIImpl::NewGlobalRef,
2881     JNIImpl::DeleteGlobalRef,
2882     JNIImpl::DeleteLocalRef,
2883     JNIImpl::IsSameObject,
2884     JNIImpl::NewLocalRef,
2885     JNIImpl::EnsureLocalCapacity,
2886     JNIImpl::AllocObject,
2887     JNIImpl::NewObject,
2888     JNIImpl::NewObjectV,
2889     JNIImpl::NewObjectA,
2890     JNIImpl::GetObjectClass,
2891     JNIImpl::IsInstanceOf,
2892     JNIImpl::GetMethodID,
2893     JNIImpl::CallObjectMethod,
2894     JNIImpl::CallObjectMethodV,
2895     JNIImpl::CallObjectMethodA,
2896     JNIImpl::CallBooleanMethod,
2897     JNIImpl::CallBooleanMethodV,
2898     JNIImpl::CallBooleanMethodA,
2899     JNIImpl::CallByteMethod,
2900     JNIImpl::CallByteMethodV,
2901     JNIImpl::CallByteMethodA,
2902     JNIImpl::CallCharMethod,
2903     JNIImpl::CallCharMethodV,
2904     JNIImpl::CallCharMethodA,
2905     JNIImpl::CallShortMethod,
2906     JNIImpl::CallShortMethodV,
2907     JNIImpl::CallShortMethodA,
2908     JNIImpl::CallIntMethod,
2909     JNIImpl::CallIntMethodV,
2910     JNIImpl::CallIntMethodA,
2911     JNIImpl::CallLongMethod,
2912     JNIImpl::CallLongMethodV,
2913     JNIImpl::CallLongMethodA,
2914     JNIImpl::CallFloatMethod,
2915     JNIImpl::CallFloatMethodV,
2916     JNIImpl::CallFloatMethodA,
2917     JNIImpl::CallDoubleMethod,
2918     JNIImpl::CallDoubleMethodV,
2919     JNIImpl::CallDoubleMethodA,
2920     JNIImpl::CallVoidMethod,
2921     JNIImpl::CallVoidMethodV,
2922     JNIImpl::CallVoidMethodA,
2923     JNIImpl::CallNonvirtualObjectMethod,
2924     JNIImpl::CallNonvirtualObjectMethodV,
2925     JNIImpl::CallNonvirtualObjectMethodA,
2926     JNIImpl::CallNonvirtualBooleanMethod,
2927     JNIImpl::CallNonvirtualBooleanMethodV,
2928     JNIImpl::CallNonvirtualBooleanMethodA,
2929     JNIImpl::CallNonvirtualByteMethod,
2930     JNIImpl::CallNonvirtualByteMethodV,
2931     JNIImpl::CallNonvirtualByteMethodA,
2932     JNIImpl::CallNonvirtualCharMethod,
2933     JNIImpl::CallNonvirtualCharMethodV,
2934     JNIImpl::CallNonvirtualCharMethodA,
2935     JNIImpl::CallNonvirtualShortMethod,
2936     JNIImpl::CallNonvirtualShortMethodV,
2937     JNIImpl::CallNonvirtualShortMethodA,
2938     JNIImpl::CallNonvirtualIntMethod,
2939     JNIImpl::CallNonvirtualIntMethodV,
2940     JNIImpl::CallNonvirtualIntMethodA,
2941     JNIImpl::CallNonvirtualLongMethod,
2942     JNIImpl::CallNonvirtualLongMethodV,
2943     JNIImpl::CallNonvirtualLongMethodA,
2944     JNIImpl::CallNonvirtualFloatMethod,
2945     JNIImpl::CallNonvirtualFloatMethodV,
2946     JNIImpl::CallNonvirtualFloatMethodA,
2947     JNIImpl::CallNonvirtualDoubleMethod,
2948     JNIImpl::CallNonvirtualDoubleMethodV,
2949     JNIImpl::CallNonvirtualDoubleMethodA,
2950     JNIImpl::CallNonvirtualVoidMethod,
2951     JNIImpl::CallNonvirtualVoidMethodV,
2952     JNIImpl::CallNonvirtualVoidMethodA,
2953     JNIImpl::GetFieldID,
2954     JNIImpl::GetObjectField,
2955     JNIImpl::GetBooleanField,
2956     JNIImpl::GetByteField,
2957     JNIImpl::GetCharField,
2958     JNIImpl::GetShortField,
2959     JNIImpl::GetIntField,
2960     JNIImpl::GetLongField,
2961     JNIImpl::GetFloatField,
2962     JNIImpl::GetDoubleField,
2963     JNIImpl::SetObjectField,
2964     JNIImpl::SetBooleanField,
2965     JNIImpl::SetByteField,
2966     JNIImpl::SetCharField,
2967     JNIImpl::SetShortField,
2968     JNIImpl::SetIntField,
2969     JNIImpl::SetLongField,
2970     JNIImpl::SetFloatField,
2971     JNIImpl::SetDoubleField,
2972     JNIImpl::GetStaticMethodID,
2973     JNIImpl::CallStaticObjectMethod,
2974     JNIImpl::CallStaticObjectMethodV,
2975     JNIImpl::CallStaticObjectMethodA,
2976     JNIImpl::CallStaticBooleanMethod,
2977     JNIImpl::CallStaticBooleanMethodV,
2978     JNIImpl::CallStaticBooleanMethodA,
2979     JNIImpl::CallStaticByteMethod,
2980     JNIImpl::CallStaticByteMethodV,
2981     JNIImpl::CallStaticByteMethodA,
2982     JNIImpl::CallStaticCharMethod,
2983     JNIImpl::CallStaticCharMethodV,
2984     JNIImpl::CallStaticCharMethodA,
2985     JNIImpl::CallStaticShortMethod,
2986     JNIImpl::CallStaticShortMethodV,
2987     JNIImpl::CallStaticShortMethodA,
2988     JNIImpl::CallStaticIntMethod,
2989     JNIImpl::CallStaticIntMethodV,
2990     JNIImpl::CallStaticIntMethodA,
2991     JNIImpl::CallStaticLongMethod,
2992     JNIImpl::CallStaticLongMethodV,
2993     JNIImpl::CallStaticLongMethodA,
2994     JNIImpl::CallStaticFloatMethod,
2995     JNIImpl::CallStaticFloatMethodV,
2996     JNIImpl::CallStaticFloatMethodA,
2997     JNIImpl::CallStaticDoubleMethod,
2998     JNIImpl::CallStaticDoubleMethodV,
2999     JNIImpl::CallStaticDoubleMethodA,
3000     JNIImpl::CallStaticVoidMethod,
3001     JNIImpl::CallStaticVoidMethodV,
3002     JNIImpl::CallStaticVoidMethodA,
3003     JNIImpl::GetStaticFieldID,
3004     JNIImpl::GetStaticObjectField,
3005     JNIImpl::GetStaticBooleanField,
3006     JNIImpl::GetStaticByteField,
3007     JNIImpl::GetStaticCharField,
3008     JNIImpl::GetStaticShortField,
3009     JNIImpl::GetStaticIntField,
3010     JNIImpl::GetStaticLongField,
3011     JNIImpl::GetStaticFloatField,
3012     JNIImpl::GetStaticDoubleField,
3013     JNIImpl::SetStaticObjectField,
3014     JNIImpl::SetStaticBooleanField,
3015     JNIImpl::SetStaticByteField,
3016     JNIImpl::SetStaticCharField,
3017     JNIImpl::SetStaticShortField,
3018     JNIImpl::SetStaticIntField,
3019     JNIImpl::SetStaticLongField,
3020     JNIImpl::SetStaticFloatField,
3021     JNIImpl::SetStaticDoubleField,
3022     JNIImpl::NewString,
3023     JNIImpl::GetStringLength,
3024     JNIImpl::GetStringChars,
3025     JNIImpl::ReleaseStringChars,
3026     JNIImpl::NewStringUTF,
3027     JNIImpl::GetStringUTFLength,
3028     JNIImpl::GetStringUTFChars,
3029     JNIImpl::ReleaseStringUTFChars,
3030     JNIImpl::GetArrayLength,
3031     JNIImpl::NewObjectArray,
3032     JNIImpl::GetObjectArrayElement,
3033     JNIImpl::SetObjectArrayElement,
3034     JNIImpl::NewBooleanArray,
3035     JNIImpl::NewByteArray,
3036     JNIImpl::NewCharArray,
3037     JNIImpl::NewShortArray,
3038     JNIImpl::NewIntArray,
3039     JNIImpl::NewLongArray,
3040     JNIImpl::NewFloatArray,
3041     JNIImpl::NewDoubleArray,
3042     JNIImpl::GetBooleanArrayElements,
3043     JNIImpl::GetByteArrayElements,
3044     JNIImpl::GetCharArrayElements,
3045     JNIImpl::GetShortArrayElements,
3046     JNIImpl::GetIntArrayElements,
3047     JNIImpl::GetLongArrayElements,
3048     JNIImpl::GetFloatArrayElements,
3049     JNIImpl::GetDoubleArrayElements,
3050     JNIImpl::ReleaseBooleanArrayElements,
3051     JNIImpl::ReleaseByteArrayElements,
3052     JNIImpl::ReleaseCharArrayElements,
3053     JNIImpl::ReleaseShortArrayElements,
3054     JNIImpl::ReleaseIntArrayElements,
3055     JNIImpl::ReleaseLongArrayElements,
3056     JNIImpl::ReleaseFloatArrayElements,
3057     JNIImpl::ReleaseDoubleArrayElements,
3058     JNIImpl::GetBooleanArrayRegion,
3059     JNIImpl::GetByteArrayRegion,
3060     JNIImpl::GetCharArrayRegion,
3061     JNIImpl::GetShortArrayRegion,
3062     JNIImpl::GetIntArrayRegion,
3063     JNIImpl::GetLongArrayRegion,
3064     JNIImpl::GetFloatArrayRegion,
3065     JNIImpl::GetDoubleArrayRegion,
3066     JNIImpl::SetBooleanArrayRegion,
3067     JNIImpl::SetByteArrayRegion,
3068     JNIImpl::SetCharArrayRegion,
3069     JNIImpl::SetShortArrayRegion,
3070     JNIImpl::SetIntArrayRegion,
3071     JNIImpl::SetLongArrayRegion,
3072     JNIImpl::SetFloatArrayRegion,
3073     JNIImpl::SetDoubleArrayRegion,
3074     JNIImpl::RegisterNatives,
3075     JNIImpl::UnregisterNatives,
3076     JNIImpl::MonitorEnter,
3077     JNIImpl::MonitorExit,
3078     JNIImpl::GetJavaVM,
3079     JNIImpl::GetStringRegion,
3080     JNIImpl::GetStringUTFRegion,
3081     JNIImpl::GetPrimitiveArrayCritical,
3082     JNIImpl::ReleasePrimitiveArrayCritical,
3083     JNIImpl::GetStringCritical,
3084     JNIImpl::ReleaseStringCritical,
3085     JNIImpl::NewWeakGlobalRef,
3086     JNIImpl::DeleteWeakGlobalRef,
3087     JNIImpl::ExceptionCheck,
3088     JNIImpl::NewDirectByteBuffer,
3089     JNIImpl::GetDirectBufferAddress,
3090     JNIImpl::GetDirectBufferCapacity,
3091     JNIImpl::GetObjectRefType,
3092   };
3093 };
3094 
GetJniNativeInterface()3095 const JNINativeInterface* GetJniNativeInterface() {
3096   // The template argument is passed down through the Encode/DecodeArtMethod/Field calls so if
3097   // JniIdType is kPointer the calls will be a simple cast with no branches. This ensures that
3098   // the normal case is still fast.
3099   return Runtime::Current()->GetJniIdType() == JniIdType::kPointer
3100              ? &JniNativeInterfaceFunctions<false>::gJniNativeInterface
3101              : &JniNativeInterfaceFunctions<true>::gJniNativeInterface;
3102 }
3103 
3104 void (*gJniSleepForeverStub[])()  = {
3105   nullptr,  // reserved0.
3106   nullptr,  // reserved1.
3107   nullptr,  // reserved2.
3108   nullptr,  // reserved3.
3109   SleepForever,
3110   SleepForever,
3111   SleepForever,
3112   SleepForever,
3113   SleepForever,
3114   SleepForever,
3115   SleepForever,
3116   SleepForever,
3117   SleepForever,
3118   SleepForever,
3119   SleepForever,
3120   SleepForever,
3121   SleepForever,
3122   SleepForever,
3123   SleepForever,
3124   SleepForever,
3125   SleepForever,
3126   SleepForever,
3127   SleepForever,
3128   SleepForever,
3129   SleepForever,
3130   SleepForever,
3131   SleepForever,
3132   SleepForever,
3133   SleepForever,
3134   SleepForever,
3135   SleepForever,
3136   SleepForever,
3137   SleepForever,
3138   SleepForever,
3139   SleepForever,
3140   SleepForever,
3141   SleepForever,
3142   SleepForever,
3143   SleepForever,
3144   SleepForever,
3145   SleepForever,
3146   SleepForever,
3147   SleepForever,
3148   SleepForever,
3149   SleepForever,
3150   SleepForever,
3151   SleepForever,
3152   SleepForever,
3153   SleepForever,
3154   SleepForever,
3155   SleepForever,
3156   SleepForever,
3157   SleepForever,
3158   SleepForever,
3159   SleepForever,
3160   SleepForever,
3161   SleepForever,
3162   SleepForever,
3163   SleepForever,
3164   SleepForever,
3165   SleepForever,
3166   SleepForever,
3167   SleepForever,
3168   SleepForever,
3169   SleepForever,
3170   SleepForever,
3171   SleepForever,
3172   SleepForever,
3173   SleepForever,
3174   SleepForever,
3175   SleepForever,
3176   SleepForever,
3177   SleepForever,
3178   SleepForever,
3179   SleepForever,
3180   SleepForever,
3181   SleepForever,
3182   SleepForever,
3183   SleepForever,
3184   SleepForever,
3185   SleepForever,
3186   SleepForever,
3187   SleepForever,
3188   SleepForever,
3189   SleepForever,
3190   SleepForever,
3191   SleepForever,
3192   SleepForever,
3193   SleepForever,
3194   SleepForever,
3195   SleepForever,
3196   SleepForever,
3197   SleepForever,
3198   SleepForever,
3199   SleepForever,
3200   SleepForever,
3201   SleepForever,
3202   SleepForever,
3203   SleepForever,
3204   SleepForever,
3205   SleepForever,
3206   SleepForever,
3207   SleepForever,
3208   SleepForever,
3209   SleepForever,
3210   SleepForever,
3211   SleepForever,
3212   SleepForever,
3213   SleepForever,
3214   SleepForever,
3215   SleepForever,
3216   SleepForever,
3217   SleepForever,
3218   SleepForever,
3219   SleepForever,
3220   SleepForever,
3221   SleepForever,
3222   SleepForever,
3223   SleepForever,
3224   SleepForever,
3225   SleepForever,
3226   SleepForever,
3227   SleepForever,
3228   SleepForever,
3229   SleepForever,
3230   SleepForever,
3231   SleepForever,
3232   SleepForever,
3233   SleepForever,
3234   SleepForever,
3235   SleepForever,
3236   SleepForever,
3237   SleepForever,
3238   SleepForever,
3239   SleepForever,
3240   SleepForever,
3241   SleepForever,
3242   SleepForever,
3243   SleepForever,
3244   SleepForever,
3245   SleepForever,
3246   SleepForever,
3247   SleepForever,
3248   SleepForever,
3249   SleepForever,
3250   SleepForever,
3251   SleepForever,
3252   SleepForever,
3253   SleepForever,
3254   SleepForever,
3255   SleepForever,
3256   SleepForever,
3257   SleepForever,
3258   SleepForever,
3259   SleepForever,
3260   SleepForever,
3261   SleepForever,
3262   SleepForever,
3263   SleepForever,
3264   SleepForever,
3265   SleepForever,
3266   SleepForever,
3267   SleepForever,
3268   SleepForever,
3269   SleepForever,
3270   SleepForever,
3271   SleepForever,
3272   SleepForever,
3273   SleepForever,
3274   SleepForever,
3275   SleepForever,
3276   SleepForever,
3277   SleepForever,
3278   SleepForever,
3279   SleepForever,
3280   SleepForever,
3281   SleepForever,
3282   SleepForever,
3283   SleepForever,
3284   SleepForever,
3285   SleepForever,
3286   SleepForever,
3287   SleepForever,
3288   SleepForever,
3289   SleepForever,
3290   SleepForever,
3291   SleepForever,
3292   SleepForever,
3293   SleepForever,
3294   SleepForever,
3295   SleepForever,
3296   SleepForever,
3297   SleepForever,
3298   SleepForever,
3299   SleepForever,
3300   SleepForever,
3301   SleepForever,
3302   SleepForever,
3303   SleepForever,
3304   SleepForever,
3305   SleepForever,
3306   SleepForever,
3307   SleepForever,
3308   SleepForever,
3309   SleepForever,
3310   SleepForever,
3311   SleepForever,
3312   SleepForever,
3313   SleepForever,
3314   SleepForever,
3315   SleepForever,
3316   SleepForever,
3317   SleepForever,
3318   SleepForever,
3319   SleepForever,
3320   SleepForever,
3321   SleepForever,
3322   SleepForever,
3323   SleepForever,
3324   SleepForever,
3325   SleepForever,
3326   SleepForever,
3327   SleepForever,
3328   SleepForever,
3329   SleepForever,
3330   SleepForever,
3331   SleepForever,
3332   SleepForever,
3333   SleepForever,
3334   SleepForever,
3335   SleepForever,
3336   SleepForever,
3337   SleepForever,
3338 };
3339 
GetRuntimeShutdownNativeInterface()3340 const JNINativeInterface* GetRuntimeShutdownNativeInterface() {
3341   return reinterpret_cast<JNINativeInterface*>(&gJniSleepForeverStub);
3342 }
3343 
3344 }  // namespace art
3345 
operator <<(std::ostream & os,const jobjectRefType & rhs)3346 std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3347   switch (rhs) {
3348   case JNIInvalidRefType:
3349     os << "JNIInvalidRefType";
3350     return os;
3351   case JNILocalRefType:
3352     os << "JNILocalRefType";
3353     return os;
3354   case JNIGlobalRefType:
3355     os << "JNIGlobalRefType";
3356     return os;
3357   case JNIWeakGlobalRefType:
3358     os << "JNIWeakGlobalRefType";
3359     return os;
3360   default:
3361     LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
3362     UNREACHABLE();
3363   }
3364 }
3365