Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 27 | namespace art { |
| 28 | |
| 29 | namespace mirror { |
| 30 | class ArtMethod; |
| 31 | class Array; |
| 32 | } // namespace mirror |
| 33 | |
| 34 | class Libraries; |
| 35 | class ParsedOptions; |
| 36 | class Runtime; |
| 37 | |
| 38 | class 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) |
| 98 | LOCKS_EXCLUDED(Locks::jni_libraries_lock_, globals_lock_, weak_globals_lock_, pins_lock_); |
| 99 | |
| 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 | |
| 130 | void PinPrimitiveArray(Thread* self, mirror::Array* array) |
| 131 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 132 | LOCKS_EXCLUDED(pins_lock_); |
| 133 | |
| 134 | void UnpinPrimitiveArray(Thread* self, mirror::Array* array) |
| 135 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 136 | LOCKS_EXCLUDED(pins_lock_); |
| 137 | |
| 138 | const JNIInvokeInterface* GetUncheckedFunctions() const { |
| 139 | return unchecked_functions_; |
| 140 | } |
| 141 | |
| 142 | private: |
| 143 | Runtime* const runtime_; |
| 144 | |
| 145 | // Used for testing. By default, we'll LOG(FATAL) the reason. |
| 146 | void (*check_jni_abort_hook_)(void* data, const std::string& reason); |
| 147 | void* check_jni_abort_hook_data_; |
| 148 | |
| 149 | // Extra checking. |
| 150 | bool check_jni_; |
| 151 | bool force_copy_; |
| 152 | const bool tracing_enabled_; |
| 153 | |
| 154 | // Extra diagnostics. |
| 155 | const std::string trace_; |
| 156 | |
| 157 | // Used to hold references to pinned primitive arrays. |
| 158 | Mutex pins_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 159 | ReferenceTable pin_table_ GUARDED_BY(pins_lock_); |
| 160 | |
| 161 | // JNI global references. |
| 162 | ReaderWriterMutex globals_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 163 | // Not guarded by globals_lock since we sometimes use SynchronizedGet in Thread::DecodeJObject. |
| 164 | IndirectReferenceTable globals_; |
| 165 | |
| 166 | std::unique_ptr<Libraries> libraries_ GUARDED_BY(Locks::jni_libraries_lock_); |
| 167 | |
| 168 | // Used by -Xcheck:jni. |
| 169 | const JNIInvokeInterface* const unchecked_functions_; |
| 170 | |
| 171 | // JNI weak global references. |
| 172 | Mutex weak_globals_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 173 | // Since weak_globals_ contain weak roots, be careful not to |
| 174 | // directly access the object references in it. Use Get() with the |
| 175 | // read barrier enabled. |
| 176 | IndirectReferenceTable weak_globals_ GUARDED_BY(weak_globals_lock_); |
| 177 | bool allow_new_weak_globals_ GUARDED_BY(weak_globals_lock_); |
| 178 | ConditionVariable weak_globals_add_condition_ GUARDED_BY(weak_globals_lock_); |
| 179 | |
| 180 | DISALLOW_COPY_AND_ASSIGN(JavaVMExt); |
| 181 | }; |
| 182 | |
| 183 | } // namespace art |
| 184 | |
| 185 | #endif // ART_RUNTIME_JAVA_VM_EXT_H_ |