Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "oat_file.h" |
| 18 | |
| 19 | #include <string> |
| 20 | |
| 21 | #include <gtest/gtest.h> |
| 22 | |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 23 | #include "common_runtime_test.h" |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 24 | #include "dexopt_test.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 25 | #include "scoped_thread_state_change-inl.h" |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 26 | #include "vdex_file.h" |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 27 | |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 28 | namespace art { |
| 29 | |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 30 | class OatFileTest : public DexoptTest { |
Andreas Gampe | 7848da4 | 2015-04-09 11:15:04 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 33 | TEST_F(OatFileTest, LoadOat) { |
| 34 | std::string dex_location = GetScratchDir() + "/LoadOat.jar"; |
| 35 | |
| 36 | Copy(GetDexSrc1(), dex_location); |
| 37 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
| 38 | |
| 39 | std::string oat_location; |
| 40 | std::string error_msg; |
| 41 | ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename( |
| 42 | dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg; |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 43 | std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1, |
Nicolas Geoffray | 3002509 | 2018-04-19 14:43:29 +0100 | [diff] [blame] | 44 | oat_location.c_str(), |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 45 | oat_location.c_str(), |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 46 | /*executable=*/ false, |
| 47 | /*low_4gb=*/ false, |
Vladimir Marko | b7bf843 | 2019-12-03 13:18:50 +0000 | [diff] [blame] | 48 | dex_location, |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 49 | &error_msg)); |
| 50 | ASSERT_TRUE(odex_file.get() != nullptr); |
| 51 | |
| 52 | // Check that the vdex file was loaded in the reserved space of odex file. |
| 53 | EXPECT_EQ(odex_file->GetVdexFile()->Begin(), odex_file->VdexBegin()); |
| 54 | } |
| 55 | |
Andreas Gampe | fc604a7 | 2018-02-08 15:43:37 -0800 | [diff] [blame] | 56 | TEST_F(OatFileTest, ChangingMultiDexUncompressed) { |
David Srbecky | 8c8f148 | 2020-02-05 20:17:10 +0000 | [diff] [blame^] | 57 | std::string dex_location = GetScratchDir() + "/MultiDexUncompressedAligned.jar"; |
Andreas Gampe | fc604a7 | 2018-02-08 15:43:37 -0800 | [diff] [blame] | 58 | |
David Srbecky | 8c8f148 | 2020-02-05 20:17:10 +0000 | [diff] [blame^] | 59 | Copy(GetTestDexFileName("MultiDexUncompressedAligned"), dex_location); |
Andreas Gampe | fc604a7 | 2018-02-08 15:43:37 -0800 | [diff] [blame] | 60 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kQuicken); |
| 61 | |
| 62 | std::string oat_location; |
| 63 | std::string error_msg; |
| 64 | ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename( |
| 65 | dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg; |
| 66 | |
| 67 | // Ensure we can load that file. Just a precondition. |
| 68 | { |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 69 | std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1, |
Nicolas Geoffray | 3002509 | 2018-04-19 14:43:29 +0100 | [diff] [blame] | 70 | oat_location.c_str(), |
Andreas Gampe | fc604a7 | 2018-02-08 15:43:37 -0800 | [diff] [blame] | 71 | oat_location.c_str(), |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 72 | /*executable=*/ false, |
| 73 | /*low_4gb=*/ false, |
Vladimir Marko | b7bf843 | 2019-12-03 13:18:50 +0000 | [diff] [blame] | 74 | dex_location, |
Andreas Gampe | fc604a7 | 2018-02-08 15:43:37 -0800 | [diff] [blame] | 75 | &error_msg)); |
| 76 | ASSERT_TRUE(odex_file != nullptr); |
| 77 | ASSERT_EQ(2u, odex_file->GetOatDexFiles().size()); |
| 78 | } |
| 79 | |
| 80 | // Now replace the source. |
David Srbecky | 8c8f148 | 2020-02-05 20:17:10 +0000 | [diff] [blame^] | 81 | Copy(GetTestDexFileName("MainUncompressedAligned"), dex_location); |
Andreas Gampe | fc604a7 | 2018-02-08 15:43:37 -0800 | [diff] [blame] | 82 | |
| 83 | // And try to load again. |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 84 | std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 85 | oat_location, |
| 86 | oat_location, |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 87 | /*executable=*/ false, |
| 88 | /*low_4gb=*/ false, |
Vladimir Marko | b7bf843 | 2019-12-03 13:18:50 +0000 | [diff] [blame] | 89 | dex_location, |
Andreas Gampe | fc604a7 | 2018-02-08 15:43:37 -0800 | [diff] [blame] | 90 | &error_msg)); |
| 91 | EXPECT_TRUE(odex_file == nullptr); |
| 92 | EXPECT_NE(std::string::npos, error_msg.find("expected 2 uncompressed dex files, but found 1")) |
| 93 | << error_msg; |
| 94 | } |
| 95 | |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 96 | } // namespace art |