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