blob: 9dbcded867432dd03586d65ec4cb0d5856166a78 [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"
Mathieu Chartierbad02672014-08-25 13:08:22 -070028#include "base/allocator.h"
David Sehr8f4b0562018-03-02 12:01:51 -080029#include "base/atomic.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070030#include "base/enums.h"
Andreas Gampe57943812017-12-06 21:39:13 -080031#include "base/logging.h" // For VLOG.
Elliott Hughes76b61672012-12-12 17:47:30 -080032#include "base/mutex.h"
David Sehr67bf42e2018-02-26 16:43:04 -080033#include "base/safe_map.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080034#include "base/stl_util.h"
Ian Rogers98379392014-02-24 16:53:16 -080035#include "class_linker-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080036#include "dex/dex_file-inl.h"
David Sehr0225f8e2018-01-31 08:52:24 +000037#include "dex/utf.h"
Mathieu Chartierd0004802014-10-15 16:59:47 -070038#include "fault_handler.h"
David Brazdil5a61bb72018-01-19 16:59:46 +000039#include "hidden_api.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070040#include "gc/accounting/card_table-inl.h"
Steven Morelande431e272017-07-18 16:53:49 -070041#include "gc_root.h"
Mathieu Chartierc56057e2014-05-04 13:18:58 -070042#include "indirect_reference_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070043#include "interpreter/interpreter.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070044#include "java_vm_ext.h"
Steven Morelande431e272017-07-18 16:53:49 -070045#include "jni_env_ext.h"
Alex Lighte00ec302017-06-16 08:56:43 -070046#include "jvalue-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047#include "mirror/class-inl.h"
48#include "mirror/class_loader.h"
Hiroshi Yamauchi02d2f292015-04-03 13:35:16 -070049#include "mirror/field-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070050#include "mirror/method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051#include "mirror/object-inl.h"
52#include "mirror/object_array-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070053#include "mirror/string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054#include "mirror/throwable.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070055#include "nativehelper/scoped_local_ref.h"
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080056#include "parsed_options.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070057#include "reflection.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070058#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070059#include "scoped_thread_state_change-inl.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070060#include "thread.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070061#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070062
George Burgess IV984387f2017-06-22 15:51:11 -070063namespace {
64// Frees the given va_list upon destruction.
65// This also guards the returns from inside of the CHECK_NON_NULL_ARGUMENTs.
66struct ScopedVAArgs {
67 explicit ScopedVAArgs(va_list* args): args(args) {}
68 ScopedVAArgs(const ScopedVAArgs&) = delete;
69 ScopedVAArgs(ScopedVAArgs&&) = delete;
70 ~ScopedVAArgs() { va_end(*args); }
71
72 private:
73 va_list* args;
74};
75} // namespace
76
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070077namespace art {
78
Mathieu Chartier24555ad2014-10-06 13:41:33 -070079// Consider turning this on when there is errors which could be related to JNI array copies such as
80// things not rendering correctly. E.g. b/16858794
81static constexpr bool kWarnJniAbort = false;
82
David Brazdilf2a300b2018-03-27 08:14:25 +000083static bool IsCallerInPlatformDex(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_) {
84 return hiddenapi::IsCallerInPlatformDex(GetCallingClass(self, /* num_frames */ 1));
David Brazdila02cb112018-01-31 11:36:39 +000085}
86
87template<typename T>
88ALWAYS_INLINE static bool ShouldBlockAccessToMember(T* member, Thread* self)
89 REQUIRES_SHARED(Locks::mutator_lock_) {
Narayan Kamathe453a8d2018-04-03 15:23:46 +010090 hiddenapi::Action action = hiddenapi::GetMemberAction(
David Brazdilf2a300b2018-03-27 08:14:25 +000091 member, self, IsCallerInPlatformDex, hiddenapi::kJNI);
Narayan Kamathe453a8d2018-04-03 15:23:46 +010092 if (action != hiddenapi::kAllow) {
93 hiddenapi::NotifyHiddenApiListener(member);
94 }
95
96 return action == hiddenapi::kDeny;
David Brazdila02cb112018-01-31 11:36:39 +000097}
98
99// Helpers to call instrumentation functions for fields. These take jobjects so we don't need to set
100// up handles for the rare case where these actually do something. Once these functions return it is
101// possible there will be a pending exception if the instrumentation happens to throw one.
Alex Lighte00ec302017-06-16 08:56:43 -0700102static void NotifySetObjectField(ArtField* field, jobject obj, jobject jval)
103 REQUIRES_SHARED(Locks::mutator_lock_) {
104 DCHECK_EQ(field->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
105 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
106 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
David Brazdila02cb112018-01-31 11:36:39 +0000107 Thread* self = Thread::Current();
Alex Lighte00ec302017-06-16 08:56:43 -0700108 ArtMethod* cur_method = self->GetCurrentMethod(/*dex_pc*/ nullptr,
109 /*check_suspended*/ true,
110 /*abort_on_error*/ false);
111
112 if (cur_method == nullptr) {
113 // Set/Get Fields can be issued without a method during runtime startup/teardown. Ignore all
114 // of these changes.
115 return;
116 }
117 DCHECK(cur_method->IsNative());
118 JValue val;
119 val.SetL(self->DecodeJObject(jval));
120 instrumentation->FieldWriteEvent(self,
121 self->DecodeJObject(obj).Ptr(),
122 cur_method,
123 0, // dex_pc is always 0 since this is a native method.
124 field,
125 val);
126 }
127}
128
129static void NotifySetPrimitiveField(ArtField* field, jobject obj, JValue val)
130 REQUIRES_SHARED(Locks::mutator_lock_) {
131 DCHECK_NE(field->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
132 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
133 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
David Brazdila02cb112018-01-31 11:36:39 +0000134 Thread* self = Thread::Current();
Alex Lighte00ec302017-06-16 08:56:43 -0700135 ArtMethod* cur_method = self->GetCurrentMethod(/*dex_pc*/ nullptr,
136 /*check_suspended*/ true,
137 /*abort_on_error*/ false);
138
139 if (cur_method == nullptr) {
140 // Set/Get Fields can be issued without a method during runtime startup/teardown. Ignore all
141 // of these changes.
142 return;
143 }
144 DCHECK(cur_method->IsNative());
145 instrumentation->FieldWriteEvent(self,
146 self->DecodeJObject(obj).Ptr(),
147 cur_method,
148 0, // dex_pc is always 0 since this is a native method.
149 field,
150 val);
151 }
152}
153
154static void NotifyGetField(ArtField* field, jobject obj)
155 REQUIRES_SHARED(Locks::mutator_lock_) {
156 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
157 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
David Brazdila02cb112018-01-31 11:36:39 +0000158 Thread* self = Thread::Current();
Alex Lighte00ec302017-06-16 08:56:43 -0700159 ArtMethod* cur_method = self->GetCurrentMethod(/*dex_pc*/ nullptr,
160 /*check_suspended*/ true,
161 /*abort_on_error*/ false);
162
163 if (cur_method == nullptr) {
164 // Set/Get Fields can be issued without a method during runtime startup/teardown. Ignore all
165 // of these changes.
166 return;
167 }
168 DCHECK(cur_method->IsNative());
169 instrumentation->FieldReadEvent(self,
170 self->DecodeJObject(obj).Ptr(),
171 cur_method,
172 0, // dex_pc is always 0 since this is a native method.
173 field);
174 }
175}
176
Elliott Hughes6b436852011-08-12 10:16:44 -0700177// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
178// separated with slashes but aren't wrapped with "L;" like regular descriptors
179// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
180// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
181// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700182static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700183 std::string result;
184 // Add the missing "L;" if necessary.
185 if (name[0] == '[') {
186 result = name;
187 } else {
188 result += 'L';
189 result += name;
190 result += ';';
191 }
192 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700193 if (result.find('.') != std::string::npos) {
194 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
195 << "\"" << name << "\"";
196 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700197 }
198 return result;
199}
200
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700201static void ThrowNoSuchMethodError(ScopedObjectAccess& soa,
202 ObjPtr<mirror::Class> c,
203 const char* name,
204 const char* sig,
205 const char* kind)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700206 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700207 std::string temp;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000208 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Ian Rogers62d6c772013-02-27 08:32:07 -0800209 "no %s method \"%s.%s%s\"",
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700210 kind,
211 c->GetDescriptor(&temp),
212 name,
213 sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700214}
215
Mathieu Chartier3398c782016-09-30 10:27:43 -0700216static void ReportInvalidJNINativeMethod(const ScopedObjectAccess& soa,
217 ObjPtr<mirror::Class> c,
218 const char* kind,
Andreas Gampe20acb0c2017-05-25 11:33:50 -0700219 jint idx)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700220 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe20acb0c2017-05-25 11:33:50 -0700221 LOG(ERROR)
David Sehr709b0702016-10-13 09:12:37 -0700222 << "Failed to register native method in " << c->PrettyDescriptor()
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700223 << " in " << c->GetDexCache()->GetLocation()->ToModifiedUtf8()
Sebastien Hertzfa65e842014-07-03 09:39:53 +0200224 << ": " << kind << " is null at index " << idx;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000225 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
Mathieu Chartier3398c782016-09-30 10:27:43 -0700226 "%s is null at index %d",
227 kind,
228 idx);
Sebastien Hertzfa65e842014-07-03 09:39:53 +0200229}
230
Mathieu Chartier0795f232016-09-27 18:43:30 -0700231static ObjPtr<mirror::Class> EnsureInitialized(Thread* self, ObjPtr<mirror::Class> klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700232 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800233 if (LIKELY(klass->IsInitialized())) {
234 return klass;
235 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700236 StackHandleScope<1> hs(self);
237 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700238 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800239 return nullptr;
240 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700241 return h_klass.Get();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800242}
243
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700244static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
245 const char* name, const char* sig, bool is_static)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700246 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700247 ObjPtr<mirror::Class> c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class>(jni_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800248 if (c == nullptr) {
249 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700250 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700251 ArtMethod* method = nullptr;
252 auto pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Vladimir Markoba118822017-06-12 15:41:56 +0100253 if (c->IsInterface()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700254 method = c->FindInterfaceMethod(name, sig, pointer_size);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700255 } else {
Vladimir Markoba118822017-06-12 15:41:56 +0100256 method = c->FindClassMethod(name, sig, pointer_size);
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700257 }
David Brazdila02cb112018-01-31 11:36:39 +0000258 if (method != nullptr && ShouldBlockAccessToMember(method, soa.Self())) {
David Brazdil5a61bb72018-01-19 16:59:46 +0000259 method = nullptr;
260 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800261 if (method == nullptr || method->IsStatic() != is_static) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700262 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800263 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -0700264 }
Andreas Gampe13b27842016-11-07 16:48:23 -0800265 return jni::EncodeArtMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700266}
267
Mathieu Chartier0795f232016-09-27 18:43:30 -0700268static ObjPtr<mirror::ClassLoader> GetClassLoader(const ScopedObjectAccess& soa)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700269 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700270 ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -0700271 // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
Andreas Gampe13b27842016-11-07 16:48:23 -0800272 if (method == jni::DecodeArtMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700273 return soa.Decode<mirror::ClassLoader>(soa.Self()->GetClassLoaderOverride());
Brian Carlstrom00fae582011-10-28 01:16:28 -0700274 }
Brian Carlstromce888532013-10-10 00:32:58 -0700275 // If we have a method, use its ClassLoader for context.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800276 if (method != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700277 return method->GetDeclaringClass()->GetClassLoader();
278 }
279 // We don't have a method, so try to use the system ClassLoader.
Mathieu Chartier0795f232016-09-27 18:43:30 -0700280 ObjPtr<mirror::ClassLoader> class_loader =
281 soa.Decode<mirror::ClassLoader>(Runtime::Current()->GetSystemClassLoader());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800282 if (class_loader != nullptr) {
Brian Carlstromce888532013-10-10 00:32:58 -0700283 return class_loader;
284 }
285 // See if the override ClassLoader is set for gtests.
Mathieu Chartier0795f232016-09-27 18:43:30 -0700286 class_loader = soa.Decode<mirror::ClassLoader>(soa.Self()->GetClassLoaderOverride());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800287 if (class_loader != nullptr) {
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700288 // If so, CommonCompilerTest should have marked the runtime as a compiler not compiling an
289 // image.
290 CHECK(Runtime::Current()->IsAotCompiler());
Andreas Gampe4585f872015-03-27 23:45:15 -0700291 CHECK(!Runtime::Current()->IsCompilingBootImage());
Brian Carlstromce888532013-10-10 00:32:58 -0700292 return class_loader;
293 }
294 // Use the BOOTCLASSPATH.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800295 return nullptr;
Brian Carlstrom00fae582011-10-28 01:16:28 -0700296}
297
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700298static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
299 const char* sig, bool is_static)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700300 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700301 StackHandleScope<2> hs(soa.Self());
302 Handle<mirror::Class> c(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700303 hs.NewHandle(EnsureInitialized(soa.Self(), soa.Decode<mirror::Class>(jni_class))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800304 if (c == nullptr) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800305 return nullptr;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700306 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700307 ArtField* field = nullptr;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800308 mirror::Class* field_type;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700309 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
310 if (sig[1] != '\0') {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700311 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(c->GetClassLoader()));
Ian Rogers98379392014-02-24 16:53:16 -0800312 field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700313 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700314 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700315 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800316 if (field_type == nullptr) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700317 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700318 DCHECK(soa.Self()->IsExceptionPending());
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800319 StackHandleScope<1> hs2(soa.Self());
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000320 Handle<mirror::Throwable> cause(hs2.NewHandle(soa.Self()->GetException()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700321 soa.Self()->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700322 std::string temp;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000323 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800324 "no type \"%s\" found and so no field \"%s\" "
325 "could be found in class \"%s\" or its superclasses", sig, name,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700326 c->GetDescriptor(&temp));
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000327 soa.Self()->GetException()->SetCause(cause.Get());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800328 return nullptr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700329 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700330 std::string temp;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700331 if (is_static) {
Vladimir Marko19a4d372016-12-08 14:41:46 +0000332 field = mirror::Class::FindStaticField(
333 soa.Self(), c.Get(), name, field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700334 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700335 field = c->FindInstanceField(name, field_type->GetDescriptor(&temp));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700336 }
David Brazdila02cb112018-01-31 11:36:39 +0000337 if (field != nullptr && ShouldBlockAccessToMember(field, soa.Self())) {
David Brazdil5a61bb72018-01-19 16:59:46 +0000338 field = nullptr;
339 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800340 if (field == nullptr) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000341 soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Ian Rogers62d6c772013-02-27 08:32:07 -0800342 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700343 sig, name, c->GetDescriptor(&temp));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800344 return nullptr;
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700345 }
Andreas Gampe08883de2016-11-08 13:20:52 -0800346 return jni::EncodeArtField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700347}
348
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800349static void ThrowAIOOBE(ScopedObjectAccess& soa, mirror::Array* array, jsize start,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700350 jsize length, const char* identifier)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700351 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -0700352 std::string type(array->PrettyTypeOf());
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000353 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Ian Rogers62d6c772013-02-27 08:32:07 -0800354 "%s offset=%d length=%d %s.length=%d",
355 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700356}
Ian Rogers0571d352011-11-03 19:51:38 -0700357
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700358static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
359 jsize array_length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700360 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000361 soa.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers62d6c772013-02-27 08:32:07 -0800362 "offset=%d length=%d string.length()=%d", start, length,
363 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700364}
Elliott Hughes814e4032011-08-23 12:07:56 -0700365
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700366int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Mathieu Chartier90443472015-07-16 20:32:27 -0700367 REQUIRES(!Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700368 // Turn the const char* into a java.lang.String.
369 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800370 if (msg != nullptr && s.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700371 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700372 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700373
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700374 // Choose an appropriate constructor and set up the arguments.
375 jvalue args[2];
376 const char* signature;
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800377 if (msg == nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700378 signature = "()V";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800379 } else if (msg != nullptr && cause == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700380 signature = "(Ljava/lang/String;)V";
381 args[0].l = s.get();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800382 } else if (msg == nullptr && cause != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700383 signature = "(Ljava/lang/Throwable;)V";
384 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700385 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700386 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
387 args[0].l = s.get();
388 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700389 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700390 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800391 if (mid == nullptr) {
Ian Rogersef28b142012-11-30 14:22:18 -0800392 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700393 LOG(ERROR) << "No <init>" << signature << " in "
David Sehr709b0702016-10-13 09:12:37 -0700394 << mirror::Class::PrettyClass(soa.Decode<mirror::Class>(exception_class));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700395 return JNI_ERR;
396 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700397
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800398 ScopedLocalRef<jthrowable> exception(
399 env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
400 if (exception.get() == nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700401 return JNI_ERR;
402 }
Ian Rogersef28b142012-11-30 14:22:18 -0800403 ScopedObjectAccess soa(env);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700404 soa.Self()->SetException(soa.Decode<mirror::Throwable>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700405 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700406}
407
Ian Rogers68d8b422014-07-17 11:09:10 -0700408static JavaVMExt* JavaVmExtFromEnv(JNIEnv* env) {
Ian Rogers55256cb2017-12-21 17:07:11 -0800409 return reinterpret_cast<JNIEnvExt*>(env)->GetVm();
Elliott Hughes75770752011-08-24 17:52:38 -0700410}
411
Ian Rogers2d10b202014-05-12 19:15:18 -0700412#define CHECK_NON_NULL_ARGUMENT(value) \
413 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, nullptr)
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700414
Ian Rogers2d10b202014-05-12 19:15:18 -0700415#define CHECK_NON_NULL_ARGUMENT_RETURN_VOID(value) \
416 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, )
417
418#define CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(value) \
419 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, 0)
420
421#define CHECK_NON_NULL_ARGUMENT_RETURN(value, return_val) \
422 CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, return_val)
423
424#define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value, return_val) \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700425 if (UNLIKELY((value) == nullptr)) { \
Andreas Gampefcecaeb2017-08-24 18:04:23 -0700426 JavaVmExtFromEnv(env)->JniAbort(name, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700427 return return_val; \
Ian Rogersbc939662013-08-15 10:26:54 -0700428 }
429
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700430#define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700431 if (UNLIKELY((length) != 0 && (value) == nullptr)) { \
Andreas Gampefcecaeb2017-08-24 18:04:23 -0700432 JavaVmExtFromEnv(env)->JniAbort(__FUNCTION__, #value " == null"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700433 return; \
Ian Rogers4ffdc6b2013-08-21 16:55:13 -0700434 }
435
Andreas Gampe3f1dc562015-05-18 15:52:22 -0700436template <bool kNative>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700437static ArtMethod* FindMethod(mirror::Class* c, const StringPiece& name, const StringPiece& sig)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700438 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700439 auto pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Alex Lighte64300b2015-12-15 15:02:47 -0800440 for (auto& method : c->GetMethods(pointer_size)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700441 if (kNative == method.IsNative() && name == method.GetName() && method.GetSignature() == sig) {
442 return &method;
Andreas Gampe3f1dc562015-05-18 15:52:22 -0700443 }
444 }
Andreas Gampe3f1dc562015-05-18 15:52:22 -0700445 return nullptr;
446}
447
Elliott Hughescdf53122011-08-19 15:46:09 -0700448class JNI {
449 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700450 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700451 return JNI_VERSION_1_6;
452 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700453
Ian Rogers25e8b912012-09-07 11:31:36 -0700454 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700455 LOG(WARNING) << "JNI DefineClass is not supported";
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800456 return nullptr;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700457 }
458
Elliott Hughescdf53122011-08-19 15:46:09 -0700459 static jclass FindClass(JNIEnv* env, const char* name) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700460 CHECK_NON_NULL_ARGUMENT(name);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700461 Runtime* runtime = Runtime::Current();
462 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700463 std::string descriptor(NormalizeJniClassDescriptor(name));
Brian Carlstromea46f952013-07-30 01:26:50 -0700464 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800465 mirror::Class* c = nullptr;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700466 if (runtime->IsStarted()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700467 StackHandleScope<1> hs(soa.Self());
468 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader(soa)));
Ian Rogers98379392014-02-24 16:53:16 -0800469 c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700470 } else {
Ian Rogers98379392014-02-24 16:53:16 -0800471 c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700472 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700473 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700474 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700475
Ian Rogers62f05122014-03-21 11:21:29 -0700476 static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700477 CHECK_NON_NULL_ARGUMENT(jlr_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700478 ScopedObjectAccess soa(env);
Andreas Gampe13b27842016-11-07 16:48:23 -0800479 return jni::EncodeArtMethod(ArtMethod::FromReflectedMethod(soa, jlr_method));
Elliott Hughesf2682d52011-08-15 16:37:04 -0700480 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700481
Ian Rogers62f05122014-03-21 11:21:29 -0700482 static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700483 CHECK_NON_NULL_ARGUMENT(jlr_field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700484 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700485 ObjPtr<mirror::Object> obj_field = soa.Decode<mirror::Object>(jlr_field);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700486 if (obj_field->GetClass() != mirror::Field::StaticClass()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700487 // Not even a java.lang.reflect.Field, return null. TODO, is this check necessary?
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700488 return nullptr;
489 }
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700490 ObjPtr<mirror::Field> field = ObjPtr<mirror::Field>::DownCast(obj_field);
Andreas Gampe08883de2016-11-08 13:20:52 -0800491 return jni::EncodeArtField(field->GetArtField());
Elliott Hughescdf53122011-08-19 15:46:09 -0700492 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700493
Elliott Hughescdf53122011-08-19 15:46:09 -0700494 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700495 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700496 ScopedObjectAccess soa(env);
Andreas Gampe13b27842016-11-07 16:48:23 -0800497 ArtMethod* m = jni::DecodeArtMethod(mid);
Neil Fuller0e844392016-09-08 13:43:31 +0100498 mirror::Executable* method;
Andreas Gampe542451c2016-07-26 09:02:02 -0700499 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700500 DCHECK(!Runtime::Current()->IsActiveTransaction());
Sebastien Hertzd3333762014-06-26 14:45:07 +0200501 if (m->IsConstructor()) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700502 method = mirror::Constructor::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), m);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200503 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700504 method = mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), m);
Sebastien Hertzd3333762014-06-26 14:45:07 +0200505 }
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700506 return soa.AddLocalReference<jobject>(method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700507 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700508
Elliott Hughescdf53122011-08-19 15:46:09 -0700509 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700510 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700511 ScopedObjectAccess soa(env);
Andreas Gampe08883de2016-11-08 13:20:52 -0800512 ArtField* f = jni::DecodeArtField(fid);
Andreas Gampee01e3642016-07-25 13:06:04 -0700513 return soa.AddLocalReference<jobject>(
Andreas Gampe542451c2016-07-26 09:02:02 -0700514 mirror::Field::CreateFromArtField<kRuntimePointerSize>(soa.Self(), f, true));
Elliott Hughescdf53122011-08-19 15:46:09 -0700515 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700516
Elliott Hughes37f7a402011-08-22 18:56:01 -0700517 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700518 CHECK_NON_NULL_ARGUMENT(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700519 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700520 ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(java_object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700521 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700522 }
523
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700524 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700525 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700526 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700527 ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(java_class);
Brian Carlstrom08ac9222015-05-22 13:43:00 -0700528 return soa.AddLocalReference<jclass>(c->IsInterface() ? nullptr : c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700529 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700530
Narayan Kamath1268b742014-07-11 19:15:11 +0100531 // Note: java_class1 should be safely castable to java_class2, and
532 // not the other way around.
Elliott Hughes37f7a402011-08-22 18:56:01 -0700533 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700534 CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
535 CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700536 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700537 ObjPtr<mirror::Class> c1 = soa.Decode<mirror::Class>(java_class1);
538 ObjPtr<mirror::Class> c2 = soa.Decode<mirror::Class>(java_class2);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700539 return c2->IsAssignableFrom(c1) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700540 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700541
Elliott Hughese84278b2012-03-22 10:06:53 -0700542 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700543 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800544 if (jobj == nullptr) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700545 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700546 return JNI_TRUE;
547 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700548 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700549 ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(jobj);
550 ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700551 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700552 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700553 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700554
Elliott Hughes37f7a402011-08-22 18:56:01 -0700555 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700556 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700557 ObjPtr<mirror::Throwable> exception = soa.Decode<mirror::Throwable>(java_exception);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800558 if (exception == nullptr) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700559 return JNI_ERR;
560 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700561 soa.Self()->SetException(exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700562 return JNI_OK;
563 }
564
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700565 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700566 CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800567 return ThrowNewException(env, c, msg, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700568 }
569
570 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers55256cb2017-12-21 17:07:11 -0800571 return static_cast<JNIEnvExt*>(env)->self_->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700572 }
573
574 static void ExceptionClear(JNIEnv* env) {
Serguei Katkova309d762014-05-26 11:23:39 +0700575 ScopedObjectAccess soa(env);
576 soa.Self()->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700577 }
578
579 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700580 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700581
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700582 // If we have no exception to describe, pass through.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000583 if (!soa.Self()->GetException()) {
Alexei Zavjalov3a1444c2014-06-25 16:04:55 +0700584 return;
585 }
586
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000587 StackHandleScope<1> hs(soa.Self());
588 Handle<mirror::Throwable> old_exception(
589 hs.NewHandle<mirror::Throwable>(soa.Self()->GetException()));
590 soa.Self()->ClearException();
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800591 ScopedLocalRef<jthrowable> exception(env,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700592 soa.AddLocalReference<jthrowable>(old_exception.Get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700593 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
594 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800595 if (mid == nullptr) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700596 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
David Sehr709b0702016-10-13 09:12:37 -0700597 << mirror::Object::PrettyTypeOf(old_exception.Get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700598 } else {
599 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800600 if (soa.Self()->IsExceptionPending()) {
David Sehr709b0702016-10-13 09:12:37 -0700601 LOG(WARNING) << "JNI WARNING: " << mirror::Object::PrettyTypeOf(soa.Self()->GetException())
Elliott Hughes72025e52011-08-23 17:50:30 -0700602 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800603 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700604 }
605 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000606 soa.Self()->SetException(old_exception.Get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700607 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700608
Elliott Hughescdf53122011-08-19 15:46:09 -0700609 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700610 ScopedObjectAccess soa(env);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000611 mirror::Object* exception = soa.Self()->GetException();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700612 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700613 }
614
Ian Rogers25e8b912012-09-07 11:31:36 -0700615 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700616 LOG(FATAL) << "JNI FatalError called: " << msg;
617 }
618
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700619 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700620 // TODO: SOA may not be necessary but I do it to please lock annotations.
621 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700622 if (EnsureLocalCapacityInternal(soa, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700623 return JNI_ERR;
624 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700625 down_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700626 return JNI_OK;
627 }
628
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700629 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700630 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700631 ObjPtr<mirror::Object> survivor = soa.Decode<mirror::Object>(java_survivor);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700632 soa.Env()->PopFrame();
633 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700634 }
635
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700636 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700637 // TODO: SOA may not be necessary but I do it to please lock annotations.
638 ScopedObjectAccess soa(env);
Ian Rogers68d8b422014-07-17 11:09:10 -0700639 return EnsureLocalCapacityInternal(soa, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700640 }
641
Elliott Hughescdf53122011-08-19 15:46:09 -0700642 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700643 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700644 ObjPtr<mirror::Object> decoded_obj = soa.Decode<mirror::Object>(obj);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700645 return soa.Vm()->AddGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700646 }
647
648 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers55256cb2017-12-21 17:07:11 -0800649 JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->GetVm();
650 Thread* self = down_cast<JNIEnvExt*>(env)->self_;
Ian Rogers68d8b422014-07-17 11:09:10 -0700651 vm->DeleteGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700652 }
653
654 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700655 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700656 ObjPtr<mirror::Object> decoded_obj = soa.Decode<mirror::Object>(obj);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700657 return soa.Vm()->AddWeakGlobalRef(soa.Self(), decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700658 }
659
660 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Ian Rogers55256cb2017-12-21 17:07:11 -0800661 JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->GetVm();
662 Thread* self = down_cast<JNIEnvExt*>(env)->self_;
Ian Rogers68d8b422014-07-17 11:09:10 -0700663 vm->DeleteWeakGlobalRef(self, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700664 }
665
666 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Ian Rogers25e8b912012-09-07 11:31:36 -0700667 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700668 ObjPtr<mirror::Object> decoded_obj = soa.Decode<mirror::Object>(obj);
Mathieu Chartiere8c48db2013-12-19 14:59:00 -0800669 // Check for null after decoding the object to handle cleared weak globals.
670 if (decoded_obj == nullptr) {
671 return nullptr;
672 }
673 return soa.AddLocalReference<jobject>(decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700674 }
675
Mathieu Chartierdd06afe2015-06-26 10:47:08 -0700676 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800677 if (obj == nullptr) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700678 return;
679 }
Mathieu Chartierdd06afe2015-06-26 10:47:08 -0700680 // SOA is only necessary to have exclusion between GC root marking and removing.
681 // We don't want to have the GC attempt to mark a null root if we just removed
682 // it. b/22119403
683 ScopedObjectAccess soa(env);
684 auto* ext_env = down_cast<JNIEnvExt*>(env);
Ian Rogers55256cb2017-12-21 17:07:11 -0800685 if (!ext_env->locals_.Remove(ext_env->local_ref_cookie_, obj)) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700686 // Attempting to delete a local reference that is not in the
687 // topmost local reference frame is a no-op. DeleteLocalRef returns
688 // void and doesn't throw any exceptions, but we should probably
689 // complain about it so the user will notice that things aren't
690 // going quite the way they expect.
691 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
692 << "failed to find entry";
693 }
694 }
695
696 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700697 if (obj1 == obj2) {
698 return JNI_TRUE;
699 } else {
700 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700701 return (soa.Decode<mirror::Object>(obj1) == soa.Decode<mirror::Object>(obj2))
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800702 ? JNI_TRUE : JNI_FALSE;
Brian Carlstromea46f952013-07-30 01:26:50 -0700703 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700704 }
705
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700706 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700707 CHECK_NON_NULL_ARGUMENT(java_class);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700708 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700709 ObjPtr<mirror::Class> c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800710 if (c == nullptr) {
711 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700712 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800713 if (c->IsStringClass()) {
714 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700715 return soa.AddLocalReference<jobject>(mirror::String::AllocEmptyString<true>(soa.Self(),
716 allocator_type));
Jeff Hao848f70a2014-01-15 13:49:50 -0800717 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700718 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700719 }
720
Ian Rogersbc939662013-08-15 10:26:54 -0700721 static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700722 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700723 va_start(args, mid);
George Burgess IV984387f2017-06-22 15:51:11 -0700724 ScopedVAArgs free_args_later(&args);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700725 CHECK_NON_NULL_ARGUMENT(java_class);
726 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700727 jobject result = NewObjectV(env, java_class, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700728 return result;
729 }
730
Elliott Hughes72025e52011-08-23 17:50:30 -0700731 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700732 CHECK_NON_NULL_ARGUMENT(java_class);
733 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700734 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700735 ObjPtr<mirror::Class> c = EnsureInitialized(soa.Self(),
736 soa.Decode<mirror::Class>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800737 if (c == nullptr) {
738 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700739 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800740 if (c->IsStringClass()) {
741 // Replace calls to String.<init> with equivalent StringFactory call.
Andreas Gampe13b27842016-11-07 16:48:23 -0800742 jmethodID sf_mid = jni::EncodeArtMethod(
743 WellKnownClasses::StringInitToStringFactory(jni::DecodeArtMethod(mid)));
Jeff Hao848f70a2014-01-15 13:49:50 -0800744 return CallStaticObjectMethodV(env, WellKnownClasses::java_lang_StringFactory, sf_mid, args);
745 }
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700746 ObjPtr<mirror::Object> result = c->AllocObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800747 if (result == nullptr) {
748 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700749 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700750 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700751 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800752 if (soa.Self()->IsExceptionPending()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800753 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700754 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800755 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700756 }
757
Elliott Hughes72025e52011-08-23 17:50:30 -0700758 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700759 CHECK_NON_NULL_ARGUMENT(java_class);
760 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700761 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700762 ObjPtr<mirror::Class> c = EnsureInitialized(soa.Self(),
763 soa.Decode<mirror::Class>(java_class));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800764 if (c == nullptr) {
765 return nullptr;
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700766 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800767 if (c->IsStringClass()) {
768 // Replace calls to String.<init> with equivalent StringFactory call.
Andreas Gampe13b27842016-11-07 16:48:23 -0800769 jmethodID sf_mid = jni::EncodeArtMethod(
770 WellKnownClasses::StringInitToStringFactory(jni::DecodeArtMethod(mid)));
Jeff Hao848f70a2014-01-15 13:49:50 -0800771 return CallStaticObjectMethodA(env, WellKnownClasses::java_lang_StringFactory, sf_mid, args);
772 }
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700773 ObjPtr<mirror::Object> result = c->AllocObject(soa.Self());
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800774 if (result == nullptr) {
775 return nullptr;
Elliott Hughes30646832011-10-13 16:59:46 -0700776 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700777 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700778 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800779 if (soa.Self()->IsExceptionPending()) {
780 return nullptr;
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700781 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800782 return local_result;
Elliott Hughescdf53122011-08-19 15:46:09 -0700783 }
784
Ian Rogersbc939662013-08-15 10:26:54 -0700785 static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700786 CHECK_NON_NULL_ARGUMENT(java_class);
787 CHECK_NON_NULL_ARGUMENT(name);
788 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700789 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700790 return FindMethodID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700791 }
792
Ian Rogersbc939662013-08-15 10:26:54 -0700793 static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
794 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700795 CHECK_NON_NULL_ARGUMENT(java_class);
796 CHECK_NON_NULL_ARGUMENT(name);
797 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700798 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -0700799 return FindMethodID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700800 }
801
Elliott Hughes72025e52011-08-23 17:50:30 -0700802 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700803 va_list ap;
804 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -0700805 ScopedVAArgs free_args_later(&ap);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700806 CHECK_NON_NULL_ARGUMENT(obj);
807 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700808 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700809 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700810 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700811 }
812
Elliott Hughes72025e52011-08-23 17:50:30 -0700813 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700814 CHECK_NON_NULL_ARGUMENT(obj);
815 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700816 ScopedObjectAccess soa(env);
817 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
818 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700819 }
820
Elliott Hughes72025e52011-08-23 17:50:30 -0700821 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -0700822 CHECK_NON_NULL_ARGUMENT(obj);
823 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700824 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700825 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700826 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700827 }
828
Elliott Hughes72025e52011-08-23 17:50:30 -0700829 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700830 va_list ap;
831 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -0700832 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -0700833 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
834 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700835 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700836 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700837 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700838 }
839
Elliott Hughes72025e52011-08-23 17:50:30 -0700840 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700841 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
842 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700843 ScopedObjectAccess soa(env);
844 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700845 }
846
Elliott Hughes72025e52011-08-23 17:50:30 -0700847 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700848 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
849 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700850 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700851 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700852 }
853
Elliott Hughes72025e52011-08-23 17:50:30 -0700854 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700855 va_list ap;
856 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -0700857 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -0700858 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
859 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700860 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700861 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700862 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700863 }
864
Elliott Hughes72025e52011-08-23 17:50:30 -0700865 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700866 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
867 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700868 ScopedObjectAccess soa(env);
869 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700870 }
871
Elliott Hughes72025e52011-08-23 17:50:30 -0700872 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700873 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
874 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700875 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700876 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700877 }
878
Elliott Hughes72025e52011-08-23 17:50:30 -0700879 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700880 va_list ap;
881 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -0700882 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -0700883 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
884 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700885 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700886 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700887 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700888 }
889
Elliott Hughes72025e52011-08-23 17:50:30 -0700890 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700891 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
892 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700893 ScopedObjectAccess soa(env);
894 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700895 }
896
Elliott Hughes72025e52011-08-23 17:50:30 -0700897 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700898 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
899 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700900 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700901 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700902 }
903
Elliott Hughes72025e52011-08-23 17:50:30 -0700904 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700905 va_list ap;
906 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -0700907 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -0700908 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
909 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700910 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700911 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700912 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700913 }
914
Elliott Hughes72025e52011-08-23 17:50:30 -0700915 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700916 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
917 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700918 ScopedObjectAccess soa(env);
919 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700920 }
921
Elliott Hughes72025e52011-08-23 17:50:30 -0700922 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700923 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
924 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700925 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700926 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -0700927 }
928
Elliott Hughes72025e52011-08-23 17:50:30 -0700929 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700930 va_list ap;
931 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -0700932 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -0700933 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
934 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -0700935 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700936 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700937 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700938 }
939
Elliott Hughes72025e52011-08-23 17:50:30 -0700940 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700941 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
942 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700943 ScopedObjectAccess soa(env);
944 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700945 }
946
Elliott Hughes72025e52011-08-23 17:50:30 -0700947 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700948 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
949 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700950 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700951 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -0700952 }
953
Elliott Hughes72025e52011-08-23 17:50:30 -0700954 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700955 va_list ap;
956 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -0700957 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -0700958 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
959 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700960 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700961 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700962 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700963 }
964
Elliott Hughes72025e52011-08-23 17:50:30 -0700965 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700966 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
967 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700968 ScopedObjectAccess soa(env);
969 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700970 }
971
Elliott Hughes72025e52011-08-23 17:50:30 -0700972 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700973 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
974 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700975 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -0700976 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -0700977 }
978
Elliott Hughes72025e52011-08-23 17:50:30 -0700979 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700980 va_list ap;
981 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -0700982 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -0700983 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
984 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700985 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700986 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700987 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700988 }
989
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700991 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
992 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700993 ScopedObjectAccess soa(env);
994 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 }
996
Elliott Hughes72025e52011-08-23 17:50:30 -0700997 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700998 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
999 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001000 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001001 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 }
1003
Elliott Hughes72025e52011-08-23 17:50:30 -07001004 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001005 va_list ap;
1006 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001007 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001008 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1009 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001010 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001011 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001012 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001013 }
1014
Elliott Hughes72025e52011-08-23 17:50:30 -07001015 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001016 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1017 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001018 ScopedObjectAccess soa(env);
1019 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001020 }
1021
Elliott Hughes72025e52011-08-23 17:50:30 -07001022 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001023 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1024 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001025 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001026 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001027 }
1028
Elliott Hughes72025e52011-08-23 17:50:30 -07001029 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 va_list ap;
1031 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001032 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001033 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1034 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001035 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001036 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001037 }
1038
Elliott Hughes72025e52011-08-23 17:50:30 -07001039 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001040 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1041 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001042 ScopedObjectAccess soa(env);
1043 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001044 }
1045
Elliott Hughes72025e52011-08-23 17:50:30 -07001046 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001047 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1048 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001049 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001050 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001051 }
1052
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001053 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001055 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001056 ScopedVAArgs free_args_later(&ap);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001057 CHECK_NON_NULL_ARGUMENT(obj);
1058 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001059 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001060 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1061 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001062 return local_result;
1063 }
1064
Ian Rogersbc939662013-08-15 10:26:54 -07001065 static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1066 va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001067 CHECK_NON_NULL_ARGUMENT(obj);
1068 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001069 ScopedObjectAccess soa(env);
1070 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1071 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001072 }
1073
Ian Rogersbc939662013-08-15 10:26:54 -07001074 static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1075 jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001076 CHECK_NON_NULL_ARGUMENT(obj);
1077 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001078 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001079 JValue result(InvokeWithJValues(soa, obj, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001080 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001081 }
1082
Ian Rogersbc939662013-08-15 10:26:54 -07001083 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1084 ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001085 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001087 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001088 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1089 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001090 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001091 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001092 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001093 }
1094
Ian Rogersbc939662013-08-15 10:26:54 -07001095 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1096 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001097 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1098 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001099 ScopedObjectAccess soa(env);
1100 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001101 }
1102
Ian Rogersbc939662013-08-15 10:26:54 -07001103 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1104 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001105 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1106 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001107 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001108 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001109 }
1110
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001111 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001113 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001114 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001115 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1116 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001117 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001118 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001119 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 }
1121
Ian Rogersbc939662013-08-15 10:26:54 -07001122 static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1123 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001124 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1125 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001126 ScopedObjectAccess soa(env);
1127 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 }
1129
Ian Rogersbc939662013-08-15 10:26:54 -07001130 static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1131 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001132 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1133 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001134 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001135 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001136 }
1137
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001138 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001139 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001140 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001141 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001142 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1143 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogersbc939662013-08-15 10:26:54 -07001144 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001145 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001146 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001147 }
1148
Ian Rogersbc939662013-08-15 10:26:54 -07001149 static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1150 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001151 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1152 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001153 ScopedObjectAccess soa(env);
1154 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001155 }
1156
Ian Rogersbc939662013-08-15 10:26:54 -07001157 static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1158 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001159 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1160 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001161 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001162 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001163 }
1164
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001165 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001167 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001168 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001169 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1170 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001171 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001172 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001173 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001174 }
1175
Ian Rogersbc939662013-08-15 10:26:54 -07001176 static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1177 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001178 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1179 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001180 ScopedObjectAccess soa(env);
1181 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001182 }
1183
Ian Rogersbc939662013-08-15 10:26:54 -07001184 static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1185 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001186 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1187 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001188 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001189 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001190 }
1191
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001192 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001193 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001194 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001195 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001196 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1197 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001198 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001199 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001200 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001201 }
1202
Ian Rogersbc939662013-08-15 10:26:54 -07001203 static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1204 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001205 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1206 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001207 ScopedObjectAccess soa(env);
1208 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001209 }
1210
Ian Rogersbc939662013-08-15 10:26:54 -07001211 static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1212 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001213 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1214 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001215 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001216 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001217 }
1218
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001219 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001221 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001222 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001223 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1224 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001225 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001226 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001227 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 }
1229
Ian Rogersbc939662013-08-15 10:26:54 -07001230 static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1231 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001232 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1233 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001234 ScopedObjectAccess soa(env);
1235 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 }
1237
Ian Rogersbc939662013-08-15 10:26:54 -07001238 static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1239 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001240 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1241 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001242 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001243 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 }
1245
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001246 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001247 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001248 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001249 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001250 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1251 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001252 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001253 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001254 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001255 }
1256
Ian Rogersbc939662013-08-15 10:26:54 -07001257 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1258 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001259 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1260 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001261 ScopedObjectAccess soa(env);
1262 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001263 }
1264
Ian Rogersbc939662013-08-15 10:26:54 -07001265 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1266 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001267 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1268 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001269 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001270 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001271 }
1272
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001273 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001275 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001276 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001277 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1278 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001279 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001280 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001281 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001282 }
1283
Ian Rogersbc939662013-08-15 10:26:54 -07001284 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1285 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001286 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1287 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001288 ScopedObjectAccess soa(env);
1289 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001290 }
1291
Ian Rogersbc939662013-08-15 10:26:54 -07001292 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1293 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001294 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1295 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001296 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001297 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001298 }
1299
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001300 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001302 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001303 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001304 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1305 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001306 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001307 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001308 }
1309
Brian Carlstromea46f952013-07-30 01:26:50 -07001310 static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1311 va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001312 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1313 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001314 ScopedObjectAccess soa(env);
1315 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 }
1317
Ian Rogersbc939662013-08-15 10:26:54 -07001318 static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1319 jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001320 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1321 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001322 ScopedObjectAccess soa(env);
Jeff Hao39b6c242015-05-19 20:30:23 -07001323 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001324 }
1325
Ian Rogersbc939662013-08-15 10:26:54 -07001326 static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001327 CHECK_NON_NULL_ARGUMENT(java_class);
1328 CHECK_NON_NULL_ARGUMENT(name);
1329 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001330 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001331 return FindFieldID(soa, java_class, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001332 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001333
Ian Rogersbc939662013-08-15 10:26:54 -07001334 static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1335 const char* sig) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001336 CHECK_NON_NULL_ARGUMENT(java_class);
1337 CHECK_NON_NULL_ARGUMENT(name);
1338 CHECK_NON_NULL_ARGUMENT(sig);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001339 ScopedObjectAccess soa(env);
Ian Rogersbc939662013-08-15 10:26:54 -07001340 return FindFieldID(soa, java_class, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001341 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001342
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001343 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001344 CHECK_NON_NULL_ARGUMENT(obj);
1345 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001346 ScopedObjectAccess soa(env);
Andreas Gampe08883de2016-11-08 13:20:52 -08001347 ArtField* f = jni::DecodeArtField(fid);
Alex Lighte00ec302017-06-16 08:56:43 -07001348 NotifyGetField(f, obj);
1349 ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(obj);
Mathieu Chartier3398c782016-09-30 10:27:43 -07001350 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001351 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001352
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001353 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001354 CHECK_NON_NULL_ARGUMENT(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001355 ScopedObjectAccess soa(env);
Andreas Gampe08883de2016-11-08 13:20:52 -08001356 ArtField* f = jni::DecodeArtField(fid);
Alex Lighte00ec302017-06-16 08:56:43 -07001357 NotifyGetField(f, nullptr);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001358 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001359 }
1360
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001361 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001362 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1363 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001364 ScopedObjectAccess soa(env);
Alex Lighte00ec302017-06-16 08:56:43 -07001365 ArtField* f = jni::DecodeArtField(fid);
1366 NotifySetObjectField(f, java_object, java_value);
Mathieu Chartier0795f232016-09-27 18:43:30 -07001367 ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(java_object);
1368 ObjPtr<mirror::Object> v = soa.Decode<mirror::Object>(java_value);
Mathieu Chartier3398c782016-09-30 10:27:43 -07001369 f->SetObject<false>(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001370 }
1371
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001372 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001373 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001374 ScopedObjectAccess soa(env);
Andreas Gampe08883de2016-11-08 13:20:52 -08001375 ArtField* f = jni::DecodeArtField(fid);
Alex Lighte00ec302017-06-16 08:56:43 -07001376 NotifySetObjectField(f, nullptr, java_value);
1377 ObjPtr<mirror::Object> v = soa.Decode<mirror::Object>(java_value);
Mathieu Chartier3398c782016-09-30 10:27:43 -07001378 f->SetObject<false>(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001379 }
1380
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001381#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001382 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1383 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001384 ScopedObjectAccess soa(env); \
Andreas Gampe08883de2016-11-08 13:20:52 -08001385 ArtField* f = jni::DecodeArtField(fid); \
Alex Lighte00ec302017-06-16 08:56:43 -07001386 NotifyGetField(f, instance); \
1387 ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(instance); \
Mathieu Chartier3398c782016-09-30 10:27:43 -07001388 return f->Get ##fn (o)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001389
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001390#define GET_STATIC_PRIMITIVE_FIELD(fn) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001391 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001392 ScopedObjectAccess soa(env); \
Andreas Gampe08883de2016-11-08 13:20:52 -08001393 ArtField* f = jni::DecodeArtField(fid); \
Alex Lighte00ec302017-06-16 08:56:43 -07001394 NotifyGetField(f, nullptr); \
Ian Rogersbc939662013-08-15 10:26:54 -07001395 return f->Get ##fn (f->GetDeclaringClass())
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001396
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001397#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001398 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1399 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001400 ScopedObjectAccess soa(env); \
Andreas Gampe08883de2016-11-08 13:20:52 -08001401 ArtField* f = jni::DecodeArtField(fid); \
Alex Lighte00ec302017-06-16 08:56:43 -07001402 NotifySetPrimitiveField(f, instance, JValue::FromPrimitive<decltype(value)>(value)); \
1403 ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(instance); \
Mathieu Chartier3398c782016-09-30 10:27:43 -07001404 f->Set ##fn <false>(o, value)
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001405
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001406#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
Ian Rogers2d10b202014-05-12 19:15:18 -07001407 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001408 ScopedObjectAccess soa(env); \
Andreas Gampe08883de2016-11-08 13:20:52 -08001409 ArtField* f = jni::DecodeArtField(fid); \
Alex Lighte00ec302017-06-16 08:56:43 -07001410 NotifySetPrimitiveField(f, nullptr, JValue::FromPrimitive<decltype(value)>(value)); \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001411 f->Set ##fn <false>(f->GetDeclaringClass(), value)
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001412
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001413 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001414 GET_PRIMITIVE_FIELD(Boolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001415 }
1416
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001417 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001418 GET_PRIMITIVE_FIELD(Byte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001419 }
1420
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001421 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001422 GET_PRIMITIVE_FIELD(Char, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001423 }
1424
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001425 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001426 GET_PRIMITIVE_FIELD(Short, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001427 }
1428
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001429 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001430 GET_PRIMITIVE_FIELD(Int, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001431 }
1432
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001433 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001434 GET_PRIMITIVE_FIELD(Long, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001435 }
1436
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001437 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001438 GET_PRIMITIVE_FIELD(Float, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001439 }
1440
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001441 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001442 GET_PRIMITIVE_FIELD(Double, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001443 }
1444
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001445 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001446 GET_STATIC_PRIMITIVE_FIELD(Boolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001447 }
1448
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001449 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001450 GET_STATIC_PRIMITIVE_FIELD(Byte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001451 }
1452
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001453 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001454 GET_STATIC_PRIMITIVE_FIELD(Char);
Elliott Hughescdf53122011-08-19 15:46:09 -07001455 }
1456
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001457 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001458 GET_STATIC_PRIMITIVE_FIELD(Short);
Elliott Hughescdf53122011-08-19 15:46:09 -07001459 }
1460
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001461 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001462 GET_STATIC_PRIMITIVE_FIELD(Int);
Elliott Hughescdf53122011-08-19 15:46:09 -07001463 }
1464
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001465 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001466 GET_STATIC_PRIMITIVE_FIELD(Long);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001467 }
1468
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001469 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001470 GET_STATIC_PRIMITIVE_FIELD(Float);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001471 }
1472
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001473 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogersbc939662013-08-15 10:26:54 -07001474 GET_STATIC_PRIMITIVE_FIELD(Double);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001475 }
1476
1477 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001478 SET_PRIMITIVE_FIELD(Boolean, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001479 }
1480
1481 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001482 SET_PRIMITIVE_FIELD(Byte, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001483 }
1484
1485 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001486 SET_PRIMITIVE_FIELD(Char, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001487 }
1488
1489 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001490 SET_PRIMITIVE_FIELD(Float, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001491 }
1492
1493 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001494 SET_PRIMITIVE_FIELD(Double, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001495 }
1496
1497 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001498 SET_PRIMITIVE_FIELD(Int, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001499 }
1500
1501 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001502 SET_PRIMITIVE_FIELD(Long, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001503 }
1504
1505 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001506 SET_PRIMITIVE_FIELD(Short, obj, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001507 }
1508
1509 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001510 SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001511 }
1512
1513 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001514 SET_STATIC_PRIMITIVE_FIELD(Byte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001515 }
1516
1517 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001518 SET_STATIC_PRIMITIVE_FIELD(Char, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001519 }
1520
1521 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001522 SET_STATIC_PRIMITIVE_FIELD(Float, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001523 }
1524
1525 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001526 SET_STATIC_PRIMITIVE_FIELD(Double, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001527 }
1528
1529 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001530 SET_STATIC_PRIMITIVE_FIELD(Int, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001531 }
1532
1533 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001534 SET_STATIC_PRIMITIVE_FIELD(Long, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001535 }
1536
1537 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogersbc939662013-08-15 10:26:54 -07001538 SET_STATIC_PRIMITIVE_FIELD(Short, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001539 }
1540
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001541 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001542 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001543 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001544 ScopedVAArgs free_args_later(&ap);
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001545 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001546 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001547 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001548 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001549 return local_result;
1550 }
1551
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001552 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001553 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001554 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001555 JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001556 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001557 }
1558
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001559 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001560 CHECK_NON_NULL_ARGUMENT(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001561 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001562 JValue result(InvokeWithJValues(soa, nullptr, mid, args));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001563 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001564 }
1565
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001566 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001567 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001568 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001569 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001570 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001571 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001572 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001573 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001574 }
1575
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001576 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001577 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001578 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001579 return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001580 }
1581
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001582 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001583 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001584 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001585 return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001586 }
1587
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001588 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001590 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001591 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001592 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001593 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001594 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001595 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001596 }
1597
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001598 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001599 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001600 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001601 return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001602 }
1603
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001604 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001605 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001606 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001607 return InvokeWithJValues(soa, nullptr, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001608 }
1609
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001610 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001611 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001612 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001613 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001614 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001615 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001616 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001617 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001618 }
1619
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001620 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001621 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001622 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001623 return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001624 }
1625
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001626 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001627 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001628 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001629 return InvokeWithJValues(soa, nullptr, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001630 }
1631
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001632 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001633 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001634 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001635 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001636 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001637 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001638 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001639 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001640 }
1641
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001642 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001643 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001644 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001645 return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 }
1647
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001648 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001649 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001650 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001651 return InvokeWithJValues(soa, nullptr, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001652 }
1653
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001654 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001655 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001656 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001657 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001658 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001659 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001660 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001661 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001662 }
1663
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001664 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001665 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001666 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001667 return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001668 }
1669
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001670 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001671 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001672 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001673 return InvokeWithJValues(soa, nullptr, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001674 }
1675
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001676 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001678 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001679 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001680 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001681 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001682 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001683 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 }
1685
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001686 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001687 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001688 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001689 return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001690 }
1691
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001692 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001693 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001694 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001695 return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 }
1697
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001698 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001700 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001701 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001702 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001703 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001704 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001705 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001706 }
1707
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001708 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001709 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001710 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001711 return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001712 }
1713
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001714 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001715 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001716 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001717 return InvokeWithJValues(soa, nullptr, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001718 }
1719
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001720 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001721 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001722 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001723 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001724 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001725 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001726 JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001727 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001728 }
1729
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001730 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001731 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001732 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001733 return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001734 }
1735
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001736 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001737 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001738 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001739 return InvokeWithJValues(soa, nullptr, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001740 }
1741
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001742 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001743 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001744 va_start(ap, mid);
George Burgess IV984387f2017-06-22 15:51:11 -07001745 ScopedVAArgs free_args_later(&ap);
Ian Rogers2d10b202014-05-12 19:15:18 -07001746 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001747 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001748 InvokeWithVarArgs(soa, nullptr, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001749 }
1750
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001751 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001752 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001753 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001754 InvokeWithVarArgs(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001755 }
1756
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001757 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001758 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001759 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001760 InvokeWithJValues(soa, nullptr, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001761 }
1762
Elliott Hughes814e4032011-08-23 12:07:56 -07001763 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers1d99e452014-01-02 17:36:41 -08001764 if (UNLIKELY(char_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001765 JavaVmExtFromEnv(env)->JniAbortF("NewString", "char_count < 0: %d", char_count);
Ian Rogers1d99e452014-01-02 17:36:41 -08001766 return nullptr;
1767 }
1768 if (UNLIKELY(chars == nullptr && char_count > 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001769 JavaVmExtFromEnv(env)->JniAbortF("NewString", "chars == null && char_count > 0");
Ian Rogers1d99e452014-01-02 17:36:41 -08001770 return nullptr;
Ian Rogersbc939662013-08-15 10:26:54 -07001771 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001772 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001773 mirror::String* result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001774 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001775 }
1776
1777 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001778 if (utf == nullptr) {
1779 return nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07001780 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001781 ScopedObjectAccess soa(env);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001782 mirror::String* result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001783 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001784 }
1785
Elliott Hughes814e4032011-08-23 12:07:56 -07001786 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001787 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001788 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07001789 return soa.Decode<mirror::String>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001790 }
1791
1792 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001793 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001794 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07001795 return soa.Decode<mirror::String>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001796 }
1797
Ian Rogersbc939662013-08-15 10:26:54 -07001798 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1799 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001800 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001801 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07001802 ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
Vladimir Marko795e3412015-11-06 16:57:03 +00001803 if (start < 0 || length < 0 || length > s->GetLength() - start) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001804 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001805 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001806 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001807 if (s->IsCompressed()) {
1808 for (int i = 0; i < length; ++i) {
1809 buf[i] = static_cast<jchar>(s->CharAt(start+i));
1810 }
1811 } else {
1812 const jchar* chars = static_cast<jchar*>(s->GetValue());
1813 memcpy(buf, chars + start, length * sizeof(jchar));
1814 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07001815 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001816 }
1817
Ian Rogersbc939662013-08-15 10:26:54 -07001818 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1819 char* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001820 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001821 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07001822 ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
Vladimir Marko795e3412015-11-06 16:57:03 +00001823 if (start < 0 || length < 0 || length > s->GetLength() - start) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001824 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001825 } else {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001826 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001827 if (s->IsCompressed()) {
1828 for (int i = 0; i < length; ++i) {
1829 buf[i] = s->CharAt(start+i);
1830 }
1831 } else {
1832 const jchar* chars = s->GetValue();
1833 size_t bytes = CountUtf8Bytes(chars + start, length);
1834 ConvertUtf16ToModifiedUtf8(buf, bytes, chars + start, length);
1835 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07001836 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001837 }
1838
Elliott Hughes75770752011-08-24 17:52:38 -07001839 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001840 CHECK_NON_NULL_ARGUMENT(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001841 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07001842 ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
Fred Shih56890e22014-06-02 11:11:52 -07001843 gc::Heap* heap = Runtime::Current()->GetHeap();
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001844 if (heap->IsMovableObject(s) || s->IsCompressed()) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001845 jchar* chars = new jchar[s->GetLength()];
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001846 if (s->IsCompressed()) {
1847 int32_t length = s->GetLength();
1848 for (int i = 0; i < length; ++i) {
1849 chars[i] = s->CharAt(i);
1850 }
1851 } else {
1852 memcpy(chars, s->GetValue(), sizeof(jchar) * s->GetLength());
1853 }
Fred Shih56890e22014-06-02 11:11:52 -07001854 if (is_copy != nullptr) {
1855 *is_copy = JNI_TRUE;
1856 }
Jeff Hao848f70a2014-01-15 13:49:50 -08001857 return chars;
Elliott Hughes75770752011-08-24 17:52:38 -07001858 }
Jeff Hao848f70a2014-01-15 13:49:50 -08001859 if (is_copy != nullptr) {
1860 *is_copy = JNI_FALSE;
1861 }
1862 return static_cast<jchar*>(s->GetValue());
Elliott Hughes814e4032011-08-23 12:07:56 -07001863 }
1864
Mathieu Chartier590fee92013-09-13 13:46:47 -07001865 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001866 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001867 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07001868 ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001869 if (s->IsCompressed() || (s->IsCompressed() == false && chars != s->GetValue())) {
Fred Shih56890e22014-06-02 11:11:52 -07001870 delete[] chars;
1871 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001872 }
1873
Elliott Hughes75770752011-08-24 17:52:38 -07001874 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Fred Shih56890e22014-06-02 11:11:52 -07001875 CHECK_NON_NULL_ARGUMENT(java_string);
1876 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07001877 ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
Fred Shih56890e22014-06-02 11:11:52 -07001878 gc::Heap* heap = Runtime::Current()->GetHeap();
Jeff Hao848f70a2014-01-15 13:49:50 -08001879 if (heap->IsMovableObject(s)) {
Fred Shih56890e22014-06-02 11:11:52 -07001880 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -07001881 HandleWrapperObjPtr<mirror::String> h(hs.NewHandleWrapper(&s));
Hiroshi Yamauchi76f55b02015-08-21 16:10:39 -07001882 if (!kUseReadBarrier) {
1883 heap->IncrementDisableMovingGC(soa.Self());
1884 } else {
1885 // For the CC collector, we only need to wait for the thread flip rather than the whole GC
1886 // to occur thanks to the to-space invariant.
1887 heap->IncrementDisableThreadFlip(soa.Self());
1888 }
Fred Shih56890e22014-06-02 11:11:52 -07001889 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001890 if (s->IsCompressed()) {
1891 if (is_copy != nullptr) {
1892 *is_copy = JNI_TRUE;
1893 }
1894 int32_t length = s->GetLength();
1895 jchar* chars = new jchar[length];
1896 for (int i = 0; i < length; ++i) {
1897 chars[i] = s->CharAt(i);
1898 }
1899 return chars;
1900 } else {
1901 if (is_copy != nullptr) {
1902 *is_copy = JNI_FALSE;
1903 }
1904 return static_cast<jchar*>(s->GetValue());
Fred Shih56890e22014-06-02 11:11:52 -07001905 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001906 }
1907
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001908 static void ReleaseStringCritical(JNIEnv* env,
1909 jstring java_string,
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001910 const jchar* chars) {
Fred Shih56890e22014-06-02 11:11:52 -07001911 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1912 ScopedObjectAccess soa(env);
Fred Shih56890e22014-06-02 11:11:52 -07001913 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier0795f232016-09-27 18:43:30 -07001914 ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
Jeff Hao848f70a2014-01-15 13:49:50 -08001915 if (heap->IsMovableObject(s)) {
Hiroshi Yamauchi76f55b02015-08-21 16:10:39 -07001916 if (!kUseReadBarrier) {
1917 heap->DecrementDisableMovingGC(soa.Self());
1918 } else {
1919 heap->DecrementDisableThreadFlip(soa.Self());
1920 }
Fred Shih56890e22014-06-02 11:11:52 -07001921 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001922 if (s->IsCompressed() || (s->IsCompressed() == false && s->GetValue() != chars)) {
1923 delete[] chars;
1924 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001925 }
1926
Elliott Hughes75770752011-08-24 17:52:38 -07001927 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001928 if (java_string == nullptr) {
1929 return nullptr;
Elliott Hughes75770752011-08-24 17:52:38 -07001930 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001931 if (is_copy != nullptr) {
Elliott Hughes75770752011-08-24 17:52:38 -07001932 *is_copy = JNI_TRUE;
1933 }
Ian Rogersef28b142012-11-30 14:22:18 -08001934 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07001935 ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001936 size_t byte_count = s->GetUtfLength();
1937 char* bytes = new char[byte_count + 1];
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001938 CHECK(bytes != nullptr); // bionic aborts anyway.
jessicahandojo3aaa37b2016-07-29 14:46:37 -07001939 if (s->IsCompressed()) {
1940 for (size_t i = 0; i < byte_count; ++i) {
1941 bytes[i] = s->CharAt(i);
1942 }
1943 } else {
1944 const uint16_t* chars = s->GetValue();
1945 ConvertUtf16ToModifiedUtf8(bytes, byte_count, chars, s->GetLength());
1946 }
Elliott Hughes75770752011-08-24 17:52:38 -07001947 bytes[byte_count] = '\0';
1948 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001949 }
1950
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001951 static void ReleaseStringUTFChars(JNIEnv*, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001952 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001953 }
1954
Elliott Hughesbd935992011-08-22 11:59:34 -07001955 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001956 CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001957 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07001958 ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(java_array);
Brian Carlstromea46f952013-07-30 01:26:50 -07001959 if (UNLIKELY(!obj->IsArrayInstance())) {
David Sehr709b0702016-10-13 09:12:37 -07001960 soa.Vm()->JniAbortF("GetArrayLength", "not an array: %s", obj->PrettyTypeOf().c_str());
Ian Rogers68d8b422014-07-17 11:09:10 -07001961 return 0;
Elliott Hughes96a98872012-12-19 14:21:15 -08001962 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08001963 mirror::Array* array = obj->AsArray();
Elliott Hughesbd935992011-08-22 11:59:34 -07001964 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001965 }
1966
Elliott Hughes814e4032011-08-23 12:07:56 -07001967 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07001968 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001969 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07001970 ObjPtr<mirror::ObjectArray<mirror::Object>> array =
1971 soa.Decode<mirror::ObjectArray<mirror::Object>>(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001972 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001973 }
1974
Ian Rogersbc939662013-08-15 10:26:54 -07001975 static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
1976 jobject java_value) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001977 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001978 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07001979 ObjPtr<mirror::ObjectArray<mirror::Object>> array =
1980 soa.Decode<mirror::ObjectArray<mirror::Object>>(java_array);
1981 ObjPtr<mirror::Object> value = soa.Decode<mirror::Object>(java_value);
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07001982 array->Set<false>(index, value.Ptr());
Elliott Hughescdf53122011-08-19 15:46:09 -07001983 }
1984
1985 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001986 return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001987 }
1988
1989 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001990 return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001991 }
1992
1993 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001994 return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001995 }
1996
1997 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001998 return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001999 }
2000
2001 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002002 return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002003 }
2004
2005 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002006 return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002007 }
2008
2009 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002010 return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002011 }
2012
Ian Rogers1d99e452014-01-02 17:36:41 -08002013 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2014 jobject initial_element) {
2015 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002016 JavaVmExtFromEnv(env)->JniAbortF("NewObjectArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002017 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002018 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002019 CHECK_NON_NULL_ARGUMENT(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07002020
2021 // Compute the array class corresponding to the given element class.
Brian Carlstromea46f952013-07-30 01:26:50 -07002022 ScopedObjectAccess soa(env);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07002023 ObjPtr<mirror::Class> array_class;
Ian Rogers1d99e452014-01-02 17:36:41 -08002024 {
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07002025 ObjPtr<mirror::Class> element_class = soa.Decode<mirror::Class>(element_jclass).Ptr();
Ian Rogers1d99e452014-01-02 17:36:41 -08002026 if (UNLIKELY(element_class->IsPrimitive())) {
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07002027 soa.Vm()->JniAbortF("NewObjectArray",
2028 "not an object type: %s",
David Sehr709b0702016-10-13 09:12:37 -07002029 element_class->PrettyDescriptor().c_str());
Ian Rogers1d99e452014-01-02 17:36:41 -08002030 return nullptr;
2031 }
Ian Rogers1d99e452014-01-02 17:36:41 -08002032 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierb74cd292014-05-29 14:31:33 -07002033 array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
Ian Rogers1d99e452014-01-02 17:36:41 -08002034 if (UNLIKELY(array_class == nullptr)) {
2035 return nullptr;
2036 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002037 }
2038
Elliott Hughes75770752011-08-24 17:52:38 -07002039 // Allocate and initialize if necessary.
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002040 mirror::ObjectArray<mirror::Object>* result =
2041 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002042 if (result != nullptr && initial_element != nullptr) {
Mathieu Chartier0795f232016-09-27 18:43:30 -07002043 ObjPtr<mirror::Object> initial_object = soa.Decode<mirror::Object>(initial_element);
Ian Rogers1d99e452014-01-02 17:36:41 -08002044 if (initial_object != nullptr) {
2045 mirror::Class* element_class = result->GetClass()->GetComponentType();
2046 if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002047 soa.Vm()->JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with "
2048 "element type of '%s'",
David Sehr709b0702016-10-13 09:12:37 -07002049 mirror::Class::PrettyDescriptor(initial_object->GetClass()).c_str(),
2050 element_class->PrettyDescriptor().c_str());
Ian Rogers68d8b422014-07-17 11:09:10 -07002051 return nullptr;
Ian Rogers1d99e452014-01-02 17:36:41 -08002052 } else {
2053 for (jsize i = 0; i < length; ++i) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002054 result->SetWithoutChecks<false>(i, initial_object.Ptr());
Ian Rogers1d99e452014-01-02 17:36:41 -08002055 }
2056 }
Elliott Hughes75770752011-08-24 17:52:38 -07002057 }
2058 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002059 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07002060 }
2061
2062 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002063 return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07002064 }
2065
Ian Rogersa15e67d2012-02-28 13:51:55 -08002066 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Mathieu Chartier3b60fea2014-04-24 17:17:21 -07002067 CHECK_NON_NULL_ARGUMENT(java_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002068 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07002069 ObjPtr<mirror::Array> array = soa.Decode<mirror::Array>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002070 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002071 soa.Vm()->JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
David Sehr709b0702016-10-13 09:12:37 -07002072 array->GetClass()->PrettyDescriptor().c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07002073 return nullptr;
2074 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002075 gc::Heap* heap = Runtime::Current()->GetHeap();
2076 if (heap->IsMovableObject(array)) {
Hiroshi Yamauchi76f55b02015-08-21 16:10:39 -07002077 if (!kUseReadBarrier) {
2078 heap->IncrementDisableMovingGC(soa.Self());
2079 } else {
2080 // For the CC collector, we only need to wait for the thread flip rather than the whole GC
2081 // to occur thanks to the to-space invariant.
2082 heap->IncrementDisableThreadFlip(soa.Self());
2083 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002084 // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
Mathieu Chartier0795f232016-09-27 18:43:30 -07002085 array = soa.Decode<mirror::Array>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002086 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002087 if (is_copy != nullptr) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002088 *is_copy = JNI_FALSE;
2089 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002090 return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002091 }
2092
Ian Rogers2d10b202014-05-12 19:15:18 -07002093 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
2094 jint mode) {
2095 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2096 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07002097 ObjPtr<mirror::Array> array = soa.Decode<mirror::Array>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002098 if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002099 soa.Vm()->JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
David Sehr709b0702016-10-13 09:12:37 -07002100 array->GetClass()->PrettyDescriptor().c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07002101 return;
2102 }
2103 const size_t component_size = array->GetClass()->GetComponentSize();
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002104 ReleasePrimitiveArray(soa, array.Ptr(), component_size, elements, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002105 }
2106
Elliott Hughes75770752011-08-24 17:52:38 -07002107 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002108 return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002109 }
2110
Elliott Hughes75770752011-08-24 17:52:38 -07002111 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002112 return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002113 }
2114
Elliott Hughes75770752011-08-24 17:52:38 -07002115 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002116 return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002117 }
2118
Elliott Hughes75770752011-08-24 17:52:38 -07002119 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002120 return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002121 }
2122
Elliott Hughes75770752011-08-24 17:52:38 -07002123 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002124 return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002125 }
2126
Elliott Hughes75770752011-08-24 17:52:38 -07002127 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002128 return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002129 }
2130
Elliott Hughes75770752011-08-24 17:52:38 -07002131 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002132 return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002133 }
2134
Elliott Hughes75770752011-08-24 17:52:38 -07002135 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002136 return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07002137 }
2138
Mathieu Chartier590fee92013-09-13 13:46:47 -07002139 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2140 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002141 ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
2142 mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 }
2144
Mathieu Chartier590fee92013-09-13 13:46:47 -07002145 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002146 ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002147 }
2148
Mathieu Chartier590fee92013-09-13 13:46:47 -07002149 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002150 ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002151 }
2152
Mathieu Chartier590fee92013-09-13 13:46:47 -07002153 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2154 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002155 ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002156 }
2157
Mathieu Chartier590fee92013-09-13 13:46:47 -07002158 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2159 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002160 ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 }
2162
Mathieu Chartier590fee92013-09-13 13:46:47 -07002163 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002164 ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002165 }
2166
Mathieu Chartier590fee92013-09-13 13:46:47 -07002167 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002168 ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002169 }
2170
Mathieu Chartier590fee92013-09-13 13:46:47 -07002171 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2172 jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002173 ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07002174 }
2175
Ian Rogersbc939662013-08-15 10:26:54 -07002176 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2177 jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002178 GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002179 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002180 }
2181
Ian Rogersbc939662013-08-15 10:26:54 -07002182 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2183 jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002184 GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 }
2186
Ian Rogersbc939662013-08-15 10:26:54 -07002187 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2188 jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002189 GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002190 }
2191
Ian Rogersbc939662013-08-15 10:26:54 -07002192 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2193 jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002194 GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002195 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002196 }
2197
Ian Rogersbc939662013-08-15 10:26:54 -07002198 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2199 jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002200 GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002201 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002202 }
2203
Ian Rogersbc939662013-08-15 10:26:54 -07002204 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2205 jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002206 GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002207 }
2208
Ian Rogersbc939662013-08-15 10:26:54 -07002209 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2210 jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002211 GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002212 }
2213
Ian Rogersbc939662013-08-15 10:26:54 -07002214 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2215 jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002216 GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002217 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002218 }
2219
Ian Rogersbc939662013-08-15 10:26:54 -07002220 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2221 const jboolean* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002222 SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002223 length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002224 }
2225
Ian Rogersbc939662013-08-15 10:26:54 -07002226 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2227 const jbyte* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002228 SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002229 }
2230
Ian Rogersbc939662013-08-15 10:26:54 -07002231 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2232 const jchar* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002233 SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002234 }
2235
Ian Rogersbc939662013-08-15 10:26:54 -07002236 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2237 const jdouble* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002238 SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002239 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002240 }
2241
Ian Rogersbc939662013-08-15 10:26:54 -07002242 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2243 const jfloat* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002244 SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08002245 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002246 }
2247
Ian Rogersbc939662013-08-15 10:26:54 -07002248 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2249 const jint* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002250 SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002251 }
2252
Ian Rogersbc939662013-08-15 10:26:54 -07002253 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2254 const jlong* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002255 SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002256 }
2257
Ian Rogersbc939662013-08-15 10:26:54 -07002258 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2259 const jshort* buf) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002260 SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002261 buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002262 }
2263
Andreas Gampe20acb0c2017-05-25 11:33:50 -07002264 static jint RegisterNatives(JNIEnv* env,
2265 jclass java_class,
2266 const JNINativeMethod* methods,
Ian Rogersbc939662013-08-15 10:26:54 -07002267 jint method_count) {
Ian Rogersbc939662013-08-15 10:26:54 -07002268 if (UNLIKELY(method_count < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002269 JavaVmExtFromEnv(env)->JniAbortF("RegisterNatives", "negative method count: %d",
2270 method_count);
2271 return JNI_ERR; // Not reached except in unit tests.
Ian Rogersbc939662013-08-15 10:26:54 -07002272 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002273 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002274 ScopedObjectAccess soa(env);
Alex Light65af20b2017-04-20 09:15:08 -07002275 StackHandleScope<1> hs(soa.Self());
2276 Handle<mirror::Class> c = hs.NewHandle(soa.Decode<mirror::Class>(java_class));
Ian Rogersbc939662013-08-15 10:26:54 -07002277 if (UNLIKELY(method_count == 0)) {
2278 LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
Alex Light65af20b2017-04-20 09:15:08 -07002279 << c->PrettyDescriptor();
Ian Rogersbc939662013-08-15 10:26:54 -07002280 return JNI_OK;
2281 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002282 CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
Ian Rogersbc939662013-08-15 10:26:54 -07002283 for (jint i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002284 const char* name = methods[i].name;
2285 const char* sig = methods[i].signature;
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002286 const void* fnPtr = methods[i].fnPtr;
2287 if (UNLIKELY(name == nullptr)) {
Andreas Gampe20acb0c2017-05-25 11:33:50 -07002288 ReportInvalidJNINativeMethod(soa, c.Get(), "method name", i);
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002289 return JNI_ERR;
2290 } else if (UNLIKELY(sig == nullptr)) {
Andreas Gampe20acb0c2017-05-25 11:33:50 -07002291 ReportInvalidJNINativeMethod(soa, c.Get(), "method signature", i);
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002292 return JNI_ERR;
2293 } else if (UNLIKELY(fnPtr == nullptr)) {
Andreas Gampe20acb0c2017-05-25 11:33:50 -07002294 ReportInvalidJNINativeMethod(soa, c.Get(), "native function", i);
Sebastien Hertzfa65e842014-07-03 09:39:53 +02002295 return JNI_ERR;
2296 }
Ian Rogers1eb512d2013-10-18 15:42:20 -07002297 bool is_fast = false;
Hiroshi Yamauchi36bce582015-05-12 12:16:10 -07002298 // Notes about fast JNI calls:
2299 //
2300 // On a normal JNI call, the calling thread usually transitions
2301 // from the kRunnable state to the kNative state. But if the
2302 // called native function needs to access any Java object, it
2303 // will have to transition back to the kRunnable state.
2304 //
2305 // There is a cost to this double transition. For a JNI call
2306 // that should be quick, this cost may dominate the call cost.
2307 //
2308 // On a fast JNI call, the calling thread avoids this double
2309 // transition by not transitioning from kRunnable to kNative and
2310 // stays in the kRunnable state.
2311 //
2312 // There are risks to using a fast JNI call because it can delay
2313 // a response to a thread suspension request which is typically
2314 // used for a GC root scanning, etc. If a fast JNI call takes a
2315 // long time, it could cause longer thread suspension latency
2316 // and GC pauses.
2317 //
2318 // Thus, fast JNI should be used with care. It should be used
2319 // for a JNI call that takes a short amount of time (eg. no
2320 // long-running loop) and does not block (eg. no locks, I/O,
2321 // etc.)
2322 //
2323 // A '!' prefix in the signature in the JNINativeMethod
2324 // indicates that it's a fast JNI call and the runtime omits the
2325 // thread state transition from kRunnable to kNative at the
2326 // entry.
Elliott Hughescdf53122011-08-19 15:46:09 -07002327 if (*sig == '!') {
Ian Rogers1eb512d2013-10-18 15:42:20 -07002328 is_fast = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002329 ++sig;
2330 }
2331
Andreas Gampe3f1dc562015-05-18 15:52:22 -07002332 // Note: the right order is to try to find the method locally
2333 // first, either as a direct or a virtual method. Then move to
2334 // the parent.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002335 ArtMethod* m = nullptr;
Ian Rogers55256cb2017-12-21 17:07:11 -08002336 bool warn_on_going_to_parent = down_cast<JNIEnvExt*>(env)->GetVm()->IsCheckJniEnabled();
Alex Light65af20b2017-04-20 09:15:08 -07002337 for (ObjPtr<mirror::Class> current_class = c.Get();
Andreas Gampe3f1dc562015-05-18 15:52:22 -07002338 current_class != nullptr;
2339 current_class = current_class->GetSuperClass()) {
2340 // Search first only comparing methods which are native.
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002341 m = FindMethod<true>(current_class.Ptr(), name, sig);
Andreas Gampe3f1dc562015-05-18 15:52:22 -07002342 if (m != nullptr) {
2343 break;
2344 }
2345
2346 // Search again comparing to all methods, to find non-native methods that match.
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002347 m = FindMethod<false>(current_class.Ptr(), name, sig);
Andreas Gampe3f1dc562015-05-18 15:52:22 -07002348 if (m != nullptr) {
2349 break;
2350 }
2351
2352 if (warn_on_going_to_parent) {
2353 LOG(WARNING) << "CheckJNI: method to register \"" << name << "\" not in the given class. "
2354 << "This is slow, consider changing your RegisterNatives calls.";
2355 warn_on_going_to_parent = false;
2356 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002357 }
Andreas Gampe3f1dc562015-05-18 15:52:22 -07002358
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002359 if (m == nullptr) {
Andreas Gampe20acb0c2017-05-25 11:33:50 -07002360 c->DumpClass(LOG_STREAM(ERROR), mirror::Class::kDumpClassFullDetail);
2361 LOG(ERROR)
Andreas Gampe3fec9ac2016-09-13 10:47:28 -07002362 << "Failed to register native method "
David Sehr709b0702016-10-13 09:12:37 -07002363 << c->PrettyDescriptor() << "." << name << sig << " in "
Ian Rogers0177e532014-02-11 16:30:46 -08002364 << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
Alex Light65af20b2017-04-20 09:15:08 -07002365 ThrowNoSuchMethodError(soa, c.Get(), name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002366 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002367 } else if (!m->IsNative()) {
Andreas Gampe20acb0c2017-05-25 11:33:50 -07002368 LOG(ERROR)
Andreas Gampe3fec9ac2016-09-13 10:47:28 -07002369 << "Failed to register non-native method "
David Sehr709b0702016-10-13 09:12:37 -07002370 << c->PrettyDescriptor() << "." << name << sig
Ian Rogersbc939662013-08-15 10:26:54 -07002371 << " as native";
Alex Light65af20b2017-04-20 09:15:08 -07002372 ThrowNoSuchMethodError(soa, c.Get(), name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002373 return JNI_ERR;
2374 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002375
David Sehr709b0702016-10-13 09:12:37 -07002376 VLOG(jni) << "[Registering JNI native method " << m->PrettyMethod() << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002377
Igor Murashkin3b6f4402017-02-16 16:13:17 -08002378 if (UNLIKELY(is_fast)) {
2379 // There are a few reasons to switch:
2380 // 1) We don't support !bang JNI anymore, it will turn to a hard error later.
2381 // 2) @FastNative is actually faster. At least 1.5x faster than !bang JNI.
2382 // and switching is super easy, remove ! in C code, add annotation in .java code.
2383 // 3) Good chance of hitting DCHECK failures in ScopedFastNativeObjectAccess
2384 // since that checks for presence of @FastNative and not for ! in the descriptor.
2385 LOG(WARNING) << "!bang JNI is deprecated. Switch to @FastNative for " << m->PrettyMethod();
2386 is_fast = false;
2387 // TODO: make this a hard register error in the future.
2388 }
2389
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002390 const void* final_function_ptr = m->RegisterNative(fnPtr);
Alex Lightd78ddec2017-04-18 15:20:38 -07002391 UNUSED(final_function_ptr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002392 }
2393 return JNI_OK;
2394 }
2395
Elliott Hughes5174fe62011-08-23 15:12:35 -07002396 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002397 CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002398 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07002399 ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002400
David Sehr709b0702016-10-13 09:12:37 -07002401 VLOG(jni) << "[Unregistering JNI native methods for " << mirror::Class::PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002402
Ian Rogers2d10b202014-05-12 19:15:18 -07002403 size_t unregistered_count = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002404 auto pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Alex Lighte64300b2015-12-15 15:02:47 -08002405 for (auto& m : c->GetMethods(pointer_size)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002406 if (m.IsNative()) {
2407 m.UnregisterNative();
Ian Rogers2d10b202014-05-12 19:15:18 -07002408 unregistered_count++;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002409 }
2410 }
2411
Ian Rogers2d10b202014-05-12 19:15:18 -07002412 if (unregistered_count == 0) {
2413 LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
David Sehr709b0702016-10-13 09:12:37 -07002414 << mirror::Class::PrettyDescriptor(c) << "' that contains no native methods";
Ian Rogers2d10b202014-05-12 19:15:18 -07002415 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002416 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002417 }
2418
Ian Rogers719d1a32014-03-06 12:13:39 -08002419 static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002420 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002421 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07002422 ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(java_object);
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002423 o = o->MonitorEnter(soa.Self());
Alex Light77fee872017-09-05 14:51:49 -07002424 if (soa.Self()->HoldsLock(o)) {
Ian Rogers55256cb2017-12-21 17:07:11 -08002425 soa.Env()->monitors_.Add(o);
Alex Light77fee872017-09-05 14:51:49 -07002426 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002427 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002428 return JNI_ERR;
2429 }
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002430 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002431 }
2432
Ian Rogers719d1a32014-03-06 12:13:39 -08002433 static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers2d10b202014-05-12 19:15:18 -07002434 CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002435 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -07002436 ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(java_object);
Alex Light77fee872017-09-05 14:51:49 -07002437 bool remove_mon = soa.Self()->HoldsLock(o);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002438 o->MonitorExit(soa.Self());
Alex Light77fee872017-09-05 14:51:49 -07002439 if (remove_mon) {
Ian Rogers55256cb2017-12-21 17:07:11 -08002440 soa.Env()->monitors_.Remove(o);
Alex Light77fee872017-09-05 14:51:49 -07002441 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002442 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002443 return JNI_ERR;
2444 }
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002445 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002446 }
2447
2448 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002449 CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
Elliott Hughescdf53122011-08-19 15:46:09 -07002450 Runtime* runtime = Runtime::Current();
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002451 if (runtime != nullptr) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002452 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002453 } else {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002454 *vm = nullptr;
Elliott Hughescdf53122011-08-19 15:46:09 -07002455 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002456 return (*vm != nullptr) ? JNI_OK : JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002457 }
2458
Elliott Hughescdf53122011-08-19 15:46:09 -07002459 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002460 if (capacity < 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002461 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64,
2462 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002463 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002464 }
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002465 if (address == nullptr && capacity != 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002466 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2467 "non-zero capacity for nullptr pointer: %" PRId64, capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002468 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002469 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002470
Brian Carlstrom85a93362014-06-25 09:30:52 -07002471 // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002472 if (capacity > INT_MAX) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002473 JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2474 "buffer capacity greater than maximum jint: %" PRId64,
2475 capacity);
Brian Carlstrom45d26c82014-06-24 23:36:28 -07002476 return nullptr;
2477 }
Elliott Hughesb5681212013-03-29 17:29:22 -07002478 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002479 jint capacity_arg = static_cast<jint>(capacity);
2480
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002481 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2482 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002483 address_arg, capacity_arg);
Ian Rogers55256cb2017-12-21 17:07:11 -08002484 return static_cast<JNIEnvExt*>(env)->self_->IsExceptionPending() ? nullptr : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002485 }
2486
Elliott Hughesb465ab02011-08-24 11:21:21 -07002487 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002488 return reinterpret_cast<void*>(env->GetLongField(
2489 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002490 }
2491
Elliott Hughesb465ab02011-08-24 11:21:21 -07002492 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002493 return static_cast<jlong>(env->GetIntField(
2494 java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002495 }
2496
Andreas Gampea8763072014-12-20 00:08:35 -08002497 static jobjectRefType GetObjectRefType(JNIEnv* env ATTRIBUTE_UNUSED, jobject java_object) {
2498 if (java_object == nullptr) {
2499 return JNIInvalidRefType;
2500 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002501
2502 // Do we definitely know what kind of reference this is?
2503 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
Andreas Gampedc061d02016-10-24 13:19:37 -07002504 IndirectRefKind kind = IndirectReferenceTable::GetIndirectRefKind(ref);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002505 switch (kind) {
Ian Rogersc0542af2014-09-03 16:16:56 -07002506 case kLocal:
2507 return JNILocalRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002508 case kGlobal:
2509 return JNIGlobalRefType;
2510 case kWeakGlobal:
2511 return JNIWeakGlobalRefType;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002512 case kHandleScopeOrInvalid:
Ian Rogersc0542af2014-09-03 16:16:56 -07002513 // Assume value is in a handle scope.
2514 return JNILocalRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002515 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002516 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
Andreas Gampea8763072014-12-20 00:08:35 -08002517 UNREACHABLE();
Elliott Hughescdf53122011-08-19 15:46:09 -07002518 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002519
2520 private:
Ian Rogers68d8b422014-07-17 11:09:10 -07002521 static jint EnsureLocalCapacityInternal(ScopedObjectAccess& soa, jint desired_capacity,
2522 const char* caller)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002523 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe88831082017-05-31 19:46:03 -07002524 if (desired_capacity < 0) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002525 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2526 return JNI_ERR;
2527 }
Andreas Gampe88831082017-05-31 19:46:03 -07002528
2529 std::string error_msg;
Ian Rogers55256cb2017-12-21 17:07:11 -08002530 if (!soa.Env()->locals_.EnsureFreeCapacity(static_cast<size_t>(desired_capacity), &error_msg)) {
Andreas Gampe88831082017-05-31 19:46:03 -07002531 std::string caller_error = android::base::StringPrintf("%s: %s", caller, error_msg.c_str());
2532 soa.Self()->ThrowOutOfMemoryError(caller_error.c_str());
2533 return JNI_ERR;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002534 }
Andreas Gampe88831082017-05-31 19:46:03 -07002535 return JNI_OK;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002536 }
2537
2538 template<typename JniT, typename ArtT>
Ian Rogers2d10b202014-05-12 19:15:18 -07002539 static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002540 ScopedObjectAccess soa(env);
Ian Rogers1d99e452014-01-02 17:36:41 -08002541 if (UNLIKELY(length < 0)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002542 soa.Vm()->JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
Ian Rogers1d99e452014-01-02 17:36:41 -08002543 return nullptr;
Elliott Hughes96a98872012-12-19 14:21:15 -08002544 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002545 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002546 return soa.AddLocalReference<JniT>(result);
2547 }
2548
Ian Rogers2d10b202014-05-12 19:15:18 -07002549 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2550 static ArtArrayT* DecodeAndCheckArrayType(ScopedObjectAccess& soa, JArrayT java_array,
2551 const char* fn_name, const char* operation)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002552 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0795f232016-09-27 18:43:30 -07002553 ObjPtr<ArtArrayT> array = soa.Decode<ArtArrayT>(java_array);
Ian Rogers2d10b202014-05-12 19:15:18 -07002554 if (UNLIKELY(ArtArrayT::GetArrayClass() != array->GetClass())) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002555 soa.Vm()->JniAbortF(fn_name,
2556 "attempt to %s %s primitive array elements with an object of type %s",
2557 operation,
David Sehr709b0702016-10-13 09:12:37 -07002558 mirror::Class::PrettyDescriptor(
2559 ArtArrayT::GetArrayClass()->GetComponentType()).c_str(),
2560 mirror::Class::PrettyDescriptor(array->GetClass()).c_str());
Ian Rogers2d10b202014-05-12 19:15:18 -07002561 return nullptr;
2562 }
2563 DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002564 return array.Ptr();
Ian Rogers2d10b202014-05-12 19:15:18 -07002565 }
2566
2567 template <typename ArrayT, typename ElementT, typename ArtArrayT>
2568 static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2569 CHECK_NON_NULL_ARGUMENT(java_array);
2570 ScopedObjectAccess soa(env);
2571 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2572 "GetArrayElements",
2573 "get");
2574 if (UNLIKELY(array == nullptr)) {
2575 return nullptr;
2576 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002577 // Only make a copy if necessary.
2578 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2579 if (is_copy != nullptr) {
2580 *is_copy = JNI_TRUE;
2581 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002582 const size_t component_size = sizeof(ElementT);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002583 size_t size = array->GetLength() * component_size;
2584 void* data = new uint64_t[RoundUp(size, 8) / 8];
2585 memcpy(data, array->GetData(), size);
Ian Rogers2d10b202014-05-12 19:15:18 -07002586 return reinterpret_cast<ElementT*>(data);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002587 } else {
2588 if (is_copy != nullptr) {
2589 *is_copy = JNI_FALSE;
2590 }
Ian Rogers2d10b202014-05-12 19:15:18 -07002591 return reinterpret_cast<ElementT*>(array->GetData());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002592 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002593 }
2594
Ian Rogers2d10b202014-05-12 19:15:18 -07002595 template <typename ArrayT, typename ElementT, typename ArtArrayT>
Mathieu Chartier590fee92013-09-13 13:46:47 -07002596 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002597 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002598 ScopedObjectAccess soa(env);
Ian Rogers2d10b202014-05-12 19:15:18 -07002599 ArtArrayT* array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(soa, java_array,
2600 "ReleaseArrayElements",
2601 "release");
2602 if (array == nullptr) {
2603 return;
2604 }
2605 ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2606 }
2607
2608 static void ReleasePrimitiveArray(ScopedObjectAccess& soa, mirror::Array* array,
2609 size_t component_size, void* elements, jint mode)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002610 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002611 void* array_data = array->GetRawData(component_size, 0);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002612 gc::Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers2d10b202014-05-12 19:15:18 -07002613 bool is_copy = array_data != elements;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002614 size_t bytes = array->GetLength() * component_size;
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002615 if (is_copy) {
2616 // Sanity check: If elements is not the same as the java array's data, it better not be a
2617 // heap address. TODO: This might be slow to check, may be worth keeping track of which
2618 // copies we make?
Mathieu Chartier9d156d52016-10-06 17:44:26 -07002619 if (heap->IsNonDiscontinuousSpaceHeapAddress(elements)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07002620 soa.Vm()->JniAbortF("ReleaseArrayElements",
2621 "invalid element pointer %p, array elements are %p",
2622 reinterpret_cast<void*>(elements), array_data);
Mathieu Chartierd68ac702014-02-11 14:50:51 -08002623 return;
2624 }
Mathieu Chartier24555ad2014-10-06 13:41:33 -07002625 if (mode != JNI_ABORT) {
2626 memcpy(array_data, elements, bytes);
2627 } else if (kWarnJniAbort && memcmp(array_data, elements, bytes) != 0) {
2628 // Warn if we have JNI_ABORT and the arrays don't match since this is usually an error.
2629 LOG(WARNING) << "Possible incorrect JNI_ABORT in Release*ArrayElements";
Andreas Gampe3fec9ac2016-09-13 10:47:28 -07002630 soa.Self()->DumpJavaStack(LOG_STREAM(WARNING));
Mathieu Chartier24555ad2014-10-06 13:41:33 -07002631 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002632 }
2633 if (mode != JNI_COMMIT) {
2634 if (is_copy) {
2635 delete[] reinterpret_cast<uint64_t*>(elements);
Mathieu Chartier3e8b2e12014-01-19 17:17:26 -08002636 } else if (heap->IsMovableObject(array)) {
Mathieu Chartier1d27b342014-01-28 12:51:09 -08002637 // Non copy to a movable object must means that we had disabled the moving GC.
Hiroshi Yamauchi76f55b02015-08-21 16:10:39 -07002638 if (!kUseReadBarrier) {
2639 heap->DecrementDisableMovingGC(soa.Self());
2640 } else {
2641 heap->DecrementDisableThreadFlip(soa.Self());
2642 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002643 }
2644 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002645 }
2646
Ian Rogers2d10b202014-05-12 19:15:18 -07002647 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2648 static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2649 jsize start, jsize length, ElementT* buf) {
2650 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2651 ScopedObjectAccess soa(env);
2652 ArtArrayT* array =
2653 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2654 "GetPrimitiveArrayRegion",
2655 "get region of");
2656 if (array != nullptr) {
Vladimir Marko795e3412015-11-06 16:57:03 +00002657 if (start < 0 || length < 0 || length > array->GetLength() - start) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002658 ThrowAIOOBE(soa, array, start, length, "src");
2659 } else {
2660 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2661 ElementT* data = array->GetData();
2662 memcpy(buf, data + start, length * sizeof(ElementT));
2663 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002664 }
2665 }
2666
Ian Rogers2d10b202014-05-12 19:15:18 -07002667 template <typename JArrayT, typename ElementT, typename ArtArrayT>
2668 static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2669 jsize start, jsize length, const ElementT* buf) {
2670 CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2671 ScopedObjectAccess soa(env);
2672 ArtArrayT* array =
2673 DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(soa, java_array,
2674 "SetPrimitiveArrayRegion",
2675 "set region of");
2676 if (array != nullptr) {
Vladimir Marko795e3412015-11-06 16:57:03 +00002677 if (start < 0 || length < 0 || length > array->GetLength() - start) {
Ian Rogers2d10b202014-05-12 19:15:18 -07002678 ThrowAIOOBE(soa, array, start, length, "dst");
2679 } else {
2680 CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2681 ElementT* data = array->GetData();
2682 memcpy(data + start, buf, length * sizeof(ElementT));
2683 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002684 }
2685 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002686};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002687
Elliott Hughes88c5c352012-03-15 18:49:48 -07002688const JNINativeInterface gJniNativeInterface = {
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -08002689 nullptr, // reserved0.
2690 nullptr, // reserved1.
2691 nullptr, // reserved2.
2692 nullptr, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002693 JNI::GetVersion,
2694 JNI::DefineClass,
2695 JNI::FindClass,
2696 JNI::FromReflectedMethod,
2697 JNI::FromReflectedField,
2698 JNI::ToReflectedMethod,
2699 JNI::GetSuperclass,
2700 JNI::IsAssignableFrom,
2701 JNI::ToReflectedField,
2702 JNI::Throw,
2703 JNI::ThrowNew,
2704 JNI::ExceptionOccurred,
2705 JNI::ExceptionDescribe,
2706 JNI::ExceptionClear,
2707 JNI::FatalError,
2708 JNI::PushLocalFrame,
2709 JNI::PopLocalFrame,
2710 JNI::NewGlobalRef,
2711 JNI::DeleteGlobalRef,
2712 JNI::DeleteLocalRef,
2713 JNI::IsSameObject,
2714 JNI::NewLocalRef,
2715 JNI::EnsureLocalCapacity,
2716 JNI::AllocObject,
2717 JNI::NewObject,
2718 JNI::NewObjectV,
2719 JNI::NewObjectA,
2720 JNI::GetObjectClass,
2721 JNI::IsInstanceOf,
2722 JNI::GetMethodID,
2723 JNI::CallObjectMethod,
2724 JNI::CallObjectMethodV,
2725 JNI::CallObjectMethodA,
2726 JNI::CallBooleanMethod,
2727 JNI::CallBooleanMethodV,
2728 JNI::CallBooleanMethodA,
2729 JNI::CallByteMethod,
2730 JNI::CallByteMethodV,
2731 JNI::CallByteMethodA,
2732 JNI::CallCharMethod,
2733 JNI::CallCharMethodV,
2734 JNI::CallCharMethodA,
2735 JNI::CallShortMethod,
2736 JNI::CallShortMethodV,
2737 JNI::CallShortMethodA,
2738 JNI::CallIntMethod,
2739 JNI::CallIntMethodV,
2740 JNI::CallIntMethodA,
2741 JNI::CallLongMethod,
2742 JNI::CallLongMethodV,
2743 JNI::CallLongMethodA,
2744 JNI::CallFloatMethod,
2745 JNI::CallFloatMethodV,
2746 JNI::CallFloatMethodA,
2747 JNI::CallDoubleMethod,
2748 JNI::CallDoubleMethodV,
2749 JNI::CallDoubleMethodA,
2750 JNI::CallVoidMethod,
2751 JNI::CallVoidMethodV,
2752 JNI::CallVoidMethodA,
2753 JNI::CallNonvirtualObjectMethod,
2754 JNI::CallNonvirtualObjectMethodV,
2755 JNI::CallNonvirtualObjectMethodA,
2756 JNI::CallNonvirtualBooleanMethod,
2757 JNI::CallNonvirtualBooleanMethodV,
2758 JNI::CallNonvirtualBooleanMethodA,
2759 JNI::CallNonvirtualByteMethod,
2760 JNI::CallNonvirtualByteMethodV,
2761 JNI::CallNonvirtualByteMethodA,
2762 JNI::CallNonvirtualCharMethod,
2763 JNI::CallNonvirtualCharMethodV,
2764 JNI::CallNonvirtualCharMethodA,
2765 JNI::CallNonvirtualShortMethod,
2766 JNI::CallNonvirtualShortMethodV,
2767 JNI::CallNonvirtualShortMethodA,
2768 JNI::CallNonvirtualIntMethod,
2769 JNI::CallNonvirtualIntMethodV,
2770 JNI::CallNonvirtualIntMethodA,
2771 JNI::CallNonvirtualLongMethod,
2772 JNI::CallNonvirtualLongMethodV,
2773 JNI::CallNonvirtualLongMethodA,
2774 JNI::CallNonvirtualFloatMethod,
2775 JNI::CallNonvirtualFloatMethodV,
2776 JNI::CallNonvirtualFloatMethodA,
2777 JNI::CallNonvirtualDoubleMethod,
2778 JNI::CallNonvirtualDoubleMethodV,
2779 JNI::CallNonvirtualDoubleMethodA,
2780 JNI::CallNonvirtualVoidMethod,
2781 JNI::CallNonvirtualVoidMethodV,
2782 JNI::CallNonvirtualVoidMethodA,
2783 JNI::GetFieldID,
2784 JNI::GetObjectField,
2785 JNI::GetBooleanField,
2786 JNI::GetByteField,
2787 JNI::GetCharField,
2788 JNI::GetShortField,
2789 JNI::GetIntField,
2790 JNI::GetLongField,
2791 JNI::GetFloatField,
2792 JNI::GetDoubleField,
2793 JNI::SetObjectField,
2794 JNI::SetBooleanField,
2795 JNI::SetByteField,
2796 JNI::SetCharField,
2797 JNI::SetShortField,
2798 JNI::SetIntField,
2799 JNI::SetLongField,
2800 JNI::SetFloatField,
2801 JNI::SetDoubleField,
2802 JNI::GetStaticMethodID,
2803 JNI::CallStaticObjectMethod,
2804 JNI::CallStaticObjectMethodV,
2805 JNI::CallStaticObjectMethodA,
2806 JNI::CallStaticBooleanMethod,
2807 JNI::CallStaticBooleanMethodV,
2808 JNI::CallStaticBooleanMethodA,
2809 JNI::CallStaticByteMethod,
2810 JNI::CallStaticByteMethodV,
2811 JNI::CallStaticByteMethodA,
2812 JNI::CallStaticCharMethod,
2813 JNI::CallStaticCharMethodV,
2814 JNI::CallStaticCharMethodA,
2815 JNI::CallStaticShortMethod,
2816 JNI::CallStaticShortMethodV,
2817 JNI::CallStaticShortMethodA,
2818 JNI::CallStaticIntMethod,
2819 JNI::CallStaticIntMethodV,
2820 JNI::CallStaticIntMethodA,
2821 JNI::CallStaticLongMethod,
2822 JNI::CallStaticLongMethodV,
2823 JNI::CallStaticLongMethodA,
2824 JNI::CallStaticFloatMethod,
2825 JNI::CallStaticFloatMethodV,
2826 JNI::CallStaticFloatMethodA,
2827 JNI::CallStaticDoubleMethod,
2828 JNI::CallStaticDoubleMethodV,
2829 JNI::CallStaticDoubleMethodA,
2830 JNI::CallStaticVoidMethod,
2831 JNI::CallStaticVoidMethodV,
2832 JNI::CallStaticVoidMethodA,
2833 JNI::GetStaticFieldID,
2834 JNI::GetStaticObjectField,
2835 JNI::GetStaticBooleanField,
2836 JNI::GetStaticByteField,
2837 JNI::GetStaticCharField,
2838 JNI::GetStaticShortField,
2839 JNI::GetStaticIntField,
2840 JNI::GetStaticLongField,
2841 JNI::GetStaticFloatField,
2842 JNI::GetStaticDoubleField,
2843 JNI::SetStaticObjectField,
2844 JNI::SetStaticBooleanField,
2845 JNI::SetStaticByteField,
2846 JNI::SetStaticCharField,
2847 JNI::SetStaticShortField,
2848 JNI::SetStaticIntField,
2849 JNI::SetStaticLongField,
2850 JNI::SetStaticFloatField,
2851 JNI::SetStaticDoubleField,
2852 JNI::NewString,
2853 JNI::GetStringLength,
2854 JNI::GetStringChars,
2855 JNI::ReleaseStringChars,
2856 JNI::NewStringUTF,
2857 JNI::GetStringUTFLength,
2858 JNI::GetStringUTFChars,
2859 JNI::ReleaseStringUTFChars,
2860 JNI::GetArrayLength,
2861 JNI::NewObjectArray,
2862 JNI::GetObjectArrayElement,
2863 JNI::SetObjectArrayElement,
2864 JNI::NewBooleanArray,
2865 JNI::NewByteArray,
2866 JNI::NewCharArray,
2867 JNI::NewShortArray,
2868 JNI::NewIntArray,
2869 JNI::NewLongArray,
2870 JNI::NewFloatArray,
2871 JNI::NewDoubleArray,
2872 JNI::GetBooleanArrayElements,
2873 JNI::GetByteArrayElements,
2874 JNI::GetCharArrayElements,
2875 JNI::GetShortArrayElements,
2876 JNI::GetIntArrayElements,
2877 JNI::GetLongArrayElements,
2878 JNI::GetFloatArrayElements,
2879 JNI::GetDoubleArrayElements,
2880 JNI::ReleaseBooleanArrayElements,
2881 JNI::ReleaseByteArrayElements,
2882 JNI::ReleaseCharArrayElements,
2883 JNI::ReleaseShortArrayElements,
2884 JNI::ReleaseIntArrayElements,
2885 JNI::ReleaseLongArrayElements,
2886 JNI::ReleaseFloatArrayElements,
2887 JNI::ReleaseDoubleArrayElements,
2888 JNI::GetBooleanArrayRegion,
2889 JNI::GetByteArrayRegion,
2890 JNI::GetCharArrayRegion,
2891 JNI::GetShortArrayRegion,
2892 JNI::GetIntArrayRegion,
2893 JNI::GetLongArrayRegion,
2894 JNI::GetFloatArrayRegion,
2895 JNI::GetDoubleArrayRegion,
2896 JNI::SetBooleanArrayRegion,
2897 JNI::SetByteArrayRegion,
2898 JNI::SetCharArrayRegion,
2899 JNI::SetShortArrayRegion,
2900 JNI::SetIntArrayRegion,
2901 JNI::SetLongArrayRegion,
2902 JNI::SetFloatArrayRegion,
2903 JNI::SetDoubleArrayRegion,
2904 JNI::RegisterNatives,
2905 JNI::UnregisterNatives,
2906 JNI::MonitorEnter,
2907 JNI::MonitorExit,
2908 JNI::GetJavaVM,
2909 JNI::GetStringRegion,
2910 JNI::GetStringUTFRegion,
2911 JNI::GetPrimitiveArrayCritical,
2912 JNI::ReleasePrimitiveArrayCritical,
2913 JNI::GetStringCritical,
2914 JNI::ReleaseStringCritical,
2915 JNI::NewWeakGlobalRef,
2916 JNI::DeleteWeakGlobalRef,
2917 JNI::ExceptionCheck,
2918 JNI::NewDirectByteBuffer,
2919 JNI::GetDirectBufferAddress,
2920 JNI::GetDirectBufferCapacity,
2921 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002922};
2923
Ian Rogers68d8b422014-07-17 11:09:10 -07002924const JNINativeInterface* GetJniNativeInterface() {
2925 return &gJniNativeInterface;
Elliott Hughes410c0c82011-09-01 17:58:25 -07002926}
2927
Mathieu Chartier4d87df62016-01-07 15:14:19 -08002928void (*gJniSleepForeverStub[])() = {
2929 nullptr, // reserved0.
2930 nullptr, // reserved1.
2931 nullptr, // reserved2.
2932 nullptr, // reserved3.
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 SleepForever,
3135 SleepForever,
3136 SleepForever,
3137 SleepForever,
3138 SleepForever,
3139 SleepForever,
3140 SleepForever,
3141 SleepForever,
3142 SleepForever,
3143 SleepForever,
3144 SleepForever,
3145 SleepForever,
3146 SleepForever,
3147 SleepForever,
3148 SleepForever,
3149 SleepForever,
3150 SleepForever,
3151 SleepForever,
3152 SleepForever,
3153 SleepForever,
3154 SleepForever,
3155 SleepForever,
3156 SleepForever,
3157 SleepForever,
3158 SleepForever,
3159 SleepForever,
3160 SleepForever,
3161 SleepForever,
3162};
3163
3164const JNINativeInterface* GetRuntimeShutdownNativeInterface() {
3165 return reinterpret_cast<JNINativeInterface*>(&gJniSleepForeverStub);
3166}
3167
Ian Rogersdf20fe02011-07-20 20:34:16 -07003168} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07003169
3170std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3171 switch (rhs) {
3172 case JNIInvalidRefType:
3173 os << "JNIInvalidRefType";
3174 return os;
3175 case JNILocalRefType:
3176 os << "JNILocalRefType";
3177 return os;
3178 case JNIGlobalRefType:
3179 os << "JNIGlobalRefType";
3180 return os;
3181 case JNIWeakGlobalRefType:
3182 os << "JNIWeakGlobalRefType";
3183 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08003184 default:
Andreas Gampe3fec9ac2016-09-13 10:47:28 -07003185 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Ian Rogersc7dd2952014-10-21 23:31:19 -07003186 UNREACHABLE();
Elliott Hughesb465ab02011-08-24 11:21:21 -07003187 }
3188}