blob: 20aa71652635737f04f7750391fe64822a6d374b [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 Hughes79082e32011-08-25 12:07:32 -070020class Libraries;
21class Method;
Elliott Hughes18c07532011-08-18 15:50:51 -070022class Thread;
Ian Rogersdf20fe02011-07-20 20:34:16 -070023
Elliott Hughesa2501992011-08-26 19:39:54 -070024void JniAbort(const char* jni_function_name);
Shih-wei Liao31384c52011-09-06 15:27:45 -070025void* FindNativeMethod(Thread* thread);
Elliott Hughesa2501992011-08-26 19:39:54 -070026
Elliott Hughesbf86d042011-08-31 17:53:14 -070027template<typename T> T Decode(JNIEnv*, jobject);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070028template<typename T> T AddLocalReference(JNIEnv*, const Object*);
Elliott Hughesbf86d042011-08-31 17:53:14 -070029
Elliott Hughes69f5bc62011-08-24 09:26:14 -070030struct JavaVMExt : public JavaVM {
Elliott Hughesa0957642011-09-02 14:27:33 -070031 JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options);
Elliott Hughesde69d7f2011-08-18 16:49:37 -070032 ~JavaVMExt();
Elliott Hughes0af55432011-08-17 18:37:28 -070033
Elliott Hughes75770752011-08-24 17:52:38 -070034 /**
35 * Loads the given shared library. 'path' is an absolute pathname.
Elliott Hughes0af55432011-08-17 18:37:28 -070036 *
Elliott Hughes75770752011-08-24 17:52:38 -070037 * Returns 'true' on success. On failure, sets 'detail' to a
38 * human-readable description of the error.
Elliott Hughes0af55432011-08-17 18:37:28 -070039 */
Elliott Hughes75770752011-08-24 17:52:38 -070040 bool LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail);
Elliott Hughesbbd76712011-08-17 10:25:24 -070041
Elliott Hughes79082e32011-08-25 12:07:32 -070042 /**
43 * Returns a pointer to the code for the native method 'm', found
44 * using dlsym(3) on every native library that's been loaded so far.
45 */
46 void* FindCodeForNativeMethod(Method* m);
47
Elliott Hughes410c0c82011-09-01 17:58:25 -070048 void VisitRoots(Heap::RootVisitor*, void*);
49
Elliott Hughesf2682d52011-08-15 16:37:04 -070050 Runtime* runtime;
Elliott Hughesbbd76712011-08-17 10:25:24 -070051
Elliott Hughesa2501992011-08-26 19:39:54 -070052 // Used for testing. By default, we'll LOG(FATAL) the reason.
53 void (*check_jni_abort_hook)(const std::string& reason);
54
Elliott Hughesa0957642011-09-02 14:27:33 -070055 // Extra checking.
Elliott Hughes515a5bc2011-08-17 11:08:34 -070056 bool check_jni;
Elliott Hughesa2501992011-08-26 19:39:54 -070057 bool force_copy;
Elliott Hughes515a5bc2011-08-17 11:08:34 -070058
Elliott Hughesa0957642011-09-02 14:27:33 -070059 // Extra diagnostics.
60 bool verbose_jni;
61 bool log_third_party_jni;
62 std::string trace;
63
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070064 // Used to provide compatibility for apps that assumed direct references.
65 bool work_around_app_jni_bugs;
66
Elliott Hughesbbd76712011-08-17 10:25:24 -070067 // Used to hold references to pinned primitive arrays.
Elliott Hughes8daa0922011-09-11 13:46:25 -070068 Mutex pins_lock;
Elliott Hughesbbd76712011-08-17 10:25:24 -070069 ReferenceTable pin_table;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070070
71 // JNI global references.
Elliott Hughes8daa0922011-09-11 13:46:25 -070072 Mutex globals_lock;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070073 IndirectReferenceTable globals;
74
75 // JNI weak global references.
Elliott Hughes8daa0922011-09-11 13:46:25 -070076 Mutex weak_globals_lock;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070077 IndirectReferenceTable weak_globals;
Elliott Hughes0af55432011-08-17 18:37:28 -070078
Elliott Hughes8daa0922011-09-11 13:46:25 -070079 Mutex libraries_lock;
Elliott Hughes79082e32011-08-25 12:07:32 -070080 Libraries* libraries;
Elliott Hughesa2501992011-08-26 19:39:54 -070081
82 // Used by -Xcheck:jni.
83 const JNIInvokeInterface* unchecked_functions;
Elliott Hughesf2682d52011-08-15 16:37:04 -070084};
85
Elliott Hughes69f5bc62011-08-24 09:26:14 -070086struct JNIEnvExt : public JNIEnv {
Elliott Hughes75770752011-08-24 17:52:38 -070087 JNIEnvExt(Thread* self, JavaVMExt* vm);
Elliott Hughesc1674ed2011-08-25 18:09:09 -070088 ~JNIEnvExt();
Elliott Hughesbbd76712011-08-17 10:25:24 -070089
Elliott Hughes40ef99e2011-08-11 17:44:34 -070090 Thread* self;
Elliott Hughes75770752011-08-24 17:52:38 -070091 JavaVMExt* vm;
Carl Shapiroea4dca82011-08-01 13:45:38 -070092
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070093 // Frequently-accessed fields cached from JavaVM.
Elliott Hughes515a5bc2011-08-17 11:08:34 -070094 bool check_jni;
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070095 bool work_around_app_jni_bugs;
Elliott Hughes515a5bc2011-08-17 11:08:34 -070096
Elliott Hughesa2501992011-08-26 19:39:54 -070097 // How many nested "critical" JNI calls are we in?
98 int critical;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070099
Elliott Hughesbbd76712011-08-17 10:25:24 -0700100 // Entered JNI monitors, for bulk exit on thread detach.
Elliott Hughes02b48d12011-09-07 17:15:51 -0700101 ReferenceTable monitors;
Elliott Hughes6c1a3942011-08-17 15:00:06 -0700102
103 // JNI local references.
104 IndirectReferenceTable locals;
Elliott Hughesa2501992011-08-26 19:39:54 -0700105
106 // Used by -Xcheck:jni.
107 const JNINativeInterface* unchecked_functions;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700108};
109
Elliott Hughesa2501992011-08-26 19:39:54 -0700110const JNINativeInterface* GetCheckJniNativeInterface();
111const JNIInvokeInterface* GetCheckJniInvokeInterface();
112
Ian Rogersdf20fe02011-07-20 20:34:16 -0700113} // namespace art
Carl Shapiroea4dca82011-08-01 13:45:38 -0700114
Elliott Hughesb465ab02011-08-24 11:21:21 -0700115std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs);
116
Ian Rogersdf20fe02011-07-20 20:34:16 -0700117#endif // ART_SRC_JNI_INTERNAL_H_