blob: f21b36acefd8ff7a4a000673a43600283ab9700c [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
17#ifndef ART_SRC_JNI_INTERNAL_H_
18#define ART_SRC_JNI_INTERNAL_H_
19
20#include "jni.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070021
Elliott Hughes410c0c82011-09-01 17:58:25 -070022#include "heap.h"
Elliott Hughes6c1a3942011-08-17 15:00:06 -070023#include "indirect_reference_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070024#include "macros.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070025#include "mutex.h"
Elliott Hughesbbd76712011-08-17 10:25:24 -070026#include "reference_table.h"
Elliott Hughesa0957642011-09-02 14:27:33 -070027#include "runtime.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070028
Elliott Hughes0af55432011-08-17 18:37:28 -070029#include <string>
30
Ian Rogersdf20fe02011-07-20 20:34:16 -070031namespace art {
32
Elliott Hughes814e4032011-08-23 12:07:56 -070033class ClassLoader;
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070034class Field;
Elliott Hughes418d20f2011-09-22 14:00:39 -070035union JValue;
Elliott Hughes79082e32011-08-25 12:07:32 -070036class Libraries;
37class Method;
Elliott Hughes18c07532011-08-18 15:50:51 -070038class Thread;
Ian Rogersdf20fe02011-07-20 20:34:16 -070039
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070040void SetJniGlobalsMax(size_t max);
Elliott Hughesa2501992011-08-26 19:39:54 -070041void JniAbort(const char* jni_function_name);
Shih-wei Liao31384c52011-09-06 15:27:45 -070042void* FindNativeMethod(Thread* thread);
Elliott Hughes844f9a02012-01-24 20:19:58 -080043jclass CacheClass(JNIEnv* env, const char* jni_class_name);
Elliott Hughesa2501992011-08-26 19:39:54 -070044
Elliott Hughesbf86d042011-08-31 17:53:14 -070045template<typename T> T Decode(JNIEnv*, jobject);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070046template<typename T> T AddLocalReference(JNIEnv*, const Object*);
Elliott Hughesbf86d042011-08-31 17:53:14 -070047
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070048inline Field* DecodeField(jfieldID fid) {
49#ifdef MOVING_GARBAGE_COLLECTOR
50 // TODO: we should make these unique weak globals if Field instances can ever move.
51 UNIMPLEMENTED(WARNING);
52#endif
53 return reinterpret_cast<Field*>(fid);
54}
55
Brian Carlstromf91c8c32011-09-21 17:30:34 -070056inline jfieldID EncodeField(Field* field) {
57#ifdef MOVING_GARBAGE_COLLECTOR
58 UNIMPLEMENTED(WARNING);
59#endif
60 return reinterpret_cast<jfieldID>(field);
61}
62
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070063inline Method* DecodeMethod(jmethodID mid) {
64#ifdef MOVING_GARBAGE_COLLECTOR
65 // TODO: we should make these unique weak globals if Method instances can ever move.
66 UNIMPLEMENTED(WARNING);
67#endif
68 return reinterpret_cast<Method*>(mid);
69}
70
Brian Carlstromf91c8c32011-09-21 17:30:34 -070071inline jmethodID EncodeMethod(Method* method) {
72#ifdef MOVING_GARBAGE_COLLECTOR
73 UNIMPLEMENTED(WARNING);
74#endif
75 return reinterpret_cast<jmethodID>(method);
76}
77
Ian Rogers0571d352011-11-03 19:51:38 -070078size_t NumArgArrayBytes(const char* shorty);
Elliott Hughes418d20f2011-09-22 14:00:39 -070079JValue InvokeWithJValues(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args);
Elliott Hughesd07986f2011-12-06 18:27:45 -080080JValue InvokeWithJValues(Thread* self, Object* receiver, Method* m, JValue* args);
Elliott Hughes418d20f2011-09-22 14:00:39 -070081
Elliott Hughes69f5bc62011-08-24 09:26:14 -070082struct JavaVMExt : public JavaVM {
Elliott Hughesa0957642011-09-02 14:27:33 -070083 JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options);
Elliott Hughesde69d7f2011-08-18 16:49:37 -070084 ~JavaVMExt();
Elliott Hughes0af55432011-08-17 18:37:28 -070085
Elliott Hughes75770752011-08-24 17:52:38 -070086 /**
87 * Loads the given shared library. 'path' is an absolute pathname.
Elliott Hughes0af55432011-08-17 18:37:28 -070088 *
Elliott Hughes75770752011-08-24 17:52:38 -070089 * Returns 'true' on success. On failure, sets 'detail' to a
90 * human-readable description of the error.
Elliott Hughes0af55432011-08-17 18:37:28 -070091 */
Elliott Hughes75770752011-08-24 17:52:38 -070092 bool LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail);
Elliott Hughesbbd76712011-08-17 10:25:24 -070093
Elliott Hughes79082e32011-08-25 12:07:32 -070094 /**
95 * Returns a pointer to the code for the native method 'm', found
96 * using dlsym(3) on every native library that's been loaded so far.
97 */
98 void* FindCodeForNativeMethod(Method* m);
99
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700100 void DumpReferenceTables();
101
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700102 void EnableCheckJni();
103
Elliott Hughes410c0c82011-09-01 17:58:25 -0700104 void VisitRoots(Heap::RootVisitor*, void*);
105
Elliott Hughesf2682d52011-08-15 16:37:04 -0700106 Runtime* runtime;
Elliott Hughesbbd76712011-08-17 10:25:24 -0700107
Elliott Hughesa2501992011-08-26 19:39:54 -0700108 // Used for testing. By default, we'll LOG(FATAL) the reason.
109 void (*check_jni_abort_hook)(const std::string& reason);
110
Elliott Hughesa0957642011-09-02 14:27:33 -0700111 // Extra checking.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700112 bool check_jni;
Elliott Hughesa2501992011-08-26 19:39:54 -0700113 bool force_copy;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700114
Elliott Hughesa0957642011-09-02 14:27:33 -0700115 // Extra diagnostics.
Elliott Hughesa0957642011-09-02 14:27:33 -0700116 std::string trace;
117
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700118 // Used to provide compatibility for apps that assumed direct references.
119 bool work_around_app_jni_bugs;
120
Elliott Hughesbbd76712011-08-17 10:25:24 -0700121 // Used to hold references to pinned primitive arrays.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700122 Mutex pins_lock;
Elliott Hughesbbd76712011-08-17 10:25:24 -0700123 ReferenceTable pin_table;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700124
125 // JNI global references.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700126 Mutex globals_lock;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700127 IndirectReferenceTable globals;
128
129 // JNI weak global references.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700130 Mutex weak_globals_lock;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700131 IndirectReferenceTable weak_globals;
Elliott Hughes0af55432011-08-17 18:37:28 -0700132
Elliott Hughes8daa0922011-09-11 13:46:25 -0700133 Mutex libraries_lock;
Elliott Hughes79082e32011-08-25 12:07:32 -0700134 Libraries* libraries;
Elliott Hughesa2501992011-08-26 19:39:54 -0700135
136 // Used by -Xcheck:jni.
137 const JNIInvokeInterface* unchecked_functions;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700138};
139
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700140struct JNIEnvExt : public JNIEnv {
Elliott Hughes75770752011-08-24 17:52:38 -0700141 JNIEnvExt(Thread* self, JavaVMExt* vm);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700142 ~JNIEnvExt();
Elliott Hughesbbd76712011-08-17 10:25:24 -0700143
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700144 void DumpReferenceTables();
145
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700146 void EnableCheckJni();
147
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700148 void PushFrame(int capacity);
149 void PopFrame();
150
Ian Rogersdc51b792011-09-22 20:41:37 -0700151 static Offset SegmentStateOffset() {
152 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
153 IndirectReferenceTable::SegmentStateOffset().Int32Value());
154 }
155
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700156 static Offset LocalRefCookieOffset() {
157 return Offset(OFFSETOF_MEMBER(JNIEnvExt, local_ref_cookie));
158 }
159
Elliott Hughes85d15452011-09-16 17:33:01 -0700160 Thread* const self;
Elliott Hughes75770752011-08-24 17:52:38 -0700161 JavaVMExt* vm;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700162
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700163 // Cookie used when using the local indirect reference table.
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700164 uint32_t local_ref_cookie;
165
166 // JNI local references.
167 IndirectReferenceTable locals;
168
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700169 // Stack of cookies corresponding to PushLocalFrame/PopLocalFrame calls.
170 // TODO: to avoid leaks (and bugs), we need to clear this vector on entry (or return)
171 // to a native method.
172 std::vector<uint32_t> stacked_local_ref_cookies;
173
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700174 // Frequently-accessed fields cached from JavaVM.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700175 bool check_jni;
176
Elliott Hughesa2501992011-08-26 19:39:54 -0700177 // How many nested "critical" JNI calls are we in?
178 int critical;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700179
Elliott Hughesbbd76712011-08-17 10:25:24 -0700180 // Entered JNI monitors, for bulk exit on thread detach.
Elliott Hughes02b48d12011-09-07 17:15:51 -0700181 ReferenceTable monitors;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700182
Elliott Hughesa2501992011-08-26 19:39:54 -0700183 // Used by -Xcheck:jni.
184 const JNINativeInterface* unchecked_functions;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700185};
186
Elliott Hughesa2501992011-08-26 19:39:54 -0700187const JNINativeInterface* GetCheckJniNativeInterface();
188const JNIInvokeInterface* GetCheckJniInvokeInterface();
189
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700190// Used to save and restore the JNIEnvExt state when not going through code created by the JNI
191// compiler
192class ScopedJniEnvLocalRefState {
193 public:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700194 explicit ScopedJniEnvLocalRefState(JNIEnvExt* env) : env_(env) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700195 saved_local_ref_cookie_ = env->local_ref_cookie;
196 env->local_ref_cookie = env->locals.GetSegmentState();
197 }
198
199 ~ScopedJniEnvLocalRefState() {
200 env_->locals.SetSegmentState(env_->local_ref_cookie);
201 env_->local_ref_cookie = saved_local_ref_cookie_;
202 }
203
204 private:
205 JNIEnvExt* env_;
206 uint32_t saved_local_ref_cookie_;
207 DISALLOW_COPY_AND_ASSIGN(ScopedJniEnvLocalRefState);
208};
209
Ian Rogersdf20fe02011-07-20 20:34:16 -0700210} // namespace art
Carl Shapiroea4dca82011-08-01 13:45:38 -0700211
Elliott Hughesb465ab02011-08-24 11:21:21 -0700212std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs);
213
Ian Rogersdf20fe02011-07-20 20:34:16 -0700214#endif // ART_SRC_JNI_INTERNAL_H_