blob: 7b49d33625b67ed5e3c847c8780bd0a70759b4c7 [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 Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_JNI_INTERNAL_H_
18#define ART_RUNTIME_JNI_INTERNAL_H_
Ian Rogersdf20fe02011-07-20 20:34:16 -070019
20#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070021
Elliott Hughes76160052012-12-12 16:31:20 -080022#include "base/macros.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080023#include "base/mutex.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070024#include "indirect_reference_table.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080025#include "object_callbacks.h"
Elliott Hughesbbd76712011-08-17 10:25:24 -070026#include "reference_table.h"
Elliott Hughesa0957642011-09-02 14:27:33 -070027#include "runtime.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070028
Elliott Hughesae80b492012-04-24 10:43:17 -070029#include <iosfwd>
Elliott Hughes0af55432011-08-17 18:37:28 -070030#include <string>
31
Elliott Hugheseac76672012-05-24 21:56:51 -070032#ifndef NATIVE_METHOD
33#define NATIVE_METHOD(className, functionName, signature) \
34 { #functionName, signature, reinterpret_cast<void*>(className ## _ ## functionName) }
35#endif
36#define REGISTER_NATIVE_METHODS(jni_class_name) \
37 RegisterNativeMethods(env, jni_class_name, gMethods, arraysize(gMethods))
38
Ian Rogersdf20fe02011-07-20 20:34:16 -070039namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040namespace mirror {
Brian Carlstromea46f952013-07-30 01:26:50 -070041 class ArtField;
42 class ArtMethod;
43 class ClassLoader;
44} // namespace mirror
Jeff Hao5d917302013-02-27 17:57:33 -080045class ArgArray;
Elliott Hughes418d20f2011-09-22 14:00:39 -070046union JValue;
Elliott Hughes79082e32011-08-25 12:07:32 -070047class Libraries;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080048class ParsedOptions;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070049class ScopedObjectAccess;
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080050template<class T> class SirtRef;
Elliott Hughes18c07532011-08-18 15:50:51 -070051class Thread;
Ian Rogersdf20fe02011-07-20 20:34:16 -070052
Ian Rogers62d6c772013-02-27 08:32:07 -080053void JniAbortF(const char* jni_function_name, const char* fmt, ...)
54 __attribute__((__format__(__printf__, 2, 3)));
Ian Rogers00f7d0e2012-07-19 15:28:27 -070055void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersabd7be92013-08-15 10:26:54 -070056 jint method_count);
Elliott Hughesa2501992011-08-26 19:39:54 -070057
Ian Rogers00f7d0e2012-07-19 15:28:27 -070058JValue InvokeWithJValues(const ScopedObjectAccess&, jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -070059 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromea46f952013-07-30 01:26:50 -070060void InvokeWithArgArray(const ScopedObjectAccess& soa, mirror::ArtMethod* method,
Ian Rogers0177e532014-02-11 16:30:46 -080061 ArgArray *arg_array, JValue* result, const char* shorty)
Ian Rogersb726dcb2012-09-05 08:57:23 -070062 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes418d20f2011-09-22 14:00:39 -070063
Elliott Hughesa4f94742012-05-29 16:28:38 -070064int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause);
65
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070066class JavaVMExt : public JavaVM {
67 public:
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080068 JavaVMExt(Runtime* runtime, ParsedOptions* options);
Elliott Hughesde69d7f2011-08-18 16:49:37 -070069 ~JavaVMExt();
Elliott Hughes0af55432011-08-17 18:37:28 -070070
Elliott Hughes75770752011-08-24 17:52:38 -070071 /**
72 * Loads the given shared library. 'path' is an absolute pathname.
Elliott Hughes0af55432011-08-17 18:37:28 -070073 *
Elliott Hughes75770752011-08-24 17:52:38 -070074 * Returns 'true' on success. On failure, sets 'detail' to a
75 * human-readable description of the error.
Elliott Hughes0af55432011-08-17 18:37:28 -070076 */
Mathieu Chartier055d46c2014-02-06 11:22:17 -080077 bool LoadNativeLibrary(const std::string& path, const SirtRef<mirror::ClassLoader>& class_loader,
Ian Rogers1eb512d2013-10-18 15:42:20 -070078 std::string* detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -070079 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesbbd76712011-08-17 10:25:24 -070080
Elliott Hughes79082e32011-08-25 12:07:32 -070081 /**
82 * Returns a pointer to the code for the native method 'm', found
83 * using dlsym(3) on every native library that's been loaded so far.
84 */
Brian Carlstromea46f952013-07-30 01:26:50 -070085 void* FindCodeForNativeMethod(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -070086 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -070087
Elliott Hughesae80b492012-04-24 10:43:17 -070088 void DumpForSigQuit(std::ostream& os);
89
Ian Rogers00f7d0e2012-07-19 15:28:27 -070090 void DumpReferenceTables(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -070091 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070092
Elliott Hughes88c5c352012-03-15 18:49:48 -070093 void SetCheckJniEnabled(bool enabled);
Elliott Hughes4ffd3132011-10-24 12:06:42 -070094
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080095 void VisitRoots(RootCallback* callback, void* arg);
Elliott Hughes410c0c82011-09-01 17:58:25 -070096
Mathieu Chartierc11d9b82013-09-19 10:01:59 -070097 void DisallowNewWeakGlobals() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
98 void AllowNewWeakGlobals() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
99 jweak AddWeakGlobalReference(Thread* self, mirror::Object* obj)
100 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
101 void DeleteWeakGlobalRef(Thread* self, jweak obj)
102 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800103 void SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg);
Mathieu Chartierc645f1d2014-03-06 18:11:53 -0800104 mirror::Object* DecodeWeakGlobal(Thread* self, IndirectRef ref)
105 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700106
Elliott Hughesf2682d52011-08-15 16:37:04 -0700107 Runtime* runtime;
Elliott Hughesbbd76712011-08-17 10:25:24 -0700108
Elliott Hughesa2501992011-08-26 19:39:54 -0700109 // Used for testing. By default, we'll LOG(FATAL) the reason.
Elliott Hughesb264f082012-04-06 17:10:10 -0700110 void (*check_jni_abort_hook)(void* data, const std::string& reason);
111 void* check_jni_abort_hook_data;
Elliott Hughesa2501992011-08-26 19:39:54 -0700112
Elliott Hughesa0957642011-09-02 14:27:33 -0700113 // Extra checking.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700114 bool check_jni;
Elliott Hughesa2501992011-08-26 19:39:54 -0700115 bool force_copy;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700116
Elliott Hughesa0957642011-09-02 14:27:33 -0700117 // Extra diagnostics.
Elliott Hughesa0957642011-09-02 14:27:33 -0700118 std::string trace;
119
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700120 // Used to provide compatibility for apps that assumed direct references.
121 bool work_around_app_jni_bugs;
122
Elliott Hughesbbd76712011-08-17 10:25:24 -0700123 // Used to hold references to pinned primitive arrays.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700124 Mutex pins_lock DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughesf8349362012-06-18 15:00:06 -0700125 ReferenceTable pin_table GUARDED_BY(pins_lock);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700126
127 // JNI global references.
Ian Rogersb8a0b942013-08-20 18:09:52 -0700128 ReaderWriterMutex globals_lock DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughesf8349362012-06-18 15:00:06 -0700129 IndirectReferenceTable globals GUARDED_BY(globals_lock);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700130
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700131 Mutex libraries_lock DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughesf8349362012-06-18 15:00:06 -0700132 Libraries* libraries GUARDED_BY(libraries_lock);
Elliott Hughesa2501992011-08-26 19:39:54 -0700133
134 // Used by -Xcheck:jni.
135 const JNIInvokeInterface* unchecked_functions;
Mathieu Chartierc11d9b82013-09-19 10:01:59 -0700136
137 private:
138 // TODO: Make the other members of this class also private.
139 // JNI weak global references.
140 Mutex weak_globals_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
141 IndirectReferenceTable weak_globals_ GUARDED_BY(weak_globals_lock_);
142 bool allow_new_weak_globals_ GUARDED_BY(weak_globals_lock_);
143 ConditionVariable weak_globals_add_condition_ GUARDED_BY(weak_globals_lock_);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700144};
145
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700146struct JNIEnvExt : public JNIEnv {
Elliott Hughes75770752011-08-24 17:52:38 -0700147 JNIEnvExt(Thread* self, JavaVMExt* vm);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700148 ~JNIEnvExt();
Elliott Hughesbbd76712011-08-17 10:25:24 -0700149
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700150 void DumpReferenceTables(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700151 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700152
Elliott Hughes88c5c352012-03-15 18:49:48 -0700153 void SetCheckJniEnabled(bool enabled);
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700154
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700155 void PushFrame(int capacity);
156 void PopFrame();
157
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800158 static Offset SegmentStateOffset();
Ian Rogersdc51b792011-09-22 20:41:37 -0700159
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700160 static Offset LocalRefCookieOffset() {
161 return Offset(OFFSETOF_MEMBER(JNIEnvExt, local_ref_cookie));
162 }
163
Ian Rogers848871b2013-08-05 10:56:33 -0700164 static Offset SelfOffset() {
165 return Offset(OFFSETOF_MEMBER(JNIEnvExt, self));
166 }
167
Mathieu Chartier590fee92013-09-13 13:46:47 -0700168 jobject NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
169 void DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
170
Elliott Hughes85d15452011-09-16 17:33:01 -0700171 Thread* const self;
Elliott Hughes75770752011-08-24 17:52:38 -0700172 JavaVMExt* vm;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700173
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700174 // Cookie used when using the local indirect reference table.
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700175 uint32_t local_ref_cookie;
176
177 // JNI local references.
178 IndirectReferenceTable locals;
179
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700180 // Stack of cookies corresponding to PushLocalFrame/PopLocalFrame calls.
181 // TODO: to avoid leaks (and bugs), we need to clear this vector on entry (or return)
182 // to a native method.
183 std::vector<uint32_t> stacked_local_ref_cookies;
184
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700185 // Frequently-accessed fields cached from JavaVM.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700186 bool check_jni;
187
Elliott Hughesa2501992011-08-26 19:39:54 -0700188 // How many nested "critical" JNI calls are we in?
189 int critical;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700190
Elliott Hughesbbd76712011-08-17 10:25:24 -0700191 // Entered JNI monitors, for bulk exit on thread detach.
Elliott Hughes02b48d12011-09-07 17:15:51 -0700192 ReferenceTable monitors;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700193
Elliott Hughesa2501992011-08-26 19:39:54 -0700194 // Used by -Xcheck:jni.
195 const JNINativeInterface* unchecked_functions;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700196};
197
Elliott Hughesa2501992011-08-26 19:39:54 -0700198const JNINativeInterface* GetCheckJniNativeInterface();
199const JNIInvokeInterface* GetCheckJniInvokeInterface();
200
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700201// Used to save and restore the JNIEnvExt state when not going through code created by the JNI
202// compiler
203class ScopedJniEnvLocalRefState {
204 public:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700205 explicit ScopedJniEnvLocalRefState(JNIEnvExt* env) : env_(env) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700206 saved_local_ref_cookie_ = env->local_ref_cookie;
207 env->local_ref_cookie = env->locals.GetSegmentState();
208 }
209
210 ~ScopedJniEnvLocalRefState() {
211 env_->locals.SetSegmentState(env_->local_ref_cookie);
212 env_->local_ref_cookie = saved_local_ref_cookie_;
213 }
214
215 private:
216 JNIEnvExt* env_;
217 uint32_t saved_local_ref_cookie_;
218 DISALLOW_COPY_AND_ASSIGN(ScopedJniEnvLocalRefState);
219};
220
Ian Rogersdf20fe02011-07-20 20:34:16 -0700221} // namespace art
Carl Shapiroea4dca82011-08-01 13:45:38 -0700222
Elliott Hughesb465ab02011-08-24 11:21:21 -0700223std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs);
224
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700225#endif // ART_RUNTIME_JNI_INTERNAL_H_