blob: dc268c49350ad780cba010957e1373ca4de9894a [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 Hughes844f9a02012-01-24 20:19:58 -080029jclass CacheClass(JNIEnv* env, const char* jni_class_name);
Elliott Hughesa2501992011-08-26 19:39:54 -070030
Elliott Hughesbf86d042011-08-31 17:53:14 -070031template<typename T> T Decode(JNIEnv*, jobject);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070032template<typename T> T AddLocalReference(JNIEnv*, const Object*);
Elliott Hughesbf86d042011-08-31 17:53:14 -070033
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070034inline Field* DecodeField(jfieldID fid) {
35#ifdef MOVING_GARBAGE_COLLECTOR
36 // TODO: we should make these unique weak globals if Field instances can ever move.
37 UNIMPLEMENTED(WARNING);
38#endif
39 return reinterpret_cast<Field*>(fid);
40}
41
Brian Carlstromf91c8c32011-09-21 17:30:34 -070042inline jfieldID EncodeField(Field* field) {
43#ifdef MOVING_GARBAGE_COLLECTOR
44 UNIMPLEMENTED(WARNING);
45#endif
46 return reinterpret_cast<jfieldID>(field);
47}
48
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070049inline Method* DecodeMethod(jmethodID mid) {
50#ifdef MOVING_GARBAGE_COLLECTOR
51 // TODO: we should make these unique weak globals if Method instances can ever move.
52 UNIMPLEMENTED(WARNING);
53#endif
54 return reinterpret_cast<Method*>(mid);
55}
56
Brian Carlstromf91c8c32011-09-21 17:30:34 -070057inline jmethodID EncodeMethod(Method* method) {
58#ifdef MOVING_GARBAGE_COLLECTOR
59 UNIMPLEMENTED(WARNING);
60#endif
61 return reinterpret_cast<jmethodID>(method);
62}
63
Ian Rogers0571d352011-11-03 19:51:38 -070064size_t NumArgArrayBytes(const char* shorty);
Elliott Hughes418d20f2011-09-22 14:00:39 -070065JValue InvokeWithJValues(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args);
Elliott Hughesd07986f2011-12-06 18:27:45 -080066JValue InvokeWithJValues(Thread* self, Object* receiver, Method* m, JValue* args);
Elliott Hughes418d20f2011-09-22 14:00:39 -070067
Elliott Hughes69f5bc62011-08-24 09:26:14 -070068struct JavaVMExt : public JavaVM {
Elliott Hughesa0957642011-09-02 14:27:33 -070069 JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options);
Elliott Hughesde69d7f2011-08-18 16:49:37 -070070 ~JavaVMExt();
Elliott Hughes0af55432011-08-17 18:37:28 -070071
Elliott Hughes75770752011-08-24 17:52:38 -070072 /**
73 * Loads the given shared library. 'path' is an absolute pathname.
Elliott Hughes0af55432011-08-17 18:37:28 -070074 *
Elliott Hughes75770752011-08-24 17:52:38 -070075 * Returns 'true' on success. On failure, sets 'detail' to a
76 * human-readable description of the error.
Elliott Hughes0af55432011-08-17 18:37:28 -070077 */
Elliott Hughes75770752011-08-24 17:52:38 -070078 bool LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail);
Elliott Hughesbbd76712011-08-17 10:25:24 -070079
Elliott Hughes79082e32011-08-25 12:07:32 -070080 /**
81 * Returns a pointer to the code for the native method 'm', found
82 * using dlsym(3) on every native library that's been loaded so far.
83 */
84 void* FindCodeForNativeMethod(Method* m);
85
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070086 void DumpReferenceTables();
87
Elliott Hughes4ffd3132011-10-24 12:06:42 -070088 void EnableCheckJni();
89
Elliott Hughes410c0c82011-09-01 17:58:25 -070090 void VisitRoots(Heap::RootVisitor*, void*);
91
Elliott Hughesf2682d52011-08-15 16:37:04 -070092 Runtime* runtime;
Elliott Hughesbbd76712011-08-17 10:25:24 -070093
Elliott Hughesa2501992011-08-26 19:39:54 -070094 // Used for testing. By default, we'll LOG(FATAL) the reason.
95 void (*check_jni_abort_hook)(const std::string& reason);
96
Elliott Hughesa0957642011-09-02 14:27:33 -070097 // Extra checking.
Elliott Hughes515a5bc2011-08-17 11:08:34 -070098 bool check_jni;
Elliott Hughesa2501992011-08-26 19:39:54 -070099 bool force_copy;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700100
Elliott Hughesa0957642011-09-02 14:27:33 -0700101 // Extra diagnostics.
Elliott Hughesa0957642011-09-02 14:27:33 -0700102 std::string trace;
103
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700104 // Used to provide compatibility for apps that assumed direct references.
105 bool work_around_app_jni_bugs;
106
Elliott Hughesbbd76712011-08-17 10:25:24 -0700107 // Used to hold references to pinned primitive arrays.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700108 Mutex pins_lock;
Elliott Hughesbbd76712011-08-17 10:25:24 -0700109 ReferenceTable pin_table;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700110
111 // JNI global references.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700112 Mutex globals_lock;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700113 IndirectReferenceTable globals;
114
115 // JNI weak global references.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700116 Mutex weak_globals_lock;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700117 IndirectReferenceTable weak_globals;
Elliott Hughes0af55432011-08-17 18:37:28 -0700118
Elliott Hughes8daa0922011-09-11 13:46:25 -0700119 Mutex libraries_lock;
Elliott Hughes79082e32011-08-25 12:07:32 -0700120 Libraries* libraries;
Elliott Hughesa2501992011-08-26 19:39:54 -0700121
122 // Used by -Xcheck:jni.
123 const JNIInvokeInterface* unchecked_functions;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700124};
125
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700126struct JNIEnvExt : public JNIEnv {
Elliott Hughes75770752011-08-24 17:52:38 -0700127 JNIEnvExt(Thread* self, JavaVMExt* vm);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700128 ~JNIEnvExt();
Elliott Hughesbbd76712011-08-17 10:25:24 -0700129
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700130 void DumpReferenceTables();
131
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700132 void EnableCheckJni();
133
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700134 void PushFrame(int capacity);
135 void PopFrame();
136
Ian Rogersdc51b792011-09-22 20:41:37 -0700137 static Offset SegmentStateOffset() {
138 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
139 IndirectReferenceTable::SegmentStateOffset().Int32Value());
140 }
141
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700142 static Offset LocalRefCookieOffset() {
143 return Offset(OFFSETOF_MEMBER(JNIEnvExt, local_ref_cookie));
144 }
145
Elliott Hughes85d15452011-09-16 17:33:01 -0700146 Thread* const self;
Elliott Hughes75770752011-08-24 17:52:38 -0700147 JavaVMExt* vm;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700148
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700149 // Cookie used when using the local indirect reference table.
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700150 uint32_t local_ref_cookie;
151
152 // JNI local references.
153 IndirectReferenceTable locals;
154
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700155 // Stack of cookies corresponding to PushLocalFrame/PopLocalFrame calls.
156 // TODO: to avoid leaks (and bugs), we need to clear this vector on entry (or return)
157 // to a native method.
158 std::vector<uint32_t> stacked_local_ref_cookies;
159
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700160 // Frequently-accessed fields cached from JavaVM.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700161 bool check_jni;
162
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_