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 Brazdil | 3e8aae0 | 2019-03-26 18:48:02 +0000 | [diff] [blame] | 33 | TEST_F(OatFileTest, ResolveRelativeEncodedDexLocation_NullAbsLocation) { |
| 34 | std::string dex_location; |
| 35 | std::string dex_file_name; |
| 36 | OatFile::ResolveRelativeEncodedDexLocation(nullptr, |
| 37 | "/data/app/foo/base.apk", |
| 38 | &dex_location, |
| 39 | &dex_file_name); |
| 40 | ASSERT_EQ("/data/app/foo/base.apk", dex_file_name); |
| 41 | ASSERT_EQ("/data/app/foo/base.apk", dex_location); |
| 42 | } |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 43 | |
David Brazdil | 3e8aae0 | 2019-03-26 18:48:02 +0000 | [diff] [blame] | 44 | TEST_F(OatFileTest, ResolveRelativeEncodedDexLocation_NullAbsLocation_Multidex) { |
| 45 | std::string dex_location; |
| 46 | std::string dex_file_name; |
| 47 | OatFile::ResolveRelativeEncodedDexLocation(nullptr, |
| 48 | "/data/app/foo/base.apk!classes2.dex", |
| 49 | &dex_location, |
| 50 | &dex_file_name); |
| 51 | ASSERT_EQ("/data/app/foo/base.apk!classes2.dex", dex_file_name); |
| 52 | ASSERT_EQ("/data/app/foo/base.apk!classes2.dex", dex_location); |
| 53 | } |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 54 | |
David Brazdil | 3e8aae0 | 2019-03-26 18:48:02 +0000 | [diff] [blame] | 55 | TEST_F(OatFileTest, ResolveRelativeEncodedDexLocation_RelLocationAbsolute) { |
| 56 | std::string dex_location; |
| 57 | std::string dex_file_name; |
| 58 | OatFile::ResolveRelativeEncodedDexLocation("base.apk", |
| 59 | "/system/framework/base.apk", |
| 60 | &dex_location, |
| 61 | &dex_file_name); |
| 62 | ASSERT_EQ(kIsTargetBuild ? "/system/framework/base.apk" : "base.apk", dex_file_name); |
| 63 | ASSERT_EQ("/system/framework/base.apk", dex_location); |
| 64 | } |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 65 | |
David Brazdil | 3e8aae0 | 2019-03-26 18:48:02 +0000 | [diff] [blame] | 66 | TEST_F(OatFileTest, ResolveRelativeEncodedDexLocation_BothAbsoluteLocations) { |
| 67 | std::string dex_location; |
| 68 | std::string dex_file_name; |
| 69 | OatFile::ResolveRelativeEncodedDexLocation("/data/app/foo/base.apk", |
| 70 | "/system/framework/base.apk", |
| 71 | &dex_location, |
| 72 | &dex_file_name); |
| 73 | ASSERT_EQ(kIsTargetBuild ? "/system/framework/base.apk" : "/data/app/foo/base.apk", |
| 74 | dex_file_name); |
| 75 | ASSERT_EQ("/system/framework/base.apk", dex_location); |
| 76 | } |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 77 | |
David Brazdil | 3e8aae0 | 2019-03-26 18:48:02 +0000 | [diff] [blame] | 78 | TEST_F(OatFileTest, ResolveRelativeEncodedDexLocation_RelSuffixOfAbsLocation1) { |
| 79 | std::string dex_location; |
| 80 | std::string dex_file_name; |
| 81 | OatFile::ResolveRelativeEncodedDexLocation("/data/app/foo/base.apk", |
| 82 | "base.apk", |
| 83 | &dex_location, |
| 84 | &dex_file_name); |
| 85 | ASSERT_EQ("/data/app/foo/base.apk", dex_file_name); |
| 86 | ASSERT_EQ("/data/app/foo/base.apk", dex_location); |
| 87 | } |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 88 | |
David Brazdil | 3e8aae0 | 2019-03-26 18:48:02 +0000 | [diff] [blame] | 89 | TEST_F(OatFileTest, ResolveRelativeEncodedDexLocation_RelSuffixOfAbsLocation2) { |
| 90 | std::string dex_location; |
| 91 | std::string dex_file_name; |
| 92 | OatFile::ResolveRelativeEncodedDexLocation("/data/app/foo/base.apk", |
| 93 | "foo/base.apk", |
| 94 | &dex_location, |
| 95 | &dex_file_name); |
| 96 | ASSERT_EQ("/data/app/foo/base.apk", dex_file_name); |
| 97 | ASSERT_EQ("/data/app/foo/base.apk", dex_location); |
| 98 | } |
| 99 | |
| 100 | TEST_F(OatFileTest, ResolveRelativeEncodedDexLocation_RelSuffixOfAbsLocation_Multidex) { |
| 101 | std::string dex_location; |
| 102 | std::string dex_file_name; |
| 103 | OatFile::ResolveRelativeEncodedDexLocation("/data/app/foo/base.apk", |
| 104 | "base.apk!classes11.dex", |
| 105 | &dex_location, |
| 106 | &dex_file_name); |
| 107 | ASSERT_EQ("/data/app/foo/base.apk!classes11.dex", dex_file_name); |
| 108 | ASSERT_EQ("/data/app/foo/base.apk!classes11.dex", dex_location); |
| 109 | } |
| 110 | |
| 111 | TEST_F(OatFileTest, ResolveRelativeEncodedDexLocation_RelNotSuffixOfAbsLocation1) { |
| 112 | std::string dex_location; |
| 113 | std::string dex_file_name; |
| 114 | OatFile::ResolveRelativeEncodedDexLocation("/data/app/foo/sludge.apk", |
| 115 | "base.apk!classes2.dex", |
| 116 | &dex_location, |
| 117 | &dex_file_name); |
| 118 | ASSERT_EQ(kIsTargetBuild ? "base.apk!classes2.dex" : "/data/app/foo/sludge.apk!classes2.dex", |
| 119 | dex_file_name); |
| 120 | ASSERT_EQ("base.apk!classes2.dex", dex_location); |
| 121 | } |
| 122 | |
| 123 | TEST_F(OatFileTest, ResolveRelativeEncodedDexLocation_RelNotSuffixOfAbsLocation2) { |
| 124 | std::string dex_location; |
| 125 | std::string dex_file_name; |
| 126 | OatFile::ResolveRelativeEncodedDexLocation("/data/app/foo/sludge.apk", |
| 127 | "o/base.apk", |
| 128 | &dex_location, |
| 129 | &dex_file_name); |
| 130 | ASSERT_EQ(kIsTargetBuild ? "o/base.apk" : "/data/app/foo/sludge.apk", dex_file_name); |
| 131 | ASSERT_EQ("o/base.apk", dex_location); |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 132 | } |
| 133 | |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 134 | TEST_F(OatFileTest, LoadOat) { |
| 135 | std::string dex_location = GetScratchDir() + "/LoadOat.jar"; |
| 136 | |
| 137 | Copy(GetDexSrc1(), dex_location); |
| 138 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed); |
| 139 | |
| 140 | std::string oat_location; |
| 141 | std::string error_msg; |
| 142 | ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename( |
| 143 | dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg; |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 144 | std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1, |
Nicolas Geoffray | 3002509 | 2018-04-19 14:43:29 +0100 | [diff] [blame] | 145 | oat_location.c_str(), |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 146 | oat_location.c_str(), |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 147 | /*executable=*/ false, |
| 148 | /*low_4gb=*/ false, |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 149 | dex_location.c_str(), |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 150 | /*reservation=*/ nullptr, |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 151 | &error_msg)); |
| 152 | ASSERT_TRUE(odex_file.get() != nullptr); |
| 153 | |
| 154 | // Check that the vdex file was loaded in the reserved space of odex file. |
| 155 | EXPECT_EQ(odex_file->GetVdexFile()->Begin(), odex_file->VdexBegin()); |
| 156 | } |
| 157 | |
Andreas Gampe | fc604a7 | 2018-02-08 15:43:37 -0800 | [diff] [blame] | 158 | TEST_F(OatFileTest, ChangingMultiDexUncompressed) { |
| 159 | std::string dex_location = GetScratchDir() + "/MultiDexUncompressed.jar"; |
| 160 | |
| 161 | Copy(GetTestDexFileName("MultiDexUncompressed"), dex_location); |
| 162 | GenerateOatForTest(dex_location.c_str(), CompilerFilter::kQuicken); |
| 163 | |
| 164 | std::string oat_location; |
| 165 | std::string error_msg; |
| 166 | ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename( |
| 167 | dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg; |
| 168 | |
| 169 | // Ensure we can load that file. Just a precondition. |
| 170 | { |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 171 | std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1, |
Nicolas Geoffray | 3002509 | 2018-04-19 14:43:29 +0100 | [diff] [blame] | 172 | oat_location.c_str(), |
Andreas Gampe | fc604a7 | 2018-02-08 15:43:37 -0800 | [diff] [blame] | 173 | oat_location.c_str(), |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 174 | /*executable=*/ false, |
| 175 | /*low_4gb=*/ false, |
Andreas Gampe | fc604a7 | 2018-02-08 15:43:37 -0800 | [diff] [blame] | 176 | dex_location.c_str(), |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 177 | /*reservation=*/ nullptr, |
Andreas Gampe | fc604a7 | 2018-02-08 15:43:37 -0800 | [diff] [blame] | 178 | &error_msg)); |
| 179 | ASSERT_TRUE(odex_file != nullptr); |
| 180 | ASSERT_EQ(2u, odex_file->GetOatDexFiles().size()); |
| 181 | } |
| 182 | |
| 183 | // Now replace the source. |
| 184 | Copy(GetTestDexFileName("MainUncompressed"), dex_location); |
| 185 | |
| 186 | // And try to load again. |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 187 | std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 188 | oat_location, |
| 189 | oat_location, |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 190 | /*executable=*/ false, |
| 191 | /*low_4gb=*/ false, |
Andreas Gampe | fc604a7 | 2018-02-08 15:43:37 -0800 | [diff] [blame] | 192 | dex_location.c_str(), |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 193 | /*reservation=*/ nullptr, |
Andreas Gampe | fc604a7 | 2018-02-08 15:43:37 -0800 | [diff] [blame] | 194 | &error_msg)); |
| 195 | EXPECT_TRUE(odex_file == nullptr); |
| 196 | EXPECT_NE(std::string::npos, error_msg.find("expected 2 uncompressed dex files, but found 1")) |
| 197 | << error_msg; |
| 198 | } |
| 199 | |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 200 | } // namespace art |