Martin Stjernholm | d4aba91 | 2022-01-25 21:05:47 +0000 | [diff] [blame] | 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 | |
Martin Stjernholm | 2847729 | 2022-10-20 21:07:33 +0100 | [diff] [blame] | 17 | #include "base/file_utils.h" |
Martin Stjernholm | d4aba91 | 2022-01-25 21:05:47 +0000 | [diff] [blame] | 18 | #include "dex2oat_environment_test.h" |
| 19 | |
| 20 | namespace art { |
| 21 | |
Martin Stjernholm | 2847729 | 2022-10-20 21:07:33 +0100 | [diff] [blame] | 22 | class Dex2oatCtsTest : public CommonArtTest, public Dex2oatScratchDirs { |
| 23 | public: |
| 24 | void SetUp() override { |
| 25 | CommonArtTest::SetUp(); |
| 26 | Dex2oatScratchDirs::SetUp(android_data_); |
| 27 | } |
| 28 | |
| 29 | void TearDown() override { |
| 30 | Dex2oatScratchDirs::TearDown(); |
| 31 | CommonArtTest::TearDown(); |
| 32 | } |
| 33 | |
| 34 | protected: |
| 35 | // Stripped down counterpart to Dex2oatEnvironmentTest::Dex2Oat that only adds |
| 36 | // enough arguments for our purposes. |
| 37 | int Dex2Oat(const std::vector<std::string>& dex2oat_args, |
| 38 | std::string* output, |
| 39 | std::string* error_msg) { |
| 40 | // This command line should work regardless of bitness, ISA, etc. |
| 41 | std::vector<std::string> argv = {std::string(kAndroidArtApexDefaultPath) + "/bin/dex2oat"}; |
| 42 | argv.insert(argv.end(), dex2oat_args.begin(), dex2oat_args.end()); |
| 43 | |
| 44 | // We must set --android-root. |
| 45 | const char* android_root = getenv("ANDROID_ROOT"); |
| 46 | CHECK(android_root != nullptr); |
| 47 | argv.push_back("--android-root=" + std::string(android_root)); |
| 48 | |
| 49 | // We need dex2oat to actually log things. |
| 50 | auto post_fork_fn = []() { return setenv("ANDROID_LOG_TAGS", "*:d", 1) == 0; }; |
| 51 | ForkAndExecResult res = ForkAndExec(argv, post_fork_fn, output); |
| 52 | if (res.stage != ForkAndExecResult::kFinished) { |
| 53 | *error_msg = strerror(errno); |
| 54 | ::testing::AssertionFailure() << "Failed to finish dex2oat invocation: " << *error_msg; |
| 55 | } |
| 56 | |
| 57 | if (!res.StandardSuccess()) { |
| 58 | // We cannot use ASSERT_TRUE since the method returns an int and not void. |
| 59 | ::testing::AssertionFailure() << "dex2oat fork/exec failed: " << *error_msg; |
| 60 | } |
| 61 | |
| 62 | return res.status_code; |
| 63 | } |
| 64 | }; |
Martin Stjernholm | d4aba91 | 2022-01-25 21:05:47 +0000 | [diff] [blame] | 65 | |
Martin Stjernholm | 7d5d950 | 2023-06-09 10:31:33 +0000 | [diff] [blame] | 66 | // Run dex2oat with --force-palette-compilation-hooks to force calls to |
Martin Stjernholm | d4aba91 | 2022-01-25 21:05:47 +0000 | [diff] [blame] | 67 | // PaletteNotify{Start,End}Dex2oatCompilation. |
| 68 | TEST_F(Dex2oatCtsTest, CompilationHooks) { |
| 69 | const std::string dex_location = GetTestDexFileName("Main"); |
| 70 | const std::string oat_location = GetScratchDir() + "/base.oat"; |
| 71 | const std::string vdex_location = GetScratchDir() + "/base.vdex"; |
| 72 | |
| 73 | std::vector<std::string> args; |
| 74 | args.emplace_back("--dex-file=" + dex_location); |
| 75 | |
| 76 | std::unique_ptr<File> oat_file(OS::CreateEmptyFile(oat_location.c_str())); |
| 77 | ASSERT_NE(oat_file, nullptr) << oat_location; |
| 78 | args.emplace_back("--oat-fd=" + std::to_string(oat_file->Fd())); |
| 79 | args.emplace_back("--oat-location=" + oat_location); |
| 80 | |
| 81 | std::unique_ptr<File> vdex_file(OS::CreateEmptyFile(vdex_location.c_str())); |
| 82 | ASSERT_NE(vdex_file, nullptr) << vdex_location; |
| 83 | args.emplace_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd())); |
| 84 | |
| 85 | args.emplace_back("--force-palette-compilation-hooks"); |
| 86 | |
| 87 | std::string output = ""; |
| 88 | std::string error_msg; |
| 89 | int res = Dex2Oat(args, &output, &error_msg); |
| 90 | EXPECT_EQ(res, 0) << error_msg; |
| 91 | EXPECT_EQ(oat_file->FlushCloseOrErase(), 0); |
| 92 | EXPECT_EQ(vdex_file->FlushCloseOrErase(), 0); |
| 93 | } |
| 94 | |
| 95 | } // namespace art |