blob: 3fe281bd5fffc1448f0579c102066b72cc2a511d [file] [log] [blame]
Andreas Gampeba26b512019-05-31 13:07:26 -07001/*
2 * Copyright (C) 2019 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
Andreas Gampe3e2446b2019-06-12 10:00:57 -070017#include "common_runtime_test.h"
18
19#include <thread>
20
Andreas Gampeba26b512019-05-31 13:07:26 -070021#include "android-base/logging.h"
22#include "base/locks.h"
23#include "base/mutex.h"
Andreas Gampeba26b512019-05-31 13:07:26 -070024#include "runtime.h"
25#include "thread-current-inl.h"
26
27namespace art {
28
29class RuntimeTest : public CommonRuntimeTest {};
30
31// Ensure that abort works with ThreadList locks held.
32
33TEST_F(RuntimeTest, AbortWithThreadListLockHeld) {
34 // This assumes the test is run single-threaded: do not start the runtime to avoid daemon threads.
35
36 constexpr const char* kDeathRegex = "Skipping all-threads dump as locks are held";
37 ASSERT_DEATH({
38 // The regex only works if we can ensure output goes to stderr.
39 android::base::SetLogger(android::base::StderrLogger);
40
41 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
42 Runtime::Abort("Attempt to abort");
43 }, kDeathRegex);
44}
45
Andreas Gampe7d42cdd2019-05-31 16:54:46 -070046
47TEST_F(RuntimeTest, AbortWithThreadSuspendCountLockHeld) {
48 // This assumes the test is run single-threaded: do not start the runtime to avoid daemon threads.
49
50 constexpr const char* kDeathRegex = "Skipping all-threads dump as locks are held";
51 ASSERT_DEATH({
52 // The regex only works if we can ensure output goes to stderr.
53 android::base::SetLogger(android::base::StderrLogger);
54
55 MutexLock mu(Thread::Current(), *Locks::thread_suspend_count_lock_);
56 Runtime::Abort("Attempt to abort");
57 }, kDeathRegex);
58}
59
Andreas Gampe3e2446b2019-06-12 10:00:57 -070060TEST_F(RuntimeTest, AbortFromUnattachedThread) {
61 // This assumes the test is run single-threaded: do not start the runtime to avoid daemon threads.
62
63 constexpr const char* kDeathRegex = "Going down";
64 ASSERT_EXIT({
65 // The regex only works if we can ensure output goes to stderr.
66 android::base::SetLogger(android::base::StderrLogger);
67
68 Thread::Current()->TransitionFromSuspendedToRunnable();
69 runtime_->Start();
70
71 std::thread t([]() {
72 LOG(FATAL) << "Going down";
73 });
74 t.join();
75 }, ::testing::KilledBySignal(SIGABRT), kDeathRegex);
76}
77
Andreas Gampeba26b512019-05-31 13:07:26 -070078} // namespace art