blob: 09efd68d7a7ffe8999dd95188546657118f2dc85 [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 Hughes410c0c82011-09-01 17:58:25 -070085 void VisitRoots(Heap::RootVisitor*, void*);
86
Elliott Hughesf2682d52011-08-15 16:37:04 -070087 Runtime* runtime;
Elliott Hughesbbd76712011-08-17 10:25:24 -070088
Elliott Hughesa2501992011-08-26 19:39:54 -070089 // Used for testing. By default, we'll LOG(FATAL) the reason.
90 void (*check_jni_abort_hook)(const std::string& reason);
91
Elliott Hughesa0957642011-09-02 14:27:33 -070092 // Extra checking.
Elliott Hughes515a5bc2011-08-17 11:08:34 -070093 bool check_jni;
Elliott Hughesa2501992011-08-26 19:39:54 -070094 bool force_copy;
Elliott Hughes515a5bc2011-08-17 11:08:34 -070095
Elliott Hughesa0957642011-09-02 14:27:33 -070096 // Extra diagnostics.
97 bool verbose_jni;
98 bool log_third_party_jni;
99 std::string trace;
100
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700101 // Used to provide compatibility for apps that assumed direct references.
102 bool work_around_app_jni_bugs;
103
Elliott Hughesbbd76712011-08-17 10:25:24 -0700104 // Used to hold references to pinned primitive arrays.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700105 Mutex pins_lock;
Elliott Hughesbbd76712011-08-17 10:25:24 -0700106 ReferenceTable pin_table;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700107
108 // JNI global references.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700109 Mutex globals_lock;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700110 IndirectReferenceTable globals;
111
112 // JNI weak global references.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700113 Mutex weak_globals_lock;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700114 IndirectReferenceTable weak_globals;
Elliott Hughes0af55432011-08-17 18:37:28 -0700115
Elliott Hughes8daa0922011-09-11 13:46:25 -0700116 Mutex libraries_lock;
Elliott Hughes79082e32011-08-25 12:07:32 -0700117 Libraries* libraries;
Elliott Hughesa2501992011-08-26 19:39:54 -0700118
119 // Used by -Xcheck:jni.
120 const JNIInvokeInterface* unchecked_functions;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700121};
122
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700123struct JNIEnvExt : public JNIEnv {
Elliott Hughes75770752011-08-24 17:52:38 -0700124 JNIEnvExt(Thread* self, JavaVMExt* vm);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700125 ~JNIEnvExt();
Elliott Hughesbbd76712011-08-17 10:25:24 -0700126
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700127 void DumpReferenceTables();
128
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700129 void PushFrame(int capacity);
130 void PopFrame();
131
Ian Rogersdc51b792011-09-22 20:41:37 -0700132 static Offset SegmentStateOffset() {
133 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
134 IndirectReferenceTable::SegmentStateOffset().Int32Value());
135 }
136
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700137 static Offset LocalRefCookieOffset() {
138 return Offset(OFFSETOF_MEMBER(JNIEnvExt, local_ref_cookie));
139 }
140
Elliott Hughes85d15452011-09-16 17:33:01 -0700141 Thread* const self;
Elliott Hughes75770752011-08-24 17:52:38 -0700142 JavaVMExt* vm;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700143
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700144 // Cookie used when using the local indirect reference table.
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700145 uint32_t local_ref_cookie;
146
147 // JNI local references.
148 IndirectReferenceTable locals;
149
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700150 // Stack of cookies corresponding to PushLocalFrame/PopLocalFrame calls.
151 // TODO: to avoid leaks (and bugs), we need to clear this vector on entry (or return)
152 // to a native method.
153 std::vector<uint32_t> stacked_local_ref_cookies;
154
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700155 // Frequently-accessed fields cached from JavaVM.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700156 bool check_jni;
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700157 bool work_around_app_jni_bugs;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700158
Elliott Hughesa2501992011-08-26 19:39:54 -0700159 // How many nested "critical" JNI calls are we in?
160 int critical;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700161
Elliott Hughesbbd76712011-08-17 10:25:24 -0700162 // Entered JNI monitors, for bulk exit on thread detach.
Elliott Hughes02b48d12011-09-07 17:15:51 -0700163 ReferenceTable monitors;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700164
Elliott Hughesa2501992011-08-26 19:39:54 -0700165 // Used by -Xcheck:jni.
166 const JNINativeInterface* unchecked_functions;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700167};
168
Elliott Hughesa2501992011-08-26 19:39:54 -0700169const JNINativeInterface* GetCheckJniNativeInterface();
170const JNIInvokeInterface* GetCheckJniInvokeInterface();
171
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700172// Used to save and restore the JNIEnvExt state when not going through code created by the JNI
173// compiler
174class ScopedJniEnvLocalRefState {
175 public:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700176 explicit ScopedJniEnvLocalRefState(JNIEnvExt* env) : env_(env) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700177 saved_local_ref_cookie_ = env->local_ref_cookie;
178 env->local_ref_cookie = env->locals.GetSegmentState();
179 }
180
181 ~ScopedJniEnvLocalRefState() {
182 env_->locals.SetSegmentState(env_->local_ref_cookie);
183 env_->local_ref_cookie = saved_local_ref_cookie_;
184 }
185
186 private:
187 JNIEnvExt* env_;
188 uint32_t saved_local_ref_cookie_;
189 DISALLOW_COPY_AND_ASSIGN(ScopedJniEnvLocalRefState);
190};
191
Ian Rogersdf20fe02011-07-20 20:34:16 -0700192} // namespace art
Carl Shapiroea4dca82011-08-01 13:45:38 -0700193
Elliott Hughesb465ab02011-08-24 11:21:21 -0700194std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs);
195
Ian Rogersdf20fe02011-07-20 20:34:16 -0700196#endif // ART_SRC_JNI_INTERNAL_H_