blob: 749b9fb6c077e3ce18c4bbe0f9000277af36ad7e [file] [log] [blame]
Ian Rogers68d8b422014-07-17 11:09:10 -07001/*
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 */
16
17#ifndef ART_RUNTIME_JAVA_VM_EXT_H_
18#define ART_RUNTIME_JAVA_VM_EXT_H_
19
20#include "jni.h"
21
22#include "base/macros.h"
23#include "base/mutex.h"
24#include "indirect_reference_table.h"
25#include "reference_table.h"
26
27namespace art {
28
29namespace mirror {
30 class ArtMethod;
31 class Array;
32} // namespace mirror
33
34class Libraries;
35class ParsedOptions;
36class Runtime;
37
38class JavaVMExt : public JavaVM {
39 public:
40 JavaVMExt(Runtime* runtime, ParsedOptions* options);
41 ~JavaVMExt();
42
43 bool ForceCopy() const {
44 return force_copy_;
45 }
46
47 bool IsCheckJniEnabled() const {
48 return check_jni_;
49 }
50
51 bool IsTracingEnabled() const {
52 return tracing_enabled_;
53 }
54
55 Runtime* GetRuntime() const {
56 return runtime_;
57 }
58
59 void SetCheckJniAbortHook(void (*hook)(void*, const std::string&), void* data) {
60 check_jni_abort_hook_ = hook;
61 check_jni_abort_hook_data_ = data;
62 }
63
64 // Aborts execution unless there is an abort handler installed in which case it will return. Its
65 // therefore important that callers return after aborting as otherwise code following the abort
66 // will be executed in the abort handler case.
67 void JniAbort(const char* jni_function_name, const char* msg);
68
69 void JniAbortV(const char* jni_function_name, const char* fmt, va_list ap);
70
71 void JniAbortF(const char* jni_function_name, const char* fmt, ...)
72 __attribute__((__format__(__printf__, 3, 4)));
73
74 // If both "-Xcheck:jni" and "-Xjnitrace:" are enabled, we print trace messages
75 // when a native method that matches the -Xjnitrace argument calls a JNI function
76 // such as NewByteArray.
77 // If -verbose:third-party-jni is on, we want to log any JNI function calls
78 // made by a third-party native method.
79 bool ShouldTrace(mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
80
81 /**
82 * Loads the given shared library. 'path' is an absolute pathname.
83 *
84 * Returns 'true' on success. On failure, sets 'detail' to a
85 * human-readable description of the error.
86 */
87 bool LoadNativeLibrary(JNIEnv* env, const std::string& path, jobject javaLoader,
88 std::string* error_msg);
89
90 /**
91 * Returns a pointer to the code for the native method 'm', found
92 * using dlsym(3) on every native library that's been loaded so far.
93 */
94 void* FindCodeForNativeMethod(mirror::ArtMethod* m)
95 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
96
97 void DumpForSigQuit(std::ostream& os)
Mathieu Chartier2e158932014-09-11 13:14:31 -070098 LOCKS_EXCLUDED(Locks::jni_libraries_lock_, globals_lock_, weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070099
100 void DumpReferenceTables(std::ostream& os)
101 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
102
103 bool SetCheckJniEnabled(bool enabled);
104
105 void VisitRoots(RootCallback* callback, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
106
107 void DisallowNewWeakGlobals() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
108
109 void AllowNewWeakGlobals() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
110
111 jobject AddGlobalRef(Thread* self, mirror::Object* obj)
112 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
113
114 jweak AddWeakGlobalRef(Thread* self, mirror::Object* obj)
115 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
116
117 void DeleteGlobalRef(Thread* self, jobject obj);
118
119 void DeleteWeakGlobalRef(Thread* self, jweak obj);
120
121 void SweepJniWeakGlobals(IsMarkedCallback* callback, void* arg)
122 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
123
124 mirror::Object* DecodeGlobal(Thread* self, IndirectRef ref)
125 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
126
127 mirror::Object* DecodeWeakGlobal(Thread* self, IndirectRef ref)
128 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
129
Ian Rogers68d8b422014-07-17 11:09:10 -0700130 const JNIInvokeInterface* GetUncheckedFunctions() const {
131 return unchecked_functions_;
132 }
133
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800134 void TrimGlobals() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
135 LOCKS_EXCLUDED(globals_lock_);
136
Ian Rogers68d8b422014-07-17 11:09:10 -0700137 private:
138 Runtime* const runtime_;
139
140 // Used for testing. By default, we'll LOG(FATAL) the reason.
141 void (*check_jni_abort_hook_)(void* data, const std::string& reason);
142 void* check_jni_abort_hook_data_;
143
144 // Extra checking.
145 bool check_jni_;
146 bool force_copy_;
147 const bool tracing_enabled_;
148
149 // Extra diagnostics.
150 const std::string trace_;
151
Ian Rogers68d8b422014-07-17 11:09:10 -0700152 // JNI global references.
153 ReaderWriterMutex globals_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
154 // Not guarded by globals_lock since we sometimes use SynchronizedGet in Thread::DecodeJObject.
155 IndirectReferenceTable globals_;
156
157 std::unique_ptr<Libraries> libraries_ GUARDED_BY(Locks::jni_libraries_lock_);
158
159 // Used by -Xcheck:jni.
160 const JNIInvokeInterface* const unchecked_functions_;
161
162 // JNI weak global references.
163 Mutex weak_globals_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
164 // Since weak_globals_ contain weak roots, be careful not to
165 // directly access the object references in it. Use Get() with the
166 // read barrier enabled.
167 IndirectReferenceTable weak_globals_ GUARDED_BY(weak_globals_lock_);
168 bool allow_new_weak_globals_ GUARDED_BY(weak_globals_lock_);
169 ConditionVariable weak_globals_add_condition_ GUARDED_BY(weak_globals_lock_);
170
171 DISALLOW_COPY_AND_ASSIGN(JavaVMExt);
172};
173
174} // namespace art
175
176#endif // ART_RUNTIME_JAVA_VM_EXT_H_