xref: /aosp_15_r20/art/dex2oat/dex2oat_cts_test.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2022 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 <sys/wait.h>
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include "android-base/result-gmock.h"
20*795d594fSAndroid Build Coastguard Worker #include "android-base/result.h"
21*795d594fSAndroid Build Coastguard Worker #include "android-base/strings.h"
22*795d594fSAndroid Build Coastguard Worker #include "base/file_utils.h"
23*795d594fSAndroid Build Coastguard Worker #include "dex2oat_environment_test.h"
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker namespace art {
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker using ::android::base::Result;
28*795d594fSAndroid Build Coastguard Worker using ::android::base::testing::HasValue;
29*795d594fSAndroid Build Coastguard Worker 
30*795d594fSAndroid Build Coastguard Worker // Test the binary with the same bitness as the test. This is also done to avoid
31*795d594fSAndroid Build Coastguard Worker // the symlink /apex/com.android.art/bin/dex2oat, which we don't have selinux
32*795d594fSAndroid Build Coastguard Worker // permission to read on S.
33*795d594fSAndroid Build Coastguard Worker #if defined(__LP64__)
34*795d594fSAndroid Build Coastguard Worker constexpr const char* kDex2oatBinary = "dex2oat64";
35*795d594fSAndroid Build Coastguard Worker #else
36*795d594fSAndroid Build Coastguard Worker constexpr const char* kDex2oatBinary = "dex2oat32";
37*795d594fSAndroid Build Coastguard Worker #endif
38*795d594fSAndroid Build Coastguard Worker 
39*795d594fSAndroid Build Coastguard Worker class Dex2oatCtsTest : public CommonArtTest, public Dex2oatScratchDirs {
40*795d594fSAndroid Build Coastguard Worker  public:
SetUp()41*795d594fSAndroid Build Coastguard Worker   void SetUp() override {
42*795d594fSAndroid Build Coastguard Worker     CommonArtTest::SetUp();
43*795d594fSAndroid Build Coastguard Worker     Dex2oatScratchDirs::SetUp(android_data_);
44*795d594fSAndroid Build Coastguard Worker   }
45*795d594fSAndroid Build Coastguard Worker 
TearDown()46*795d594fSAndroid Build Coastguard Worker   void TearDown() override {
47*795d594fSAndroid Build Coastguard Worker     Dex2oatScratchDirs::TearDown();
48*795d594fSAndroid Build Coastguard Worker     CommonArtTest::TearDown();
49*795d594fSAndroid Build Coastguard Worker   }
50*795d594fSAndroid Build Coastguard Worker 
51*795d594fSAndroid Build Coastguard Worker  protected:
52*795d594fSAndroid Build Coastguard Worker   // Stripped down counterpart to Dex2oatEnvironmentTest::Dex2Oat that only adds
53*795d594fSAndroid Build Coastguard Worker   // enough arguments for our purposes.
Dex2Oat(const std::vector<std::string> & dex2oat_args,std::string * output)54*795d594fSAndroid Build Coastguard Worker   Result<int> Dex2Oat(const std::vector<std::string>& dex2oat_args, std::string* output) {
55*795d594fSAndroid Build Coastguard Worker     std::vector<std::string> argv = {std::string(kAndroidArtApexDefaultPath) + "/bin/" +
56*795d594fSAndroid Build Coastguard Worker                                      kDex2oatBinary};
57*795d594fSAndroid Build Coastguard Worker     argv.insert(argv.end(), dex2oat_args.begin(), dex2oat_args.end());
58*795d594fSAndroid Build Coastguard Worker 
59*795d594fSAndroid Build Coastguard Worker     // We must set --android-root.
60*795d594fSAndroid Build Coastguard Worker     const char* android_root = getenv("ANDROID_ROOT");
61*795d594fSAndroid Build Coastguard Worker     CHECK(android_root != nullptr);
62*795d594fSAndroid Build Coastguard Worker     argv.push_back("--android-root=" + std::string(android_root));
63*795d594fSAndroid Build Coastguard Worker 
64*795d594fSAndroid Build Coastguard Worker     // We need dex2oat to actually log things.
65*795d594fSAndroid Build Coastguard Worker     auto post_fork_fn = []() { return setenv("ANDROID_LOG_TAGS", "*:d", 1) == 0; };
66*795d594fSAndroid Build Coastguard Worker 
67*795d594fSAndroid Build Coastguard Worker     ForkAndExecResult res = ForkAndExec(argv, post_fork_fn, output);
68*795d594fSAndroid Build Coastguard Worker     if (res.stage != ForkAndExecResult::kFinished) {
69*795d594fSAndroid Build Coastguard Worker       return ErrnoErrorf("Failed to finish dex2oat invocation '{}'",
70*795d594fSAndroid Build Coastguard Worker                          android::base::Join(argv, ' '));
71*795d594fSAndroid Build Coastguard Worker     }
72*795d594fSAndroid Build Coastguard Worker 
73*795d594fSAndroid Build Coastguard Worker     if (!WIFEXITED(res.status_code)) {
74*795d594fSAndroid Build Coastguard Worker       return Errorf("dex2oat didn't terminate normally (status_code={:#x}): {}",
75*795d594fSAndroid Build Coastguard Worker                     res.status_code,
76*795d594fSAndroid Build Coastguard Worker                     android::base::Join(argv, ' '));
77*795d594fSAndroid Build Coastguard Worker     }
78*795d594fSAndroid Build Coastguard Worker 
79*795d594fSAndroid Build Coastguard Worker     return WEXITSTATUS(res.status_code);
80*795d594fSAndroid Build Coastguard Worker   }
81*795d594fSAndroid Build Coastguard Worker };
82*795d594fSAndroid Build Coastguard Worker 
83*795d594fSAndroid Build Coastguard Worker // Run dex2oat with --force-palette-compilation-hooks to force calls to
84*795d594fSAndroid Build Coastguard Worker // PaletteNotify{Start,End}Dex2oatCompilation.
TEST_F(Dex2oatCtsTest,CompilationHooks)85*795d594fSAndroid Build Coastguard Worker TEST_F(Dex2oatCtsTest, CompilationHooks) {
86*795d594fSAndroid Build Coastguard Worker   const std::string dex_location = GetTestDexFileName("Main");
87*795d594fSAndroid Build Coastguard Worker   const std::string oat_location = GetScratchDir() + "/base.oat";
88*795d594fSAndroid Build Coastguard Worker   const std::string vdex_location = GetScratchDir() + "/base.vdex";
89*795d594fSAndroid Build Coastguard Worker 
90*795d594fSAndroid Build Coastguard Worker   std::vector<std::string> args;
91*795d594fSAndroid Build Coastguard Worker   args.emplace_back("--dex-file=" + dex_location);
92*795d594fSAndroid Build Coastguard Worker 
93*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<File> oat_file(OS::CreateEmptyFile(oat_location.c_str()));
94*795d594fSAndroid Build Coastguard Worker   ASSERT_NE(oat_file, nullptr) << oat_location;
95*795d594fSAndroid Build Coastguard Worker   args.emplace_back("--oat-fd=" + std::to_string(oat_file->Fd()));
96*795d594fSAndroid Build Coastguard Worker   args.emplace_back("--oat-location=" + oat_location);
97*795d594fSAndroid Build Coastguard Worker 
98*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<File> vdex_file(OS::CreateEmptyFile(vdex_location.c_str()));
99*795d594fSAndroid Build Coastguard Worker   ASSERT_NE(vdex_file, nullptr) << vdex_location;
100*795d594fSAndroid Build Coastguard Worker   args.emplace_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
101*795d594fSAndroid Build Coastguard Worker 
102*795d594fSAndroid Build Coastguard Worker   args.emplace_back("--force-palette-compilation-hooks");
103*795d594fSAndroid Build Coastguard Worker 
104*795d594fSAndroid Build Coastguard Worker   std::string output = "";
105*795d594fSAndroid Build Coastguard Worker   EXPECT_THAT(Dex2Oat(args, &output), HasValue(0));
106*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(oat_file->FlushCloseOrErase(), 0);
107*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(vdex_file->FlushCloseOrErase(), 0);
108*795d594fSAndroid Build Coastguard Worker }
109*795d594fSAndroid Build Coastguard Worker 
110*795d594fSAndroid Build Coastguard Worker }  // namespace art
111