| 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 | |
| 33 | TEST_F(OatFileTest, ResolveRelativeEncodedDexLocation) { |
| Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 34 | EXPECT_EQ(std::string("/data/app/foo/base.apk"), |
| 35 | OatFile::ResolveRelativeEncodedDexLocation( |
| 36 | nullptr, "/data/app/foo/base.apk")); |
| 37 | |
| Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 38 | EXPECT_EQ(std::string("/data/app/foo/base.apk"), |
| 39 | OatFile::ResolveRelativeEncodedDexLocation( |
| 40 | "/data/app/foo/base.apk", "base.apk")); |
| 41 | |
| 42 | EXPECT_EQ(std::string("/data/app/foo/base.apk"), |
| 43 | OatFile::ResolveRelativeEncodedDexLocation( |
| 44 | "/data/app/foo/base.apk", "foo/base.apk")); |
| 45 | |
| Calin Juravle | a308a32 | 2017-07-18 16:51:51 -0700 | [diff] [blame] | 46 | EXPECT_EQ(std::string("/data/app/foo/base.apk!classes2.dex"), |
| Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 47 | OatFile::ResolveRelativeEncodedDexLocation( |
| Calin Juravle | a308a32 | 2017-07-18 16:51:51 -0700 | [diff] [blame] | 48 | "/data/app/foo/base.apk", "base.apk!classes2.dex")); |
| Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 49 | |
| Calin Juravle | a308a32 | 2017-07-18 16:51:51 -0700 | [diff] [blame] | 50 | EXPECT_EQ(std::string("/data/app/foo/base.apk!classes11.dex"), |
| Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 51 | OatFile::ResolveRelativeEncodedDexLocation( |
| Calin Juravle | a308a32 | 2017-07-18 16:51:51 -0700 | [diff] [blame] | 52 | "/data/app/foo/base.apk", "base.apk!classes11.dex")); |
| Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 53 | |
| Nicolas Geoffray | 8baa11a | 2019-01-24 10:30:03 +0000 | [diff] [blame] | 54 | // Host and target differ in their way of handling locations |
| 55 | // that are prefix of one another, due to boot image files. |
| 56 | if (kIsTargetBuild) { |
| 57 | EXPECT_EQ(std::string("/system/framework/base.apk"), |
| 58 | OatFile::ResolveRelativeEncodedDexLocation( |
| 59 | "/data/app/foo/base.apk", "/system/framework/base.apk")); |
| 60 | EXPECT_EQ(std::string("base.apk"), |
| 61 | OatFile::ResolveRelativeEncodedDexLocation( |
| 62 | "/data/app/foo/sludge.apk", "base.apk")); |
| 63 | EXPECT_EQ(std::string("o/base.apk"), |
| 64 | OatFile::ResolveRelativeEncodedDexLocation( |
| 65 | "/data/app/foo/base.apk", "o/base.apk")); |
| 66 | } else { |
| 67 | EXPECT_EQ(std::string("/data/app/foo/base.apk"), |
| 68 | OatFile::ResolveRelativeEncodedDexLocation( |
| 69 | "/data/app/foo/base.apk", "/system/framework/base.apk")); |
| 70 | EXPECT_EQ(std::string("/data/app/foo/sludge.apk"), |
| 71 | OatFile::ResolveRelativeEncodedDexLocation( |
| 72 | "/data/app/foo/sludge.apk", "base.apk")); |
| 73 | EXPECT_EQ(std::string("/data/app/foo/base.apk"), |
| 74 | OatFile::ResolveRelativeEncodedDexLocation( |
| 75 | "/data/app/foo/base.apk", "o/base.apk")); |
| 76 | } |
| Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 79 | TEST_F(OatFileTest, LoadOat) { |
| 80 | std::string dex_location = GetScratchDir() + "/LoadOat.jar"; |
| 81 | |
| 82 | Copy(GetDexSrc1(), dex_location); |
| 83 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
| 84 | |
| 85 | std::string oat_location; |
| 86 | std::string error_msg; |
| 87 | ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename( |
| 88 | dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg; |
| Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 89 | std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1, |
| Nicolas Geoffray | 3002509 | 2018-04-19 14:43:29 +0100 | [diff] [blame] | 90 | oat_location.c_str(), |
| David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 91 | oat_location.c_str(), |
| Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 92 | /*executable=*/ false, |
| 93 | /*low_4gb=*/ false, |
| David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 94 | dex_location.c_str(), |
| Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 95 | /*reservation=*/ nullptr, |
| David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 96 | &error_msg)); |
| 97 | ASSERT_TRUE(odex_file.get() != nullptr); |
| 98 | |
| 99 | // Check that the vdex file was loaded in the reserved space of odex file. |
| 100 | EXPECT_EQ(odex_file->GetVdexFile()->Begin(), odex_file->VdexBegin()); |
| 101 | } |
| 102 | |
| Andreas Gampe | fc604a7 | 2018-02-08 15:43:37 -0800 | [diff] [blame] | 103 | TEST_F(OatFileTest, ChangingMultiDexUncompressed) { |
| 104 | std::string dex_location = GetScratchDir() + "/MultiDexUncompressed.jar"; |
| 105 | |
| 106 | Copy(GetTestDexFileName("MultiDexUncompressed"), dex_location); |
| 107 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kQuicken); |
| 108 | |
| 109 | std::string oat_location; |
| 110 | std::string error_msg; |
| 111 | ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename( |
| 112 | dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg; |
| 113 | |
| 114 | // Ensure we can load that file. Just a precondition. |
| 115 | { |
| Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 116 | std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1, |
| Nicolas Geoffray | 3002509 | 2018-04-19 14:43:29 +0100 | [diff] [blame] | 117 | oat_location.c_str(), |
| Andreas Gampe | fc604a7 | 2018-02-08 15:43:37 -0800 | [diff] [blame] | 118 | oat_location.c_str(), |
| Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 119 | /*executable=*/ false, |
| 120 | /*low_4gb=*/ false, |
| Andreas Gampe | fc604a7 | 2018-02-08 15:43:37 -0800 | [diff] [blame] | 121 | dex_location.c_str(), |
| Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 122 | /*reservation=*/ nullptr, |
| Andreas Gampe | fc604a7 | 2018-02-08 15:43:37 -0800 | [diff] [blame] | 123 | &error_msg)); |
| 124 | ASSERT_TRUE(odex_file != nullptr); |
| 125 | ASSERT_EQ(2u, odex_file->GetOatDexFiles().size()); |
| 126 | } |
| 127 | |
| 128 | // Now replace the source. |
| 129 | Copy(GetTestDexFileName("MainUncompressed"), dex_location); |
| 130 | |
| 131 | // And try to load again. |
| Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 132 | std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1, |
| Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 133 | oat_location, |
| 134 | oat_location, |
| Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 135 | /*executable=*/ false, |
| 136 | /*low_4gb=*/ false, |
| Andreas Gampe | fc604a7 | 2018-02-08 15:43:37 -0800 | [diff] [blame] | 137 | dex_location.c_str(), |
| Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 138 | /*reservation=*/ nullptr, |
| Andreas Gampe | fc604a7 | 2018-02-08 15:43:37 -0800 | [diff] [blame] | 139 | &error_msg)); |
| 140 | EXPECT_TRUE(odex_file == nullptr); |
| 141 | EXPECT_NE(std::string::npos, error_msg.find("expected 2 uncompressed dex files, but found 1")) |
| 142 | << error_msg; |
| 143 | } |
| 144 | |
| Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 145 | } // namespace art |