blob: f7caa0f56dde8ad32c06a08c8f91a602fa8de671 [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"
Elliott Hughesbbd76712011-08-17 10:25:24 -070025#include "reference_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "root_visitor.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;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070048class ScopedObjectAccess;
Elliott Hughes18c07532011-08-18 15:50:51 -070049class Thread;
Ian Rogersdf20fe02011-07-20 20:34:16 -070050
Ian Rogers62d6c772013-02-27 08:32:07 -080051void JniAbortF(const char* jni_function_name, const char* fmt, ...)
52 __attribute__((__format__(__printf__, 2, 3)));
Ian Rogers00f7d0e2012-07-19 15:28:27 -070053void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
Ian Rogersabd7be92013-08-15 10:26:54 -070054 jint method_count);
Elliott Hughesa2501992011-08-26 19:39:54 -070055
Ian Rogers00f7d0e2012-07-19 15:28:27 -070056JValue InvokeWithJValues(const ScopedObjectAccess&, jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -070057 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromea46f952013-07-30 01:26:50 -070058void InvokeWithArgArray(const ScopedObjectAccess& soa, mirror::ArtMethod* method,
Jeff Hao6474d192013-03-26 14:08:09 -070059 ArgArray *arg_array, JValue* result, char result_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -070060 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes418d20f2011-09-22 14:00:39 -070061
Elliott Hughesa4f94742012-05-29 16:28:38 -070062int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause);
63
Elliott Hughes69f5bc62011-08-24 09:26:14 -070064struct JavaVMExt : public JavaVM {
Elliott Hughesa0957642011-09-02 14:27:33 -070065 JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options);
Elliott Hughesde69d7f2011-08-18 16:49:37 -070066 ~JavaVMExt();
Elliott Hughes0af55432011-08-17 18:37:28 -070067
Elliott Hughes75770752011-08-24 17:52:38 -070068 /**
69 * Loads the given shared library. 'path' is an absolute pathname.
Elliott Hughes0af55432011-08-17 18:37:28 -070070 *
Elliott Hughes75770752011-08-24 17:52:38 -070071 * Returns 'true' on success. On failure, sets 'detail' to a
72 * human-readable description of the error.
Elliott Hughes0af55432011-08-17 18:37:28 -070073 */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074 bool LoadNativeLibrary(const std::string& path, mirror::ClassLoader* class_loader,
75 std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -070076 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesbbd76712011-08-17 10:25:24 -070077
Elliott Hughes79082e32011-08-25 12:07:32 -070078 /**
79 * Returns a pointer to the code for the native method 'm', found
80 * using dlsym(3) on every native library that's been loaded so far.
81 */
Brian Carlstromea46f952013-07-30 01:26:50 -070082 void* FindCodeForNativeMethod(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -070083 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -070084
Elliott Hughesae80b492012-04-24 10:43:17 -070085 void DumpForSigQuit(std::ostream& os);
86
Ian Rogers00f7d0e2012-07-19 15:28:27 -070087 void DumpReferenceTables(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -070088 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070089
Elliott Hughes88c5c352012-03-15 18:49:48 -070090 void SetCheckJniEnabled(bool enabled);
Elliott Hughes4ffd3132011-10-24 12:06:42 -070091
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092 void VisitRoots(RootVisitor*, void*);
Elliott Hughes410c0c82011-09-01 17:58:25 -070093
Elliott Hughesf2682d52011-08-15 16:37:04 -070094 Runtime* runtime;
Elliott Hughesbbd76712011-08-17 10:25:24 -070095
Elliott Hughesa2501992011-08-26 19:39:54 -070096 // Used for testing. By default, we'll LOG(FATAL) the reason.
Elliott Hughesb264f082012-04-06 17:10:10 -070097 void (*check_jni_abort_hook)(void* data, const std::string& reason);
98 void* check_jni_abort_hook_data;
Elliott Hughesa2501992011-08-26 19:39:54 -070099
Elliott Hughesa0957642011-09-02 14:27:33 -0700100 // Extra checking.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700101 bool check_jni;
Elliott Hughesa2501992011-08-26 19:39:54 -0700102 bool force_copy;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700103
Elliott Hughesa0957642011-09-02 14:27:33 -0700104 // Extra diagnostics.
Elliott Hughesa0957642011-09-02 14:27:33 -0700105 std::string trace;
106
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700107 // Used to provide compatibility for apps that assumed direct references.
108 bool work_around_app_jni_bugs;
109
Elliott Hughesbbd76712011-08-17 10:25:24 -0700110 // Used to hold references to pinned primitive arrays.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700111 Mutex pins_lock DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughesf8349362012-06-18 15:00:06 -0700112 ReferenceTable pin_table GUARDED_BY(pins_lock);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700113
114 // JNI global references.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700115 Mutex globals_lock DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughesf8349362012-06-18 15:00:06 -0700116 IndirectReferenceTable globals GUARDED_BY(globals_lock);
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700117
118 // JNI weak global references.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700119 Mutex weak_globals_lock DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughesf8349362012-06-18 15:00:06 -0700120 IndirectReferenceTable weak_globals GUARDED_BY(weak_globals_lock);
Elliott Hughes0af55432011-08-17 18:37:28 -0700121
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700122 Mutex libraries_lock DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughesf8349362012-06-18 15:00:06 -0700123 Libraries* libraries GUARDED_BY(libraries_lock);
Elliott Hughesa2501992011-08-26 19:39:54 -0700124
125 // Used by -Xcheck:jni.
126 const JNIInvokeInterface* unchecked_functions;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700127};
128
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700129struct JNIEnvExt : public JNIEnv {
Elliott Hughes75770752011-08-24 17:52:38 -0700130 JNIEnvExt(Thread* self, JavaVMExt* vm);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700131 ~JNIEnvExt();
Elliott Hughesbbd76712011-08-17 10:25:24 -0700132
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700133 void DumpReferenceTables(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700134 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700135
Elliott Hughes88c5c352012-03-15 18:49:48 -0700136 void SetCheckJniEnabled(bool enabled);
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700137
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700138 void PushFrame(int capacity);
139 void PopFrame();
140
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800141 static Offset SegmentStateOffset();
Ian Rogersdc51b792011-09-22 20:41:37 -0700142
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700143 static Offset LocalRefCookieOffset() {
144 return Offset(OFFSETOF_MEMBER(JNIEnvExt, local_ref_cookie));
145 }
146
Ian Rogers848871b2013-08-05 10:56:33 -0700147 static Offset SelfOffset() {
148 return Offset(OFFSETOF_MEMBER(JNIEnvExt, self));
149 }
150
Elliott Hughes85d15452011-09-16 17:33:01 -0700151 Thread* const self;
Elliott Hughes75770752011-08-24 17:52:38 -0700152 JavaVMExt* vm;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700153
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700154 // Cookie used when using the local indirect reference table.
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700155 uint32_t local_ref_cookie;
156
157 // JNI local references.
158 IndirectReferenceTable locals;
159
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700160 // Stack of cookies corresponding to PushLocalFrame/PopLocalFrame calls.
161 // TODO: to avoid leaks (and bugs), we need to clear this vector on entry (or return)
162 // to a native method.
163 std::vector<uint32_t> stacked_local_ref_cookies;
164
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700165 // Frequently-accessed fields cached from JavaVM.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700166 bool check_jni;
167
Elliott Hughesa2501992011-08-26 19:39:54 -0700168 // How many nested "critical" JNI calls are we in?
169 int critical;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700170
Elliott Hughesbbd76712011-08-17 10:25:24 -0700171 // Entered JNI monitors, for bulk exit on thread detach.
Elliott Hughes02b48d12011-09-07 17:15:51 -0700172 ReferenceTable monitors;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700173
Elliott Hughesa2501992011-08-26 19:39:54 -0700174 // Used by -Xcheck:jni.
175 const JNINativeInterface* unchecked_functions;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700176};
177
Elliott Hughesa2501992011-08-26 19:39:54 -0700178const JNINativeInterface* GetCheckJniNativeInterface();
179const JNIInvokeInterface* GetCheckJniInvokeInterface();
180
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700181// Used to save and restore the JNIEnvExt state when not going through code created by the JNI
182// compiler
183class ScopedJniEnvLocalRefState {
184 public:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700185 explicit ScopedJniEnvLocalRefState(JNIEnvExt* env) : env_(env) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700186 saved_local_ref_cookie_ = env->local_ref_cookie;
187 env->local_ref_cookie = env->locals.GetSegmentState();
188 }
189
190 ~ScopedJniEnvLocalRefState() {
191 env_->locals.SetSegmentState(env_->local_ref_cookie);
192 env_->local_ref_cookie = saved_local_ref_cookie_;
193 }
194
195 private:
196 JNIEnvExt* env_;
197 uint32_t saved_local_ref_cookie_;
198 DISALLOW_COPY_AND_ASSIGN(ScopedJniEnvLocalRefState);
199};
200
Ian Rogersdf20fe02011-07-20 20:34:16 -0700201} // namespace art
Carl Shapiroea4dca82011-08-01 13:45:38 -0700202
Elliott Hughesb465ab02011-08-24 11:21:21 -0700203std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs);
204
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700205#endif // ART_RUNTIME_JNI_INTERNAL_H_