blob: 4a60c045ccdab58e41ce0bb0479f36fc8fcef32d [file] [log] [blame]
Andreas Gampe04bbb5b2017-01-19 17:49:03 +00001/*
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 Gampea5814f92017-01-18 21:43:16 -080019#include <signal.h>
20#include <sys/types.h>
21#include <unistd.h>
Andreas Gampe0f01b582017-01-18 15:22:37 -080022
23#include <initializer_list>
Andreas Gampe04bbb5b2017-01-19 17:49:03 +000024#include <memory>
Igor Murashkin5573c372017-11-16 13:34:30 -080025#include <mutex>
Andreas Gampe04bbb5b2017-01-19 17:49:03 +000026#include <string>
27
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070028#include "jni.h"
29
Andreas Gampe04bbb5b2017-01-19 17:49:03 +000030#include "art_method-inl.h"
David Sehr79e26072018-04-06 17:58:50 -070031#include "base/mem_map.h"
Andreas Gampe04bbb5b2017-01-19 17:49:03 +000032#include "base/mutex.h"
Andreas Gampe0f01b582017-01-18 15:22:37 -080033#include "class_linker.h"
Andreas Gampe04bbb5b2017-01-19 17:49:03 +000034#include "common_runtime_test.h"
David Sehr312f3b22018-03-19 08:39:26 -070035#include "dex/class_reference.h"
Andreas Gampe0f01b582017-01-18 15:22:37 -080036#include "handle.h"
37#include "handle_scope-inl.h"
Andreas Gampe0f01b582017-01-18 15:22:37 -080038#include "mirror/class-inl.h"
39#include "mirror/class_loader.h"
Vladimir Markof52d92f2019-03-29 12:33:02 +000040#include "monitor-inl.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070041#include "nativehelper/scoped_local_ref.h"
Vladimir Markof52d92f2019-03-29 12:33:02 +000042#include "obj_ptr-inl.h"
Andreas Gampe04bbb5b2017-01-19 17:49:03 +000043#include "runtime.h"
44#include "scoped_thread_state_change-inl.h"
Andreas Gampe04bbb5b2017-01-19 17:49:03 +000045#include "thread-inl.h"
46#include "thread_list.h"
47#include "well_known_classes.h"
48
49namespace art {
50
51class RuntimeCallbacksTest : public CommonRuntimeTest {
52 protected:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010053 void SetUp() override {
Andreas Gampe04bbb5b2017-01-19 17:49:03 +000054 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 Levillainbbc6e7e2018-08-24 16:58:47 +010063 void TearDown() override {
Andreas Gampe04bbb5b2017-01-19 17:49:03 +000064 {
65 Thread* self = Thread::Current();
66 ScopedObjectAccess soa(self);
67 ScopedThreadSuspension sts(self, kWaitingForDebuggerToAttach);
68 ScopedSuspendAll ssa("RuntimeCallbacksTest TearDown");
Andreas Gampe04bbb5b2017-01-19 17:49:03 +000069 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
89class 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 Levillainbbc6e7e2018-08-24 16:58:47 +0100104 void AddListener() override REQUIRES(Locks::mutator_lock_) {
Andreas Gampeac30fa22017-01-18 21:02:36 -0800105 Runtime::Current()->GetRuntimeCallbacks()->AddThreadLifecycleCallback(&cb_);
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000106 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100107 void RemoveListener() override REQUIRES(Locks::mutator_lock_) {
Andreas Gampeac30fa22017-01-18 21:02:36 -0800108 Runtime::Current()->GetRuntimeCallbacks()->RemoveThreadLifecycleCallback(&cb_);
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000109 }
110
111 enum CallbackState {
112 kBase,
113 kStarted,
114 kDied,
115 kWrongStart,
116 kWrongDeath,
117 };
118
119 struct Callback : public ThreadLifecycleCallback {
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100120 void ThreadStart(Thread* self) override {
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000121 if (state == CallbackState::kBase) {
122 state = CallbackState::kStarted;
123 stored_self = self;
124 } else {
125 state = CallbackState::kWrongStart;
126 }
127 }
128
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100129 void ThreadDeath(Thread* self) override {
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000130 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 Gampe04bbb5b2017-01-19 17:49:03 +0000144TEST_F(ThreadLifecycleCallbackRuntimeCallbacksTest, ThreadLifecycleCallbackJava) {
145 Thread* self = Thread::Current();
146
147 self->TransitionFromSuspendedToRunnable();
148 bool started = runtime_->Start();
149 ASSERT_TRUE(started);
Mathieu Chartierada33d72018-12-17 13:17:30 -0800150 // Make sure the workers are done starting so we don't get callbacks for them.
151 runtime_->WaitForThreadPoolWorkersToStart();
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000152
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
193TEST_F(ThreadLifecycleCallbackRuntimeCallbacksTest, ThreadLifecycleCallbackAttach) {
194 std::string error_msg;
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100195 MemMap stack = MemMap::MapAnonymous("ThreadLifecycleCallback Thread",
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100196 128 * kPageSize, // Just some small stack.
197 PROT_READ | PROT_WRITE,
Vladimir Marko11306592018-10-26 14:22:59 +0100198 /*low_4gb=*/ false,
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100199 &error_msg);
200 ASSERT_TRUE(stack.IsValid()) << error_msg;
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000201
202 const char* reason = "ThreadLifecycleCallback test thread";
203 pthread_attr_t attr;
204 CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), reason);
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100205 CHECK_PTHREAD_CALL(pthread_attr_setstack, (&attr, stack.Begin(), stack.Size()), reason);
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000206 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 Gampe0f01b582017-01-18 15:22:37 -0800221class ClassLoadCallbackRuntimeCallbacksTest : public RuntimeCallbacksTest {
222 protected:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100223 void AddListener() override REQUIRES(Locks::mutator_lock_) {
Andreas Gampeac30fa22017-01-18 21:02:36 -0800224 Runtime::Current()->GetRuntimeCallbacks()->AddClassLoadCallback(&cb_);
Andreas Gampe0f01b582017-01-18 15:22:37 -0800225 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100226 void RemoveListener() override REQUIRES(Locks::mutator_lock_) {
Andreas Gampeac30fa22017-01-18 21:02:36 -0800227 Runtime::Current()->GetRuntimeCallbacks()->RemoveClassLoadCallback(&cb_);
Andreas Gampe0f01b582017-01-18 15:22:37 -0800228 }
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 Levillainf73caca2018-08-24 17:19:07 +0100256 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 Gampe3f1dcd32018-12-28 09:39:56 -0800260 const dex::ClassDef& initial_class_def ATTRIBUTE_UNUSED,
Roland Levillainf73caca2018-08-24 17:19:07 +0100261 /*out*/DexFile const** final_dex_file ATTRIBUTE_UNUSED,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800262 /*out*/dex::ClassDef const** final_class_def ATTRIBUTE_UNUSED) override
Roland Levillainf73caca2018-08-24 17:19:07 +0100263 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe641a4732017-08-24 13:21:35 -0700264 const std::string& location = initial_dex_file.GetLocation();
Alex Lightb0f11922017-01-23 14:25:17 -0800265 std::string event =
266 std::string("PreDefine:") + descriptor + " <" +
Andreas Gampe5555dd12017-08-24 13:50:21 -0700267 location.substr(location.rfind('/') + 1, location.size()) + ">";
Alex Lightb0f11922017-01-23 14:25:17 -0800268 data.push_back(event);
269 }
270
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100271 void ClassLoad(Handle<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe0f01b582017-01-18 15:22:37 -0800272 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 Levillainbbc6e7e2018-08-24 16:58:47 +0100278 Handle<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe0f01b582017-01-18 15:22:37 -0800279 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
291TEST_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 Gampefa4333d2017-02-14 11:10:34 -0800301 ASSERT_TRUE(h_Y != nullptr);
Andreas Gampe0f01b582017-01-18 15:22:37 -0800302
Alex Lightb0f11922017-01-23 14:25:17 -0800303 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 Gampe0f01b582017-01-18 15:22:37 -0800309 EXPECT_TRUE(expect1);
310
311 cb_.data.clear();
312
313 ASSERT_TRUE(class_linker_->EnsureInitialized(Thread::Current(), h_Y, true, true));
314
Alex Lightb0f11922017-01-23 14:25:17 -0800315 bool expect2 = Expect({ "PreDefine:LY$Z; <art-gtest-XandY.jar>",
316 "Load:LY$Z;",
317 "Prepare:LY$Z;[LY$Z;]" });
Andreas Gampe0f01b582017-01-18 15:22:37 -0800318 EXPECT_TRUE(expect2);
319}
320
Andreas Gampea5814f92017-01-18 21:43:16 -0800321class RuntimeSigQuitCallbackRuntimeCallbacksTest : public RuntimeCallbacksTest {
322 protected:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100323 void AddListener() override REQUIRES(Locks::mutator_lock_) {
Andreas Gampea5814f92017-01-18 21:43:16 -0800324 Runtime::Current()->GetRuntimeCallbacks()->AddRuntimeSigQuitCallback(&cb_);
325 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100326 void RemoveListener() override REQUIRES(Locks::mutator_lock_) {
Andreas Gampea5814f92017-01-18 21:43:16 -0800327 Runtime::Current()->GetRuntimeCallbacks()->RemoveRuntimeSigQuitCallback(&cb_);
328 }
329
330 struct Callback : public RuntimeSigQuitCallback {
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100331 void SigQuit() override {
Andreas Gampea5814f92017-01-18 21:43:16 -0800332 ++sigquit_count;
333 }
334
335 size_t sigquit_count = 0;
336 };
337
338 Callback cb_;
339};
340
341TEST_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 Gampe48864112017-01-19 17:23:17 -0800364class RuntimePhaseCallbackRuntimeCallbacksTest : public RuntimeCallbacksTest {
365 protected:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100366 void AddListener() override REQUIRES(Locks::mutator_lock_) {
Andreas Gampe48864112017-01-19 17:23:17 -0800367 Runtime::Current()->GetRuntimeCallbacks()->AddRuntimePhaseCallback(&cb_);
368 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100369 void RemoveListener() override REQUIRES(Locks::mutator_lock_) {
Andreas Gampe48864112017-01-19 17:23:17 -0800370 Runtime::Current()->GetRuntimeCallbacks()->RemoveRuntimePhaseCallback(&cb_);
371 }
372
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100373 void TearDown() override {
Andreas Gampe48864112017-01-19 17:23:17 -0800374 // Bypass RuntimeCallbacksTest::TearDown, as the runtime is already gone.
375 CommonRuntimeTest::TearDown();
376 }
377
378 struct Callback : public RuntimePhaseCallback {
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100379 void NextRuntimePhase(RuntimePhaseCallback::RuntimePhase p) override {
Andreas Gampe96eca782017-01-19 19:45:30 -0800380 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 Gampe48864112017-01-19 17:23:17 -0800387 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 Gampe96eca782017-01-19 19:45:30 -0800399 size_t initial_agents_seen = 0;
Andreas Gampe48864112017-01-19 17:23:17 -0800400 size_t start_seen = 0;
401 size_t init_seen = 0;
402 size_t death_seen = 0;
403 };
404
405 Callback cb_;
406};
407
408TEST_F(RuntimePhaseCallbackRuntimeCallbacksTest, Phases) {
Andreas Gampe96eca782017-01-19 19:45:30 -0800409 ASSERT_EQ(0u, cb_.initial_agents_seen);
Andreas Gampe48864112017-01-19 17:23:17 -0800410 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 Gampe96eca782017-01-19 19:45:30 -0800422 ASSERT_EQ(0u, cb_.initial_agents_seen);
Andreas Gampe48864112017-01-19 17:23:17 -0800423 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 Gampe96eca782017-01-19 19:45:30 -0800430 ASSERT_EQ(0u, cb_.initial_agents_seen);
Andreas Gampe48864112017-01-19 17:23:17 -0800431 ASSERT_EQ(1u, cb_.start_seen);
432 ASSERT_EQ(1u, cb_.init_seen);
433 ASSERT_EQ(1u, cb_.death_seen);
434}
435
Alex Light77fee872017-09-05 14:51:49 -0700436class MonitorWaitCallbacksTest : public RuntimeCallbacksTest {
437 protected:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100438 void AddListener() override REQUIRES(Locks::mutator_lock_) {
Alex Light77fee872017-09-05 14:51:49 -0700439 Runtime::Current()->GetRuntimeCallbacks()->AddMonitorCallback(&cb_);
440 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100441 void RemoveListener() override REQUIRES(Locks::mutator_lock_) {
Alex Light77fee872017-09-05 14:51:49 -0700442 Runtime::Current()->GetRuntimeCallbacks()->RemoveMonitorCallback(&cb_);
443 }
444
445 struct Callback : public MonitorCallback {
Vladimir Markof52d92f2019-03-29 12:33:02 +0000446 bool IsInterestingObject(ObjPtr<mirror::Object> obj)
447 REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Light77fee872017-09-05 14:51:49 -0700448 if (!obj->IsClass()) {
449 return false;
450 }
451 std::lock_guard<std::mutex> lock(ref_guard_);
Vladimir Marko4617d582019-03-28 13:48:31 +0000452 ObjPtr<mirror::Class> k = obj->AsClass();
Alex Light77fee872017-09-05 14:51:49 -0700453 ClassReference test = { &k->GetDexFile(), k->GetDexClassDefIndex() };
454 return ref_ == test;
455 }
456
Vladimir Markof52d92f2019-03-29 12:33:02 +0000457 void SetInterestingObject(ObjPtr<mirror::Object> obj)
458 REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Light77fee872017-09-05 14:51:49 -0700459 std::lock_guard<std::mutex> lock(ref_guard_);
Vladimir Marko4617d582019-03-28 13:48:31 +0000460 ObjPtr<mirror::Class> k = obj->AsClass();
Alex Light77fee872017-09-05 14:51:49 -0700461 ref_ = { &k->GetDexFile(), k->GetDexClassDefIndex() };
462 }
463
Andreas Gampefa6a1b02018-09-07 08:11:55 -0700464 void MonitorContendedLocking(Monitor* mon ATTRIBUTE_UNUSED) override
Alex Light77fee872017-09-05 14:51:49 -0700465 REQUIRES_SHARED(Locks::mutator_lock_) { }
466
Andreas Gampefa6a1b02018-09-07 08:11:55 -0700467 void MonitorContendedLocked(Monitor* mon ATTRIBUTE_UNUSED) override
Alex Light77fee872017-09-05 14:51:49 -0700468 REQUIRES_SHARED(Locks::mutator_lock_) { }
469
Andreas Gampefa6a1b02018-09-07 08:11:55 -0700470 void ObjectWaitStart(Handle<mirror::Object> obj, int64_t millis ATTRIBUTE_UNUSED) override
Alex Light77fee872017-09-05 14:51:49 -0700471 REQUIRES_SHARED(Locks::mutator_lock_) {
472 if (IsInterestingObject(obj.Get())) {
473 saw_wait_start_ = true;
474 }
475 }
476
Andreas Gampefa6a1b02018-09-07 08:11:55 -0700477 void MonitorWaitFinished(Monitor* m, bool timed_out ATTRIBUTE_UNUSED) override
Alex Light77fee872017-09-05 14:51:49 -0700478 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.
495TEST_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 Markof52d92f2019-03-29 12:33:02 +0000506 soa.Decode<mirror::Class>(WellKnownClasses::java_util_Collections));
Alex Light77fee872017-09-05 14:51:49 -0700507 Monitor::Wait(
508 self,
509 // Just a random class
Vladimir Markof52d92f2019-03-29 12:33:02 +0000510 soa.Decode<mirror::Class>(WellKnownClasses::java_util_Collections),
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700511 /*ms=*/0,
512 /*ns=*/0,
513 /*interruptShouldThrow=*/false,
514 /*why=*/kWaiting);
Alex Light77fee872017-09-05 14:51:49 -0700515 }
516 }
517 ASSERT_TRUE(cb_.saw_wait_start_);
518 ASSERT_FALSE(cb_.saw_wait_finished_);
519}
520
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000521} // namespace art