blob: ed9d3abfe280e20dbee61500c794df7a260e40df [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
Alex Light185d1342016-08-11 10:48:03 -070039class JavaVMExt;
40// Hook definition for runtime plugins.
41using GetEnvHook = jint (*)(JavaVMExt* vm, /*out*/void** new_env, jint version);
42
Ian Rogers68d8b422014-07-17 11:09:10 -070043class JavaVMExt : public JavaVM {
44 public:
Igor Murashkinaaebaa02015-01-26 10:55:53 -080045 JavaVMExt(Runtime* runtime, const RuntimeArgumentMap& runtime_options);
Ian Rogers68d8b422014-07-17 11:09:10 -070046 ~JavaVMExt();
47
48 bool ForceCopy() const {
49 return force_copy_;
50 }
51
52 bool IsCheckJniEnabled() const {
53 return check_jni_;
54 }
55
56 bool IsTracingEnabled() const {
57 return tracing_enabled_;
58 }
59
60 Runtime* GetRuntime() const {
61 return runtime_;
62 }
63
64 void SetCheckJniAbortHook(void (*hook)(void*, const std::string&), void* data) {
65 check_jni_abort_hook_ = hook;
66 check_jni_abort_hook_data_ = data;
67 }
68
69 // Aborts execution unless there is an abort handler installed in which case it will return. Its
70 // therefore important that callers return after aborting as otherwise code following the abort
71 // will be executed in the abort handler case.
72 void JniAbort(const char* jni_function_name, const char* msg);
73
74 void JniAbortV(const char* jni_function_name, const char* fmt, va_list ap);
75
76 void JniAbortF(const char* jni_function_name, const char* fmt, ...)
77 __attribute__((__format__(__printf__, 3, 4)));
78
79 // If both "-Xcheck:jni" and "-Xjnitrace:" are enabled, we print trace messages
80 // when a native method that matches the -Xjnitrace argument calls a JNI function
81 // such as NewByteArray.
82 // If -verbose:third-party-jni is on, we want to log any JNI function calls
83 // made by a third-party native method.
Mathieu Chartier90443472015-07-16 20:32:27 -070084 bool ShouldTrace(ArtMethod* method) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070085
86 /**
87 * Loads the given shared library. 'path' is an absolute pathname.
88 *
Dmitriy Ivanovf5a30992015-11-11 14:18:55 -080089 * Returns 'true' on success. On failure, sets 'error_msg' to a
Ian Rogers68d8b422014-07-17 11:09:10 -070090 * human-readable description of the error.
91 */
Dimitry Ivanov942dc2982016-02-24 13:33:33 -080092 bool LoadNativeLibrary(JNIEnv* env,
93 const std::string& path,
94 jobject class_loader,
95 jstring library_path,
Dimitry Ivanovd5bbadf2015-12-15 14:08:18 -080096 std::string* error_msg);
Ian Rogers68d8b422014-07-17 11:09:10 -070097
Mathieu Chartier598302a2015-09-23 14:52:39 -070098 // Unload native libraries with cleared class loaders.
99 void UnloadNativeLibraries()
100 REQUIRES(!Locks::jni_libraries_lock_)
101 SHARED_REQUIRES(Locks::mutator_lock_);
102
Ian Rogers68d8b422014-07-17 11:09:10 -0700103 /**
104 * Returns a pointer to the code for the native method 'm', found
105 * using dlsym(3) on every native library that's been loaded so far.
106 */
Mathieu Chartiere401d142015-04-22 13:56:20 -0700107 void* FindCodeForNativeMethod(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -0700108 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700109
110 void DumpForSigQuit(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -0700111 REQUIRES(!Locks::jni_libraries_lock_, !globals_lock_, !weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700112
113 void DumpReferenceTables(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -0700114 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!globals_lock_, !weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700115
116 bool SetCheckJniEnabled(bool enabled);
117
Mathieu Chartier90443472015-07-16 20:32:27 -0700118 void VisitRoots(RootVisitor* visitor) SHARED_REQUIRES(Locks::mutator_lock_)
119 REQUIRES(!globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700120
Mathieu Chartier90443472015-07-16 20:32:27 -0700121 void DisallowNewWeakGlobals() SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_);
122 void AllowNewWeakGlobals() SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_);
Mathieu Chartier90443472015-07-16 20:32:27 -0700123 void BroadcastForNewWeakGlobals() SHARED_REQUIRES(Locks::mutator_lock_)
124 REQUIRES(!weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700125
126 jobject AddGlobalRef(Thread* self, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700127 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700128
129 jweak AddWeakGlobalRef(Thread* self, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -0700130 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700131
Mathieu Chartier90443472015-07-16 20:32:27 -0700132 void DeleteGlobalRef(Thread* self, jobject obj) REQUIRES(!globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700133
Mathieu Chartier90443472015-07-16 20:32:27 -0700134 void DeleteWeakGlobalRef(Thread* self, jweak obj) REQUIRES(!weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700135
Mathieu Chartier97509952015-07-13 14:35:43 -0700136 void SweepJniWeakGlobals(IsMarkedVisitor* visitor)
Mathieu Chartier90443472015-07-16 20:32:27 -0700137 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700138
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700139 mirror::Object* DecodeGlobal(IndirectRef ref)
Mathieu Chartier90443472015-07-16 20:32:27 -0700140 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700141
Jeff Hao83c81952015-05-27 19:29:29 -0700142 void UpdateGlobal(Thread* self, IndirectRef ref, mirror::Object* result)
Mathieu Chartier90443472015-07-16 20:32:27 -0700143 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!globals_lock_);
Jeff Hao83c81952015-05-27 19:29:29 -0700144
Ian Rogers68d8b422014-07-17 11:09:10 -0700145 mirror::Object* DecodeWeakGlobal(Thread* self, IndirectRef ref)
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700146 SHARED_REQUIRES(Locks::mutator_lock_)
147 REQUIRES(!weak_globals_lock_);
148
149 mirror::Object* DecodeWeakGlobalLocked(Thread* self, IndirectRef ref)
150 SHARED_REQUIRES(Locks::mutator_lock_)
151 REQUIRES(weak_globals_lock_);
152
Hiroshi Yamauchi498b1602015-09-16 21:11:44 -0700153 // Like DecodeWeakGlobal() but to be used only during a runtime shutdown where self may be
154 // null.
155 mirror::Object* DecodeWeakGlobalDuringShutdown(Thread* self, IndirectRef ref)
156 SHARED_REQUIRES(Locks::mutator_lock_)
157 REQUIRES(!weak_globals_lock_);
158
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -0800159 // Checks if the weak global ref has been cleared by the GC without decode (read barrier.)
160 bool IsWeakGlobalCleared(Thread* self, IndirectRef ref)
161 SHARED_REQUIRES(Locks::mutator_lock_)
162 REQUIRES(!weak_globals_lock_);
163
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700164 Mutex& WeakGlobalsLock() RETURN_CAPABILITY(weak_globals_lock_) {
165 return weak_globals_lock_;
166 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700167
Jeff Hao83c81952015-05-27 19:29:29 -0700168 void UpdateWeakGlobal(Thread* self, IndirectRef ref, mirror::Object* result)
Mathieu Chartier90443472015-07-16 20:32:27 -0700169 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_);
Jeff Hao83c81952015-05-27 19:29:29 -0700170
Ian Rogers68d8b422014-07-17 11:09:10 -0700171 const JNIInvokeInterface* GetUncheckedFunctions() const {
172 return unchecked_functions_;
173 }
174
Mathieu Chartier90443472015-07-16 20:32:27 -0700175 void TrimGlobals() SHARED_REQUIRES(Locks::mutator_lock_)
176 REQUIRES(!globals_lock_);
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800177
Alex Light185d1342016-08-11 10:48:03 -0700178 jint HandleGetEnv(/*out*/void** env, jint version);
179
180 void AddEnvironmentHook(GetEnvHook hook);
181
182 static bool IsBadJniVersion(int version);
183
Ian Rogers68d8b422014-07-17 11:09:10 -0700184 private:
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700185 // Return true if self can currently access weak globals.
186 bool MayAccessWeakGlobalsUnlocked(Thread* self) const SHARED_REQUIRES(Locks::mutator_lock_);
187 bool MayAccessWeakGlobals(Thread* self) const
188 SHARED_REQUIRES(Locks::mutator_lock_)
189 REQUIRES(weak_globals_lock_);
190
Ian Rogers68d8b422014-07-17 11:09:10 -0700191 Runtime* const runtime_;
192
193 // Used for testing. By default, we'll LOG(FATAL) the reason.
194 void (*check_jni_abort_hook_)(void* data, const std::string& reason);
195 void* check_jni_abort_hook_data_;
196
197 // Extra checking.
198 bool check_jni_;
199 bool force_copy_;
200 const bool tracing_enabled_;
201
202 // Extra diagnostics.
203 const std::string trace_;
204
Ian Rogers68d8b422014-07-17 11:09:10 -0700205 // JNI global references.
206 ReaderWriterMutex globals_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
207 // Not guarded by globals_lock since we sometimes use SynchronizedGet in Thread::DecodeJObject.
208 IndirectReferenceTable globals_;
209
Mathieu Chartier598302a2015-09-23 14:52:39 -0700210 // No lock annotation since UnloadNativeLibraries is called on libraries_ but locks the
211 // jni_libraries_lock_ internally.
212 std::unique_ptr<Libraries> libraries_;
Ian Rogers68d8b422014-07-17 11:09:10 -0700213
214 // Used by -Xcheck:jni.
215 const JNIInvokeInterface* const unchecked_functions_;
216
217 // JNI weak global references.
218 Mutex weak_globals_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
219 // Since weak_globals_ contain weak roots, be careful not to
220 // directly access the object references in it. Use Get() with the
221 // read barrier enabled.
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700222 // Not guarded by weak_globals_lock since we may use SynchronizedGet in DecodeWeakGlobal.
223 IndirectReferenceTable weak_globals_;
224 // Not guarded by weak_globals_lock since we may use SynchronizedGet in DecodeWeakGlobal.
225 Atomic<bool> allow_accessing_weak_globals_;
Ian Rogers68d8b422014-07-17 11:09:10 -0700226 ConditionVariable weak_globals_add_condition_ GUARDED_BY(weak_globals_lock_);
227
Alex Light185d1342016-08-11 10:48:03 -0700228 // TODO Maybe move this to Runtime.
229 std::vector<GetEnvHook> env_hooks_;
230
Ian Rogers68d8b422014-07-17 11:09:10 -0700231 DISALLOW_COPY_AND_ASSIGN(JavaVMExt);
232};
233
234} // namespace art
235
236#endif // ART_RUNTIME_JAVA_VM_EXT_H_