1 /*
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
17 #include "common_runtime_test.h"
18
19 #include <thread>
20
21 #include "android-base/logging.h"
22 #include "base/locks.h"
23 #include "base/mutex.h"
24 #include "runtime.h"
25 #include "thread-current-inl.h"
26
27 namespace art {
28
29 class RuntimeTest : public CommonRuntimeTest {};
30
31 // Ensure that abort works with ThreadList locks held.
32
TEST_F(RuntimeTest,AbortWithThreadListLockHeld)33 TEST_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
46
TEST_F(RuntimeTest,AbortWithThreadSuspendCountLockHeld)47 TEST_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
TEST_F(RuntimeTest,AbortFromUnattachedThread)60 TEST_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
78 } // namespace art
79