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