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