xref: /aosp_15_r20/art/runtime/runtime_test.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2019 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "common_runtime_test.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include <thread>
20*795d594fSAndroid Build Coastguard Worker 
21*795d594fSAndroid Build Coastguard Worker #include "android-base/logging.h"
22*795d594fSAndroid Build Coastguard Worker #include "base/locks.h"
23*795d594fSAndroid Build Coastguard Worker #include "base/mutex.h"
24*795d594fSAndroid Build Coastguard Worker #include "oat/elf_file.h"
25*795d594fSAndroid Build Coastguard Worker #include "runtime.h"
26*795d594fSAndroid Build Coastguard Worker #include "thread-current-inl.h"
27*795d594fSAndroid Build Coastguard Worker 
28*795d594fSAndroid Build Coastguard Worker #ifdef ART_TARGET_ANDROID
29*795d594fSAndroid Build Coastguard Worker #include "android-base/properties.h"
30*795d594fSAndroid Build Coastguard Worker #endif
31*795d594fSAndroid Build Coastguard Worker 
32*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
33*795d594fSAndroid Build Coastguard Worker 
34*795d594fSAndroid Build Coastguard Worker class RuntimeTest : public CommonRuntimeTest {};
35*795d594fSAndroid Build Coastguard Worker 
36*795d594fSAndroid Build Coastguard Worker // Ensure that abort works with ThreadList locks held.
37*795d594fSAndroid Build Coastguard Worker 
TEST_F(RuntimeTest,AbortWithThreadListLockHeld)38*795d594fSAndroid Build Coastguard Worker TEST_F(RuntimeTest, AbortWithThreadListLockHeld) {
39*795d594fSAndroid Build Coastguard Worker   // This assumes the test is run single-threaded: do not start the runtime to avoid daemon threads.
40*795d594fSAndroid Build Coastguard Worker 
41*795d594fSAndroid Build Coastguard Worker   constexpr const char* kDeathRegex = "Skipping all-threads dump as locks are held";
42*795d594fSAndroid Build Coastguard Worker   ASSERT_DEATH({
43*795d594fSAndroid Build Coastguard Worker     // The regex only works if we can ensure output goes to stderr.
44*795d594fSAndroid Build Coastguard Worker     android::base::SetLogger(android::base::StderrLogger);
45*795d594fSAndroid Build Coastguard Worker 
46*795d594fSAndroid Build Coastguard Worker     MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
47*795d594fSAndroid Build Coastguard Worker     Runtime::Abort("Attempt to abort");
48*795d594fSAndroid Build Coastguard Worker   }, kDeathRegex);
49*795d594fSAndroid Build Coastguard Worker }
50*795d594fSAndroid Build Coastguard Worker 
51*795d594fSAndroid Build Coastguard Worker 
TEST_F(RuntimeTest,AbortWithThreadSuspendCountLockHeld)52*795d594fSAndroid Build Coastguard Worker TEST_F(RuntimeTest, AbortWithThreadSuspendCountLockHeld) {
53*795d594fSAndroid Build Coastguard Worker   // This assumes the test is run single-threaded: do not start the runtime to avoid daemon threads.
54*795d594fSAndroid Build Coastguard Worker 
55*795d594fSAndroid Build Coastguard Worker   constexpr const char* kDeathRegex = "Skipping all-threads dump as locks are held";
56*795d594fSAndroid Build Coastguard Worker   ASSERT_DEATH({
57*795d594fSAndroid Build Coastguard Worker     // The regex only works if we can ensure output goes to stderr.
58*795d594fSAndroid Build Coastguard Worker     android::base::SetLogger(android::base::StderrLogger);
59*795d594fSAndroid Build Coastguard Worker 
60*795d594fSAndroid Build Coastguard Worker     MutexLock mu(Thread::Current(), *Locks::thread_suspend_count_lock_);
61*795d594fSAndroid Build Coastguard Worker     Runtime::Abort("Attempt to abort");
62*795d594fSAndroid Build Coastguard Worker   }, kDeathRegex);
63*795d594fSAndroid Build Coastguard Worker }
64*795d594fSAndroid Build Coastguard Worker 
TEST_F(RuntimeTest,AbortFromUnattachedThread)65*795d594fSAndroid Build Coastguard Worker TEST_F(RuntimeTest, AbortFromUnattachedThread) {
66*795d594fSAndroid Build Coastguard Worker   // This assumes the test is run single-threaded: do not start the runtime to avoid daemon threads.
67*795d594fSAndroid Build Coastguard Worker 
68*795d594fSAndroid Build Coastguard Worker   constexpr const char* kDeathRegex = "Going down";
69*795d594fSAndroid Build Coastguard Worker   ASSERT_EXIT({
70*795d594fSAndroid Build Coastguard Worker     // The regex only works if we can ensure output goes to stderr.
71*795d594fSAndroid Build Coastguard Worker     android::base::SetLogger(android::base::StderrLogger);
72*795d594fSAndroid Build Coastguard Worker 
73*795d594fSAndroid Build Coastguard Worker     Thread::Current()->TransitionFromSuspendedToRunnable();
74*795d594fSAndroid Build Coastguard Worker     runtime_->Start();
75*795d594fSAndroid Build Coastguard Worker 
76*795d594fSAndroid Build Coastguard Worker     std::thread t([]() {
77*795d594fSAndroid Build Coastguard Worker       LOG(FATAL) << "Going down";
78*795d594fSAndroid Build Coastguard Worker     });
79*795d594fSAndroid Build Coastguard Worker     t.join();
80*795d594fSAndroid Build Coastguard Worker   }, ::testing::KilledBySignal(SIGABRT), kDeathRegex);
81*795d594fSAndroid Build Coastguard Worker }
82*795d594fSAndroid Build Coastguard Worker 
83*795d594fSAndroid Build Coastguard Worker // It is possible to run tests that validate an existing deployed on-device ART APEX ('standalone'
84*795d594fSAndroid Build Coastguard Worker // tests). If these tests expect to load ELF files with a particular alignment, but those ELF files
85*795d594fSAndroid Build Coastguard Worker // were created with a different alignment, there will be many difficult-to-debug failures. This
86*795d594fSAndroid Build Coastguard Worker // test aims to identify this mismatch, related to whether or not the runtimes were built to be
87*795d594fSAndroid Build Coastguard Worker // page-size agnostic.
TEST_F(RuntimeTest,ElfAlignmentMismatch)88*795d594fSAndroid Build Coastguard Worker TEST_F(RuntimeTest, ElfAlignmentMismatch) {
89*795d594fSAndroid Build Coastguard Worker #ifdef ART_TARGET_ANDROID
90*795d594fSAndroid Build Coastguard Worker   bool platform_pga = android::base::GetBoolProperty("ro.product.build.no_bionic_page_size_macro",
91*795d594fSAndroid Build Coastguard Worker                                                      false);
92*795d594fSAndroid Build Coastguard Worker   if (kPageSizeAgnostic != platform_pga) {
93*795d594fSAndroid Build Coastguard Worker     LOG(WARNING) << "Test configured with kPageSizeAgnostic=" << kPageSizeAgnostic << ", but "
94*795d594fSAndroid Build Coastguard Worker                  << "platform ro.product.build.no_bionic_page_size_macro=" << platform_pga << ".";
95*795d594fSAndroid Build Coastguard Worker   }
96*795d594fSAndroid Build Coastguard Worker #endif
97*795d594fSAndroid Build Coastguard Worker   // Determine the alignment of the ART APEX by reading the alignment of boot.oat.
98*795d594fSAndroid Build Coastguard Worker   std::string core_oat_location = GetSystemImageFilename(GetCoreOatLocation().c_str(),
99*795d594fSAndroid Build Coastguard Worker                                                          kRuntimeQuickCodeISA);
100*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<File> core_oat_file(OS::OpenFileForReading(core_oat_location.c_str()));
101*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(core_oat_file.get() != nullptr) << core_oat_location;
102*795d594fSAndroid Build Coastguard Worker 
103*795d594fSAndroid Build Coastguard Worker   std::string error_msg;
104*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<ElfFile> elf_file(ElfFile::Open(core_oat_file.get(),
105*795d594fSAndroid Build Coastguard Worker                                                   /*writable=*/false,
106*795d594fSAndroid Build Coastguard Worker                                                   /*program_header_only=*/true,
107*795d594fSAndroid Build Coastguard Worker                                                   /*low_4gb=*/false,
108*795d594fSAndroid Build Coastguard Worker                                                   &error_msg));
109*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(elf_file != nullptr) << error_msg;
110*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(kElfSegmentAlignment, elf_file->GetElfSegmentAlignmentFromFile());
111*795d594fSAndroid Build Coastguard Worker }
112*795d594fSAndroid Build Coastguard Worker 
113*795d594fSAndroid Build Coastguard Worker class RuntimeInitMetricsDefaultTest : public CommonRuntimeTest {};
114*795d594fSAndroid Build Coastguard Worker 
TEST_F(RuntimeInitMetricsDefaultTest,MetricsAreNotInitialized)115*795d594fSAndroid Build Coastguard Worker TEST_F(RuntimeInitMetricsDefaultTest, MetricsAreNotInitialized) {
116*795d594fSAndroid Build Coastguard Worker   ASSERT_FALSE(runtime_->AreMetricsInitialized());
117*795d594fSAndroid Build Coastguard Worker }
118*795d594fSAndroid Build Coastguard Worker 
119*795d594fSAndroid Build Coastguard Worker class RuntimeInitMetricsZygoteTest : public CommonRuntimeTest {
SetUpRuntimeOptions(RuntimeOptions * options)120*795d594fSAndroid Build Coastguard Worker   void SetUpRuntimeOptions(RuntimeOptions* options) override {
121*795d594fSAndroid Build Coastguard Worker     CommonRuntimeTest::SetUpRuntimeOptions(options);
122*795d594fSAndroid Build Coastguard Worker     options->emplace_back(std::make_pair("-Xzygote", nullptr));
123*795d594fSAndroid Build Coastguard Worker   }
124*795d594fSAndroid Build Coastguard Worker };
125*795d594fSAndroid Build Coastguard Worker 
TEST_F(RuntimeInitMetricsZygoteTest,MetricsAreInitialized)126*795d594fSAndroid Build Coastguard Worker TEST_F(RuntimeInitMetricsZygoteTest, MetricsAreInitialized) {
127*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(runtime_->AreMetricsInitialized());
128*795d594fSAndroid Build Coastguard Worker }
129*795d594fSAndroid Build Coastguard Worker 
130*795d594fSAndroid Build Coastguard Worker class RuntimeInitMetricsForceEnableTest : public CommonRuntimeTest {
SetUpRuntimeOptions(RuntimeOptions * options)131*795d594fSAndroid Build Coastguard Worker   void SetUpRuntimeOptions(RuntimeOptions* options) override {
132*795d594fSAndroid Build Coastguard Worker     CommonRuntimeTest::SetUpRuntimeOptions(options);
133*795d594fSAndroid Build Coastguard Worker     options->emplace_back(std::make_pair("-Xmetrics-force-enable:true", nullptr));
134*795d594fSAndroid Build Coastguard Worker   }
135*795d594fSAndroid Build Coastguard Worker };
136*795d594fSAndroid Build Coastguard Worker 
TEST_F(RuntimeInitMetricsForceEnableTest,MetricsAreInitialized)137*795d594fSAndroid Build Coastguard Worker TEST_F(RuntimeInitMetricsForceEnableTest, MetricsAreInitialized) {
138*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(runtime_->AreMetricsInitialized());
139*795d594fSAndroid Build Coastguard Worker }
140*795d594fSAndroid Build Coastguard Worker 
141*795d594fSAndroid Build Coastguard Worker }  // namespace art
142