blob: e12a7cd6de19bf3bb4fff0f713d72d23d95361a4 [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 Hughesae80b492012-04-24 10:43:17 -070029#include <iosfwd>
Elliott Hughes0af55432011-08-17 18:37:28 -070030#include <string>
31
Ian Rogersdf20fe02011-07-20 20:34:16 -070032namespace art {
33
Elliott Hughes814e4032011-08-23 12:07:56 -070034class ClassLoader;
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070035class Field;
Elliott Hughes418d20f2011-09-22 14:00:39 -070036union JValue;
Elliott Hughes79082e32011-08-25 12:07:32 -070037class Libraries;
38class Method;
Elliott Hughes18c07532011-08-18 15:50:51 -070039class Thread;
Ian Rogersdf20fe02011-07-20 20:34:16 -070040
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070041void SetJniGlobalsMax(size_t max);
Elliott Hughesa2501992011-08-26 19:39:54 -070042void JniAbort(const char* jni_function_name);
Shih-wei Liao31384c52011-09-06 15:27:45 -070043void* FindNativeMethod(Thread* thread);
Elliott Hughes844f9a02012-01-24 20:19:58 -080044jclass CacheClass(JNIEnv* env, const char* jni_class_name);
Elliott Hughesa2501992011-08-26 19:39:54 -070045
Elliott Hughesbf86d042011-08-31 17:53:14 -070046template<typename T> T Decode(JNIEnv*, jobject);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070047template<typename T> T AddLocalReference(JNIEnv*, const Object*);
Elliott Hughesbf86d042011-08-31 17:53:14 -070048
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070049inline Field* DecodeField(jfieldID fid) {
50#ifdef MOVING_GARBAGE_COLLECTOR
51 // TODO: we should make these unique weak globals if Field instances can ever move.
52 UNIMPLEMENTED(WARNING);
53#endif
54 return reinterpret_cast<Field*>(fid);
55}
56
Brian Carlstromf91c8c32011-09-21 17:30:34 -070057inline jfieldID EncodeField(Field* field) {
58#ifdef MOVING_GARBAGE_COLLECTOR
59 UNIMPLEMENTED(WARNING);
60#endif
61 return reinterpret_cast<jfieldID>(field);
62}
63
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070064inline Method* DecodeMethod(jmethodID mid) {
65#ifdef MOVING_GARBAGE_COLLECTOR
66 // TODO: we should make these unique weak globals if Method instances can ever move.
67 UNIMPLEMENTED(WARNING);
68#endif
69 return reinterpret_cast<Method*>(mid);
70}
71
Brian Carlstromf91c8c32011-09-21 17:30:34 -070072inline jmethodID EncodeMethod(Method* method) {
73#ifdef MOVING_GARBAGE_COLLECTOR
74 UNIMPLEMENTED(WARNING);
75#endif
76 return reinterpret_cast<jmethodID>(method);
77}
78
Ian Rogers45619fc2012-02-29 11:15:25 -080079size_t NumArgArrayBytes(const char* shorty, uint32_t shorty_len);
Elliott Hughes418d20f2011-09-22 14:00:39 -070080JValue InvokeWithJValues(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args);
Elliott Hughesd07986f2011-12-06 18:27:45 -080081JValue InvokeWithJValues(Thread* self, Object* receiver, Method* m, JValue* args);
Elliott Hughes418d20f2011-09-22 14:00:39 -070082
Elliott Hughes69f5bc62011-08-24 09:26:14 -070083struct JavaVMExt : public JavaVM {
Elliott Hughesa0957642011-09-02 14:27:33 -070084 JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options);
Elliott Hughesde69d7f2011-08-18 16:49:37 -070085 ~JavaVMExt();
Elliott Hughes0af55432011-08-17 18:37:28 -070086
Elliott Hughes75770752011-08-24 17:52:38 -070087 /**
88 * Loads the given shared library. 'path' is an absolute pathname.
Elliott Hughes0af55432011-08-17 18:37:28 -070089 *
Elliott Hughes75770752011-08-24 17:52:38 -070090 * Returns 'true' on success. On failure, sets 'detail' to a
91 * human-readable description of the error.
Elliott Hughes0af55432011-08-17 18:37:28 -070092 */
Elliott Hughes75770752011-08-24 17:52:38 -070093 bool LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail);
Elliott Hughesbbd76712011-08-17 10:25:24 -070094
Elliott Hughes79082e32011-08-25 12:07:32 -070095 /**
96 * Returns a pointer to the code for the native method 'm', found
97 * using dlsym(3) on every native library that's been loaded so far.
98 */
99 void* FindCodeForNativeMethod(Method* m);
100
Elliott Hughesae80b492012-04-24 10:43:17 -0700101 void DumpForSigQuit(std::ostream& os);
102
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700103 void DumpReferenceTables();
104
Elliott Hughes88c5c352012-03-15 18:49:48 -0700105 void SetCheckJniEnabled(bool enabled);
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700106
Elliott Hughes410c0c82011-09-01 17:58:25 -0700107 void VisitRoots(Heap::RootVisitor*, void*);
108
Elliott Hughesf2682d52011-08-15 16:37:04 -0700109 Runtime* runtime;
Elliott Hughesbbd76712011-08-17 10:25:24 -0700110
Elliott Hughesa2501992011-08-26 19:39:54 -0700111 // Used for testing. By default, we'll LOG(FATAL) the reason.
Elliott Hughesb264f082012-04-06 17:10:10 -0700112 void (*check_jni_abort_hook)(void* data, const std::string& reason);
113 void* check_jni_abort_hook_data;
Elliott Hughesa2501992011-08-26 19:39:54 -0700114
Elliott Hughesa0957642011-09-02 14:27:33 -0700115 // Extra checking.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700116 bool check_jni;
Elliott Hughesa2501992011-08-26 19:39:54 -0700117 bool force_copy;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700118
Elliott Hughesa0957642011-09-02 14:27:33 -0700119 // Extra diagnostics.
Elliott Hughesa0957642011-09-02 14:27:33 -0700120 std::string trace;
121
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700122 // Used to provide compatibility for apps that assumed direct references.
123 bool work_around_app_jni_bugs;
124
Elliott Hughesbbd76712011-08-17 10:25:24 -0700125 // Used to hold references to pinned primitive arrays.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700126 Mutex pins_lock;
Elliott Hughesbbd76712011-08-17 10:25:24 -0700127 ReferenceTable pin_table;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700128
129 // JNI global references.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700130 Mutex globals_lock;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700131 IndirectReferenceTable globals;
132
133 // JNI weak global references.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700134 Mutex weak_globals_lock;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700135 IndirectReferenceTable weak_globals;
Elliott Hughes0af55432011-08-17 18:37:28 -0700136
Elliott Hughes8daa0922011-09-11 13:46:25 -0700137 Mutex libraries_lock;
Elliott Hughes79082e32011-08-25 12:07:32 -0700138 Libraries* libraries;
Elliott Hughesa2501992011-08-26 19:39:54 -0700139
140 // Used by -Xcheck:jni.
141 const JNIInvokeInterface* unchecked_functions;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700142};
143
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700144struct JNIEnvExt : public JNIEnv {
Elliott Hughes75770752011-08-24 17:52:38 -0700145 JNIEnvExt(Thread* self, JavaVMExt* vm);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700146 ~JNIEnvExt();
Elliott Hughesbbd76712011-08-17 10:25:24 -0700147
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700148 void DumpReferenceTables();
149
Elliott Hughes88c5c352012-03-15 18:49:48 -0700150 void SetCheckJniEnabled(bool enabled);
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700151
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700152 void PushFrame(int capacity);
153 void PopFrame();
154
Ian Rogersdc51b792011-09-22 20:41:37 -0700155 static Offset SegmentStateOffset() {
156 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
157 IndirectReferenceTable::SegmentStateOffset().Int32Value());
158 }
159
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700160 static Offset LocalRefCookieOffset() {
161 return Offset(OFFSETOF_MEMBER(JNIEnvExt, local_ref_cookie));
162 }
163
Elliott Hughes85d15452011-09-16 17:33:01 -0700164 Thread* const self;
Elliott Hughes75770752011-08-24 17:52:38 -0700165 JavaVMExt* vm;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700166
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700167 // Cookie used when using the local indirect reference table.
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700168 uint32_t local_ref_cookie;
169
170 // JNI local references.
171 IndirectReferenceTable locals;
172
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700173 // Stack of cookies corresponding to PushLocalFrame/PopLocalFrame calls.
174 // TODO: to avoid leaks (and bugs), we need to clear this vector on entry (or return)
175 // to a native method.
176 std::vector<uint32_t> stacked_local_ref_cookies;
177
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700178 // Frequently-accessed fields cached from JavaVM.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700179 bool check_jni;
180
Elliott Hughesa2501992011-08-26 19:39:54 -0700181 // How many nested "critical" JNI calls are we in?
182 int critical;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700183
Elliott Hughesbbd76712011-08-17 10:25:24 -0700184 // Entered JNI monitors, for bulk exit on thread detach.
Elliott Hughes02b48d12011-09-07 17:15:51 -0700185 ReferenceTable monitors;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700186
Elliott Hughesa2501992011-08-26 19:39:54 -0700187 // Used by -Xcheck:jni.
188 const JNINativeInterface* unchecked_functions;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700189};
190
Elliott Hughesa2501992011-08-26 19:39:54 -0700191const JNINativeInterface* GetCheckJniNativeInterface();
192const JNIInvokeInterface* GetCheckJniInvokeInterface();
193
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700194// Used to save and restore the JNIEnvExt state when not going through code created by the JNI
195// compiler
196class ScopedJniEnvLocalRefState {
197 public:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700198 explicit ScopedJniEnvLocalRefState(JNIEnvExt* env) : env_(env) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700199 saved_local_ref_cookie_ = env->local_ref_cookie;
200 env->local_ref_cookie = env->locals.GetSegmentState();
201 }
202
203 ~ScopedJniEnvLocalRefState() {
204 env_->locals.SetSegmentState(env_->local_ref_cookie);
205 env_->local_ref_cookie = saved_local_ref_cookie_;
206 }
207
208 private:
209 JNIEnvExt* env_;
210 uint32_t saved_local_ref_cookie_;
211 DISALLOW_COPY_AND_ASSIGN(ScopedJniEnvLocalRefState);
212};
213
Ian Rogersdf20fe02011-07-20 20:34:16 -0700214} // namespace art
Carl Shapiroea4dca82011-08-01 13:45:38 -0700215
Elliott Hughesb465ab02011-08-24 11:21:21 -0700216std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs);
217
Ian Rogersdf20fe02011-07-20 20:34:16 -0700218#endif // ART_SRC_JNI_INTERNAL_H_