blob: 329a5afcba780ecbceefca023be7a72b3e5a976d [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 Hughesa2501992011-08-26 19:39:54 -070026void JniAbort(const char* jni_function_name);
Shih-wei Liao31384c52011-09-06 15:27:45 -070027void* FindNativeMethod(Thread* thread);
Elliott Hughesa2501992011-08-26 19:39:54 -070028
Elliott Hughesbf86d042011-08-31 17:53:14 -070029template<typename T> T Decode(JNIEnv*, jobject);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070030template<typename T> T AddLocalReference(JNIEnv*, const Object*);
Elliott Hughesbf86d042011-08-31 17:53:14 -070031
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070032inline Field* DecodeField(jfieldID fid) {
33#ifdef MOVING_GARBAGE_COLLECTOR
34 // TODO: we should make these unique weak globals if Field instances can ever move.
35 UNIMPLEMENTED(WARNING);
36#endif
37 return reinterpret_cast<Field*>(fid);
38}
39
Brian Carlstromf91c8c32011-09-21 17:30:34 -070040inline jfieldID EncodeField(Field* field) {
41#ifdef MOVING_GARBAGE_COLLECTOR
42 UNIMPLEMENTED(WARNING);
43#endif
44 return reinterpret_cast<jfieldID>(field);
45}
46
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070047inline Method* DecodeMethod(jmethodID mid) {
48#ifdef MOVING_GARBAGE_COLLECTOR
49 // TODO: we should make these unique weak globals if Method instances can ever move.
50 UNIMPLEMENTED(WARNING);
51#endif
52 return reinterpret_cast<Method*>(mid);
53}
54
Brian Carlstromf91c8c32011-09-21 17:30:34 -070055inline jmethodID EncodeMethod(Method* method) {
56#ifdef MOVING_GARBAGE_COLLECTOR
57 UNIMPLEMENTED(WARNING);
58#endif
59 return reinterpret_cast<jmethodID>(method);
60}
61
Elliott Hughes418d20f2011-09-22 14:00:39 -070062JValue InvokeWithJValues(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args);
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 */
Elliott Hughes75770752011-08-24 17:52:38 -070074 bool LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail);
Elliott Hughesbbd76712011-08-17 10:25:24 -070075
Elliott Hughes79082e32011-08-25 12:07:32 -070076 /**
77 * Returns a pointer to the code for the native method 'm', found
78 * using dlsym(3) on every native library that's been loaded so far.
79 */
80 void* FindCodeForNativeMethod(Method* m);
81
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070082 void DumpReferenceTables();
83
Elliott Hughes410c0c82011-09-01 17:58:25 -070084 void VisitRoots(Heap::RootVisitor*, void*);
85
Elliott Hughesf2682d52011-08-15 16:37:04 -070086 Runtime* runtime;
Elliott Hughesbbd76712011-08-17 10:25:24 -070087
Elliott Hughesa2501992011-08-26 19:39:54 -070088 // Used for testing. By default, we'll LOG(FATAL) the reason.
89 void (*check_jni_abort_hook)(const std::string& reason);
90
Elliott Hughesa0957642011-09-02 14:27:33 -070091 // Extra checking.
Elliott Hughes515a5bc2011-08-17 11:08:34 -070092 bool check_jni;
Elliott Hughesa2501992011-08-26 19:39:54 -070093 bool force_copy;
Elliott Hughes515a5bc2011-08-17 11:08:34 -070094
Elliott Hughesa0957642011-09-02 14:27:33 -070095 // Extra diagnostics.
96 bool verbose_jni;
97 bool log_third_party_jni;
98 std::string trace;
99
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700100 // Used to provide compatibility for apps that assumed direct references.
101 bool work_around_app_jni_bugs;
102
Elliott Hughesbbd76712011-08-17 10:25:24 -0700103 // Used to hold references to pinned primitive arrays.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700104 Mutex pins_lock;
Elliott Hughesbbd76712011-08-17 10:25:24 -0700105 ReferenceTable pin_table;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700106
107 // JNI global references.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700108 Mutex globals_lock;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700109 IndirectReferenceTable globals;
110
111 // JNI weak global references.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700112 Mutex weak_globals_lock;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700113 IndirectReferenceTable weak_globals;
Elliott Hughes0af55432011-08-17 18:37:28 -0700114
Elliott Hughes8daa0922011-09-11 13:46:25 -0700115 Mutex libraries_lock;
Elliott Hughes79082e32011-08-25 12:07:32 -0700116 Libraries* libraries;
Elliott Hughesa2501992011-08-26 19:39:54 -0700117
118 // Used by -Xcheck:jni.
119 const JNIInvokeInterface* unchecked_functions;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700120};
121
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700122struct JNIEnvExt : public JNIEnv {
Elliott Hughes75770752011-08-24 17:52:38 -0700123 JNIEnvExt(Thread* self, JavaVMExt* vm);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700124 ~JNIEnvExt();
Elliott Hughesbbd76712011-08-17 10:25:24 -0700125
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700126 void DumpReferenceTables();
127
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700128 void PushFrame(int capacity);
129 void PopFrame();
130
Ian Rogersdc51b792011-09-22 20:41:37 -0700131 static Offset SegmentStateOffset() {
132 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
133 IndirectReferenceTable::SegmentStateOffset().Int32Value());
134 }
135
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700136 static Offset LocalRefCookieOffset() {
137 return Offset(OFFSETOF_MEMBER(JNIEnvExt, local_ref_cookie));
138 }
139
Elliott Hughes85d15452011-09-16 17:33:01 -0700140 Thread* const self;
Elliott Hughes75770752011-08-24 17:52:38 -0700141 JavaVMExt* vm;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700142
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700143 // Cookie used when using the local indirect reference table.
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700144 uint32_t local_ref_cookie;
145
146 // JNI local references.
147 IndirectReferenceTable locals;
148
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700149 // Stack of cookies corresponding to PushLocalFrame/PopLocalFrame calls.
150 // TODO: to avoid leaks (and bugs), we need to clear this vector on entry (or return)
151 // to a native method.
152 std::vector<uint32_t> stacked_local_ref_cookies;
153
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700154 // Frequently-accessed fields cached from JavaVM.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700155 bool check_jni;
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700156 bool work_around_app_jni_bugs;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700157
Elliott Hughesa2501992011-08-26 19:39:54 -0700158 // How many nested "critical" JNI calls are we in?
159 int critical;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700160
Elliott Hughesbbd76712011-08-17 10:25:24 -0700161 // Entered JNI monitors, for bulk exit on thread detach.
Elliott Hughes02b48d12011-09-07 17:15:51 -0700162 ReferenceTable monitors;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700163
Elliott Hughesa2501992011-08-26 19:39:54 -0700164 // Used by -Xcheck:jni.
165 const JNINativeInterface* unchecked_functions;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700166};
167
Elliott Hughesa2501992011-08-26 19:39:54 -0700168const JNINativeInterface* GetCheckJniNativeInterface();
169const JNIInvokeInterface* GetCheckJniInvokeInterface();
170
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700171// Used to save and restore the JNIEnvExt state when not going through code created by the JNI
172// compiler
173class ScopedJniEnvLocalRefState {
174 public:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700175 explicit ScopedJniEnvLocalRefState(JNIEnvExt* env) : env_(env) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700176 saved_local_ref_cookie_ = env->local_ref_cookie;
177 env->local_ref_cookie = env->locals.GetSegmentState();
178 }
179
180 ~ScopedJniEnvLocalRefState() {
181 env_->locals.SetSegmentState(env_->local_ref_cookie);
182 env_->local_ref_cookie = saved_local_ref_cookie_;
183 }
184
185 private:
186 JNIEnvExt* env_;
187 uint32_t saved_local_ref_cookie_;
188 DISALLOW_COPY_AND_ASSIGN(ScopedJniEnvLocalRefState);
189};
190
Ian Rogersdf20fe02011-07-20 20:34:16 -0700191} // namespace art
Carl Shapiroea4dca82011-08-01 13:45:38 -0700192
Elliott Hughesb465ab02011-08-24 11:21:21 -0700193std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs);
194
Ian Rogersdf20fe02011-07-20 20:34:16 -0700195#endif // ART_SRC_JNI_INTERNAL_H_