blob: 408d3542ed6fe695cddce4b9f73d5514a01b3d88 [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
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010017#ifndef ART_RUNTIME_JNI_JAVA_VM_EXT_H_
18#define ART_RUNTIME_JNI_JAVA_VM_EXT_H_
Ian Rogers68d8b422014-07-17 11:09:10 -070019
20#include "jni.h"
21
22#include "base/macros.h"
23#include "base/mutex.h"
24#include "indirect_reference_table.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070025#include "obj_ptr.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070026#include "reference_table.h"
27
28namespace art {
29
30namespace mirror {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080031class Array;
Ian Rogers68d8b422014-07-17 11:09:10 -070032} // namespace mirror
33
Mathieu Chartiere401d142015-04-22 13:56:20 -070034class ArtMethod;
Andreas Gampe57cf00b2017-06-05 17:15:32 -070035class IsMarkedVisitor;
Ian Rogers68d8b422014-07-17 11:09:10 -070036class Libraries;
37class ParsedOptions;
38class Runtime;
Igor Murashkinaaebaa02015-01-26 10:55:53 -080039struct RuntimeArgumentMap;
Ian Rogers68d8b422014-07-17 11:09:10 -070040
Alex Light185d1342016-08-11 10:48:03 -070041class JavaVMExt;
42// Hook definition for runtime plugins.
43using GetEnvHook = jint (*)(JavaVMExt* vm, /*out*/void** new_env, jint version);
44
Ian Rogers68d8b422014-07-17 11:09:10 -070045class JavaVMExt : public JavaVM {
46 public:
Richard Uhlerda0a69e2016-10-11 15:06:38 +010047 // Creates a new JavaVMExt object.
48 // Returns nullptr on error, in which case error_msg is set to a message
49 // describing the error.
50 static std::unique_ptr<JavaVMExt> Create(Runtime* runtime,
51 const RuntimeArgumentMap& runtime_options,
52 std::string* error_msg);
53
54
Ian Rogers68d8b422014-07-17 11:09:10 -070055 ~JavaVMExt();
56
57 bool ForceCopy() const {
58 return force_copy_;
59 }
60
61 bool IsCheckJniEnabled() const {
62 return check_jni_;
63 }
64
65 bool IsTracingEnabled() const {
66 return tracing_enabled_;
67 }
68
69 Runtime* GetRuntime() const {
70 return runtime_;
71 }
72
73 void SetCheckJniAbortHook(void (*hook)(void*, const std::string&), void* data) {
74 check_jni_abort_hook_ = hook;
75 check_jni_abort_hook_data_ = data;
76 }
77
78 // Aborts execution unless there is an abort handler installed in which case it will return. Its
79 // therefore important that callers return after aborting as otherwise code following the abort
80 // will be executed in the abort handler case.
81 void JniAbort(const char* jni_function_name, const char* msg);
82
83 void JniAbortV(const char* jni_function_name, const char* fmt, va_list ap);
84
85 void JniAbortF(const char* jni_function_name, const char* fmt, ...)
86 __attribute__((__format__(__printf__, 3, 4)));
87
88 // If both "-Xcheck:jni" and "-Xjnitrace:" are enabled, we print trace messages
89 // when a native method that matches the -Xjnitrace argument calls a JNI function
90 // such as NewByteArray.
91 // If -verbose:third-party-jni is on, we want to log any JNI function calls
92 // made by a third-party native method.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070093 bool ShouldTrace(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070094
95 /**
96 * Loads the given shared library. 'path' is an absolute pathname.
97 *
Dmitriy Ivanovf5a30992015-11-11 14:18:55 -080098 * Returns 'true' on success. On failure, sets 'error_msg' to a
Ian Rogers68d8b422014-07-17 11:09:10 -070099 * human-readable description of the error.
100 */
Dimitry Ivanov942dc2982016-02-24 13:33:33 -0800101 bool LoadNativeLibrary(JNIEnv* env,
102 const std::string& path,
103 jobject class_loader,
Dimitry Ivanovd5bbadf2015-12-15 14:08:18 -0800104 std::string* error_msg);
Ian Rogers68d8b422014-07-17 11:09:10 -0700105
Mathieu Chartier598302a2015-09-23 14:52:39 -0700106 // Unload native libraries with cleared class loaders.
107 void UnloadNativeLibraries()
108 REQUIRES(!Locks::jni_libraries_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700109 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier598302a2015-09-23 14:52:39 -0700110
Ian Rogers68d8b422014-07-17 11:09:10 -0700111 /**
112 * Returns a pointer to the code for the native method 'm', found
113 * using dlsym(3) on every native library that's been loaded so far.
114 */
Mathieu Chartiere401d142015-04-22 13:56:20 -0700115 void* FindCodeForNativeMethod(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700116 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700117
118 void DumpForSigQuit(std::ostream& os)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700119 REQUIRES(!Locks::jni_libraries_lock_,
120 !Locks::jni_globals_lock_,
121 !Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700122
123 void DumpReferenceTables(std::ostream& os)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700124 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe8a2a1fc2017-09-29 17:53:18 -0700125 REQUIRES(!Locks::jni_globals_lock_,
126 !Locks::jni_weak_globals_lock_,
127 !Locks::alloc_tracker_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700128
129 bool SetCheckJniEnabled(bool enabled);
130
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700131 void VisitRoots(RootVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700132 REQUIRES(!Locks::jni_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700133
Andreas Gampe05a364c2016-10-14 13:27:12 -0700134 void DisallowNewWeakGlobals()
135 REQUIRES_SHARED(Locks::mutator_lock_)
136 REQUIRES(!Locks::jni_weak_globals_lock_);
137 void AllowNewWeakGlobals()
138 REQUIRES_SHARED(Locks::mutator_lock_)
139 REQUIRES(!Locks::jni_weak_globals_lock_);
140 void BroadcastForNewWeakGlobals()
Andreas Gampe05a364c2016-10-14 13:27:12 -0700141 REQUIRES(!Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700142
Mathieu Chartier0795f232016-09-27 18:43:30 -0700143 jobject AddGlobalRef(Thread* self, ObjPtr<mirror::Object> obj)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700144 REQUIRES_SHARED(Locks::mutator_lock_)
145 REQUIRES(!Locks::jni_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700146
Mathieu Chartier0795f232016-09-27 18:43:30 -0700147 jweak AddWeakGlobalRef(Thread* self, ObjPtr<mirror::Object> obj)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700148 REQUIRES_SHARED(Locks::mutator_lock_)
149 REQUIRES(!Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700150
Andreas Gampe05a364c2016-10-14 13:27:12 -0700151 void DeleteGlobalRef(Thread* self, jobject obj) REQUIRES(!Locks::jni_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700152
Andreas Gampe05a364c2016-10-14 13:27:12 -0700153 void DeleteWeakGlobalRef(Thread* self, jweak obj) REQUIRES(!Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700154
Mathieu Chartier97509952015-07-13 14:35:43 -0700155 void SweepJniWeakGlobals(IsMarkedVisitor* visitor)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700156 REQUIRES_SHARED(Locks::mutator_lock_)
157 REQUIRES(!Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700158
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700159 ObjPtr<mirror::Object> DecodeGlobal(IndirectRef ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700160 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700161
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700162 void UpdateGlobal(Thread* self, IndirectRef ref, ObjPtr<mirror::Object> result)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700163 REQUIRES_SHARED(Locks::mutator_lock_)
164 REQUIRES(!Locks::jni_globals_lock_);
Jeff Hao83c81952015-05-27 19:29:29 -0700165
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700166 ObjPtr<mirror::Object> DecodeWeakGlobal(Thread* self, IndirectRef ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700167 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700168 REQUIRES(!Locks::jni_weak_globals_lock_);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700169
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700170 ObjPtr<mirror::Object> DecodeWeakGlobalLocked(Thread* self, IndirectRef ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700171 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700172 REQUIRES(Locks::jni_weak_globals_lock_);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700173
Hiroshi Yamauchi498b1602015-09-16 21:11:44 -0700174 // Like DecodeWeakGlobal() but to be used only during a runtime shutdown where self may be
175 // null.
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700176 ObjPtr<mirror::Object> DecodeWeakGlobalDuringShutdown(Thread* self, IndirectRef ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700177 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700178 REQUIRES(!Locks::jni_weak_globals_lock_);
Hiroshi Yamauchi498b1602015-09-16 21:11:44 -0700179
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -0800180 // Checks if the weak global ref has been cleared by the GC without decode (read barrier.)
181 bool IsWeakGlobalCleared(Thread* self, IndirectRef ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700182 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700183 REQUIRES(!Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700184
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700185 void UpdateWeakGlobal(Thread* self, IndirectRef ref, ObjPtr<mirror::Object> result)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700186 REQUIRES_SHARED(Locks::mutator_lock_)
187 REQUIRES(!Locks::jni_weak_globals_lock_);
Jeff Hao83c81952015-05-27 19:29:29 -0700188
Ian Rogers68d8b422014-07-17 11:09:10 -0700189 const JNIInvokeInterface* GetUncheckedFunctions() const {
190 return unchecked_functions_;
191 }
192
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700193 void TrimGlobals() REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700194 REQUIRES(!Locks::jni_globals_lock_);
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800195
Alex Light185d1342016-08-11 10:48:03 -0700196 jint HandleGetEnv(/*out*/void** env, jint version);
197
198 void AddEnvironmentHook(GetEnvHook hook);
199
200 static bool IsBadJniVersion(int version);
201
Andreas Gampe473191c2017-12-28 16:55:31 -0800202 // Return the library search path for the given classloader, if the classloader is of a
203 // well-known type. The jobject will be a local reference and is expected to be managed by the
204 // caller.
205 static jstring GetLibrarySearchPath(JNIEnv* env, jobject class_loader);
206
Ian Rogers68d8b422014-07-17 11:09:10 -0700207 private:
Richard Uhlerda0a69e2016-10-11 15:06:38 +0100208 // The constructor should not be called directly. It may leave the object in
209 // an erroneous state, and the result needs to be checked.
210 JavaVMExt(Runtime* runtime, const RuntimeArgumentMap& runtime_options, std::string* error_msg);
211
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700212 // Return true if self can currently access weak globals.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700213 bool MayAccessWeakGlobalsUnlocked(Thread* self) const REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700214 bool MayAccessWeakGlobals(Thread* self) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700215 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700216 REQUIRES(Locks::jni_weak_globals_lock_);
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700217
Andreas Gampe1b35b462017-09-29 18:52:15 -0700218 void CheckGlobalRefAllocationTracking();
219
Ian Rogers68d8b422014-07-17 11:09:10 -0700220 Runtime* const runtime_;
221
222 // Used for testing. By default, we'll LOG(FATAL) the reason.
223 void (*check_jni_abort_hook_)(void* data, const std::string& reason);
224 void* check_jni_abort_hook_data_;
225
226 // Extra checking.
227 bool check_jni_;
Ian Rogersac4d45a2018-02-15 11:19:01 -0800228 const bool force_copy_;
Ian Rogers68d8b422014-07-17 11:09:10 -0700229 const bool tracing_enabled_;
230
231 // Extra diagnostics.
232 const std::string trace_;
233
Ian Rogers68d8b422014-07-17 11:09:10 -0700234 // Not guarded by globals_lock since we sometimes use SynchronizedGet in Thread::DecodeJObject.
235 IndirectReferenceTable globals_;
236
Mathieu Chartier598302a2015-09-23 14:52:39 -0700237 // No lock annotation since UnloadNativeLibraries is called on libraries_ but locks the
238 // jni_libraries_lock_ internally.
239 std::unique_ptr<Libraries> libraries_;
Ian Rogers68d8b422014-07-17 11:09:10 -0700240
241 // Used by -Xcheck:jni.
242 const JNIInvokeInterface* const unchecked_functions_;
243
Ian Rogers68d8b422014-07-17 11:09:10 -0700244 // Since weak_globals_ contain weak roots, be careful not to
245 // directly access the object references in it. Use Get() with the
246 // read barrier enabled.
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700247 // Not guarded by weak_globals_lock since we may use SynchronizedGet in DecodeWeakGlobal.
248 IndirectReferenceTable weak_globals_;
249 // Not guarded by weak_globals_lock since we may use SynchronizedGet in DecodeWeakGlobal.
250 Atomic<bool> allow_accessing_weak_globals_;
Andreas Gampe05a364c2016-10-14 13:27:12 -0700251 ConditionVariable weak_globals_add_condition_ GUARDED_BY(Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700252
Alex Light185d1342016-08-11 10:48:03 -0700253 // TODO Maybe move this to Runtime.
254 std::vector<GetEnvHook> env_hooks_;
255
Andreas Gampe1b35b462017-09-29 18:52:15 -0700256 size_t enable_allocation_tracking_delta_;
257 std::atomic<bool> allocation_tracking_enabled_;
258 std::atomic<bool> old_allocation_tracking_state_;
259
Ian Rogers68d8b422014-07-17 11:09:10 -0700260 DISALLOW_COPY_AND_ASSIGN(JavaVMExt);
261};
262
263} // namespace art
264
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +0100265#endif // ART_RUNTIME_JNI_JAVA_VM_EXT_H_