blob: 25e9e62ce786514b7826e73f462bee3fc6b960e4 [file] [log] [blame]
Ian Rogersdf20fe02011-07-20 20:34:16 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_JNI_INTERNAL_H_
4#define ART_SRC_JNI_INTERNAL_H_
5
6#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07007
Elliott Hughes410c0c82011-09-01 17:58:25 -07008#include "heap.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -07009#include "indirect_reference_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "macros.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070011#include "mutex.h"
Elliott Hughesbbd76712011-08-17 10:25:24 -070012#include "reference_table.h"
Elliott Hughesa0957642011-09-02 14:27:33 -070013#include "runtime.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070014
Elliott Hughes0af55432011-08-17 18:37:28 -070015#include <string>
16
Ian Rogersdf20fe02011-07-20 20:34:16 -070017namespace art {
18
Elliott Hughes814e4032011-08-23 12:07:56 -070019class ClassLoader;
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070020class Field;
Elliott Hughes418d20f2011-09-22 14:00:39 -070021union JValue;
Elliott Hughes79082e32011-08-25 12:07:32 -070022class Libraries;
23class Method;
Elliott Hughes18c07532011-08-18 15:50:51 -070024class Thread;
Ian Rogersdf20fe02011-07-20 20:34:16 -070025
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070026void SetJniGlobalsMax(size_t max);
Elliott Hughesa2501992011-08-26 19:39:54 -070027void JniAbort(const char* jni_function_name);
Shih-wei Liao31384c52011-09-06 15:27:45 -070028void* FindNativeMethod(Thread* thread);
Elliott Hughesa2501992011-08-26 19:39:54 -070029
Elliott Hughesbf86d042011-08-31 17:53:14 -070030template<typename T> T Decode(JNIEnv*, jobject);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070031template<typename T> T AddLocalReference(JNIEnv*, const Object*);
Elliott Hughesbf86d042011-08-31 17:53:14 -070032
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070033inline Field* DecodeField(jfieldID fid) {
34#ifdef MOVING_GARBAGE_COLLECTOR
35 // TODO: we should make these unique weak globals if Field instances can ever move.
36 UNIMPLEMENTED(WARNING);
37#endif
38 return reinterpret_cast<Field*>(fid);
39}
40
Brian Carlstromf91c8c32011-09-21 17:30:34 -070041inline jfieldID EncodeField(Field* field) {
42#ifdef MOVING_GARBAGE_COLLECTOR
43 UNIMPLEMENTED(WARNING);
44#endif
45 return reinterpret_cast<jfieldID>(field);
46}
47
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070048inline Method* DecodeMethod(jmethodID mid) {
49#ifdef MOVING_GARBAGE_COLLECTOR
50 // TODO: we should make these unique weak globals if Method instances can ever move.
51 UNIMPLEMENTED(WARNING);
52#endif
53 return reinterpret_cast<Method*>(mid);
54}
55
Brian Carlstromf91c8c32011-09-21 17:30:34 -070056inline jmethodID EncodeMethod(Method* method) {
57#ifdef MOVING_GARBAGE_COLLECTOR
58 UNIMPLEMENTED(WARNING);
59#endif
60 return reinterpret_cast<jmethodID>(method);
61}
62
Elliott Hughes418d20f2011-09-22 14:00:39 -070063JValue InvokeWithJValues(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args);
64
Elliott Hughes69f5bc62011-08-24 09:26:14 -070065struct JavaVMExt : public JavaVM {
Elliott Hughesa0957642011-09-02 14:27:33 -070066 JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options);
Elliott Hughesde69d7f2011-08-18 16:49:37 -070067 ~JavaVMExt();
Elliott Hughes0af55432011-08-17 18:37:28 -070068
Elliott Hughes75770752011-08-24 17:52:38 -070069 /**
70 * Loads the given shared library. 'path' is an absolute pathname.
Elliott Hughes0af55432011-08-17 18:37:28 -070071 *
Elliott Hughes75770752011-08-24 17:52:38 -070072 * Returns 'true' on success. On failure, sets 'detail' to a
73 * human-readable description of the error.
Elliott Hughes0af55432011-08-17 18:37:28 -070074 */
Elliott Hughes75770752011-08-24 17:52:38 -070075 bool LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail);
Elliott Hughesbbd76712011-08-17 10:25:24 -070076
Elliott Hughes79082e32011-08-25 12:07:32 -070077 /**
78 * Returns a pointer to the code for the native method 'm', found
79 * using dlsym(3) on every native library that's been loaded so far.
80 */
81 void* FindCodeForNativeMethod(Method* m);
82
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070083 void DumpReferenceTables();
84
Elliott Hughes4ffd3132011-10-24 12:06:42 -070085 void EnableCheckJni();
86
Elliott Hughes410c0c82011-09-01 17:58:25 -070087 void VisitRoots(Heap::RootVisitor*, void*);
88
Elliott Hughesf2682d52011-08-15 16:37:04 -070089 Runtime* runtime;
Elliott Hughesbbd76712011-08-17 10:25:24 -070090
Elliott Hughesa2501992011-08-26 19:39:54 -070091 // Used for testing. By default, we'll LOG(FATAL) the reason.
92 void (*check_jni_abort_hook)(const std::string& reason);
93
Elliott Hughesa0957642011-09-02 14:27:33 -070094 // Extra checking.
Elliott Hughes515a5bc2011-08-17 11:08:34 -070095 bool check_jni;
Elliott Hughesa2501992011-08-26 19:39:54 -070096 bool force_copy;
Elliott Hughes515a5bc2011-08-17 11:08:34 -070097
Elliott Hughesa0957642011-09-02 14:27:33 -070098 // Extra diagnostics.
99 bool verbose_jni;
100 bool log_third_party_jni;
101 std::string trace;
102
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700103 // Used to provide compatibility for apps that assumed direct references.
104 bool work_around_app_jni_bugs;
105
Elliott Hughesbbd76712011-08-17 10:25:24 -0700106 // Used to hold references to pinned primitive arrays.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700107 Mutex pins_lock;
Elliott Hughesbbd76712011-08-17 10:25:24 -0700108 ReferenceTable pin_table;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700109
110 // JNI global references.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700111 Mutex globals_lock;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700112 IndirectReferenceTable globals;
113
114 // JNI weak global references.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700115 Mutex weak_globals_lock;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700116 IndirectReferenceTable weak_globals;
Elliott Hughes0af55432011-08-17 18:37:28 -0700117
Elliott Hughes8daa0922011-09-11 13:46:25 -0700118 Mutex libraries_lock;
Elliott Hughes79082e32011-08-25 12:07:32 -0700119 Libraries* libraries;
Elliott Hughesa2501992011-08-26 19:39:54 -0700120
121 // Used by -Xcheck:jni.
122 const JNIInvokeInterface* unchecked_functions;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700123};
124
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700125struct JNIEnvExt : public JNIEnv {
Elliott Hughes75770752011-08-24 17:52:38 -0700126 JNIEnvExt(Thread* self, JavaVMExt* vm);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700127 ~JNIEnvExt();
Elliott Hughesbbd76712011-08-17 10:25:24 -0700128
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700129 void DumpReferenceTables();
130
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700131 void EnableCheckJni();
132
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700133 void PushFrame(int capacity);
134 void PopFrame();
135
Ian Rogersdc51b792011-09-22 20:41:37 -0700136 static Offset SegmentStateOffset() {
137 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
138 IndirectReferenceTable::SegmentStateOffset().Int32Value());
139 }
140
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700141 static Offset LocalRefCookieOffset() {
142 return Offset(OFFSETOF_MEMBER(JNIEnvExt, local_ref_cookie));
143 }
144
Elliott Hughes85d15452011-09-16 17:33:01 -0700145 Thread* const self;
Elliott Hughes75770752011-08-24 17:52:38 -0700146 JavaVMExt* vm;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700147
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700148 // Cookie used when using the local indirect reference table.
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700149 uint32_t local_ref_cookie;
150
151 // JNI local references.
152 IndirectReferenceTable locals;
153
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700154 // Stack of cookies corresponding to PushLocalFrame/PopLocalFrame calls.
155 // TODO: to avoid leaks (and bugs), we need to clear this vector on entry (or return)
156 // to a native method.
157 std::vector<uint32_t> stacked_local_ref_cookies;
158
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700159 // Frequently-accessed fields cached from JavaVM.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700160 bool check_jni;
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700161 bool work_around_app_jni_bugs;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700162
Elliott Hughesa2501992011-08-26 19:39:54 -0700163 // How many nested "critical" JNI calls are we in?
164 int critical;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700165
Elliott Hughesbbd76712011-08-17 10:25:24 -0700166 // Entered JNI monitors, for bulk exit on thread detach.
Elliott Hughes02b48d12011-09-07 17:15:51 -0700167 ReferenceTable monitors;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700168
Elliott Hughesa2501992011-08-26 19:39:54 -0700169 // Used by -Xcheck:jni.
170 const JNINativeInterface* unchecked_functions;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700171};
172
Elliott Hughesa2501992011-08-26 19:39:54 -0700173const JNINativeInterface* GetCheckJniNativeInterface();
174const JNIInvokeInterface* GetCheckJniInvokeInterface();
175
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700176// Used to save and restore the JNIEnvExt state when not going through code created by the JNI
177// compiler
178class ScopedJniEnvLocalRefState {
179 public:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700180 explicit ScopedJniEnvLocalRefState(JNIEnvExt* env) : env_(env) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700181 saved_local_ref_cookie_ = env->local_ref_cookie;
182 env->local_ref_cookie = env->locals.GetSegmentState();
183 }
184
185 ~ScopedJniEnvLocalRefState() {
186 env_->locals.SetSegmentState(env_->local_ref_cookie);
187 env_->local_ref_cookie = saved_local_ref_cookie_;
188 }
189
190 private:
191 JNIEnvExt* env_;
192 uint32_t saved_local_ref_cookie_;
193 DISALLOW_COPY_AND_ASSIGN(ScopedJniEnvLocalRefState);
194};
195
Ian Rogersdf20fe02011-07-20 20:34:16 -0700196} // namespace art
Carl Shapiroea4dca82011-08-01 13:45:38 -0700197
Elliott Hughesb465ab02011-08-24 11:21:21 -0700198std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs);
199
Ian Rogersdf20fe02011-07-20 20:34:16 -0700200#endif // ART_SRC_JNI_INTERNAL_H_