Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #include "runtime_callbacks.h" |
| 18 | |
Andreas Gampe | a5814f9 | 2017-01-18 21:43:16 -0800 | [diff] [blame] | 19 | #include <signal.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <unistd.h> |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 22 | |
| 23 | #include <initializer_list> |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 24 | #include <memory> |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 25 | #include <mutex> |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 26 | #include <string> |
| 27 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 28 | #include "jni.h" |
| 29 | |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 30 | #include "art_method-inl.h" |
David Sehr | 79e2607 | 2018-04-06 17:58:50 -0700 | [diff] [blame] | 31 | #include "base/mem_map.h" |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 32 | #include "base/mutex.h" |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 33 | #include "class_linker.h" |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 34 | #include "common_runtime_test.h" |
David Sehr | 312f3b2 | 2018-03-19 08:39:26 -0700 | [diff] [blame] | 35 | #include "dex/class_reference.h" |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 36 | #include "handle.h" |
| 37 | #include "handle_scope-inl.h" |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 38 | #include "mirror/class-inl.h" |
| 39 | #include "mirror/class_loader.h" |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 40 | #include "monitor-inl.h" |
Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 41 | #include "nativehelper/scoped_local_ref.h" |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 42 | #include "obj_ptr-inl.h" |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 43 | #include "runtime.h" |
| 44 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 45 | #include "thread-inl.h" |
| 46 | #include "thread_list.h" |
| 47 | #include "well_known_classes.h" |
| 48 | |
| 49 | namespace art { |
| 50 | |
| 51 | class RuntimeCallbacksTest : public CommonRuntimeTest { |
| 52 | protected: |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 53 | void SetUp() override { |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 54 | CommonRuntimeTest::SetUp(); |
| 55 | |
| 56 | Thread* self = Thread::Current(); |
| 57 | ScopedObjectAccess soa(self); |
| 58 | ScopedThreadSuspension sts(self, kWaitingForDebuggerToAttach); |
| 59 | ScopedSuspendAll ssa("RuntimeCallbacksTest SetUp"); |
| 60 | AddListener(); |
| 61 | } |
| 62 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 63 | void TearDown() override { |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 64 | { |
| 65 | Thread* self = Thread::Current(); |
| 66 | ScopedObjectAccess soa(self); |
| 67 | ScopedThreadSuspension sts(self, kWaitingForDebuggerToAttach); |
| 68 | ScopedSuspendAll ssa("RuntimeCallbacksTest TearDown"); |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 69 | RemoveListener(); |
| 70 | } |
| 71 | |
| 72 | CommonRuntimeTest::TearDown(); |
| 73 | } |
| 74 | |
| 75 | virtual void AddListener() REQUIRES(Locks::mutator_lock_) = 0; |
| 76 | virtual void RemoveListener() REQUIRES(Locks::mutator_lock_) = 0; |
| 77 | |
| 78 | void MakeExecutable(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 79 | CHECK(klass != nullptr); |
| 80 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 81 | for (auto& m : klass->GetMethods(pointer_size)) { |
| 82 | if (!m.IsAbstract()) { |
| 83 | class_linker_->SetEntryPointsToInterpreter(&m); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | class ThreadLifecycleCallbackRuntimeCallbacksTest : public RuntimeCallbacksTest { |
| 90 | public: |
| 91 | static void* PthreadsCallback(void* arg ATTRIBUTE_UNUSED) { |
| 92 | // Attach. |
| 93 | Runtime* runtime = Runtime::Current(); |
| 94 | CHECK(runtime->AttachCurrentThread("ThreadLifecycle test thread", true, nullptr, false)); |
| 95 | |
| 96 | // Detach. |
| 97 | runtime->DetachCurrentThread(); |
| 98 | |
| 99 | // Die... |
| 100 | return nullptr; |
| 101 | } |
| 102 | |
| 103 | protected: |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 104 | void AddListener() override REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 105 | Runtime::Current()->GetRuntimeCallbacks()->AddThreadLifecycleCallback(&cb_); |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 106 | } |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 107 | void RemoveListener() override REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 108 | Runtime::Current()->GetRuntimeCallbacks()->RemoveThreadLifecycleCallback(&cb_); |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | enum CallbackState { |
| 112 | kBase, |
| 113 | kStarted, |
| 114 | kDied, |
| 115 | kWrongStart, |
| 116 | kWrongDeath, |
| 117 | }; |
| 118 | |
| 119 | struct Callback : public ThreadLifecycleCallback { |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 120 | void ThreadStart(Thread* self) override { |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 121 | if (state == CallbackState::kBase) { |
| 122 | state = CallbackState::kStarted; |
| 123 | stored_self = self; |
| 124 | } else { |
| 125 | state = CallbackState::kWrongStart; |
| 126 | } |
| 127 | } |
| 128 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 129 | void ThreadDeath(Thread* self) override { |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 130 | if (state == CallbackState::kStarted && self == stored_self) { |
| 131 | state = CallbackState::kDied; |
| 132 | } else { |
| 133 | state = CallbackState::kWrongDeath; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | Thread* stored_self; |
| 138 | CallbackState state = CallbackState::kBase; |
| 139 | }; |
| 140 | |
| 141 | Callback cb_; |
| 142 | }; |
| 143 | |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 144 | TEST_F(ThreadLifecycleCallbackRuntimeCallbacksTest, ThreadLifecycleCallbackJava) { |
| 145 | Thread* self = Thread::Current(); |
| 146 | |
| 147 | self->TransitionFromSuspendedToRunnable(); |
| 148 | bool started = runtime_->Start(); |
| 149 | ASSERT_TRUE(started); |
Mathieu Chartier | ada33d7 | 2018-12-17 13:17:30 -0800 | [diff] [blame] | 150 | // Make sure the workers are done starting so we don't get callbacks for them. |
| 151 | runtime_->WaitForThreadPoolWorkersToStart(); |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 152 | |
| 153 | cb_.state = CallbackState::kBase; // Ignore main thread attach. |
| 154 | |
| 155 | { |
| 156 | ScopedObjectAccess soa(self); |
| 157 | MakeExecutable(soa.Decode<mirror::Class>(WellKnownClasses::java_lang_Thread)); |
| 158 | } |
| 159 | |
| 160 | JNIEnv* env = self->GetJniEnv(); |
| 161 | |
| 162 | ScopedLocalRef<jobject> thread_name(env, |
| 163 | env->NewStringUTF("ThreadLifecycleCallback test thread")); |
| 164 | ASSERT_TRUE(thread_name.get() != nullptr); |
| 165 | |
| 166 | ScopedLocalRef<jobject> thread(env, env->AllocObject(WellKnownClasses::java_lang_Thread)); |
| 167 | ASSERT_TRUE(thread.get() != nullptr); |
| 168 | |
| 169 | env->CallNonvirtualVoidMethod(thread.get(), |
| 170 | WellKnownClasses::java_lang_Thread, |
| 171 | WellKnownClasses::java_lang_Thread_init, |
| 172 | runtime_->GetMainThreadGroup(), |
| 173 | thread_name.get(), |
| 174 | kMinThreadPriority, |
| 175 | JNI_FALSE); |
| 176 | ASSERT_FALSE(env->ExceptionCheck()); |
| 177 | |
| 178 | jmethodID start_id = env->GetMethodID(WellKnownClasses::java_lang_Thread, "start", "()V"); |
| 179 | ASSERT_TRUE(start_id != nullptr); |
| 180 | |
| 181 | env->CallVoidMethod(thread.get(), start_id); |
| 182 | ASSERT_FALSE(env->ExceptionCheck()); |
| 183 | |
| 184 | jmethodID join_id = env->GetMethodID(WellKnownClasses::java_lang_Thread, "join", "()V"); |
| 185 | ASSERT_TRUE(join_id != nullptr); |
| 186 | |
| 187 | env->CallVoidMethod(thread.get(), join_id); |
| 188 | ASSERT_FALSE(env->ExceptionCheck()); |
| 189 | |
| 190 | EXPECT_TRUE(cb_.state == CallbackState::kDied) << static_cast<int>(cb_.state); |
| 191 | } |
| 192 | |
| 193 | TEST_F(ThreadLifecycleCallbackRuntimeCallbacksTest, ThreadLifecycleCallbackAttach) { |
| 194 | std::string error_msg; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 195 | MemMap stack = MemMap::MapAnonymous("ThreadLifecycleCallback Thread", |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 196 | 128 * kPageSize, // Just some small stack. |
| 197 | PROT_READ | PROT_WRITE, |
Vladimir Marko | 1130659 | 2018-10-26 14:22:59 +0100 | [diff] [blame] | 198 | /*low_4gb=*/ false, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 199 | &error_msg); |
| 200 | ASSERT_TRUE(stack.IsValid()) << error_msg; |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 201 | |
| 202 | const char* reason = "ThreadLifecycleCallback test thread"; |
| 203 | pthread_attr_t attr; |
| 204 | CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), reason); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 205 | CHECK_PTHREAD_CALL(pthread_attr_setstack, (&attr, stack.Begin(), stack.Size()), reason); |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 206 | pthread_t pthread; |
| 207 | CHECK_PTHREAD_CALL(pthread_create, |
| 208 | (&pthread, |
| 209 | &attr, |
| 210 | &ThreadLifecycleCallbackRuntimeCallbacksTest::PthreadsCallback, |
| 211 | this), |
| 212 | reason); |
| 213 | CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), reason); |
| 214 | |
| 215 | CHECK_PTHREAD_CALL(pthread_join, (pthread, nullptr), "ThreadLifecycleCallback test shutdown"); |
| 216 | |
| 217 | // Detach is not a ThreadDeath event, so we expect to be in state Started. |
| 218 | EXPECT_TRUE(cb_.state == CallbackState::kStarted) << static_cast<int>(cb_.state); |
| 219 | } |
| 220 | |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 221 | class ClassLoadCallbackRuntimeCallbacksTest : public RuntimeCallbacksTest { |
| 222 | protected: |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 223 | void AddListener() override REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 224 | Runtime::Current()->GetRuntimeCallbacks()->AddClassLoadCallback(&cb_); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 225 | } |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 226 | void RemoveListener() override REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 227 | Runtime::Current()->GetRuntimeCallbacks()->RemoveClassLoadCallback(&cb_); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | bool Expect(std::initializer_list<const char*> list) { |
| 231 | if (cb_.data.size() != list.size()) { |
| 232 | PrintError(list); |
| 233 | return false; |
| 234 | } |
| 235 | |
| 236 | if (!std::equal(cb_.data.begin(), cb_.data.end(), list.begin())) { |
| 237 | PrintError(list); |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | return true; |
| 242 | } |
| 243 | |
| 244 | void PrintError(std::initializer_list<const char*> list) { |
| 245 | LOG(ERROR) << "Expected:"; |
| 246 | for (const char* expected : list) { |
| 247 | LOG(ERROR) << " " << expected; |
| 248 | } |
| 249 | LOG(ERROR) << "Found:"; |
| 250 | for (const auto& s : cb_.data) { |
| 251 | LOG(ERROR) << " " << s; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | struct Callback : public ClassLoadCallback { |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 256 | void ClassPreDefine(const char* descriptor, |
| 257 | Handle<mirror::Class> klass ATTRIBUTE_UNUSED, |
| 258 | Handle<mirror::ClassLoader> class_loader ATTRIBUTE_UNUSED, |
| 259 | const DexFile& initial_dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 260 | const dex::ClassDef& initial_class_def ATTRIBUTE_UNUSED, |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 261 | /*out*/DexFile const** final_dex_file ATTRIBUTE_UNUSED, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 262 | /*out*/dex::ClassDef const** final_class_def ATTRIBUTE_UNUSED) override |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 263 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 641a473 | 2017-08-24 13:21:35 -0700 | [diff] [blame] | 264 | const std::string& location = initial_dex_file.GetLocation(); |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 265 | std::string event = |
| 266 | std::string("PreDefine:") + descriptor + " <" + |
Andreas Gampe | 5555dd1 | 2017-08-24 13:50:21 -0700 | [diff] [blame] | 267 | location.substr(location.rfind('/') + 1, location.size()) + ">"; |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 268 | data.push_back(event); |
| 269 | } |
| 270 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 271 | void ClassLoad(Handle<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 272 | std::string tmp; |
| 273 | std::string event = std::string("Load:") + klass->GetDescriptor(&tmp); |
| 274 | data.push_back(event); |
| 275 | } |
| 276 | |
| 277 | void ClassPrepare(Handle<mirror::Class> temp_klass, |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 278 | Handle<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 279 | std::string tmp, tmp2; |
| 280 | std::string event = std::string("Prepare:") + klass->GetDescriptor(&tmp) |
| 281 | + "[" + temp_klass->GetDescriptor(&tmp2) + "]"; |
| 282 | data.push_back(event); |
| 283 | } |
| 284 | |
| 285 | std::vector<std::string> data; |
| 286 | }; |
| 287 | |
| 288 | Callback cb_; |
| 289 | }; |
| 290 | |
| 291 | TEST_F(ClassLoadCallbackRuntimeCallbacksTest, ClassLoadCallback) { |
| 292 | ScopedObjectAccess soa(Thread::Current()); |
| 293 | jobject jclass_loader = LoadDex("XandY"); |
| 294 | VariableSizedHandleScope hs(soa.Self()); |
| 295 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle( |
| 296 | soa.Decode<mirror::ClassLoader>(jclass_loader))); |
| 297 | |
| 298 | const char* descriptor_y = "LY;"; |
| 299 | Handle<mirror::Class> h_Y( |
| 300 | hs.NewHandle(class_linker_->FindClass(soa.Self(), descriptor_y, class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 301 | ASSERT_TRUE(h_Y != nullptr); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 302 | |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 303 | bool expect1 = Expect({ "PreDefine:LY; <art-gtest-XandY.jar>", |
| 304 | "PreDefine:LX; <art-gtest-XandY.jar>", |
| 305 | "Load:LX;", |
| 306 | "Prepare:LX;[LX;]", |
| 307 | "Load:LY;", |
| 308 | "Prepare:LY;[LY;]" }); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 309 | EXPECT_TRUE(expect1); |
| 310 | |
| 311 | cb_.data.clear(); |
| 312 | |
| 313 | ASSERT_TRUE(class_linker_->EnsureInitialized(Thread::Current(), h_Y, true, true)); |
| 314 | |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 315 | bool expect2 = Expect({ "PreDefine:LY$Z; <art-gtest-XandY.jar>", |
| 316 | "Load:LY$Z;", |
| 317 | "Prepare:LY$Z;[LY$Z;]" }); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 318 | EXPECT_TRUE(expect2); |
| 319 | } |
| 320 | |
Andreas Gampe | a5814f9 | 2017-01-18 21:43:16 -0800 | [diff] [blame] | 321 | class RuntimeSigQuitCallbackRuntimeCallbacksTest : public RuntimeCallbacksTest { |
| 322 | protected: |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 323 | void AddListener() override REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | a5814f9 | 2017-01-18 21:43:16 -0800 | [diff] [blame] | 324 | Runtime::Current()->GetRuntimeCallbacks()->AddRuntimeSigQuitCallback(&cb_); |
| 325 | } |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 326 | void RemoveListener() override REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | a5814f9 | 2017-01-18 21:43:16 -0800 | [diff] [blame] | 327 | Runtime::Current()->GetRuntimeCallbacks()->RemoveRuntimeSigQuitCallback(&cb_); |
| 328 | } |
| 329 | |
| 330 | struct Callback : public RuntimeSigQuitCallback { |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 331 | void SigQuit() override { |
Andreas Gampe | a5814f9 | 2017-01-18 21:43:16 -0800 | [diff] [blame] | 332 | ++sigquit_count; |
| 333 | } |
| 334 | |
| 335 | size_t sigquit_count = 0; |
| 336 | }; |
| 337 | |
| 338 | Callback cb_; |
| 339 | }; |
| 340 | |
| 341 | TEST_F(RuntimeSigQuitCallbackRuntimeCallbacksTest, SigQuit) { |
| 342 | // The runtime needs to be started for the signal handler. |
| 343 | Thread* self = Thread::Current(); |
| 344 | |
| 345 | self->TransitionFromSuspendedToRunnable(); |
| 346 | bool started = runtime_->Start(); |
| 347 | ASSERT_TRUE(started); |
| 348 | |
| 349 | EXPECT_EQ(0u, cb_.sigquit_count); |
| 350 | |
| 351 | kill(getpid(), SIGQUIT); |
| 352 | |
| 353 | // Try a few times. |
| 354 | for (size_t i = 0; i != 30; ++i) { |
| 355 | if (cb_.sigquit_count == 0) { |
| 356 | sleep(1); |
| 357 | } else { |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | EXPECT_EQ(1u, cb_.sigquit_count); |
| 362 | } |
| 363 | |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame] | 364 | class RuntimePhaseCallbackRuntimeCallbacksTest : public RuntimeCallbacksTest { |
| 365 | protected: |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 366 | void AddListener() override REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame] | 367 | Runtime::Current()->GetRuntimeCallbacks()->AddRuntimePhaseCallback(&cb_); |
| 368 | } |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 369 | void RemoveListener() override REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame] | 370 | Runtime::Current()->GetRuntimeCallbacks()->RemoveRuntimePhaseCallback(&cb_); |
| 371 | } |
| 372 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 373 | void TearDown() override { |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame] | 374 | // Bypass RuntimeCallbacksTest::TearDown, as the runtime is already gone. |
| 375 | CommonRuntimeTest::TearDown(); |
| 376 | } |
| 377 | |
| 378 | struct Callback : public RuntimePhaseCallback { |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 379 | void NextRuntimePhase(RuntimePhaseCallback::RuntimePhase p) override { |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 380 | if (p == RuntimePhaseCallback::RuntimePhase::kInitialAgents) { |
| 381 | if (start_seen > 0 || init_seen > 0 || death_seen > 0) { |
| 382 | LOG(FATAL) << "Unexpected order"; |
| 383 | } |
| 384 | ++initial_agents_seen; |
| 385 | } else if (p == RuntimePhaseCallback::RuntimePhase::kStart) { |
| 386 | if (init_seen > 0 || death_seen > 0) { |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame] | 387 | LOG(FATAL) << "Init seen before start."; |
| 388 | } |
| 389 | ++start_seen; |
| 390 | } else if (p == RuntimePhaseCallback::RuntimePhase::kInit) { |
| 391 | ++init_seen; |
| 392 | } else if (p == RuntimePhaseCallback::RuntimePhase::kDeath) { |
| 393 | ++death_seen; |
| 394 | } else { |
| 395 | LOG(FATAL) << "Unknown phase " << static_cast<uint32_t>(p); |
| 396 | } |
| 397 | } |
| 398 | |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 399 | size_t initial_agents_seen = 0; |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame] | 400 | size_t start_seen = 0; |
| 401 | size_t init_seen = 0; |
| 402 | size_t death_seen = 0; |
| 403 | }; |
| 404 | |
| 405 | Callback cb_; |
| 406 | }; |
| 407 | |
| 408 | TEST_F(RuntimePhaseCallbackRuntimeCallbacksTest, Phases) { |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 409 | ASSERT_EQ(0u, cb_.initial_agents_seen); |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame] | 410 | ASSERT_EQ(0u, cb_.start_seen); |
| 411 | ASSERT_EQ(0u, cb_.init_seen); |
| 412 | ASSERT_EQ(0u, cb_.death_seen); |
| 413 | |
| 414 | // Start the runtime. |
| 415 | { |
| 416 | Thread* self = Thread::Current(); |
| 417 | self->TransitionFromSuspendedToRunnable(); |
| 418 | bool started = runtime_->Start(); |
| 419 | ASSERT_TRUE(started); |
| 420 | } |
| 421 | |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 422 | ASSERT_EQ(0u, cb_.initial_agents_seen); |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame] | 423 | ASSERT_EQ(1u, cb_.start_seen); |
| 424 | ASSERT_EQ(1u, cb_.init_seen); |
| 425 | ASSERT_EQ(0u, cb_.death_seen); |
| 426 | |
| 427 | // Delete the runtime. |
| 428 | runtime_.reset(); |
| 429 | |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 430 | ASSERT_EQ(0u, cb_.initial_agents_seen); |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame] | 431 | ASSERT_EQ(1u, cb_.start_seen); |
| 432 | ASSERT_EQ(1u, cb_.init_seen); |
| 433 | ASSERT_EQ(1u, cb_.death_seen); |
| 434 | } |
| 435 | |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 436 | class MonitorWaitCallbacksTest : public RuntimeCallbacksTest { |
| 437 | protected: |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 438 | void AddListener() override REQUIRES(Locks::mutator_lock_) { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 439 | Runtime::Current()->GetRuntimeCallbacks()->AddMonitorCallback(&cb_); |
| 440 | } |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 441 | void RemoveListener() override REQUIRES(Locks::mutator_lock_) { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 442 | Runtime::Current()->GetRuntimeCallbacks()->RemoveMonitorCallback(&cb_); |
| 443 | } |
| 444 | |
| 445 | struct Callback : public MonitorCallback { |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 446 | bool IsInterestingObject(ObjPtr<mirror::Object> obj) |
| 447 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 448 | if (!obj->IsClass()) { |
| 449 | return false; |
| 450 | } |
| 451 | std::lock_guard<std::mutex> lock(ref_guard_); |
Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 452 | ObjPtr<mirror::Class> k = obj->AsClass(); |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 453 | ClassReference test = { &k->GetDexFile(), k->GetDexClassDefIndex() }; |
| 454 | return ref_ == test; |
| 455 | } |
| 456 | |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 457 | void SetInterestingObject(ObjPtr<mirror::Object> obj) |
| 458 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 459 | std::lock_guard<std::mutex> lock(ref_guard_); |
Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 460 | ObjPtr<mirror::Class> k = obj->AsClass(); |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 461 | ref_ = { &k->GetDexFile(), k->GetDexClassDefIndex() }; |
| 462 | } |
| 463 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 464 | void MonitorContendedLocking(Monitor* mon ATTRIBUTE_UNUSED) override |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 465 | REQUIRES_SHARED(Locks::mutator_lock_) { } |
| 466 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 467 | void MonitorContendedLocked(Monitor* mon ATTRIBUTE_UNUSED) override |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 468 | REQUIRES_SHARED(Locks::mutator_lock_) { } |
| 469 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 470 | void ObjectWaitStart(Handle<mirror::Object> obj, int64_t millis ATTRIBUTE_UNUSED) override |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 471 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 472 | if (IsInterestingObject(obj.Get())) { |
| 473 | saw_wait_start_ = true; |
| 474 | } |
| 475 | } |
| 476 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 477 | void MonitorWaitFinished(Monitor* m, bool timed_out ATTRIBUTE_UNUSED) override |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 478 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 479 | if (IsInterestingObject(m->GetObject())) { |
| 480 | saw_wait_finished_ = true; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | std::mutex ref_guard_; |
| 485 | ClassReference ref_ = {nullptr, 0}; |
| 486 | bool saw_wait_start_ = false; |
| 487 | bool saw_wait_finished_ = false; |
| 488 | }; |
| 489 | |
| 490 | Callback cb_; |
| 491 | }; |
| 492 | |
| 493 | // TODO It would be good to have more tests for this but due to the multi-threaded nature of the |
| 494 | // callbacks this is difficult. For now the run-tests 1931 & 1932 should be sufficient. |
| 495 | TEST_F(MonitorWaitCallbacksTest, WaitUnlocked) { |
| 496 | ASSERT_FALSE(cb_.saw_wait_finished_); |
| 497 | ASSERT_FALSE(cb_.saw_wait_start_); |
| 498 | { |
| 499 | Thread* self = Thread::Current(); |
| 500 | self->TransitionFromSuspendedToRunnable(); |
| 501 | bool started = runtime_->Start(); |
| 502 | ASSERT_TRUE(started); |
| 503 | { |
| 504 | ScopedObjectAccess soa(self); |
| 505 | cb_.SetInterestingObject( |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 506 | soa.Decode<mirror::Class>(WellKnownClasses::java_util_Collections)); |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 507 | Monitor::Wait( |
| 508 | self, |
| 509 | // Just a random class |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 510 | soa.Decode<mirror::Class>(WellKnownClasses::java_util_Collections), |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 511 | /*ms=*/0, |
| 512 | /*ns=*/0, |
| 513 | /*interruptShouldThrow=*/false, |
| 514 | /*why=*/kWaiting); |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | ASSERT_TRUE(cb_.saw_wait_start_); |
| 518 | ASSERT_FALSE(cb_.saw_wait_finished_); |
| 519 | } |
| 520 | |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 521 | } // namespace art |