blob: 927f94b58825c4f972d081fedb425d6b738704e0 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Ian Rogersdf20fe02011-07-20 20:34:16 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070018
Elliott Hughes0af55432011-08-17 18:37:28 -070019#include <dlfcn.h>
Elliott Hughes79082e32011-08-25 12:07:32 -070020
21#include <cstdarg>
Ian Rogers700a4022014-05-19 16:49:03 -070022#include <memory>
Elliott Hughes0af55432011-08-17 18:37:28 -070023#include <utility>
24#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070025
Mathieu Chartierc7853442015-03-27 14:35:38 -070026#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070027#include "art_method-inl.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080028#include "atomic.h"
Mathieu Chartierbad02672014-08-25 13:08:22 -070029#include "base/allocator.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070030#include "base/enums.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080031#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080032#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080033#include "base/stl_util.h"
Ian Rogers98379392014-02-24 16:53:16 -080034#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070035#include "dex_file-inl.h"
Mathieu Chartierd0004802014-10-15 16:59:47 -070036#include "fault_handler.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070037#include "gc/accounting/card_table-inl.h"
Steven Morelande431e272017-07-18 16:53:49 -070038#include "gc_root.h"
Mathieu Chartierc56057e2014-05-04 13:18:58 -070039#include "indirect_reference_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070040#include "interpreter/interpreter.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070041#include "java_vm_ext.h"
Steven Morelande431e272017-07-18 16:53:49 -070042#include "jni_env_ext.h"
Alex Lighte00ec302017-06-16 08:56:43 -070043#include "jvalue-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044#include "mirror/class-inl.h"
45#include "mirror/class_loader.h"
Hiroshi Yamauchi02d2f292015-04-03 13:35:16 -070046#include "mirror/field-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070047#include "mirror/method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048#include "mirror/object-inl.h"
49#include "mirror/object_array-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070050#include "mirror/string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051#include "mirror/throwable.h"
Steven Morelande431e272017-07-18 16:53:49 -070052#include "nativehelper/ScopedLocalRef.h"
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080053#include "parsed_options.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070054#include "reflection.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070055#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070056#include "safe_map.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070057#include "scoped_thread_state_change-inl.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070058#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059#include "utf.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070060#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070061
George Burgess IV984387f2017-06-22 15:51:11 -070062namespace {
63// Frees the given va_list upon destruction.
64// This also guards the returns from inside of the CHECK_NON_NULL_ARGUMENTs.
65struct ScopedVAArgs {
66 explicit ScopedVAArgs(va_list* args): args(args) {}
67 ScopedVAArgs(const ScopedVAArgs&) = delete;
68 ScopedVAArgs(ScopedVAArgs&&) = delete;
69 ~ScopedVAArgs() { va_end(*args); }
70
71 private:
72 va_list* args;
73};
74} // namespace
75
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070076namespace art {
77
Mathieu Chartier24555ad2014-10-06 13:41:33 -070078// Consider turning this on when there is errors which could be related to JNI array copies such as
79// things not rendering correctly. E.g. b/16858794
80static constexpr bool kWarnJniAbort = false;
81
Alex Lighte00ec302017-06-16 08:56:43 -070082// Helpers to call instrumentation functions for fields. These take jobjects so we don't need to set
83// up handles for the rare case where these actually do something. Once these functions return it is
84// possible there will be a pending exception if the instrumentation happens to throw one.
85static void NotifySetObjectField(ArtField* field, jobject obj, jobject jval)
86 REQUIRES_SHARED(Locks::mutator_lock_) {
87 DCHECK_EQ(field->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
88 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
89 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
90 Thread* self = Thread::Current();
91 ArtMethod* cur_method = self->GetCurrentMethod(/*dex_pc*/ nullptr,
92 /*check_suspended*/ true,
93 /*abort_on_error*/ false);
94
95 if (cur_method == nullptr) {
96 // Set/Get Fields can be issued without a method during runtime startup/teardown. Ignore all
97 // of these changes.
98 return;
99 }
100 DCHECK(cur_method->IsNative());
101 JValue val;
102 val.SetL(self->DecodeJObject(jval));
103 instrumentation->FieldWriteEvent(self,
104 self->DecodeJObject(obj).Ptr(),
105 cur_method,
106 0, // dex_pc is always 0 since this is a native method.
107 field,
108 val);
109 }
110}
111
112static void NotifySetPrimitiveField(ArtField* field, jobject obj, JValue val)
113 REQUIRES_SHARED(Locks::mutator_lock_) {
114 DCHECK_NE(field->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
115 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
116 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
117 Thread* self = Thread::Current();
118 ArtMethod* cur_method = self->GetCurrentMethod(/*dex_pc*/ nullptr,
119 /*check_suspended*/ true,
120 /*abort_on_error*/ false);
121
122 if (cur_method == nullptr) {
123 // Set/Get Fields can be issued without a method during runtime startup/teardown. Ignore all
124 // of these changes.
125 return;
126 }
127 DCHECK(cur_method->IsNative());
128 instrumentation->FieldWriteEvent(self,
129 self->DecodeJObject(obj).Ptr(),
130 cur_method,
131 0, // dex_pc is always 0 since this is a native method.
132 field,
133 val);
134 }
135}
136
137static void NotifyGetField(ArtField* field, jobject obj)
138 REQUIRES_SHARED(Locks::mutator_lock_) {
139 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
140 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
141 Thread* self = Thread::Current();
142 ArtMethod* cur_method = self->GetCurrentMethod(/*dex_pc*/ nullptr,
143 /*check_suspended*/ true,
144 /*abort_on_error*/ false);
145
146 if (cur_method == nullptr) {
147 // Set/Get Fields can be issued without a method during runtime startup/teardown. Ignore all
148 // of these changes.
149 return;
150 }
151 DCHECK(cur_method->IsNative());
152 instrumentation->FieldReadEvent(self,
153 self->DecodeJObject(obj).Ptr(),
154 cur_method,
155 0, // dex_pc is always 0 since this is a native method.
156 field);
157 }
158}
159
Elliott Hughes6b436852011-08-12 10:16:44 -0700160// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
161// separated with slashes but aren't wrapped with "L;" like regular descriptors
162// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
163// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
164// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700165static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700166 std::string result;
167 // Add the missing "L;" if necessary.
168 if (name[0] == '[') {
169 result = name;
170 } else {
171 result += 'L';
172 result += name;
173 result += ';';
174 }
175 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700176 if (result.find('.') != std::string::npos) {
177 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
178 << "\"" << name << "\"";
179 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700180 }
181 return result;
182}
183
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700184static void ThrowNoSuchMethodError(ScopedObjectAccess& soa,
185 ObjPtr<mirror::Class> c,
186 const char* name,
187 const char* sig,
188 const char* kind)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700189 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700190 std::string temp;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000191 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Ian Rogers62d6c772013-02-27 08:32:07 -0800192 "no %s method \"%s.%s%s\"",
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700193 kind,
194 c->GetDescriptor(&temp),
195 name,
196 sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700197}
198
Mathieu Chartier3398c782016-09-30 10:27:43 -0700199static void ReportInvalidJNINativeMethod(const ScopedObjectAccess& soa,
200 ObjPtr<mirror::Class> c,
201 const char* kind,
Andreas Gampe20acb0c2017-05-25 11:33:50 -0700202 jint idx)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700203 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe20acb0c2017-05-25 11:33:50 -0700204 LOG(ERROR)
David Sehr709b0702016-10-13 09:12:37 -0700205 << "Failed to register native method in " << c->PrettyDescriptor()
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700206 << " in " << c->GetDexCache()->GetLocation()->ToModifiedUtf8()
Sebastien Hertzfa65e842014-07-03 09:39:53 +0200207 << ": " << kind << " is null at index " << idx;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000208 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Mathieu Chartier3398c782016-09-30 10:27:43 -0700209 "%s is null at index %d",
210 kind,
211 idx);
Sebastien Hertzfa65e842014-07-03 09:39:53 +0200212}
213
Mathieu Chartier0795f232016-09-27 18:43:30 -0700214static ObjPtr<mirror::Class> EnsureInitialized(Thread* self, ObjPtr<mirror::Class> klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700215 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800216 if (LIKELY(klass->IsInitialized())) {
217 return klass;
218 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700219 StackHandleScope<1> hs(self);
220 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700221 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800222 return nullptr;
223 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700224 return h_klass.Get();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800225}
226
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700227static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
228 const char* name, const char* sig, bool is_static)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700229 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700230 ObjPtr<mirror::Class> c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class>(jni_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800231 if (c == nullptr) {
232 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700233 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700234 ArtMethod* method = nullptr;
235 auto pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Vladimir Markoba118822017-06-12 15:41:56 +0100236 if (c->IsInterface()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700237 method = c->FindInterfaceMethod(name, sig, pointer_size);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700238 } else {
Vladimir Markoba118822017-06-12 15:41:56 +0100239 method = c->FindClassMethod(name, sig, pointer_size);
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700240 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800241 if (method == nullptr || method->IsStatic() != is_static) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700242 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800243 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700244 }
Andreas Gampe13b27842016-11-07 16:48:23 -0800245 return jni::EncodeArtMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700246}
247
Mathieu Chartier0795f232016-09-27 18:43:30 -0700248static ObjPtr<mirror::ClassLoader> GetClassLoader(const ScopedObjectAccess& soa)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700249 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700250 ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700251 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
Andreas Gampe13b27842016-11-07 16:48:23 -0800252 if (method == jni::DecodeArtMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700253 return soa.Decode<mirror::ClassLoader>(soa.Self()->GetClassLoaderOverride());
Brian Carlstrom00fae582011-10-28 01:16:28 -0700254 }
Brian Carlstromce888532013-10-10 00:32:58 -0700255 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800256 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700257 return method->GetDeclaringClass()->GetClassLoader();
258 }
259 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartier0795f232016-09-27 18:43:30 -0700260 ObjPtr<mirror::ClassLoader> class_loader =
261 soa.Decode<mirror::ClassLoader>(Runtime::Current()->GetSystemClassLoader());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800262 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700263 return class_loader;
264 }
265 // See if the override ClassLoader is set for gtests.
Mathieu Chartier0795f232016-09-27 18:43:30 -0700266 class_loader = soa.Decode<mirror::ClassLoader>(soa.Self()->GetClassLoaderOverride());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800267 if (class_loader != nullptr) {
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700268 // If so, CommonCompilerTest should have marked the runtime as a compiler not compiling an
269 // image.
270 CHECK(Runtime::Current()->IsAotCompiler());
Andreas Gampe4585f872015-03-27 23:45:15 -0700271 CHECK(!Runtime::Current()->IsCompilingBootImage());
Brian Carlstromce888532013-10-10 00:32:58 -0700272 return class_loader;
273 }
274 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800275 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700276}
277
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700278static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
279 const char* sig, bool is_static)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700280 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700281 StackHandleScope<2> hs(soa.Self());
282 Handle<mirror::Class> c(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700283 hs.NewHandle(EnsureInitialized(soa.Self(), soa.Decode<mirror::Class>(jni_class))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800284 if (c == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800285 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700286 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700287 ArtField* field = nullptr;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800288 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700289 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
290 if (sig[1] != '\0') {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700291 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(c->GetClassLoader()));
Ian Rogers98379392014-02-24 16:53:16 -0800292 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700293 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700294 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700295 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800296 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700297 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700298 DCHECK(soa.Self()->IsExceptionPending());
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800299 StackHandleScope<1> hs2(soa.Self());
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000300 Handle<mirror::Throwable> cause(hs2.NewHandle(soa.Self()->GetException()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700301 soa.Self()->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700302 std::string temp;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000303 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800304 "no type \"%s\" found and so no field \"%s\" "
305 "could be found in class \"%s\" or its superclasses", sig, name,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700306 c->GetDescriptor(&temp));
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000307 soa.Self()->GetException()->SetCause(cause.Get());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800308 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700309 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700310 std::string temp;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700311 if (is_static) {
Vladimir Marko19a4d372016-12-08 14:41:46 +0000312 field = mirror::Class::FindStaticField(
313 soa.Self(), c.Get(), name, field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700314 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700315 field = c->FindInstanceField(name, field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700316 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800317 if (field == nullptr) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000318 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogers62d6c772013-02-27 08:32:07 -0800319 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700320 sig, name, c->GetDescriptor(&temp));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800321 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700322 }
Andreas Gampe08883de2016-11-08 13:20:52 -0800323 return jni::EncodeArtField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700324}
325
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800326static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700327 jsize length, const char* identifier)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700328 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -0700329 std::string type(array->PrettyTypeOf());
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000330 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Ian Rogers62d6c772013-02-27 08:32:07 -0800331 "%s offset=%d length=%d %s.length=%d",
332 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700333}
Ian Rogers0571d352011-11-03 19:51:38 -0700334
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700335static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
336 jsize array_length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700337 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000338 soa.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers62d6c772013-02-27 08:32:07 -0800339 "offset=%d length=%d string.length()=%d", start, length,
340 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700341}
Elliott Hughes814e4032011-08-23 12:07:56 -0700342
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700343int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Mathieu Chartier90443472015-07-16 20:32:27 -0700344 REQUIRES(!Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700345 // Turn the const char* into a java.lang.String.
346 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800347 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700348 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700349 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700350
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700351 // Choose an appropriate constructor and set up the arguments.
352 jvalue args[2];
353 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800354 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700355 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800356 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700357 signature = "(Ljava/lang/String;)V";
358 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800359 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700360 signature = "(Ljava/lang/Throwable;)V";
361 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700362 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700363 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
364 args[0].l = s.get();
365 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700366 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700367 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800368 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800369 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700370 LOG(ERROR) << "No <init>" << signature << " in "
David Sehr709b0702016-10-13 09:12:37 -0700371 << mirror::Class::PrettyClass(soa.Decode<mirror::Class>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700372 return JNI_ERR;
373 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700374
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800375 ScopedLocalRef<jthrowable> exception(
376 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
377 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700378 return JNI_ERR;
379 }
Ian Rogersef28b142012-11-30 14:22:18 -0800380 ScopedObjectAccess soa(env);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700381 soa.Self()->SetException(soa.Decode<mirror::Throwable>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700382 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700383}
384
Ian Rogers68d8b422014-07-17 11:09:10 -0700385static JavaVMExt* JavaVmExtFromEnv(JNIEnv* env) {
386 return reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughes75770752011-08-24 17:52:38 -0700387}
388
Ian Rogers2d10b202014-05-12 19:15:18 -0700389#define CHECK_NON_NULL_ARGUMENT(value) \
390 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, nullptr)
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700391
Ian Rogers2d10b202014-05-12 19:15:18 -0700392#define CHECK_NON_NULL_ARGUMENT_RETURN_VOID(value) \
393 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, )
394
395#define CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(value) \
396 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, 0)
397
398#define CHECK_NON_NULL_ARGUMENT_RETURN(value, return_val) \
399 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, return_val)
400
401#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value, return_val) \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700402 if (UNLIKELY((value) == nullptr)) { \
Ian Rogers68d8b422014-07-17 11:09:10 -0700403 JavaVmExtFromEnv(env)->JniAbortF(name, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700404 return return_val; \
Ian Rogersbc939662013-08-15 10:26:54 -0700405 }
406
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700407#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700408 if (UNLIKELY((length) != 0 && (value) == nullptr)) { \
Ian Rogers68d8b422014-07-17 11:09:10 -0700409 JavaVmExtFromEnv(env)->JniAbortF(__FUNCTION__, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700410 return; \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700411 }
412
Andreas Gampe3f1dc562015-05-18 15:52:22 -0700413template <bool kNative>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700414static ArtMethod* FindMethod(mirror::Class* c, const StringPiece& name, const StringPiece& sig)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700415 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700416 auto pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Alex Lighte64300b2015-12-15 15:02:47 -0800417 for (auto& method : c->GetMethods(pointer_size)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700418 if (kNative == method.IsNative() && name == method.GetName() && method.GetSignature() == sig) {
419 return &method;
Andreas Gampe3f1dc562015-05-18 15:52:22 -0700420 }
421 }
Andreas Gampe3f1dc562015-05-18 15:52:22 -0700422 return nullptr;
423}
424
Elliott Hughescdf53122011-08-19 15:46:09 -0700425class JNI {
426 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700427 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700428 return JNI_VERSION_1_6;
429 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700430
Ian Rogers25e8b912012-09-07 11:31:36 -0700431 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700432 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800433 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700434 }
435
Elliott Hughescdf53122011-08-19 15:46:09 -0700436 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700437 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700438 Runtime* runtime = Runtime::Current();
439 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700440 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700441 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800442 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700443 if (runtime->IsStarted()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700444 StackHandleScope<1> hs(soa.Self());
445 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader(soa)));
Ian Rogers98379392014-02-24 16:53:16 -0800446 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700447 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800448 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700449 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700450 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700451 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700452
Ian Rogers62f05122014-03-21 11:21:29 -0700453 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700454 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700455 ScopedObjectAccess soa(env);
Andreas Gampe13b27842016-11-07 16:48:23 -0800456 return jni::EncodeArtMethod(ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700457 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700458
Ian Rogers62f05122014-03-21 11:21:29 -0700459 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700460 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700461 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700462 ObjPtr<mirror::Object> obj_field = soa.Decode<mirror::Object>(jlr_field);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700463 if (obj_field->GetClass() != mirror::Field::StaticClass()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700464 // Not even a java.lang.reflect.Field, return null. TODO, is this check necessary?
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700465 return nullptr;
466 }
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700467 ObjPtr<mirror::Field> field = ObjPtr<mirror::Field>::DownCast(obj_field);
Andreas Gampe08883de2016-11-08 13:20:52 -0800468 return jni::EncodeArtField(field->GetArtField());
Elliott Hughescdf53122011-08-19 15:46:09 -0700469 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700470
Elliott Hughescdf53122011-08-19 15:46:09 -0700471 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700472 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700473 ScopedObjectAccess soa(env);
Andreas Gampe13b27842016-11-07 16:48:23 -0800474 ArtMethod* m = jni::DecodeArtMethod(mid);
Neil Fuller0e844392016-09-08 13:43:31 +0100475 mirror::Executable* method;
Andreas Gampe542451c2016-07-26 09:02:02 -0700476 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700477 DCHECK(!Runtime::Current()->IsActiveTransaction());
Sebastien Hertzd3333762014-06-26 14:45:07 +0200478 if (m->IsConstructor()) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700479 method = mirror::Constructor::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), m);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200480 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700481 method = mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), m);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200482 }
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700483 return soa.AddLocalReference<jobject>(method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700484 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700485
Elliott Hughescdf53122011-08-19 15:46:09 -0700486 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700487 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700488 ScopedObjectAccess soa(env);
Andreas Gampe08883de2016-11-08 13:20:52 -0800489 ArtField* f = jni::DecodeArtField(fid);
Andreas Gampee01e3642016-07-25 13:06:04 -0700490 return soa.AddLocalReference<jobject>(
Andreas Gampe542451c2016-07-26 09:02:02 -0700491 mirror::Field::CreateFromArtField<kRuntimePointerSize>(soa.Self(), f, true));
Elliott Hughescdf53122011-08-19 15:46:09 -0700492 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700493
Elliott Hughes37f7a402011-08-22 18:56:01 -0700494 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700495 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700496 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700497 ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700498 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700499 }
500
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700501 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700502 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700503 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700504 ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(java_class);
Brian Carlstrom08ac9222015-05-22 13:43:00 -0700505 return soa.AddLocalReference<jclass>(c->IsInterface() ? nullptr : c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700506 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700507
Narayan Kamath1268b742014-07-11 19:15:11 +0100508 // Note: java_class1 should be safely castable to java_class2, and
509 // not the other way around.
Elliott Hughes37f7a402011-08-22 18:56:01 -0700510 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700511 CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
512 CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700513 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700514 ObjPtr<mirror::Class> c1 = soa.Decode<mirror::Class>(java_class1);
515 ObjPtr<mirror::Class> c2 = soa.Decode<mirror::Class>(java_class2);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700516 return c2->IsAssignableFrom(c1) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700517 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700518
Elliott Hughese84278b2012-03-22 10:06:53 -0700519 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700520 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800521 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700522 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700523 return JNI_TRUE;
524 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700525 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700526 ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(jobj);
527 ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700528 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700529 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700530 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700531
Elliott Hughes37f7a402011-08-22 18:56:01 -0700532 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700533 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700534 ObjPtr<mirror::Throwable> exception = soa.Decode<mirror::Throwable>(java_exception);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800535 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700536 return JNI_ERR;
537 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700538 soa.Self()->SetException(exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700539 return JNI_OK;
540 }
541
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700542 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700543 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800544 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700545 }
546
547 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700548 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700549 }
550
551 static void ExceptionClear(JNIEnv* env) {
Serguei Katkova309d762014-05-26 11:23:39 +0700552 ScopedObjectAccess soa(env);
553 soa.Self()->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700554 }
555
556 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700557 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700558
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700559 // If we have no exception to describe, pass through.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000560 if (!soa.Self()->GetException()) {
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700561 return;
562 }
563
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000564 StackHandleScope<1> hs(soa.Self());
565 Handle<mirror::Throwable> old_exception(
566 hs.NewHandle<mirror::Throwable>(soa.Self()->GetException()));
567 soa.Self()->ClearException();
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800568 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700569 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700570 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
571 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800572 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700573 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
David Sehr709b0702016-10-13 09:12:37 -0700574 << mirror::Object::PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700575 } else {
576 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800577 if (soa.Self()->IsExceptionPending()) {
David Sehr709b0702016-10-13 09:12:37 -0700578 LOG(WARNING) << "JNI WARNING: " << mirror::Object::PrettyTypeOf(soa.Self()->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700579 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800580 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700581 }
582 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000583 soa.Self()->SetException(old_exception.Get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700584 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700585
Elliott Hughescdf53122011-08-19 15:46:09 -0700586 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700587 ScopedObjectAccess soa(env);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000588 mirror::Object* exception = soa.Self()->GetException();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700589 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700590 }
591
Ian Rogers25e8b912012-09-07 11:31:36 -0700592 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700593 LOG(FATAL) << "JNI FatalError called: " << msg;
594 }
595
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700596 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700597 // TODO: SOA may not be necessary but I do it to please lock annotations.
598 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700599 if (EnsureLocalCapacityInternal(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700600 return JNI_ERR;
601 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700602 down_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700603 return JNI_OK;
604 }
605
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700606 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700607 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700608 ObjPtr<mirror::Object> survivor = soa.Decode<mirror::Object>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700609 soa.Env()->PopFrame();
610 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700611 }
612
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700613 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700614 // TODO: SOA may not be necessary but I do it to please lock annotations.
615 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700616 return EnsureLocalCapacityInternal(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700617 }
618
Elliott Hughescdf53122011-08-19 15:46:09 -0700619 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700620 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700621 ObjPtr<mirror::Object> decoded_obj = soa.Decode<mirror::Object>(obj);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700622 return soa.Vm()->AddGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700623 }
624
625 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700626 JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->vm;
627 Thread* self = down_cast<JNIEnvExt*>(env)->self;
628 vm->DeleteGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700629 }
630
631 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700632 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700633 ObjPtr<mirror::Object> decoded_obj = soa.Decode<mirror::Object>(obj);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700634 return soa.Vm()->AddWeakGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700635 }
636
637 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700638 JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->vm;
639 Thread* self = down_cast<JNIEnvExt*>(env)->self;
640 vm->DeleteWeakGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700641 }
642
643 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700644 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700645 ObjPtr<mirror::Object> decoded_obj = soa.Decode<mirror::Object>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800646 // Check for null after decoding the object to handle cleared weak globals.
647 if (decoded_obj == nullptr) {
648 return nullptr;
649 }
650 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700651 }
652
Mathieu Chartierdd06afe2015-06-26 10:47:08 -0700653 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800654 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700655 return;
656 }
Mathieu Chartierdd06afe2015-06-26 10:47:08 -0700657 // SOA is only necessary to have exclusion between GC root marking and removing.
658 // We don't want to have the GC attempt to mark a null root if we just removed
659 // it. b/22119403
660 ScopedObjectAccess soa(env);
661 auto* ext_env = down_cast<JNIEnvExt*>(env);
662 if (!ext_env->locals.Remove(ext_env->local_ref_cookie, obj)) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700663 // Attempting to delete a local reference that is not in the
664 // topmost local reference frame is a no-op. DeleteLocalRef returns
665 // void and doesn't throw any exceptions, but we should probably
666 // complain about it so the user will notice that things aren't
667 // going quite the way they expect.
668 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
669 << "failed to find entry";
670 }
671 }
672
673 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700674 if (obj1 == obj2) {
675 return JNI_TRUE;
676 } else {
677 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700678 return (soa.Decode<mirror::Object>(obj1) == soa.Decode<mirror::Object>(obj2))
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800679 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700680 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700681 }
682
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700683 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700684 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700685 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700686 ObjPtr<mirror::Class> c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800687 if (c == nullptr) {
688 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700689 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800690 if (c->IsStringClass()) {
691 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700692 return soa.AddLocalReference<jobject>(mirror::String::AllocEmptyString<true>(soa.Self(),
693 allocator_type));
Jeff Hao848f70a2014-01-15 13:49:50 -0800694 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700695 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700696 }
697
Ian Rogersbc939662013-08-15 10:26:54 -0700698 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700699 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700700 va_start(args, mid);
George Burgess IV984387f2017-06-22 15:51:11 -0700701 ScopedVAArgs free_args_later(&args);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700702 CHECK_NON_NULL_ARGUMENT(java_class);
703 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700704 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700705 return result;
706 }
707
Elliott Hughes72025e52011-08-23 17:50:30 -0700708 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700709 CHECK_NON_NULL_ARGUMENT(java_class);
710 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700711 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700712 ObjPtr<mirror::Class> c = EnsureInitialized(soa.Self(),
713 soa.Decode<mirror::Class>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800714 if (c == nullptr) {
715 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700716 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800717 if (c->IsStringClass()) {
718 // Replace calls to String.<init> with equivalent StringFactory call.
Andreas Gampe13b27842016-11-07 16:48:23 -0800719 jmethodID sf_mid = jni::EncodeArtMethod(
720 WellKnownClasses::StringInitToStringFactory(jni::DecodeArtMethod(mid)));
Jeff Hao848f70a2014-01-15 13:49:50 -0800721 return CallStaticObjectMethodV(env, WellKnownClasses::java_lang_StringFactory, sf_mid, args);
722 }
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700723 ObjPtr<mirror::Object> result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800724 if (result == nullptr) {
725 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700726 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700727 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700728 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800729 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800730 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700731 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800732 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700733 }
734
Elliott Hughes72025e52011-08-23 17:50:30 -0700735 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700736 CHECK_NON_NULL_ARGUMENT(java_class);
737 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700738 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700739 ObjPtr<mirror::Class> c = EnsureInitialized(soa.Self(),
740 soa.Decode<mirror::Class>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800741 if (c == nullptr) {
742 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700743 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800744 if (c->IsStringClass()) {
745 // Replace calls to String.<init> with equivalent StringFactory call.
Andreas Gampe13b27842016-11-07 16:48:23 -0800746 jmethodID sf_mid = jni::EncodeArtMethod(
747 WellKnownClasses::StringInitToStringFactory(jni::DecodeArtMethod(mid)));
Jeff Hao848f70a2014-01-15 13:49:50 -0800748 return CallStaticObjectMethodA(env, WellKnownClasses::java_lang_StringFactory, sf_mid, args);
749 }
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700750 ObjPtr<mirror::Object> result = c->AllocObject(soa.Self());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800751 if (result == nullptr) {
752 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700753 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700754 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700755 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800756 if (soa.Self()->IsExceptionPending()) {
757 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700758 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800759 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700760 }
761
Ian Rogersbc939662013-08-15 10:26:54 -0700762 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700763 CHECK_NON_NULL_ARGUMENT(java_class);
764 CHECK_NON_NULL_ARGUMENT(name);
765 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700766 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700767 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700768 }
769
Ian Rogersbc939662013-08-15 10:26:54 -0700770 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
771 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700772 CHECK_NON_NULL_ARGUMENT(java_class);
773 CHECK_NON_NULL_ARGUMENT(name);
774 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700775 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700776 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700777 }
778
Elliott Hughes72025e52011-08-23 17:50:30 -0700779 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700780 va_list ap;
781 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -0700782 ScopedVAArgs free_args_later(&ap);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700783 CHECK_NON_NULL_ARGUMENT(obj);
784 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700785 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700786 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700787 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700788 }
789
Elliott Hughes72025e52011-08-23 17:50:30 -0700790 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700791 CHECK_NON_NULL_ARGUMENT(obj);
792 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700793 ScopedObjectAccess soa(env);
794 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
795 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700796 }
797
Elliott Hughes72025e52011-08-23 17:50:30 -0700798 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700799 CHECK_NON_NULL_ARGUMENT(obj);
800 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700801 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700802 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700803 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700804 }
805
Elliott Hughes72025e52011-08-23 17:50:30 -0700806 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700807 va_list ap;
808 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -0700809 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -0700810 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
811 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700812 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700813 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700814 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700815 }
816
Elliott Hughes72025e52011-08-23 17:50:30 -0700817 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700818 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
819 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700820 ScopedObjectAccess soa(env);
821 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700822 }
823
Elliott Hughes72025e52011-08-23 17:50:30 -0700824 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700825 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
826 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700827 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700828 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700829 }
830
Elliott Hughes72025e52011-08-23 17:50:30 -0700831 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700832 va_list ap;
833 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -0700834 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -0700835 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
836 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700837 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700838 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700839 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700840 }
841
Elliott Hughes72025e52011-08-23 17:50:30 -0700842 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700843 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
844 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700845 ScopedObjectAccess soa(env);
846 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700847 }
848
Elliott Hughes72025e52011-08-23 17:50:30 -0700849 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700850 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
851 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700852 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700853 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700854 }
855
Elliott Hughes72025e52011-08-23 17:50:30 -0700856 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700857 va_list ap;
858 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -0700859 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -0700860 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
861 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700862 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700863 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700864 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700865 }
866
Elliott Hughes72025e52011-08-23 17:50:30 -0700867 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700868 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
869 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700870 ScopedObjectAccess soa(env);
871 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700872 }
873
Elliott Hughes72025e52011-08-23 17:50:30 -0700874 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700875 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
876 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700877 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700878 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700879 }
880
Elliott Hughes72025e52011-08-23 17:50:30 -0700881 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700882 va_list ap;
883 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -0700884 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -0700885 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
886 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700887 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700888 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700889 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700890 }
891
Elliott Hughes72025e52011-08-23 17:50:30 -0700892 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700893 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
894 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700895 ScopedObjectAccess soa(env);
896 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700897 }
898
Elliott Hughes72025e52011-08-23 17:50:30 -0700899 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700900 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
901 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700902 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700903 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700904 }
905
Elliott Hughes72025e52011-08-23 17:50:30 -0700906 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700907 va_list ap;
908 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -0700909 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -0700910 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
911 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700912 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700913 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700914 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700915 }
916
Elliott Hughes72025e52011-08-23 17:50:30 -0700917 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700918 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
919 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700920 ScopedObjectAccess soa(env);
921 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700922 }
923
Elliott Hughes72025e52011-08-23 17:50:30 -0700924 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700925 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
926 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700927 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700928 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700929 }
930
Elliott Hughes72025e52011-08-23 17:50:30 -0700931 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700932 va_list ap;
933 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -0700934 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -0700935 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
936 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700937 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700938 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700939 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700940 }
941
Elliott Hughes72025e52011-08-23 17:50:30 -0700942 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700943 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
944 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700945 ScopedObjectAccess soa(env);
946 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700947 }
948
Elliott Hughes72025e52011-08-23 17:50:30 -0700949 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700950 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
951 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700952 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700953 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700954 }
955
Elliott Hughes72025e52011-08-23 17:50:30 -0700956 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700957 va_list ap;
958 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -0700959 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -0700960 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
961 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700962 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700963 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700964 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700965 }
966
Elliott Hughes72025e52011-08-23 17:50:30 -0700967 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700968 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
969 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700970 ScopedObjectAccess soa(env);
971 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700972 }
973
Elliott Hughes72025e52011-08-23 17:50:30 -0700974 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700975 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
976 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700977 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700978 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700979 }
980
Elliott Hughes72025e52011-08-23 17:50:30 -0700981 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700982 va_list ap;
983 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -0700984 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -0700985 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
986 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700987 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700988 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700989 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700990 }
991
Elliott Hughes72025e52011-08-23 17:50:30 -0700992 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700993 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
994 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700995 ScopedObjectAccess soa(env);
996 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -0700997 }
998
Elliott Hughes72025e52011-08-23 17:50:30 -0700999 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001000 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1001 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001002 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001003 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001004 }
1005
Elliott Hughes72025e52011-08-23 17:50:30 -07001006 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001007 va_list ap;
1008 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001009 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001010 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1011 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001012 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001013 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001014 }
1015
Elliott Hughes72025e52011-08-23 17:50:30 -07001016 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001017 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1018 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001019 ScopedObjectAccess soa(env);
1020 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 }
1022
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001024 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1025 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001026 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001027 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001028 }
1029
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001030 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001031 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001032 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001033 ScopedVAArgs free_args_later(&ap);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001034 CHECK_NON_NULL_ARGUMENT(obj);
1035 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001036 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001037 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1038 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001039 return local_result;
1040 }
1041
Ian Rogersbc939662013-08-15 10:26:54 -07001042 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1043 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001044 CHECK_NON_NULL_ARGUMENT(obj);
1045 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001046 ScopedObjectAccess soa(env);
1047 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1048 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001049 }
1050
Ian Rogersbc939662013-08-15 10:26:54 -07001051 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1052 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001053 CHECK_NON_NULL_ARGUMENT(obj);
1054 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001055 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001056 JValue result(InvokeWithJValues(soa, obj, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001057 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001058 }
1059
Ian Rogersbc939662013-08-15 10:26:54 -07001060 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1061 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001063 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001064 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001065 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1066 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001067 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001068 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001069 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001070 }
1071
Ian Rogersbc939662013-08-15 10:26:54 -07001072 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1073 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001074 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1075 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001076 ScopedObjectAccess soa(env);
1077 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 }
1079
Ian Rogersbc939662013-08-15 10:26:54 -07001080 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1081 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001082 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1083 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001084 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001085 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001086 }
1087
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001088 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001091 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001092 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1093 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001094 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001095 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001096 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 }
1098
Ian Rogersbc939662013-08-15 10:26:54 -07001099 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1100 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001101 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1102 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001103 ScopedObjectAccess soa(env);
1104 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001105 }
1106
Ian Rogersbc939662013-08-15 10:26:54 -07001107 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1108 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001109 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1110 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001111 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001112 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 }
1114
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001115 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001116 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001118 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001119 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1120 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001121 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001122 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001123 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001124 }
1125
Ian Rogersbc939662013-08-15 10:26:54 -07001126 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1127 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001128 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1129 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001130 ScopedObjectAccess soa(env);
1131 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001132 }
1133
Ian Rogersbc939662013-08-15 10:26:54 -07001134 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1135 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001136 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1137 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001138 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001139 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001140 }
1141
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001142 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001143 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001144 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001145 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001146 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1147 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001148 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001149 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001150 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001151 }
1152
Ian Rogersbc939662013-08-15 10:26:54 -07001153 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1154 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001155 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1156 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001157 ScopedObjectAccess soa(env);
1158 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001159 }
1160
Ian Rogersbc939662013-08-15 10:26:54 -07001161 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1162 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001163 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1164 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001165 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001166 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001167 }
1168
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001169 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001170 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001171 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001172 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001173 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1174 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001175 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001176 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001177 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001178 }
1179
Ian Rogersbc939662013-08-15 10:26:54 -07001180 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1181 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001182 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1183 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001184 ScopedObjectAccess soa(env);
1185 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001186 }
1187
Ian Rogersbc939662013-08-15 10:26:54 -07001188 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1189 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001190 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1191 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001192 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001193 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 }
1195
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001196 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001197 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001198 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001199 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001200 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1201 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001202 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001203 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001204 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001205 }
1206
Ian Rogersbc939662013-08-15 10:26:54 -07001207 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1208 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001209 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1210 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001211 ScopedObjectAccess soa(env);
1212 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001213 }
1214
Ian Rogersbc939662013-08-15 10:26:54 -07001215 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1216 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001217 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1218 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001219 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001220 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001221 }
1222
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001223 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001224 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001225 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001226 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001227 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1228 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001229 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001230 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001231 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 }
1233
Ian Rogersbc939662013-08-15 10:26:54 -07001234 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1235 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001236 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1237 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001238 ScopedObjectAccess soa(env);
1239 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001240 }
1241
Ian Rogersbc939662013-08-15 10:26:54 -07001242 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1243 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001244 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1245 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001246 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001247 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001248 }
1249
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001250 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001251 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001252 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001253 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001254 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1255 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001256 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001257 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001258 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 }
1260
Ian Rogersbc939662013-08-15 10:26:54 -07001261 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1262 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001263 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1264 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001265 ScopedObjectAccess soa(env);
1266 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001267 }
1268
Ian Rogersbc939662013-08-15 10:26:54 -07001269 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1270 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001271 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1272 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001273 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001274 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001275 }
1276
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001277 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001279 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001280 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001281 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1282 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001283 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001284 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001285 }
1286
Brian Carlstromea46f952013-07-30 01:26:50 -07001287 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1288 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001289 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1290 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001291 ScopedObjectAccess soa(env);
1292 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001293 }
1294
Ian Rogersbc939662013-08-15 10:26:54 -07001295 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1296 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001297 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1298 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001299 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001300 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 }
1302
Ian Rogersbc939662013-08-15 10:26:54 -07001303 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001304 CHECK_NON_NULL_ARGUMENT(java_class);
1305 CHECK_NON_NULL_ARGUMENT(name);
1306 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001307 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001308 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001309 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001310
Ian Rogersbc939662013-08-15 10:26:54 -07001311 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1312 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001313 CHECK_NON_NULL_ARGUMENT(java_class);
1314 CHECK_NON_NULL_ARGUMENT(name);
1315 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001316 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001317 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001318 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001319
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001320 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001321 CHECK_NON_NULL_ARGUMENT(obj);
1322 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001323 ScopedObjectAccess soa(env);
Andreas Gampe08883de2016-11-08 13:20:52 -08001324 ArtField* f = jni::DecodeArtField(fid);
Alex Lighte00ec302017-06-16 08:56:43 -07001325 NotifyGetField(f, obj);
1326 ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(obj);
Mathieu Chartier3398c782016-09-30 10:27:43 -07001327 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001328 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001329
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001330 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001331 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001332 ScopedObjectAccess soa(env);
Andreas Gampe08883de2016-11-08 13:20:52 -08001333 ArtField* f = jni::DecodeArtField(fid);
Alex Lighte00ec302017-06-16 08:56:43 -07001334 NotifyGetField(f, nullptr);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001335 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001336 }
1337
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001338 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001339 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1340 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001341 ScopedObjectAccess soa(env);
Alex Lighte00ec302017-06-16 08:56:43 -07001342 ArtField* f = jni::DecodeArtField(fid);
1343 NotifySetObjectField(f, java_object, java_value);
Mathieu Chartier0795f232016-09-27 18:43:30 -07001344 ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(java_object);
1345 ObjPtr<mirror::Object> v = soa.Decode<mirror::Object>(java_value);
Mathieu Chartier3398c782016-09-30 10:27:43 -07001346 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001347 }
1348
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001349 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001350 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001351 ScopedObjectAccess soa(env);
Andreas Gampe08883de2016-11-08 13:20:52 -08001352 ArtField* f = jni::DecodeArtField(fid);
Alex Lighte00ec302017-06-16 08:56:43 -07001353 NotifySetObjectField(f, nullptr, java_value);
1354 ObjPtr<mirror::Object> v = soa.Decode<mirror::Object>(java_value);
Mathieu Chartier3398c782016-09-30 10:27:43 -07001355 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001356 }
1357
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001358#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001359 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1360 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001361 ScopedObjectAccess soa(env); \
Andreas Gampe08883de2016-11-08 13:20:52 -08001362 ArtField* f = jni::DecodeArtField(fid); \
Alex Lighte00ec302017-06-16 08:56:43 -07001363 NotifyGetField(f, instance); \
1364 ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(instance); \
Mathieu Chartier3398c782016-09-30 10:27:43 -07001365 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001366
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001367#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001368 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001369 ScopedObjectAccess soa(env); \
Andreas Gampe08883de2016-11-08 13:20:52 -08001370 ArtField* f = jni::DecodeArtField(fid); \
Alex Lighte00ec302017-06-16 08:56:43 -07001371 NotifyGetField(f, nullptr); \
Ian Rogersbc939662013-08-15 10:26:54 -07001372 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001373
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001374#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001375 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1376 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001377 ScopedObjectAccess soa(env); \
Andreas Gampe08883de2016-11-08 13:20:52 -08001378 ArtField* f = jni::DecodeArtField(fid); \
Alex Lighte00ec302017-06-16 08:56:43 -07001379 NotifySetPrimitiveField(f, instance, JValue::FromPrimitive<decltype(value)>(value)); \
1380 ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(instance); \
Mathieu Chartier3398c782016-09-30 10:27:43 -07001381 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001382
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001383#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001384 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001385 ScopedObjectAccess soa(env); \
Andreas Gampe08883de2016-11-08 13:20:52 -08001386 ArtField* f = jni::DecodeArtField(fid); \
Alex Lighte00ec302017-06-16 08:56:43 -07001387 NotifySetPrimitiveField(f, nullptr, JValue::FromPrimitive<decltype(value)>(value)); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001388 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001389
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001390 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001391 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001392 }
1393
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001394 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001395 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001396 }
1397
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001398 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001399 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001400 }
1401
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001402 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001403 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001404 }
1405
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001406 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001407 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001408 }
1409
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001410 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001411 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001412 }
1413
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001414 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001415 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001416 }
1417
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001418 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001419 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001420 }
1421
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001422 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001423 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001424 }
1425
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001426 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001427 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001428 }
1429
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001430 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001431 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001432 }
1433
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001434 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001435 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001436 }
1437
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001438 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001439 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001440 }
1441
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001442 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001443 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001444 }
1445
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001446 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001447 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001448 }
1449
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001450 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001451 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001452 }
1453
1454 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001455 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001456 }
1457
1458 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001459 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001460 }
1461
1462 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001463 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001464 }
1465
1466 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001467 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001468 }
1469
1470 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001471 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001472 }
1473
1474 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001475 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001476 }
1477
1478 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001479 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001480 }
1481
1482 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001483 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001484 }
1485
1486 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001487 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001488 }
1489
1490 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001491 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001492 }
1493
1494 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001495 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001496 }
1497
1498 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001499 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001500 }
1501
1502 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001503 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001504 }
1505
1506 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001507 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001508 }
1509
1510 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001511 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001512 }
1513
1514 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001515 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001516 }
1517
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001518 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001519 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001520 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001521 ScopedVAArgs free_args_later(&ap);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001522 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001523 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001524 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001525 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001526 return local_result;
1527 }
1528
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001529 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001530 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001531 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001532 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001533 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001534 }
1535
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001536 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001537 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001538 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001539 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001540 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001541 }
1542
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001543 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001544 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001545 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001546 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001547 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001548 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001549 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001550 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001551 }
1552
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001553 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001554 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001555 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001556 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001557 }
1558
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001559 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001560 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001561 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001562 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 }
1564
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001565 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001567 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001568 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001569 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001570 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001571 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001572 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001573 }
1574
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001575 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001576 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001577 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001578 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001579 }
1580
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001581 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001582 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001583 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001584 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 }
1586
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001587 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001588 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001589 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001590 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001591 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001592 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001593 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001594 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001595 }
1596
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001597 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001598 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001599 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001600 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001601 }
1602
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001603 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001604 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001605 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001606 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001607 }
1608
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001609 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001611 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001612 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001613 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001614 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001615 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001616 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001617 }
1618
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001619 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001620 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001621 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001622 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001623 }
1624
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001625 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001626 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001627 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001628 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001629 }
1630
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001631 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001632 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001633 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001634 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001635 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001636 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001637 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001638 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 }
1640
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001641 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001642 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001643 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001644 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001645 }
1646
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001647 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001648 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001649 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001650 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001651 }
1652
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001653 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001654 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001655 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001656 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001657 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001658 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001659 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001660 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001661 }
1662
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001663 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001664 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001665 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001666 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 }
1668
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001669 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001670 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001671 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001672 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001673 }
1674
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001675 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001676 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001677 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001678 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001679 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001680 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001681 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001682 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001683 }
1684
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001685 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001686 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001687 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001688 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001689 }
1690
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001691 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001692 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001693 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001694 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001695 }
1696
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001697 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001698 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001699 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001700 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001701 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001702 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001703 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001704 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001705 }
1706
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001707 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001708 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001709 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001710 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001711 }
1712
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001713 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001714 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001715 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001716 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001717 }
1718
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001719 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001720 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001721 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001722 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001723 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001724 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001725 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001726 }
1727
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001728 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001729 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001730 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001731 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001732 }
1733
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001734 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001735 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001736 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001737 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001738 }
1739
Elliott Hughes814e4032011-08-23 12:07:56 -07001740 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001741 if (UNLIKELY(char_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001742 JavaVmExtFromEnv(env)->JniAbortF("NewString", "char_count < 0: %d", char_count);
Ian Rogers1d99e452014-01-02 17:36:41 -08001743 return nullptr;
1744 }
1745 if (UNLIKELY(chars == nullptr && char_count > 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001746 JavaVmExtFromEnv(env)->JniAbortF("NewString", "chars == null && char_count > 0");
Ian Rogers1d99e452014-01-02 17:36:41 -08001747 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001748 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001749 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001750 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001751 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001752 }
1753
1754 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001755 if (utf == nullptr) {
1756 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001757 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001758 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001759 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001760 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001761 }
1762
Elliott Hughes814e4032011-08-23 12:07:56 -07001763 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001764 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001765 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07001766 return soa.Decode<mirror::String>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001767 }
1768
1769 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001770 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001771 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07001772 return soa.Decode<mirror::String>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001773 }
1774
Ian Rogersbc939662013-08-15 10:26:54 -07001775 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1776 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001777 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001778 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07001779 ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
Vladimir Marko795e3412015-11-06 16:57:03 +00001780 if (start < 0 || length < 0 || length > s->GetLength() - start) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001781 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001782 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001783 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001784 if (s->IsCompressed()) {
1785 for (int i = 0; i < length; ++i) {
1786 buf[i] = static_cast<jchar>(s->CharAt(start+i));
1787 }
1788 } else {
1789 const jchar* chars = static_cast<jchar*>(s->GetValue());
1790 memcpy(buf, chars + start, length * sizeof(jchar));
1791 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07001792 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001793 }
1794
Ian Rogersbc939662013-08-15 10:26:54 -07001795 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1796 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001797 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001798 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07001799 ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
Vladimir Marko795e3412015-11-06 16:57:03 +00001800 if (start < 0 || length < 0 || length > s->GetLength() - start) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001801 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001802 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001803 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001804 if (s->IsCompressed()) {
1805 for (int i = 0; i < length; ++i) {
1806 buf[i] = s->CharAt(start+i);
1807 }
1808 } else {
1809 const jchar* chars = s->GetValue();
1810 size_t bytes = CountUtf8Bytes(chars + start, length);
1811 ConvertUtf16ToModifiedUtf8(buf, bytes, chars + start, length);
1812 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07001813 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001814 }
1815
Elliott Hughes75770752011-08-24 17:52:38 -07001816 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001817 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001818 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07001819 ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
Fred Shih56890e22014-06-02 11:11:52 -07001820 gc::Heap* heap = Runtime::Current()->GetHeap();
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001821 if (heap->IsMovableObject(s) || s->IsCompressed()) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001822 jchar* chars = new jchar[s->GetLength()];
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001823 if (s->IsCompressed()) {
1824 int32_t length = s->GetLength();
1825 for (int i = 0; i < length; ++i) {
1826 chars[i] = s->CharAt(i);
1827 }
1828 } else {
1829 memcpy(chars, s->GetValue(), sizeof(jchar) * s->GetLength());
1830 }
Fred Shih56890e22014-06-02 11:11:52 -07001831 if (is_copy != nullptr) {
1832 *is_copy = JNI_TRUE;
1833 }
Jeff Hao848f70a2014-01-15 13:49:50 -08001834 return chars;
Elliott Hughes75770752011-08-24 17:52:38 -07001835 }
Jeff Hao848f70a2014-01-15 13:49:50 -08001836 if (is_copy != nullptr) {
1837 *is_copy = JNI_FALSE;
1838 }
1839 return static_cast<jchar*>(s->GetValue());
Elliott Hughes814e4032011-08-23 12:07:56 -07001840 }
1841
Mathieu Chartier590fee92013-09-13 13:46:47 -07001842 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001843 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001844 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07001845 ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001846 if (s->IsCompressed() || (s->IsCompressed() == false && chars != s->GetValue())) {
Fred Shih56890e22014-06-02 11:11:52 -07001847 delete[] chars;
1848 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001849 }
1850
Elliott Hughes75770752011-08-24 17:52:38 -07001851 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Fred Shih56890e22014-06-02 11:11:52 -07001852 CHECK_NON_NULL_ARGUMENT(java_string);
1853 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07001854 ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
Fred Shih56890e22014-06-02 11:11:52 -07001855 gc::Heap* heap = Runtime::Current()->GetHeap();
Jeff Hao848f70a2014-01-15 13:49:50 -08001856 if (heap->IsMovableObject(s)) {
Fred Shih56890e22014-06-02 11:11:52 -07001857 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -07001858 HandleWrapperObjPtr<mirror::String> h(hs.NewHandleWrapper(&s));
Hiroshi Yamauchi76f55b02015-08-21 16:10:39 -07001859 if (!kUseReadBarrier) {
1860 heap->IncrementDisableMovingGC(soa.Self());
1861 } else {
1862 // For the CC collector, we only need to wait for the thread flip rather than the whole GC
1863 // to occur thanks to the to-space invariant.
1864 heap->IncrementDisableThreadFlip(soa.Self());
1865 }
Fred Shih56890e22014-06-02 11:11:52 -07001866 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001867 if (s->IsCompressed()) {
1868 if (is_copy != nullptr) {
1869 *is_copy = JNI_TRUE;
1870 }
1871 int32_t length = s->GetLength();
1872 jchar* chars = new jchar[length];
1873 for (int i = 0; i < length; ++i) {
1874 chars[i] = s->CharAt(i);
1875 }
1876 return chars;
1877 } else {
1878 if (is_copy != nullptr) {
1879 *is_copy = JNI_FALSE;
1880 }
1881 return static_cast<jchar*>(s->GetValue());
Fred Shih56890e22014-06-02 11:11:52 -07001882 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001883 }
1884
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001885 static void ReleaseStringCritical(JNIEnv* env,
1886 jstring java_string,
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001887 const jchar* chars) {
Fred Shih56890e22014-06-02 11:11:52 -07001888 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1889 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001890 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier0795f232016-09-27 18:43:30 -07001891 ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
Jeff Hao848f70a2014-01-15 13:49:50 -08001892 if (heap->IsMovableObject(s)) {
Hiroshi Yamauchi76f55b02015-08-21 16:10:39 -07001893 if (!kUseReadBarrier) {
1894 heap->DecrementDisableMovingGC(soa.Self());
1895 } else {
1896 heap->DecrementDisableThreadFlip(soa.Self());
1897 }
Fred Shih56890e22014-06-02 11:11:52 -07001898 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001899 if (s->IsCompressed() || (s->IsCompressed() == false && s->GetValue() != chars)) {
1900 delete[] chars;
1901 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001902 }
1903
Elliott Hughes75770752011-08-24 17:52:38 -07001904 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001905 if (java_string == nullptr) {
1906 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07001907 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001908 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07001909 *is_copy = JNI_TRUE;
1910 }
Ian Rogersef28b142012-11-30 14:22:18 -08001911 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07001912 ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001913 size_t byte_count = s->GetUtfLength();
1914 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001915 CHECK(bytes != nullptr); // bionic aborts anyway.
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001916 if (s->IsCompressed()) {
1917 for (size_t i = 0; i < byte_count; ++i) {
1918 bytes[i] = s->CharAt(i);
1919 }
1920 } else {
1921 const uint16_t* chars = s->GetValue();
1922 ConvertUtf16ToModifiedUtf8(bytes, byte_count, chars, s->GetLength());
1923 }
Elliott Hughes75770752011-08-24 17:52:38 -07001924 bytes[byte_count] = '\0';
1925 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001926 }
1927
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001928 static void ReleaseStringUTFChars(JNIEnv*, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001929 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001930 }
1931
Elliott Hughesbd935992011-08-22 11:59:34 -07001932 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001933 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001934 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07001935 ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07001936 if (UNLIKELY(!obj->IsArrayInstance())) {
David Sehr709b0702016-10-13 09:12:37 -07001937 soa.Vm()->JniAbortF("GetArrayLength", "not an array: %s", obj->PrettyTypeOf().c_str());
Ian Rogers68d8b422014-07-17 11:09:10 -07001938 return 0;
Elliott Hughes96a98872012-12-19 14:21:15 -08001939 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001940 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07001941 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001942 }
1943
Elliott Hughes814e4032011-08-23 12:07:56 -07001944 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001945 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001946 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07001947 ObjPtr<mirror::ObjectArray<mirror::Object>> array =
1948 soa.Decode<mirror::ObjectArray<mirror::Object>>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001949 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001950 }
1951
Ian Rogersbc939662013-08-15 10:26:54 -07001952 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
1953 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001954 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001955 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07001956 ObjPtr<mirror::ObjectArray<mirror::Object>> array =
1957 soa.Decode<mirror::ObjectArray<mirror::Object>>(java_array);
1958 ObjPtr<mirror::Object> value = soa.Decode<mirror::Object>(java_value);
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07001959 array->Set<false>(index, value.Ptr());
Elliott Hughescdf53122011-08-19 15:46:09 -07001960 }
1961
1962 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001963 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001964 }
1965
1966 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001967 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001968 }
1969
1970 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001971 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001972 }
1973
1974 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001975 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001976 }
1977
1978 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001979 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001980 }
1981
1982 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001983 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 }
1985
1986 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001987 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001988 }
1989
Ian Rogers1d99e452014-01-02 17:36:41 -08001990 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
1991 jobject initial_element) {
1992 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001993 JavaVmExtFromEnv(env)->JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08001994 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08001995 }
Ian Rogers2d10b202014-05-12 19:15:18 -07001996 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001997
1998 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07001999 ScopedObjectAccess soa(env);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07002000 ObjPtr<mirror::Class> array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002001 {
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07002002 ObjPtr<mirror::Class> element_class = soa.Decode<mirror::Class>(element_jclass).Ptr();
Ian Rogers1d99e452014-01-02 17:36:41 -08002003 if (UNLIKELY(element_class->IsPrimitive())) {
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07002004 soa.Vm()->JniAbortF("NewObjectArray",
2005 "not an object type: %s",
David Sehr709b0702016-10-13 09:12:37 -07002006 element_class->PrettyDescriptor().c_str());
Ian Rogers1d99e452014-01-02 17:36:41 -08002007 return nullptr;
2008 }
Ian Rogers1d99e452014-01-02 17:36:41 -08002009 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -07002010 array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08002011 if (UNLIKELY(array_class == nullptr)) {
2012 return nullptr;
2013 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002014 }
2015
Elliott Hughes75770752011-08-24 17:52:38 -07002016 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002017 mirror::ObjectArray<mirror::Object>* result =
2018 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002019 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartier0795f232016-09-27 18:43:30 -07002020 ObjPtr<mirror::Object> initial_object = soa.Decode<mirror::Object>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002021 if (initial_object != nullptr) {
2022 mirror::Class* element_class = result->GetClass()->GetComponentType();
2023 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002024 soa.Vm()->JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with "
2025 "element type of '%s'",
David Sehr709b0702016-10-13 09:12:37 -07002026 mirror::Class::PrettyDescriptor(initial_object->GetClass()).c_str(),
2027 element_class->PrettyDescriptor().c_str());
Ian Rogers68d8b422014-07-17 11:09:10 -07002028 return nullptr;
Ian Rogers1d99e452014-01-02 17:36:41 -08002029 } else {
2030 for (jsize i = 0; i < length; ++i) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002031 result->SetWithoutChecks<false>(i, initial_object.Ptr());
Ian Rogers1d99e452014-01-02 17:36:41 -08002032 }
2033 }
Elliott Hughes75770752011-08-24 17:52:38 -07002034 }
2035 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002036 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 }
2038
2039 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002040 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002041 }
2042
Ian Rogersa15e67d2012-02-28 13:51:55 -08002043 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002044 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002045 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07002046 ObjPtr<mirror::Array> array = soa.Decode<mirror::Array>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002047 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002048 soa.Vm()->JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
David Sehr709b0702016-10-13 09:12:37 -07002049 array->GetClass()->PrettyDescriptor().c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07002050 return nullptr;
2051 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002052 gc::Heap* heap = Runtime::Current()->GetHeap();
2053 if (heap->IsMovableObject(array)) {
Hiroshi Yamauchi76f55b02015-08-21 16:10:39 -07002054 if (!kUseReadBarrier) {
2055 heap->IncrementDisableMovingGC(soa.Self());
2056 } else {
2057 // For the CC collector, we only need to wait for the thread flip rather than the whole GC
2058 // to occur thanks to the to-space invariant.
2059 heap->IncrementDisableThreadFlip(soa.Self());
2060 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002061 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartier0795f232016-09-27 18:43:30 -07002062 array = soa.Decode<mirror::Array>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002063 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002064 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002065 *is_copy = JNI_FALSE;
2066 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002067 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002068 }
2069
Ian Rogers2d10b202014-05-12 19:15:18 -07002070 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
2071 jint mode) {
2072 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2073 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07002074 ObjPtr<mirror::Array> array = soa.Decode<mirror::Array>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002075 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002076 soa.Vm()->JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
David Sehr709b0702016-10-13 09:12:37 -07002077 array->GetClass()->PrettyDescriptor().c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07002078 return;
2079 }
2080 const size_t component_size = array->GetClass()->GetComponentSize();
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002081 ReleasePrimitiveArray(soa, array.Ptr(), component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002082 }
2083
Elliott Hughes75770752011-08-24 17:52:38 -07002084 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002085 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002086 }
2087
Elliott Hughes75770752011-08-24 17:52:38 -07002088 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002089 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 }
2091
Elliott Hughes75770752011-08-24 17:52:38 -07002092 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002093 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002094 }
2095
Elliott Hughes75770752011-08-24 17:52:38 -07002096 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002097 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002098 }
2099
Elliott Hughes75770752011-08-24 17:52:38 -07002100 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002101 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002102 }
2103
Elliott Hughes75770752011-08-24 17:52:38 -07002104 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002105 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002106 }
2107
Elliott Hughes75770752011-08-24 17:52:38 -07002108 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002109 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002110 }
2111
Elliott Hughes75770752011-08-24 17:52:38 -07002112 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002113 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002114 }
2115
Mathieu Chartier590fee92013-09-13 13:46:47 -07002116 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2117 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002118 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
2119 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002120 }
2121
Mathieu Chartier590fee92013-09-13 13:46:47 -07002122 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002123 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002124 }
2125
Mathieu Chartier590fee92013-09-13 13:46:47 -07002126 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002127 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002128 }
2129
Mathieu Chartier590fee92013-09-13 13:46:47 -07002130 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2131 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002132 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002133 }
2134
Mathieu Chartier590fee92013-09-13 13:46:47 -07002135 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2136 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002137 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002138 }
2139
Mathieu Chartier590fee92013-09-13 13:46:47 -07002140 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002141 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002142 }
2143
Mathieu Chartier590fee92013-09-13 13:46:47 -07002144 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002145 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002146 }
2147
Mathieu Chartier590fee92013-09-13 13:46:47 -07002148 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2149 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002150 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 }
2152
Ian Rogersbc939662013-08-15 10:26:54 -07002153 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2154 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002155 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002156 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002157 }
2158
Ian Rogersbc939662013-08-15 10:26:54 -07002159 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2160 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002161 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002162 }
2163
Ian Rogersbc939662013-08-15 10:26:54 -07002164 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2165 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002166 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002167 }
2168
Ian Rogersbc939662013-08-15 10:26:54 -07002169 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2170 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002171 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002172 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002173 }
2174
Ian Rogersbc939662013-08-15 10:26:54 -07002175 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2176 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002177 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002178 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002179 }
2180
Ian Rogersbc939662013-08-15 10:26:54 -07002181 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2182 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002183 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002184 }
2185
Ian Rogersbc939662013-08-15 10:26:54 -07002186 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2187 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002188 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 }
2190
Ian Rogersbc939662013-08-15 10:26:54 -07002191 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2192 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002193 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002194 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002195 }
2196
Ian Rogersbc939662013-08-15 10:26:54 -07002197 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2198 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002199 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002200 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002201 }
2202
Ian Rogersbc939662013-08-15 10:26:54 -07002203 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2204 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002205 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002206 }
2207
Ian Rogersbc939662013-08-15 10:26:54 -07002208 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2209 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002210 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002211 }
2212
Ian Rogersbc939662013-08-15 10:26:54 -07002213 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2214 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002215 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002216 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002217 }
2218
Ian Rogersbc939662013-08-15 10:26:54 -07002219 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2220 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002221 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002222 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002223 }
2224
Ian Rogersbc939662013-08-15 10:26:54 -07002225 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2226 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002227 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002228 }
2229
Ian Rogersbc939662013-08-15 10:26:54 -07002230 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2231 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002232 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002233 }
2234
Ian Rogersbc939662013-08-15 10:26:54 -07002235 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2236 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002237 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002238 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002239 }
2240
Andreas Gampe20acb0c2017-05-25 11:33:50 -07002241 static jint RegisterNatives(JNIEnv* env,
2242 jclass java_class,
2243 const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07002244 jint method_count) {
Ian Rogersbc939662013-08-15 10:26:54 -07002245 if (UNLIKELY(method_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002246 JavaVmExtFromEnv(env)->JniAbortF("RegisterNatives", "negative method count: %d",
2247 method_count);
2248 return JNI_ERR; // Not reached except in unit tests.
Ian Rogersbc939662013-08-15 10:26:54 -07002249 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002250 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002251 ScopedObjectAccess soa(env);
Alex Light65af20b2017-04-20 09:15:08 -07002252 StackHandleScope<1> hs(soa.Self());
2253 Handle<mirror::Class> c = hs.NewHandle(soa.Decode<mirror::Class>(java_class));
Ian Rogersbc939662013-08-15 10:26:54 -07002254 if (UNLIKELY(method_count == 0)) {
2255 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
Alex Light65af20b2017-04-20 09:15:08 -07002256 << c->PrettyDescriptor();
Ian Rogersbc939662013-08-15 10:26:54 -07002257 return JNI_OK;
2258 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002259 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002260 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002261 const char* name = methods[i].name;
2262 const char* sig = methods[i].signature;
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002263 const void* fnPtr = methods[i].fnPtr;
2264 if (UNLIKELY(name == nullptr)) {
Andreas Gampe20acb0c2017-05-25 11:33:50 -07002265 ReportInvalidJNINativeMethod(soa, c.Get(), "method name", i);
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002266 return JNI_ERR;
2267 } else if (UNLIKELY(sig == nullptr)) {
Andreas Gampe20acb0c2017-05-25 11:33:50 -07002268 ReportInvalidJNINativeMethod(soa, c.Get(), "method signature", i);
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002269 return JNI_ERR;
2270 } else if (UNLIKELY(fnPtr == nullptr)) {
Andreas Gampe20acb0c2017-05-25 11:33:50 -07002271 ReportInvalidJNINativeMethod(soa, c.Get(), "native function", i);
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002272 return JNI_ERR;
2273 }
Ian Rogers1eb512d2013-10-18 15:42:20 -07002274 bool is_fast = false;
Hiroshi Yamauchi36bce582015-05-12 12:16:10 -07002275 // Notes about fast JNI calls:
2276 //
2277 // On a normal JNI call, the calling thread usually transitions
2278 // from the kRunnable state to the kNative state. But if the
2279 // called native function needs to access any Java object, it
2280 // will have to transition back to the kRunnable state.
2281 //
2282 // There is a cost to this double transition. For a JNI call
2283 // that should be quick, this cost may dominate the call cost.
2284 //
2285 // On a fast JNI call, the calling thread avoids this double
2286 // transition by not transitioning from kRunnable to kNative and
2287 // stays in the kRunnable state.
2288 //
2289 // There are risks to using a fast JNI call because it can delay
2290 // a response to a thread suspension request which is typically
2291 // used for a GC root scanning, etc. If a fast JNI call takes a
2292 // long time, it could cause longer thread suspension latency
2293 // and GC pauses.
2294 //
2295 // Thus, fast JNI should be used with care. It should be used
2296 // for a JNI call that takes a short amount of time (eg. no
2297 // long-running loop) and does not block (eg. no locks, I/O,
2298 // etc.)
2299 //
2300 // A '!' prefix in the signature in the JNINativeMethod
2301 // indicates that it's a fast JNI call and the runtime omits the
2302 // thread state transition from kRunnable to kNative at the
2303 // entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002304 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002305 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002306 ++sig;
2307 }
2308
Andreas Gampe3f1dc562015-05-18 15:52:22 -07002309 // Note: the right order is to try to find the method locally
2310 // first, either as a direct or a virtual method. Then move to
2311 // the parent.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002312 ArtMethod* m = nullptr;
Andreas Gampe3f1dc562015-05-18 15:52:22 -07002313 bool warn_on_going_to_parent = down_cast<JNIEnvExt*>(env)->vm->IsCheckJniEnabled();
Alex Light65af20b2017-04-20 09:15:08 -07002314 for (ObjPtr<mirror::Class> current_class = c.Get();
Andreas Gampe3f1dc562015-05-18 15:52:22 -07002315 current_class != nullptr;
2316 current_class = current_class->GetSuperClass()) {
2317 // Search first only comparing methods which are native.
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002318 m = FindMethod<true>(current_class.Ptr(), name, sig);
Andreas Gampe3f1dc562015-05-18 15:52:22 -07002319 if (m != nullptr) {
2320 break;
2321 }
2322
2323 // Search again comparing to all methods, to find non-native methods that match.
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002324 m = FindMethod<false>(current_class.Ptr(), name, sig);
Andreas Gampe3f1dc562015-05-18 15:52:22 -07002325 if (m != nullptr) {
2326 break;
2327 }
2328
2329 if (warn_on_going_to_parent) {
2330 LOG(WARNING) << "CheckJNI: method to register \"" << name << "\" not in the given class. "
2331 << "This is slow, consider changing your RegisterNatives calls.";
2332 warn_on_going_to_parent = false;
2333 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002334 }
Andreas Gampe3f1dc562015-05-18 15:52:22 -07002335
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002336 if (m == nullptr) {
Andreas Gampe20acb0c2017-05-25 11:33:50 -07002337 c->DumpClass(LOG_STREAM(ERROR), mirror::Class::kDumpClassFullDetail);
2338 LOG(ERROR)
Andreas Gampe3fec9ac2016-09-13 10:47:28 -07002339 << "Failed to register native method "
David Sehr709b0702016-10-13 09:12:37 -07002340 << c->PrettyDescriptor() << "." << name << sig << " in "
Ian Rogers0177e532014-02-11 16:30:46 -08002341 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Alex Light65af20b2017-04-20 09:15:08 -07002342 ThrowNoSuchMethodError(soa, c.Get(), name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002343 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002344 } else if (!m->IsNative()) {
Andreas Gampe20acb0c2017-05-25 11:33:50 -07002345 LOG(ERROR)
Andreas Gampe3fec9ac2016-09-13 10:47:28 -07002346 << "Failed to register non-native method "
David Sehr709b0702016-10-13 09:12:37 -07002347 << c->PrettyDescriptor() << "." << name << sig
Ian Rogersbc939662013-08-15 10:26:54 -07002348 << " as native";
Alex Light65af20b2017-04-20 09:15:08 -07002349 ThrowNoSuchMethodError(soa, c.Get(), name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002350 return JNI_ERR;
2351 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002352
David Sehr709b0702016-10-13 09:12:37 -07002353 VLOG(jni) << "[Registering JNI native method " << m->PrettyMethod() << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002354
Igor Murashkin3b6f4402017-02-16 16:13:17 -08002355 if (UNLIKELY(is_fast)) {
2356 // There are a few reasons to switch:
2357 // 1) We don't support !bang JNI anymore, it will turn to a hard error later.
2358 // 2) @FastNative is actually faster. At least 1.5x faster than !bang JNI.
2359 // and switching is super easy, remove ! in C code, add annotation in .java code.
2360 // 3) Good chance of hitting DCHECK failures in ScopedFastNativeObjectAccess
2361 // since that checks for presence of @FastNative and not for ! in the descriptor.
2362 LOG(WARNING) << "!bang JNI is deprecated. Switch to @FastNative for " << m->PrettyMethod();
2363 is_fast = false;
2364 // TODO: make this a hard register error in the future.
2365 }
2366
Alex Lightd78ddec2017-04-18 15:20:38 -07002367 const void* final_function_ptr = m->RegisterNative(fnPtr, is_fast);
2368 UNUSED(final_function_ptr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002369 }
2370 return JNI_OK;
2371 }
2372
Elliott Hughes5174fe62011-08-23 15:12:35 -07002373 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002374 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002375 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07002376 ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002377
David Sehr709b0702016-10-13 09:12:37 -07002378 VLOG(jni) << "[Unregistering JNI native methods for " << mirror::Class::PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002379
Ian Rogers2d10b202014-05-12 19:15:18 -07002380 size_t unregistered_count = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002381 auto pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Alex Lighte64300b2015-12-15 15:02:47 -08002382 for (auto& m : c->GetMethods(pointer_size)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002383 if (m.IsNative()) {
2384 m.UnregisterNative();
Ian Rogers2d10b202014-05-12 19:15:18 -07002385 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002386 }
2387 }
2388
Ian Rogers2d10b202014-05-12 19:15:18 -07002389 if (unregistered_count == 0) {
2390 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
David Sehr709b0702016-10-13 09:12:37 -07002391 << mirror::Class::PrettyDescriptor(c) << "' that contains no native methods";
Ian Rogers2d10b202014-05-12 19:15:18 -07002392 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002393 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002394 }
2395
Ian Rogers719d1a32014-03-06 12:13:39 -08002396 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002397 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002398 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07002399 ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(java_object);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002400 o = o->MonitorEnter(soa.Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002401 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002402 return JNI_ERR;
2403 }
Mathieu Chartierc4f39252016-10-05 18:32:08 -07002404 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002405 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002406 }
2407
Ian Rogers719d1a32014-03-06 12:13:39 -08002408 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002409 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002410 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07002411 ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002412 o->MonitorExit(soa.Self());
2413 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002414 return JNI_ERR;
2415 }
Mathieu Chartierc4f39252016-10-05 18:32:08 -07002416 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002417 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002418 }
2419
2420 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002421 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002422 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002423 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002424 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002425 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002426 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002427 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002428 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002429 }
2430
Elliott Hughescdf53122011-08-19 15:46:09 -07002431 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002432 if (capacity < 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002433 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64,
2434 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002435 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002436 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002437 if (address == nullptr && capacity != 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002438 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2439 "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002440 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002441 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002442
Brian Carlstrom85a93362014-06-25 09:30:52 -07002443 // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002444 if (capacity > INT_MAX) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002445 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2446 "buffer capacity greater than maximum jint: %" PRId64,
2447 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002448 return nullptr;
2449 }
Elliott Hughesb5681212013-03-29 17:29:22 -07002450 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002451 jint capacity_arg = static_cast<jint>(capacity);
2452
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002453 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2454 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002455 address_arg, capacity_arg);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002456 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002457 }
2458
Elliott Hughesb465ab02011-08-24 11:21:21 -07002459 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002460 return reinterpret_cast<void*>(env->GetLongField(
2461 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002462 }
2463
Elliott Hughesb465ab02011-08-24 11:21:21 -07002464 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002465 return static_cast<jlong>(env->GetIntField(
2466 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002467 }
2468
Andreas Gampea8763072014-12-20 00:08:35 -08002469 static jobjectRefType GetObjectRefType(JNIEnv* env ATTRIBUTE_UNUSED, jobject java_object) {
2470 if (java_object == nullptr) {
2471 return JNIInvalidRefType;
2472 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002473
2474 // Do we definitely know what kind of reference this is?
2475 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
Andreas Gampedc061d02016-10-24 13:19:37 -07002476 IndirectRefKind kind = IndirectReferenceTable::GetIndirectRefKind(ref);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002477 switch (kind) {
Ian Rogersc0542af2014-09-03 16:16:56 -07002478 case kLocal:
2479 return JNILocalRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002480 case kGlobal:
2481 return JNIGlobalRefType;
2482 case kWeakGlobal:
2483 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002484 case kHandleScopeOrInvalid:
Ian Rogersc0542af2014-09-03 16:16:56 -07002485 // Assume value is in a handle scope.
2486 return JNILocalRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002487 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002488 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
Andreas Gampea8763072014-12-20 00:08:35 -08002489 UNREACHABLE();
Elliott Hughescdf53122011-08-19 15:46:09 -07002490 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002491
2492 private:
Ian Rogers68d8b422014-07-17 11:09:10 -07002493 static jint EnsureLocalCapacityInternal(ScopedObjectAccess& soa, jint desired_capacity,
2494 const char* caller)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002495 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe88831082017-05-31 19:46:03 -07002496 if (desired_capacity < 0) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002497 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2498 return JNI_ERR;
2499 }
Andreas Gampe88831082017-05-31 19:46:03 -07002500
2501 std::string error_msg;
2502 if (!soa.Env()->locals.EnsureFreeCapacity(static_cast<size_t>(desired_capacity), &error_msg)) {
2503 std::string caller_error = android::base::StringPrintf("%s: %s", caller, error_msg.c_str());
2504 soa.Self()->ThrowOutOfMemoryError(caller_error.c_str());
2505 return JNI_ERR;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002506 }
Andreas Gampe88831082017-05-31 19:46:03 -07002507 return JNI_OK;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002508 }
2509
2510 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002511 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002512 ScopedObjectAccess soa(env);
Ian Rogers1d99e452014-01-02 17:36:41 -08002513 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002514 soa.Vm()->JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002515 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002516 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002517 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002518 return soa.AddLocalReference<JniT>(result);
2519 }
2520
Ian Rogers2d10b202014-05-12 19:15:18 -07002521 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2522 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2523 const char* fn_name, const char* operation)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002524 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0795f232016-09-27 18:43:30 -07002525 ObjPtr<ArtArrayT> array = soa.Decode<ArtArrayT>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002526 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002527 soa.Vm()->JniAbortF(fn_name,
2528 "attempt to %s %s primitive array elements with an object of type %s",
2529 operation,
David Sehr709b0702016-10-13 09:12:37 -07002530 mirror::Class::PrettyDescriptor(
2531 ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2532 mirror::Class::PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07002533 return nullptr;
2534 }
2535 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002536 return array.Ptr();
Ian Rogers2d10b202014-05-12 19:15:18 -07002537 }
2538
2539 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2540 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2541 CHECK_NON_NULL_ARGUMENT(java_array);
2542 ScopedObjectAccess soa(env);
2543 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2544 "GetArrayElements",
2545 "get");
2546 if (UNLIKELY(array == nullptr)) {
2547 return nullptr;
2548 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002549 // Only make a copy if necessary.
2550 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2551 if (is_copy != nullptr) {
2552 *is_copy = JNI_TRUE;
2553 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002554 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002555 size_t size = array->GetLength() * component_size;
2556 void* data = new uint64_t[RoundUp(size, 8) / 8];
2557 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002558 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002559 } else {
2560 if (is_copy != nullptr) {
2561 *is_copy = JNI_FALSE;
2562 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002563 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002564 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002565 }
2566
Ian Rogers2d10b202014-05-12 19:15:18 -07002567 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002568 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002569 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002570 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002571 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2572 "ReleaseArrayElements",
2573 "release");
2574 if (array == nullptr) {
2575 return;
2576 }
2577 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2578 }
2579
2580 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2581 size_t component_size, void* elements, jint mode)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002582 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002583 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002584 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002585 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002586 size_t bytes = array->GetLength() * component_size;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002587 if (is_copy) {
2588 // Sanity check: If elements is not the same as the java array's data, it better not be a
2589 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2590 // copies we make?
Mathieu Chartier9d156d52016-10-06 17:44:26 -07002591 if (heap->IsNonDiscontinuousSpaceHeapAddress(elements)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002592 soa.Vm()->JniAbortF("ReleaseArrayElements",
2593 "invalid element pointer %p, array elements are %p",
2594 reinterpret_cast<void*>(elements), array_data);
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002595 return;
2596 }
Mathieu Chartier24555ad2014-10-06 13:41:33 -07002597 if (mode != JNI_ABORT) {
2598 memcpy(array_data, elements, bytes);
2599 } else if (kWarnJniAbort && memcmp(array_data, elements, bytes) != 0) {
2600 // Warn if we have JNI_ABORT and the arrays don't match since this is usually an error.
2601 LOG(WARNING) << "Possible incorrect JNI_ABORT in Release*ArrayElements";
Andreas Gampe3fec9ac2016-09-13 10:47:28 -07002602 soa.Self()->DumpJavaStack(LOG_STREAM(WARNING));
Mathieu Chartier24555ad2014-10-06 13:41:33 -07002603 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002604 }
2605 if (mode != JNI_COMMIT) {
2606 if (is_copy) {
2607 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002608 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002609 // Non copy to a movable object must means that we had disabled the moving GC.
Hiroshi Yamauchi76f55b02015-08-21 16:10:39 -07002610 if (!kUseReadBarrier) {
2611 heap->DecrementDisableMovingGC(soa.Self());
2612 } else {
2613 heap->DecrementDisableThreadFlip(soa.Self());
2614 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002615 }
2616 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002617 }
2618
Ian Rogers2d10b202014-05-12 19:15:18 -07002619 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2620 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2621 jsize start, jsize length, ElementT* buf) {
2622 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2623 ScopedObjectAccess soa(env);
2624 ArtArrayT* array =
2625 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2626 "GetPrimitiveArrayRegion",
2627 "get region of");
2628 if (array != nullptr) {
Vladimir Marko795e3412015-11-06 16:57:03 +00002629 if (start < 0 || length < 0 || length > array->GetLength() - start) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002630 ThrowAIOOBE(soa, array, start, length, "src");
2631 } else {
2632 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2633 ElementT* data = array->GetData();
2634 memcpy(buf, data + start, length * sizeof(ElementT));
2635 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002636 }
2637 }
2638
Ian Rogers2d10b202014-05-12 19:15:18 -07002639 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2640 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2641 jsize start, jsize length, const ElementT* buf) {
2642 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2643 ScopedObjectAccess soa(env);
2644 ArtArrayT* array =
2645 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2646 "SetPrimitiveArrayRegion",
2647 "set region of");
2648 if (array != nullptr) {
Vladimir Marko795e3412015-11-06 16:57:03 +00002649 if (start < 0 || length < 0 || length > array->GetLength() - start) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002650 ThrowAIOOBE(soa, array, start, length, "dst");
2651 } else {
2652 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2653 ElementT* data = array->GetData();
2654 memcpy(data + start, buf, length * sizeof(ElementT));
2655 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002656 }
2657 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002658};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002659
Elliott Hughes88c5c352012-03-15 18:49:48 -07002660const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002661 nullptr, // reserved0.
2662 nullptr, // reserved1.
2663 nullptr, // reserved2.
2664 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002665 JNI::GetVersion,
2666 JNI::DefineClass,
2667 JNI::FindClass,
2668 JNI::FromReflectedMethod,
2669 JNI::FromReflectedField,
2670 JNI::ToReflectedMethod,
2671 JNI::GetSuperclass,
2672 JNI::IsAssignableFrom,
2673 JNI::ToReflectedField,
2674 JNI::Throw,
2675 JNI::ThrowNew,
2676 JNI::ExceptionOccurred,
2677 JNI::ExceptionDescribe,
2678 JNI::ExceptionClear,
2679 JNI::FatalError,
2680 JNI::PushLocalFrame,
2681 JNI::PopLocalFrame,
2682 JNI::NewGlobalRef,
2683 JNI::DeleteGlobalRef,
2684 JNI::DeleteLocalRef,
2685 JNI::IsSameObject,
2686 JNI::NewLocalRef,
2687 JNI::EnsureLocalCapacity,
2688 JNI::AllocObject,
2689 JNI::NewObject,
2690 JNI::NewObjectV,
2691 JNI::NewObjectA,
2692 JNI::GetObjectClass,
2693 JNI::IsInstanceOf,
2694 JNI::GetMethodID,
2695 JNI::CallObjectMethod,
2696 JNI::CallObjectMethodV,
2697 JNI::CallObjectMethodA,
2698 JNI::CallBooleanMethod,
2699 JNI::CallBooleanMethodV,
2700 JNI::CallBooleanMethodA,
2701 JNI::CallByteMethod,
2702 JNI::CallByteMethodV,
2703 JNI::CallByteMethodA,
2704 JNI::CallCharMethod,
2705 JNI::CallCharMethodV,
2706 JNI::CallCharMethodA,
2707 JNI::CallShortMethod,
2708 JNI::CallShortMethodV,
2709 JNI::CallShortMethodA,
2710 JNI::CallIntMethod,
2711 JNI::CallIntMethodV,
2712 JNI::CallIntMethodA,
2713 JNI::CallLongMethod,
2714 JNI::CallLongMethodV,
2715 JNI::CallLongMethodA,
2716 JNI::CallFloatMethod,
2717 JNI::CallFloatMethodV,
2718 JNI::CallFloatMethodA,
2719 JNI::CallDoubleMethod,
2720 JNI::CallDoubleMethodV,
2721 JNI::CallDoubleMethodA,
2722 JNI::CallVoidMethod,
2723 JNI::CallVoidMethodV,
2724 JNI::CallVoidMethodA,
2725 JNI::CallNonvirtualObjectMethod,
2726 JNI::CallNonvirtualObjectMethodV,
2727 JNI::CallNonvirtualObjectMethodA,
2728 JNI::CallNonvirtualBooleanMethod,
2729 JNI::CallNonvirtualBooleanMethodV,
2730 JNI::CallNonvirtualBooleanMethodA,
2731 JNI::CallNonvirtualByteMethod,
2732 JNI::CallNonvirtualByteMethodV,
2733 JNI::CallNonvirtualByteMethodA,
2734 JNI::CallNonvirtualCharMethod,
2735 JNI::CallNonvirtualCharMethodV,
2736 JNI::CallNonvirtualCharMethodA,
2737 JNI::CallNonvirtualShortMethod,
2738 JNI::CallNonvirtualShortMethodV,
2739 JNI::CallNonvirtualShortMethodA,
2740 JNI::CallNonvirtualIntMethod,
2741 JNI::CallNonvirtualIntMethodV,
2742 JNI::CallNonvirtualIntMethodA,
2743 JNI::CallNonvirtualLongMethod,
2744 JNI::CallNonvirtualLongMethodV,
2745 JNI::CallNonvirtualLongMethodA,
2746 JNI::CallNonvirtualFloatMethod,
2747 JNI::CallNonvirtualFloatMethodV,
2748 JNI::CallNonvirtualFloatMethodA,
2749 JNI::CallNonvirtualDoubleMethod,
2750 JNI::CallNonvirtualDoubleMethodV,
2751 JNI::CallNonvirtualDoubleMethodA,
2752 JNI::CallNonvirtualVoidMethod,
2753 JNI::CallNonvirtualVoidMethodV,
2754 JNI::CallNonvirtualVoidMethodA,
2755 JNI::GetFieldID,
2756 JNI::GetObjectField,
2757 JNI::GetBooleanField,
2758 JNI::GetByteField,
2759 JNI::GetCharField,
2760 JNI::GetShortField,
2761 JNI::GetIntField,
2762 JNI::GetLongField,
2763 JNI::GetFloatField,
2764 JNI::GetDoubleField,
2765 JNI::SetObjectField,
2766 JNI::SetBooleanField,
2767 JNI::SetByteField,
2768 JNI::SetCharField,
2769 JNI::SetShortField,
2770 JNI::SetIntField,
2771 JNI::SetLongField,
2772 JNI::SetFloatField,
2773 JNI::SetDoubleField,
2774 JNI::GetStaticMethodID,
2775 JNI::CallStaticObjectMethod,
2776 JNI::CallStaticObjectMethodV,
2777 JNI::CallStaticObjectMethodA,
2778 JNI::CallStaticBooleanMethod,
2779 JNI::CallStaticBooleanMethodV,
2780 JNI::CallStaticBooleanMethodA,
2781 JNI::CallStaticByteMethod,
2782 JNI::CallStaticByteMethodV,
2783 JNI::CallStaticByteMethodA,
2784 JNI::CallStaticCharMethod,
2785 JNI::CallStaticCharMethodV,
2786 JNI::CallStaticCharMethodA,
2787 JNI::CallStaticShortMethod,
2788 JNI::CallStaticShortMethodV,
2789 JNI::CallStaticShortMethodA,
2790 JNI::CallStaticIntMethod,
2791 JNI::CallStaticIntMethodV,
2792 JNI::CallStaticIntMethodA,
2793 JNI::CallStaticLongMethod,
2794 JNI::CallStaticLongMethodV,
2795 JNI::CallStaticLongMethodA,
2796 JNI::CallStaticFloatMethod,
2797 JNI::CallStaticFloatMethodV,
2798 JNI::CallStaticFloatMethodA,
2799 JNI::CallStaticDoubleMethod,
2800 JNI::CallStaticDoubleMethodV,
2801 JNI::CallStaticDoubleMethodA,
2802 JNI::CallStaticVoidMethod,
2803 JNI::CallStaticVoidMethodV,
2804 JNI::CallStaticVoidMethodA,
2805 JNI::GetStaticFieldID,
2806 JNI::GetStaticObjectField,
2807 JNI::GetStaticBooleanField,
2808 JNI::GetStaticByteField,
2809 JNI::GetStaticCharField,
2810 JNI::GetStaticShortField,
2811 JNI::GetStaticIntField,
2812 JNI::GetStaticLongField,
2813 JNI::GetStaticFloatField,
2814 JNI::GetStaticDoubleField,
2815 JNI::SetStaticObjectField,
2816 JNI::SetStaticBooleanField,
2817 JNI::SetStaticByteField,
2818 JNI::SetStaticCharField,
2819 JNI::SetStaticShortField,
2820 JNI::SetStaticIntField,
2821 JNI::SetStaticLongField,
2822 JNI::SetStaticFloatField,
2823 JNI::SetStaticDoubleField,
2824 JNI::NewString,
2825 JNI::GetStringLength,
2826 JNI::GetStringChars,
2827 JNI::ReleaseStringChars,
2828 JNI::NewStringUTF,
2829 JNI::GetStringUTFLength,
2830 JNI::GetStringUTFChars,
2831 JNI::ReleaseStringUTFChars,
2832 JNI::GetArrayLength,
2833 JNI::NewObjectArray,
2834 JNI::GetObjectArrayElement,
2835 JNI::SetObjectArrayElement,
2836 JNI::NewBooleanArray,
2837 JNI::NewByteArray,
2838 JNI::NewCharArray,
2839 JNI::NewShortArray,
2840 JNI::NewIntArray,
2841 JNI::NewLongArray,
2842 JNI::NewFloatArray,
2843 JNI::NewDoubleArray,
2844 JNI::GetBooleanArrayElements,
2845 JNI::GetByteArrayElements,
2846 JNI::GetCharArrayElements,
2847 JNI::GetShortArrayElements,
2848 JNI::GetIntArrayElements,
2849 JNI::GetLongArrayElements,
2850 JNI::GetFloatArrayElements,
2851 JNI::GetDoubleArrayElements,
2852 JNI::ReleaseBooleanArrayElements,
2853 JNI::ReleaseByteArrayElements,
2854 JNI::ReleaseCharArrayElements,
2855 JNI::ReleaseShortArrayElements,
2856 JNI::ReleaseIntArrayElements,
2857 JNI::ReleaseLongArrayElements,
2858 JNI::ReleaseFloatArrayElements,
2859 JNI::ReleaseDoubleArrayElements,
2860 JNI::GetBooleanArrayRegion,
2861 JNI::GetByteArrayRegion,
2862 JNI::GetCharArrayRegion,
2863 JNI::GetShortArrayRegion,
2864 JNI::GetIntArrayRegion,
2865 JNI::GetLongArrayRegion,
2866 JNI::GetFloatArrayRegion,
2867 JNI::GetDoubleArrayRegion,
2868 JNI::SetBooleanArrayRegion,
2869 JNI::SetByteArrayRegion,
2870 JNI::SetCharArrayRegion,
2871 JNI::SetShortArrayRegion,
2872 JNI::SetIntArrayRegion,
2873 JNI::SetLongArrayRegion,
2874 JNI::SetFloatArrayRegion,
2875 JNI::SetDoubleArrayRegion,
2876 JNI::RegisterNatives,
2877 JNI::UnregisterNatives,
2878 JNI::MonitorEnter,
2879 JNI::MonitorExit,
2880 JNI::GetJavaVM,
2881 JNI::GetStringRegion,
2882 JNI::GetStringUTFRegion,
2883 JNI::GetPrimitiveArrayCritical,
2884 JNI::ReleasePrimitiveArrayCritical,
2885 JNI::GetStringCritical,
2886 JNI::ReleaseStringCritical,
2887 JNI::NewWeakGlobalRef,
2888 JNI::DeleteWeakGlobalRef,
2889 JNI::ExceptionCheck,
2890 JNI::NewDirectByteBuffer,
2891 JNI::GetDirectBufferAddress,
2892 JNI::GetDirectBufferCapacity,
2893 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002894};
2895
Ian Rogers68d8b422014-07-17 11:09:10 -07002896const JNINativeInterface* GetJniNativeInterface() {
2897 return &gJniNativeInterface;
Elliott Hughes410c0c82011-09-01 17:58:25 -07002898}
2899
Mathieu Chartier4d87df62016-01-07 15:14:19 -08002900void (*gJniSleepForeverStub[])() = {
2901 nullptr, // reserved0.
2902 nullptr, // reserved1.
2903 nullptr, // reserved2.
2904 nullptr, // reserved3.
2905 SleepForever,
2906 SleepForever,
2907 SleepForever,
2908 SleepForever,
2909 SleepForever,
2910 SleepForever,
2911 SleepForever,
2912 SleepForever,
2913 SleepForever,
2914 SleepForever,
2915 SleepForever,
2916 SleepForever,
2917 SleepForever,
2918 SleepForever,
2919 SleepForever,
2920 SleepForever,
2921 SleepForever,
2922 SleepForever,
2923 SleepForever,
2924 SleepForever,
2925 SleepForever,
2926 SleepForever,
2927 SleepForever,
2928 SleepForever,
2929 SleepForever,
2930 SleepForever,
2931 SleepForever,
2932 SleepForever,
2933 SleepForever,
2934 SleepForever,
2935 SleepForever,
2936 SleepForever,
2937 SleepForever,
2938 SleepForever,
2939 SleepForever,
2940 SleepForever,
2941 SleepForever,
2942 SleepForever,
2943 SleepForever,
2944 SleepForever,
2945 SleepForever,
2946 SleepForever,
2947 SleepForever,
2948 SleepForever,
2949 SleepForever,
2950 SleepForever,
2951 SleepForever,
2952 SleepForever,
2953 SleepForever,
2954 SleepForever,
2955 SleepForever,
2956 SleepForever,
2957 SleepForever,
2958 SleepForever,
2959 SleepForever,
2960 SleepForever,
2961 SleepForever,
2962 SleepForever,
2963 SleepForever,
2964 SleepForever,
2965 SleepForever,
2966 SleepForever,
2967 SleepForever,
2968 SleepForever,
2969 SleepForever,
2970 SleepForever,
2971 SleepForever,
2972 SleepForever,
2973 SleepForever,
2974 SleepForever,
2975 SleepForever,
2976 SleepForever,
2977 SleepForever,
2978 SleepForever,
2979 SleepForever,
2980 SleepForever,
2981 SleepForever,
2982 SleepForever,
2983 SleepForever,
2984 SleepForever,
2985 SleepForever,
2986 SleepForever,
2987 SleepForever,
2988 SleepForever,
2989 SleepForever,
2990 SleepForever,
2991 SleepForever,
2992 SleepForever,
2993 SleepForever,
2994 SleepForever,
2995 SleepForever,
2996 SleepForever,
2997 SleepForever,
2998 SleepForever,
2999 SleepForever,
3000 SleepForever,
3001 SleepForever,
3002 SleepForever,
3003 SleepForever,
3004 SleepForever,
3005 SleepForever,
3006 SleepForever,
3007 SleepForever,
3008 SleepForever,
3009 SleepForever,
3010 SleepForever,
3011 SleepForever,
3012 SleepForever,
3013 SleepForever,
3014 SleepForever,
3015 SleepForever,
3016 SleepForever,
3017 SleepForever,
3018 SleepForever,
3019 SleepForever,
3020 SleepForever,
3021 SleepForever,
3022 SleepForever,
3023 SleepForever,
3024 SleepForever,
3025 SleepForever,
3026 SleepForever,
3027 SleepForever,
3028 SleepForever,
3029 SleepForever,
3030 SleepForever,
3031 SleepForever,
3032 SleepForever,
3033 SleepForever,
3034 SleepForever,
3035 SleepForever,
3036 SleepForever,
3037 SleepForever,
3038 SleepForever,
3039 SleepForever,
3040 SleepForever,
3041 SleepForever,
3042 SleepForever,
3043 SleepForever,
3044 SleepForever,
3045 SleepForever,
3046 SleepForever,
3047 SleepForever,
3048 SleepForever,
3049 SleepForever,
3050 SleepForever,
3051 SleepForever,
3052 SleepForever,
3053 SleepForever,
3054 SleepForever,
3055 SleepForever,
3056 SleepForever,
3057 SleepForever,
3058 SleepForever,
3059 SleepForever,
3060 SleepForever,
3061 SleepForever,
3062 SleepForever,
3063 SleepForever,
3064 SleepForever,
3065 SleepForever,
3066 SleepForever,
3067 SleepForever,
3068 SleepForever,
3069 SleepForever,
3070 SleepForever,
3071 SleepForever,
3072 SleepForever,
3073 SleepForever,
3074 SleepForever,
3075 SleepForever,
3076 SleepForever,
3077 SleepForever,
3078 SleepForever,
3079 SleepForever,
3080 SleepForever,
3081 SleepForever,
3082 SleepForever,
3083 SleepForever,
3084 SleepForever,
3085 SleepForever,
3086 SleepForever,
3087 SleepForever,
3088 SleepForever,
3089 SleepForever,
3090 SleepForever,
3091 SleepForever,
3092 SleepForever,
3093 SleepForever,
3094 SleepForever,
3095 SleepForever,
3096 SleepForever,
3097 SleepForever,
3098 SleepForever,
3099 SleepForever,
3100 SleepForever,
3101 SleepForever,
3102 SleepForever,
3103 SleepForever,
3104 SleepForever,
3105 SleepForever,
3106 SleepForever,
3107 SleepForever,
3108 SleepForever,
3109 SleepForever,
3110 SleepForever,
3111 SleepForever,
3112 SleepForever,
3113 SleepForever,
3114 SleepForever,
3115 SleepForever,
3116 SleepForever,
3117 SleepForever,
3118 SleepForever,
3119 SleepForever,
3120 SleepForever,
3121 SleepForever,
3122 SleepForever,
3123 SleepForever,
3124 SleepForever,
3125 SleepForever,
3126 SleepForever,
3127 SleepForever,
3128 SleepForever,
3129 SleepForever,
3130 SleepForever,
3131 SleepForever,
3132 SleepForever,
3133 SleepForever,
3134};
3135
3136const JNINativeInterface* GetRuntimeShutdownNativeInterface() {
3137 return reinterpret_cast<JNINativeInterface*>(&gJniSleepForeverStub);
3138}
3139
Ian Rogersdf20fe02011-07-20 20:34:16 -07003140} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003141
3142std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3143 switch (rhs) {
3144 case JNIInvalidRefType:
3145 os << "JNIInvalidRefType";
3146 return os;
3147 case JNILocalRefType:
3148 os << "JNILocalRefType";
3149 return os;
3150 case JNIGlobalRefType:
3151 os << "JNIGlobalRefType";
3152 return os;
3153 case JNIWeakGlobalRefType:
3154 os << "JNIWeakGlobalRefType";
3155 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003156 default:
Andreas Gampe3fec9ac2016-09-13 10:47:28 -07003157 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Ian Rogersc7dd2952014-10-21 23:31:19 -07003158 UNREACHABLE();
Elliott Hughesb465ab02011-08-24 11:21:21 -07003159 }
3160}