blob: 3d055cd7ce8c55b0a0a911b6f4445790e0a2c843 [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 {
Ian Rogers68d8b422014-07-17 11:09:10 -070030 class Array;
31} // namespace mirror
32
Mathieu Chartiere401d142015-04-22 13:56:20 -070033class ArtMethod;
Ian Rogers68d8b422014-07-17 11:09:10 -070034class Libraries;
35class ParsedOptions;
36class Runtime;
Igor Murashkinaaebaa02015-01-26 10:55:53 -080037struct RuntimeArgumentMap;
Ian Rogers68d8b422014-07-17 11:09:10 -070038
39class JavaVMExt : public JavaVM {
40 public:
Igor Murashkinaaebaa02015-01-26 10:55:53 -080041 JavaVMExt(Runtime* runtime, const RuntimeArgumentMap& runtime_options);
Ian Rogers68d8b422014-07-17 11:09:10 -070042 ~JavaVMExt();
43
44 bool ForceCopy() const {
45 return force_copy_;
46 }
47
48 bool IsCheckJniEnabled() const {
49 return check_jni_;
50 }
51
52 bool IsTracingEnabled() const {
53 return tracing_enabled_;
54 }
55
56 Runtime* GetRuntime() const {
57 return runtime_;
58 }
59
60 void SetCheckJniAbortHook(void (*hook)(void*, const std::string&), void* data) {
61 check_jni_abort_hook_ = hook;
62 check_jni_abort_hook_data_ = data;
63 }
64
65 // Aborts execution unless there is an abort handler installed in which case it will return. Its
66 // therefore important that callers return after aborting as otherwise code following the abort
67 // will be executed in the abort handler case.
68 void JniAbort(const char* jni_function_name, const char* msg);
69
70 void JniAbortV(const char* jni_function_name, const char* fmt, va_list ap);
71
72 void JniAbortF(const char* jni_function_name, const char* fmt, ...)
73 __attribute__((__format__(__printf__, 3, 4)));
74
75 // If both "-Xcheck:jni" and "-Xjnitrace:" are enabled, we print trace messages
76 // when a native method that matches the -Xjnitrace argument calls a JNI function
77 // such as NewByteArray.
78 // If -verbose:third-party-jni is on, we want to log any JNI function calls
79 // made by a third-party native method.
Mathieu Chartier90443472015-07-16 20:32:27 -070080 bool ShouldTrace(ArtMethod* method) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070081
82 /**
83 * Loads the given shared library. 'path' is an absolute pathname.
84 *
Dmitriy Ivanovf5a30992015-11-11 14:18:55 -080085 * Returns 'true' on success. On failure, sets 'error_msg' to a
Ian Rogers68d8b422014-07-17 11:09:10 -070086 * human-readable description of the error.
87 */
Dimitry Ivanov942dc2982016-02-24 13:33:33 -080088 bool LoadNativeLibrary(JNIEnv* env,
89 const std::string& path,
90 jobject class_loader,
91 jstring library_path,
Dimitry Ivanovd5bbadf2015-12-15 14:08:18 -080092 std::string* error_msg);
Ian Rogers68d8b422014-07-17 11:09:10 -070093
Mathieu Chartier598302a2015-09-23 14:52:39 -070094 // Unload native libraries with cleared class loaders.
95 void UnloadNativeLibraries()
96 REQUIRES(!Locks::jni_libraries_lock_)
97 SHARED_REQUIRES(Locks::mutator_lock_);
98
Ian Rogers68d8b422014-07-17 11:09:10 -070099 /**
100 * Returns a pointer to the code for the native method 'm', found
101 * using dlsym(3) on every native library that's been loaded so far.
102 */
Mathieu Chartiere401d142015-04-22 13:56:20 -0700103 void* FindCodeForNativeMethod(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -0700104 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700105
106 void DumpForSigQuit(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -0700107 REQUIRES(!Locks::jni_libraries_lock_, !globals_lock_, !weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700108
109 void DumpReferenceTables(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -0700110 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!globals_lock_, !weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700111
112 bool SetCheckJniEnabled(bool enabled);
113
Mathieu Chartier90443472015-07-16 20:32:27 -0700114 void VisitRoots(RootVisitor* visitor) SHARED_REQUIRES(Locks::mutator_lock_)
115 REQUIRES(!globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700116
Mathieu Chartier90443472015-07-16 20:32:27 -0700117 void DisallowNewWeakGlobals() SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_);
118 void AllowNewWeakGlobals() SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_);
Mathieu Chartier90443472015-07-16 20:32:27 -0700119 void BroadcastForNewWeakGlobals() SHARED_REQUIRES(Locks::mutator_lock_)
120 REQUIRES(!weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700121
122 jobject AddGlobalRef(Thread* self, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700123 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700124
125 jweak AddWeakGlobalRef(Thread* self, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700126 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700127
Mathieu Chartier90443472015-07-16 20:32:27 -0700128 void DeleteGlobalRef(Thread* self, jobject obj) REQUIRES(!globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700129
Mathieu Chartier90443472015-07-16 20:32:27 -0700130 void DeleteWeakGlobalRef(Thread* self, jweak obj) REQUIRES(!weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700131
Mathieu Chartier97509952015-07-13 14:35:43 -0700132 void SweepJniWeakGlobals(IsMarkedVisitor* visitor)
Mathieu Chartier90443472015-07-16 20:32:27 -0700133 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700134
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700135 mirror::Object* DecodeGlobal(IndirectRef ref)
Mathieu Chartier90443472015-07-16 20:32:27 -0700136 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700137
Jeff Hao83c81952015-05-27 19:29:29 -0700138 void UpdateGlobal(Thread* self, IndirectRef ref, mirror::Object* result)
Mathieu Chartier90443472015-07-16 20:32:27 -0700139 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!globals_lock_);
Jeff Hao83c81952015-05-27 19:29:29 -0700140
Ian Rogers68d8b422014-07-17 11:09:10 -0700141 mirror::Object* DecodeWeakGlobal(Thread* self, IndirectRef ref)
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700142 SHARED_REQUIRES(Locks::mutator_lock_)
143 REQUIRES(!weak_globals_lock_);
144
145 mirror::Object* DecodeWeakGlobalLocked(Thread* self, IndirectRef ref)
146 SHARED_REQUIRES(Locks::mutator_lock_)
147 REQUIRES(weak_globals_lock_);
148
Hiroshi Yamauchi498b1602015-09-16 21:11:44 -0700149 // Like DecodeWeakGlobal() but to be used only during a runtime shutdown where self may be
150 // null.
151 mirror::Object* DecodeWeakGlobalDuringShutdown(Thread* self, IndirectRef ref)
152 SHARED_REQUIRES(Locks::mutator_lock_)
153 REQUIRES(!weak_globals_lock_);
154
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -0800155 // Checks if the weak global ref has been cleared by the GC without decode (read barrier.)
156 bool IsWeakGlobalCleared(Thread* self, IndirectRef ref)
157 SHARED_REQUIRES(Locks::mutator_lock_)
158 REQUIRES(!weak_globals_lock_);
159
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700160 Mutex& WeakGlobalsLock() RETURN_CAPABILITY(weak_globals_lock_) {
161 return weak_globals_lock_;
162 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700163
Jeff Hao83c81952015-05-27 19:29:29 -0700164 void UpdateWeakGlobal(Thread* self, IndirectRef ref, mirror::Object* result)
Mathieu Chartier90443472015-07-16 20:32:27 -0700165 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_);
Jeff Hao83c81952015-05-27 19:29:29 -0700166
Ian Rogers68d8b422014-07-17 11:09:10 -0700167 const JNIInvokeInterface* GetUncheckedFunctions() const {
168 return unchecked_functions_;
169 }
170
Mathieu Chartier90443472015-07-16 20:32:27 -0700171 void TrimGlobals() SHARED_REQUIRES(Locks::mutator_lock_)
172 REQUIRES(!globals_lock_);
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800173
Ian Rogers68d8b422014-07-17 11:09:10 -0700174 private:
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700175 // Return true if self can currently access weak globals.
176 bool MayAccessWeakGlobalsUnlocked(Thread* self) const SHARED_REQUIRES(Locks::mutator_lock_);
177 bool MayAccessWeakGlobals(Thread* self) const
178 SHARED_REQUIRES(Locks::mutator_lock_)
179 REQUIRES(weak_globals_lock_);
180
Ian Rogers68d8b422014-07-17 11:09:10 -0700181 Runtime* const runtime_;
182
183 // Used for testing. By default, we'll LOG(FATAL) the reason.
184 void (*check_jni_abort_hook_)(void* data, const std::string& reason);
185 void* check_jni_abort_hook_data_;
186
187 // Extra checking.
188 bool check_jni_;
189 bool force_copy_;
190 const bool tracing_enabled_;
191
192 // Extra diagnostics.
193 const std::string trace_;
194
Ian Rogers68d8b422014-07-17 11:09:10 -0700195 // JNI global references.
196 ReaderWriterMutex globals_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
197 // Not guarded by globals_lock since we sometimes use SynchronizedGet in Thread::DecodeJObject.
198 IndirectReferenceTable globals_;
199
Mathieu Chartier598302a2015-09-23 14:52:39 -0700200 // No lock annotation since UnloadNativeLibraries is called on libraries_ but locks the
201 // jni_libraries_lock_ internally.
202 std::unique_ptr<Libraries> libraries_;
Ian Rogers68d8b422014-07-17 11:09:10 -0700203
204 // Used by -Xcheck:jni.
205 const JNIInvokeInterface* const unchecked_functions_;
206
207 // JNI weak global references.
208 Mutex weak_globals_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
209 // Since weak_globals_ contain weak roots, be careful not to
210 // directly access the object references in it. Use Get() with the
211 // read barrier enabled.
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700212 // Not guarded by weak_globals_lock since we may use SynchronizedGet in DecodeWeakGlobal.
213 IndirectReferenceTable weak_globals_;
214 // Not guarded by weak_globals_lock since we may use SynchronizedGet in DecodeWeakGlobal.
215 Atomic<bool> allow_accessing_weak_globals_;
Ian Rogers68d8b422014-07-17 11:09:10 -0700216 ConditionVariable weak_globals_add_condition_ GUARDED_BY(weak_globals_lock_);
217
218 DISALLOW_COPY_AND_ASSIGN(JavaVMExt);
219};
220
221} // namespace art
222
223#endif // ART_RUNTIME_JAVA_VM_EXT_H_