blob: 11f7750d1374a8b4ff05b140aa5704479e003e23 [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 Hughesbbd76712011-08-17 10:25:24 -070011#include "reference_table.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070012
Elliott Hughes0af55432011-08-17 18:37:28 -070013#include <string>
14
Ian Rogersdf20fe02011-07-20 20:34:16 -070015namespace art {
16
Elliott Hughes814e4032011-08-23 12:07:56 -070017class ClassLoader;
Elliott Hughes79082e32011-08-25 12:07:32 -070018class Libraries;
19class Method;
Elliott Hughes18c07532011-08-18 15:50:51 -070020class Mutex;
Elliott Hughesf2682d52011-08-15 16:37:04 -070021class Runtime;
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);
25
Elliott Hughesbf86d042011-08-31 17:53:14 -070026template<typename T> T Decode(JNIEnv*, jobject);
Elliott Hughescf4c6c42011-09-01 15:16:42 -070027template<typename T> T AddLocalReference(JNIEnv*, const Object*);
Elliott Hughesbf86d042011-08-31 17:53:14 -070028
Elliott Hughes69f5bc62011-08-24 09:26:14 -070029struct JavaVMExt : public JavaVM {
Elliott Hughes0af55432011-08-17 18:37:28 -070030 JavaVMExt(Runtime* runtime, bool check_jni, bool verbose_jni);
Elliott Hughesde69d7f2011-08-18 16:49:37 -070031 ~JavaVMExt();
Elliott Hughes0af55432011-08-17 18:37:28 -070032
Elliott Hughes75770752011-08-24 17:52:38 -070033 /**
34 * Loads the given shared library. 'path' is an absolute pathname.
Elliott Hughes0af55432011-08-17 18:37:28 -070035 *
Elliott Hughes75770752011-08-24 17:52:38 -070036 * Returns 'true' on success. On failure, sets 'detail' to a
37 * human-readable description of the error.
Elliott Hughes0af55432011-08-17 18:37:28 -070038 */
Elliott Hughes75770752011-08-24 17:52:38 -070039 bool LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail);
Elliott Hughesbbd76712011-08-17 10:25:24 -070040
Elliott Hughes79082e32011-08-25 12:07:32 -070041 /**
42 * Returns a pointer to the code for the native method 'm', found
43 * using dlsym(3) on every native library that's been loaded so far.
44 */
45 void* FindCodeForNativeMethod(Method* m);
46
Elliott Hughes410c0c82011-09-01 17:58:25 -070047 void VisitRoots(Heap::RootVisitor*, void*);
48
Elliott Hughesf2682d52011-08-15 16:37:04 -070049 Runtime* runtime;
Elliott Hughesbbd76712011-08-17 10:25:24 -070050
Elliott Hughesa2501992011-08-26 19:39:54 -070051 // Used for testing. By default, we'll LOG(FATAL) the reason.
52 void (*check_jni_abort_hook)(const std::string& reason);
53
Elliott Hughes515a5bc2011-08-17 11:08:34 -070054 bool check_jni;
Elliott Hughes0af55432011-08-17 18:37:28 -070055 bool verbose_jni;
Elliott Hughesa2501992011-08-26 19:39:54 -070056 bool force_copy;
Elliott Hughes515a5bc2011-08-17 11:08:34 -070057
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070058 // Used to provide compatibility for apps that assumed direct references.
59 bool work_around_app_jni_bugs;
60
Elliott Hughesbbd76712011-08-17 10:25:24 -070061 // Used to hold references to pinned primitive arrays.
Elliott Hughes75770752011-08-24 17:52:38 -070062 Mutex* pins_lock;
Elliott Hughesbbd76712011-08-17 10:25:24 -070063 ReferenceTable pin_table;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070064
65 // JNI global references.
Elliott Hughes18c07532011-08-18 15:50:51 -070066 Mutex* globals_lock;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070067 IndirectReferenceTable globals;
68
69 // JNI weak global references.
Elliott Hughes18c07532011-08-18 15:50:51 -070070 Mutex* weak_globals_lock;
Elliott Hughes6c1a3942011-08-17 15:00:06 -070071 IndirectReferenceTable weak_globals;
Elliott Hughes0af55432011-08-17 18:37:28 -070072
Elliott Hughes79082e32011-08-25 12:07:32 -070073 Mutex* libraries_lock;
74 Libraries* libraries;
Elliott Hughesa2501992011-08-26 19:39:54 -070075
76 // Used by -Xcheck:jni.
77 const JNIInvokeInterface* unchecked_functions;
Elliott Hughesf2682d52011-08-15 16:37:04 -070078};
79
Elliott Hughes69f5bc62011-08-24 09:26:14 -070080struct JNIEnvExt : public JNIEnv {
Elliott Hughes75770752011-08-24 17:52:38 -070081 JNIEnvExt(Thread* self, JavaVMExt* vm);
Elliott Hughesc1674ed2011-08-25 18:09:09 -070082 ~JNIEnvExt();
Elliott Hughesbbd76712011-08-17 10:25:24 -070083
Elliott Hughes40ef99e2011-08-11 17:44:34 -070084 Thread* self;
Elliott Hughes75770752011-08-24 17:52:38 -070085 JavaVMExt* vm;
Carl Shapiroea4dca82011-08-01 13:45:38 -070086
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070087 // Frequently-accessed fields cached from JavaVM.
Elliott Hughes515a5bc2011-08-17 11:08:34 -070088 bool check_jni;
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -070089 bool work_around_app_jni_bugs;
Elliott Hughes515a5bc2011-08-17 11:08:34 -070090
Elliott Hughesa2501992011-08-26 19:39:54 -070091 // How many nested "critical" JNI calls are we in?
92 int critical;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070093
Elliott Hughesbbd76712011-08-17 10:25:24 -070094 // Entered JNI monitors, for bulk exit on thread detach.
Elliott Hughes6c1a3942011-08-17 15:00:06 -070095 ReferenceTable monitors;
96
97 // JNI local references.
98 IndirectReferenceTable locals;
Elliott Hughesa2501992011-08-26 19:39:54 -070099
100 // Used by -Xcheck:jni.
101 const JNINativeInterface* unchecked_functions;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700102};
103
Elliott Hughesa2501992011-08-26 19:39:54 -0700104const JNINativeInterface* GetCheckJniNativeInterface();
105const JNIInvokeInterface* GetCheckJniInvokeInterface();
106
Ian Rogersdf20fe02011-07-20 20:34:16 -0700107} // namespace art
Carl Shapiroea4dca82011-08-01 13:45:38 -0700108
Elliott Hughesb465ab02011-08-24 11:21:21 -0700109std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs);
110
Ian Rogersdf20fe02011-07-20 20:34:16 -0700111#endif // ART_SRC_JNI_INTERNAL_H_