• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <dlfcn.h>
20 
21 #include <string>
22 
23 #include "common_runtime_test.h"
24 #include "dexopt_test.h"
25 #include "gtest/gtest.h"
26 #include "scoped_thread_state_change-inl.h"
27 #include "vdex_file.h"
28 
29 namespace art HIDDEN {
30 
31 class OatFileTest : public DexoptTest {};
32 
TEST_F(OatFileTest,LoadOat)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))
43       << error_msg;
44   std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/-1,
45                                                    oat_location,
46                                                    oat_location,
47                                                    /*executable=*/false,
48                                                    /*low_4gb=*/false,
49                                                    dex_location,
50                                                    &error_msg));
51   ASSERT_TRUE(odex_file.get() != nullptr);
52 
53   // Check that the vdex file was loaded in the reserved space of odex file.
54   EXPECT_EQ(odex_file->GetVdexFile()->Begin(), odex_file->VdexBegin());
55 }
56 
TEST_F(OatFileTest,ChangingMultiDexUncompressed)57 TEST_F(OatFileTest, ChangingMultiDexUncompressed) {
58   std::string dex_location = GetScratchDir() + "/MultiDexUncompressedAligned.jar";
59 
60   Copy(GetTestDexFileName("MultiDexUncompressedAligned"), dex_location);
61   GenerateOatForTest(dex_location.c_str(), CompilerFilter::kVerify);
62 
63   std::string oat_location;
64   std::string error_msg;
65   ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
66       dex_location, kRuntimeISA, &oat_location, &error_msg))
67       << error_msg;
68 
69   // Ensure we can load that file. Just a precondition.
70   {
71     std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/-1,
72                                                      oat_location,
73                                                      oat_location,
74                                                      /*executable=*/false,
75                                                      /*low_4gb=*/false,
76                                                      dex_location,
77                                                      &error_msg));
78     ASSERT_TRUE(odex_file != nullptr);
79     ASSERT_EQ(2u, odex_file->GetOatDexFiles().size());
80   }
81 
82   // Now replace the source.
83   Copy(GetTestDexFileName("MainUncompressedAligned"), dex_location);
84 
85   // And try to load again.
86   std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/-1,
87                                                    oat_location,
88                                                    oat_location,
89                                                    /*executable=*/false,
90                                                    /*low_4gb=*/false,
91                                                    dex_location,
92                                                    &error_msg));
93   EXPECT_TRUE(odex_file == nullptr);
94   EXPECT_NE(std::string::npos, error_msg.find("expected 2 uncompressed dex files, but found 1"))
95       << error_msg;
96 }
97 
TEST_F(OatFileTest,DlOpenLoad)98 TEST_F(OatFileTest, DlOpenLoad) {
99   std::string dex_location = GetScratchDir() + "/LoadOat.jar";
100 
101   Copy(GetDexSrc1(), dex_location);
102   GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
103 
104   std::string oat_location;
105   std::string error_msg;
106   ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
107       dex_location, kRuntimeISA, &oat_location, &error_msg))
108       << error_msg;
109 
110   // Clear previous errors if any.
111   dlerror();
112   error_msg.clear();
113   std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/-1,
114                                                    oat_location,
115                                                    oat_location,
116                                                    /*executable=*/true,
117                                                    /*low_4gb=*/false,
118                                                    dex_location,
119                                                    &error_msg));
120   ASSERT_NE(odex_file.get(), nullptr) << error_msg;
121 
122 #ifdef __GLIBC__
123   if (!error_msg.empty()) {
124     // If a valid oat file was returned but there was an error message, then dlopen failed
125     // but the backup ART ELF loader successfully loaded the oat file.
126     // The only expected reason for this is a bug in glibc that prevents loading dynamic
127     // shared objects with a read-only dynamic section:
128     // https://sourceware.org/bugzilla/show_bug.cgi?id=28340.
129     ASSERT_TRUE(error_msg == "DlOpen does not support read-only .dynamic section.") << error_msg;
130     GTEST_SKIP() << error_msg;
131   }
132 #else
133   // If a valid oat file was returned with no error message, then dlopen was successful.
134   ASSERT_TRUE(error_msg.empty()) << error_msg;
135 #endif
136 
137   const char *dlerror_msg = dlerror();
138   ASSERT_EQ(dlerror_msg, nullptr) << dlerror_msg;
139 
140   // Ensure that the oat file is loaded with dlopen by requesting information about it
141   // using dladdr.
142   Dl_info info;
143   ASSERT_NE(dladdr(odex_file->Begin(), &info), 0);
144   EXPECT_STREQ(info.dli_fname, oat_location.c_str())
145       << "dli_fname: " << info.dli_fname
146       << ", location: " << oat_location;
147   EXPECT_STREQ(info.dli_sname, "oatdata") << info.dli_sname;
148 }
149 
150 }  // namespace art
151