blob: 389c96b9993a21090aa3a97428531a171b9c02f6 [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
Elliott Hugheseac76672012-05-24 21:56:51 -070032#ifndef NATIVE_METHOD
33#define NATIVE_METHOD(className, functionName, signature) \
34 { #functionName, signature, reinterpret_cast<void*>(className ## _ ## functionName) }
35#endif
36#define REGISTER_NATIVE_METHODS(jni_class_name) \
37 RegisterNativeMethods(env, jni_class_name, gMethods, arraysize(gMethods))
38
Ian Rogersdf20fe02011-07-20 20:34:16 -070039namespace art {
40
Elliott Hughes814e4032011-08-23 12:07:56 -070041class ClassLoader;
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070042class Field;
Elliott Hughes418d20f2011-09-22 14:00:39 -070043union JValue;
Elliott Hughes79082e32011-08-25 12:07:32 -070044class Libraries;
45class Method;
Elliott Hughes18c07532011-08-18 15:50:51 -070046class Thread;
Ian Rogersdf20fe02011-07-20 20:34:16 -070047
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070048void SetJniGlobalsMax(size_t max);
Elliott Hughesa2501992011-08-26 19:39:54 -070049void JniAbort(const char* jni_function_name);
Shih-wei Liao31384c52011-09-06 15:27:45 -070050void* FindNativeMethod(Thread* thread);
Elliott Hugheseac76672012-05-24 21:56:51 -070051void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods, size_t method_count);
Elliott Hughesa2501992011-08-26 19:39:54 -070052
Elliott Hughesbf86d042011-08-31 17:53:14 -070053template<typename T> T Decode(JNIEnv*, jobject);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070054template<typename T> T AddLocalReference(JNIEnv*, const Object*);
Elliott Hughesbf86d042011-08-31 17:53:14 -070055
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070056inline Field* DecodeField(jfieldID fid) {
57#ifdef MOVING_GARBAGE_COLLECTOR
58 // TODO: we should make these unique weak globals if Field instances can ever move.
59 UNIMPLEMENTED(WARNING);
60#endif
61 return reinterpret_cast<Field*>(fid);
62}
63
Brian Carlstromf91c8c32011-09-21 17:30:34 -070064inline jfieldID EncodeField(Field* field) {
65#ifdef MOVING_GARBAGE_COLLECTOR
66 UNIMPLEMENTED(WARNING);
67#endif
68 return reinterpret_cast<jfieldID>(field);
69}
70
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070071inline Method* DecodeMethod(jmethodID mid) {
72#ifdef MOVING_GARBAGE_COLLECTOR
73 // TODO: we should make these unique weak globals if Method instances can ever move.
74 UNIMPLEMENTED(WARNING);
75#endif
76 return reinterpret_cast<Method*>(mid);
77}
78
Brian Carlstromf91c8c32011-09-21 17:30:34 -070079inline jmethodID EncodeMethod(Method* method) {
80#ifdef MOVING_GARBAGE_COLLECTOR
81 UNIMPLEMENTED(WARNING);
82#endif
83 return reinterpret_cast<jmethodID>(method);
84}
85
Ian Rogers45619fc2012-02-29 11:15:25 -080086size_t NumArgArrayBytes(const char* shorty, uint32_t shorty_len);
Elliott Hughes418d20f2011-09-22 14:00:39 -070087JValue InvokeWithJValues(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args);
Elliott Hughesd07986f2011-12-06 18:27:45 -080088JValue InvokeWithJValues(Thread* self, Object* receiver, Method* m, JValue* args);
Elliott Hughes418d20f2011-09-22 14:00:39 -070089
Elliott Hughesa4f94742012-05-29 16:28:38 -070090int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause);
91
Elliott Hughes69f5bc62011-08-24 09:26:14 -070092struct JavaVMExt : public JavaVM {
Elliott Hughesa0957642011-09-02 14:27:33 -070093 JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options);
Elliott Hughesde69d7f2011-08-18 16:49:37 -070094 ~JavaVMExt();
Elliott Hughes0af55432011-08-17 18:37:28 -070095
Elliott Hughes75770752011-08-24 17:52:38 -070096 /**
97 * Loads the given shared library. 'path' is an absolute pathname.
Elliott Hughes0af55432011-08-17 18:37:28 -070098 *
Elliott Hughes75770752011-08-24 17:52:38 -070099 * Returns 'true' on success. On failure, sets 'detail' to a
100 * human-readable description of the error.
Elliott Hughes0af55432011-08-17 18:37:28 -0700101 */
Elliott Hughes75770752011-08-24 17:52:38 -0700102 bool LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail);
Elliott Hughesbbd76712011-08-17 10:25:24 -0700103
Elliott Hughes79082e32011-08-25 12:07:32 -0700104 /**
105 * Returns a pointer to the code for the native method 'm', found
106 * using dlsym(3) on every native library that's been loaded so far.
107 */
108 void* FindCodeForNativeMethod(Method* m);
109
Elliott Hughesae80b492012-04-24 10:43:17 -0700110 void DumpForSigQuit(std::ostream& os);
111
Elliott Hughes73e66f72012-05-09 09:34:45 -0700112 void DumpReferenceTables(std::ostream& os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700113
Elliott Hughes88c5c352012-03-15 18:49:48 -0700114 void SetCheckJniEnabled(bool enabled);
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700115
Elliott Hughes410c0c82011-09-01 17:58:25 -0700116 void VisitRoots(Heap::RootVisitor*, void*);
117
Elliott Hughesf2682d52011-08-15 16:37:04 -0700118 Runtime* runtime;
Elliott Hughesbbd76712011-08-17 10:25:24 -0700119
Elliott Hughesa2501992011-08-26 19:39:54 -0700120 // Used for testing. By default, we'll LOG(FATAL) the reason.
Elliott Hughesb264f082012-04-06 17:10:10 -0700121 void (*check_jni_abort_hook)(void* data, const std::string& reason);
122 void* check_jni_abort_hook_data;
Elliott Hughesa2501992011-08-26 19:39:54 -0700123
Elliott Hughesa0957642011-09-02 14:27:33 -0700124 // Extra checking.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700125 bool check_jni;
Elliott Hughesa2501992011-08-26 19:39:54 -0700126 bool force_copy;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700127
Elliott Hughesa0957642011-09-02 14:27:33 -0700128 // Extra diagnostics.
Elliott Hughesa0957642011-09-02 14:27:33 -0700129 std::string trace;
130
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700131 // Used to provide compatibility for apps that assumed direct references.
132 bool work_around_app_jni_bugs;
133
Elliott Hughesbbd76712011-08-17 10:25:24 -0700134 // Used to hold references to pinned primitive arrays.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700135 Mutex pins_lock;
Elliott Hughesbbd76712011-08-17 10:25:24 -0700136 ReferenceTable pin_table;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700137
138 // JNI global references.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700139 Mutex globals_lock;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700140 IndirectReferenceTable globals;
141
142 // JNI weak global references.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700143 Mutex weak_globals_lock;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700144 IndirectReferenceTable weak_globals;
Elliott Hughes0af55432011-08-17 18:37:28 -0700145
Elliott Hughes8daa0922011-09-11 13:46:25 -0700146 Mutex libraries_lock;
Elliott Hughes79082e32011-08-25 12:07:32 -0700147 Libraries* libraries;
Elliott Hughesa2501992011-08-26 19:39:54 -0700148
149 // Used by -Xcheck:jni.
150 const JNIInvokeInterface* unchecked_functions;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700151};
152
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700153struct JNIEnvExt : public JNIEnv {
Elliott Hughes75770752011-08-24 17:52:38 -0700154 JNIEnvExt(Thread* self, JavaVMExt* vm);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700155 ~JNIEnvExt();
Elliott Hughesbbd76712011-08-17 10:25:24 -0700156
Elliott Hughes73e66f72012-05-09 09:34:45 -0700157 void DumpReferenceTables(std::ostream& os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700158
Elliott Hughes88c5c352012-03-15 18:49:48 -0700159 void SetCheckJniEnabled(bool enabled);
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700160
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700161 void PushFrame(int capacity);
162 void PopFrame();
163
Ian Rogersdc51b792011-09-22 20:41:37 -0700164 static Offset SegmentStateOffset() {
165 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
166 IndirectReferenceTable::SegmentStateOffset().Int32Value());
167 }
168
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700169 static Offset LocalRefCookieOffset() {
170 return Offset(OFFSETOF_MEMBER(JNIEnvExt, local_ref_cookie));
171 }
172
Elliott Hughes85d15452011-09-16 17:33:01 -0700173 Thread* const self;
Elliott Hughes75770752011-08-24 17:52:38 -0700174 JavaVMExt* vm;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700175
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700176 // Cookie used when using the local indirect reference table.
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700177 uint32_t local_ref_cookie;
178
179 // JNI local references.
180 IndirectReferenceTable locals;
181
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700182 // Stack of cookies corresponding to PushLocalFrame/PopLocalFrame calls.
183 // TODO: to avoid leaks (and bugs), we need to clear this vector on entry (or return)
184 // to a native method.
185 std::vector<uint32_t> stacked_local_ref_cookies;
186
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700187 // Frequently-accessed fields cached from JavaVM.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700188 bool check_jni;
189
Elliott Hughesa2501992011-08-26 19:39:54 -0700190 // How many nested "critical" JNI calls are we in?
191 int critical;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700192
Elliott Hughesbbd76712011-08-17 10:25:24 -0700193 // Entered JNI monitors, for bulk exit on thread detach.
Elliott Hughes02b48d12011-09-07 17:15:51 -0700194 ReferenceTable monitors;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700195
Elliott Hughesa2501992011-08-26 19:39:54 -0700196 // Used by -Xcheck:jni.
197 const JNINativeInterface* unchecked_functions;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700198};
199
Elliott Hughesa2501992011-08-26 19:39:54 -0700200const JNINativeInterface* GetCheckJniNativeInterface();
201const JNIInvokeInterface* GetCheckJniInvokeInterface();
202
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700203// Used to save and restore the JNIEnvExt state when not going through code created by the JNI
204// compiler
205class ScopedJniEnvLocalRefState {
206 public:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700207 explicit ScopedJniEnvLocalRefState(JNIEnvExt* env) : env_(env) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700208 saved_local_ref_cookie_ = env->local_ref_cookie;
209 env->local_ref_cookie = env->locals.GetSegmentState();
210 }
211
212 ~ScopedJniEnvLocalRefState() {
213 env_->locals.SetSegmentState(env_->local_ref_cookie);
214 env_->local_ref_cookie = saved_local_ref_cookie_;
215 }
216
217 private:
218 JNIEnvExt* env_;
219 uint32_t saved_local_ref_cookie_;
220 DISALLOW_COPY_AND_ASSIGN(ScopedJniEnvLocalRefState);
221};
222
Ian Rogersdf20fe02011-07-20 20:34:16 -0700223} // namespace art
Carl Shapiroea4dca82011-08-01 13:45:38 -0700224
Elliott Hughesb465ab02011-08-24 11:21:21 -0700225std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs);
226
Ian Rogersdf20fe02011-07-20 20:34:16 -0700227#endif // ART_SRC_JNI_INTERNAL_H_