1 /*
2  * Copyright (C) 2017 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 <gtest/gtest.h>
18 
19 #include "android-base/logging.h"
20 #include "android-base/stringprintf.h"
21 #include "android-base/strings.h"
22 
23 #include "base/stl_util.h"
24 #include "class_linker.h"
25 #include "dexopt_test.h"
26 #include "dex/utf.h"
27 #include "intern_table.h"
28 #include "noop_compiler_callbacks.h"
29 #include "oat_file.h"
30 
31 namespace art {
32 namespace gc {
33 namespace space {
34 
35 class ImageSpaceTest : public CommonRuntimeTest {
36  protected:
SetUpRuntimeOptions(RuntimeOptions * options)37   void SetUpRuntimeOptions(RuntimeOptions* options) override {
38     // Disable implicit dex2oat invocations when loading image spaces.
39     options->emplace_back("-Xnoimage-dex2oat", nullptr);
40     // Disable relocation.
41     options->emplace_back("-Xnorelocate", nullptr);
42   }
43 
GetFilenameBase(const std::string & full_path)44   std::string GetFilenameBase(const std::string& full_path) {
45     size_t slash_pos = full_path.rfind('/');
46     CHECK_NE(std::string::npos, slash_pos);
47     size_t dot_pos = full_path.rfind('.');
48     CHECK_NE(std::string::npos, dot_pos);
49     CHECK_GT(dot_pos, slash_pos + 1u);
50     return full_path.substr(slash_pos + 1u, dot_pos - (slash_pos + 1u));
51   }
52 };
53 
TEST_F(ImageSpaceTest,StringDeduplication)54 TEST_F(ImageSpaceTest, StringDeduplication) {
55   const char* const kBaseNames[] = { "Extension1", "Extension2" };
56 
57   ScratchDir scratch;
58   const std::string& scratch_dir = scratch.GetPath();
59   std::string image_dir = scratch_dir + GetInstructionSetString(kRuntimeISA);
60   int mkdir_result = mkdir(image_dir.c_str(), 0700);
61   ASSERT_EQ(0, mkdir_result);
62 
63   // Prepare boot class path variables, exclude core-icu4j and conscrypt
64   // which are not in the primary boot image.
65   std::vector<std::string> bcp = GetLibCoreDexFileNames();
66   std::vector<std::string> bcp_locations = GetLibCoreDexLocations();
67   CHECK_EQ(bcp.size(), bcp_locations.size());
68   ASSERT_NE(std::string::npos, bcp.back().find("conscrypt"));
69   bcp.pop_back();
70   bcp_locations.pop_back();
71   ASSERT_NE(std::string::npos, bcp.back().find("core-icu4j"));
72   bcp.pop_back();
73   bcp_locations.pop_back();
74   std::string base_bcp_string = android::base::Join(bcp, ':');
75   std::string base_bcp_locations_string = android::base::Join(bcp_locations, ':');
76   std::string base_image_location = GetImageLocation();
77 
78   // Compile the two extensions independently.
79   std::vector<std::string> extension_image_locations;
80   for (const char* base_name : kBaseNames) {
81     std::string jar_name = GetTestDexFileName(base_name);
82     ArrayRef<const std::string> dex_files(&jar_name, /*size=*/ 1u);
83     ScratchFile profile_file;
84     GenerateProfile(dex_files, profile_file.GetFile());
85     std::vector<std::string> extra_args = {
86         "--profile-file=" + profile_file.GetFilename(),
87         "--runtime-arg",
88         "-Xbootclasspath:" + base_bcp_string + ':' + jar_name,
89         "--runtime-arg",
90         "-Xbootclasspath-locations:" + base_bcp_locations_string + ':' + jar_name,
91         "--boot-image=" + base_image_location,
92     };
93     std::string prefix = GetFilenameBase(base_image_location);
94     std::string error_msg;
95     bool success = CompileBootImage(extra_args, image_dir + '/' + prefix, dex_files, &error_msg);
96     ASSERT_TRUE(success) << error_msg;
97     bcp.push_back(jar_name);
98     bcp_locations.push_back(jar_name);
99     extension_image_locations.push_back(
100         scratch_dir + prefix + '-' + GetFilenameBase(jar_name) + ".art");
101   }
102 
103   // Also compile the second extension as an app with app image.
104   const char* app_base_name = kBaseNames[std::size(kBaseNames) - 1u];
105   std::string app_jar_name = GetTestDexFileName(app_base_name);
106   std::string app_odex_name = scratch_dir + app_base_name + ".odex";
107   std::string app_image_name = scratch_dir + app_base_name + ".art";
108   {
109     ArrayRef<const std::string> dex_files(&app_jar_name, /*size=*/ 1u);
110     ScratchFile profile_file;
111     GenerateProfile(dex_files, profile_file.GetFile());
112     std::vector<std::string> argv;
113     std::string error_msg;
114     bool success = StartDex2OatCommandLine(&argv, &error_msg, /*use_runtime_bcp_and_image=*/ false);
115     ASSERT_TRUE(success) << error_msg;
116     argv.insert(argv.end(), {
117         "--profile-file=" + profile_file.GetFilename(),
118         "--runtime-arg",
119         "-Xbootclasspath:" + base_bcp_string,
120         "--runtime-arg",
121         "-Xbootclasspath-locations:" + base_bcp_locations_string,
122         "--boot-image=" + base_image_location,
123         "--dex-file=" + app_jar_name,
124         "--dex-location=" + app_jar_name,
125         "--oat-file=" + app_odex_name,
126         "--app-image-file=" + app_image_name,
127         "--initialize-app-image-classes=true",
128     });
129     success = RunDex2Oat(argv, &error_msg);
130     ASSERT_TRUE(success) << error_msg;
131   }
132 
133   std::string full_image_locations;
134   std::vector<std::unique_ptr<gc::space::ImageSpace>> boot_image_spaces;
135   MemMap extra_reservation;
136   auto load_boot_image = [&]() REQUIRES_SHARED(Locks::mutator_lock_) {
137     boot_image_spaces.clear();
138     extra_reservation = MemMap::Invalid();
139     return ImageSpace::LoadBootImage(bcp,
140                                      bcp_locations,
141                                      full_image_locations,
142                                      kRuntimeISA,
143                                      ImageSpaceLoadingOrder::kSystemFirst,
144                                      /*relocate=*/ false,
145                                      /*executable=*/ true,
146                                      /*is_zygote=*/ false,
147                                      /*extra_reservation_size=*/ 0u,
148                                      &boot_image_spaces,
149                                      &extra_reservation);
150   };
151 
152   const char test_string[] = "SharedBootImageExtensionTestString";
153   size_t test_string_length = std::size(test_string) - 1u;  // Equals UTF-16 length.
154   uint32_t hash = ComputeUtf16HashFromModifiedUtf8(test_string, test_string_length);
155   InternTable::Utf8String utf8_test_string(test_string_length, test_string, hash);
156   auto contains_test_string = [utf8_test_string](ImageSpace* space)
157       REQUIRES_SHARED(Locks::mutator_lock_) {
158     const ImageHeader& image_header = space->GetImageHeader();
159     if (image_header.GetInternedStringsSection().Size() != 0u) {
160       const uint8_t* data = space->Begin() + image_header.GetInternedStringsSection().Offset();
161       size_t read_count;
162       InternTable::UnorderedSet temp_set(data, /*make_copy_of_data=*/ false, &read_count);
163       return temp_set.find(utf8_test_string) != temp_set.end();
164     } else {
165       return false;
166     }
167   };
168 
169   // Load extensions and test for the presence of the test string.
170   ScopedObjectAccess soa(Thread::Current());
171   ASSERT_EQ(2u, extension_image_locations.size());
172   full_image_locations = base_image_location +
173                              ImageSpace::kComponentSeparator + extension_image_locations[0] +
174                              ImageSpace::kComponentSeparator + extension_image_locations[1];
175   bool success = load_boot_image();
176   ASSERT_TRUE(success);
177   ASSERT_EQ(bcp.size(), boot_image_spaces.size());
178   EXPECT_TRUE(contains_test_string(boot_image_spaces[boot_image_spaces.size() - 2u].get()));
179   // The string in the second extension should be replaced and removed from interned string section.
180   EXPECT_FALSE(contains_test_string(boot_image_spaces[boot_image_spaces.size() - 1u].get()));
181 
182   // Reload extensions in reverse order and test for the presence of the test string.
183   std::swap(bcp[bcp.size() - 2u], bcp[bcp.size() - 1u]);
184   std::swap(bcp_locations[bcp_locations.size() - 2u], bcp_locations[bcp_locations.size() - 1u]);
185   full_image_locations = base_image_location +
186                              ImageSpace::kComponentSeparator + extension_image_locations[1] +
187                              ImageSpace::kComponentSeparator + extension_image_locations[0];
188   success = load_boot_image();
189   ASSERT_TRUE(success);
190   ASSERT_EQ(bcp.size(), boot_image_spaces.size());
191   EXPECT_TRUE(contains_test_string(boot_image_spaces[boot_image_spaces.size() - 2u].get()));
192   // The string in the second extension should be replaced and removed from interned string section.
193   EXPECT_FALSE(contains_test_string(boot_image_spaces[boot_image_spaces.size() - 1u].get()));
194 
195   // Reload the image without the second extension.
196   bcp.erase(bcp.end() - 2u);
197   bcp_locations.erase(bcp_locations.end() - 2u);
198   full_image_locations =
199       base_image_location + ImageSpace::kComponentSeparator + extension_image_locations[0];
200   success = load_boot_image();
201   ASSERT_TRUE(success);
202   ASSERT_EQ(bcp.size(), boot_image_spaces.size());
203   ASSERT_TRUE(contains_test_string(boot_image_spaces[boot_image_spaces.size() - 1u].get()));
204 
205   // Load the app odex file and app image.
206   std::string error_msg;
207   std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
208                                                    app_odex_name.c_str(),
209                                                    app_odex_name.c_str(),
210                                                    /*executable=*/ false,
211                                                    /*low_4gb=*/ false,
212                                                    app_jar_name,
213                                                    &error_msg));
214   ASSERT_TRUE(odex_file != nullptr) << error_msg;
215   std::vector<ImageSpace*> non_owning_boot_image_spaces =
216       MakeNonOwningPointerVector(boot_image_spaces);
217   std::unique_ptr<ImageSpace> app_image_space = ImageSpace::CreateFromAppImage(
218       app_image_name.c_str(),
219       odex_file.get(),
220       ArrayRef<ImageSpace* const>(non_owning_boot_image_spaces),
221       &error_msg);
222   ASSERT_TRUE(app_image_space != nullptr) << error_msg;
223 
224   // The string in the app image should be replaced and removed from interned string section.
225   EXPECT_FALSE(contains_test_string(app_image_space.get()));
226 }
227 
TEST_F(DexoptTest,ValidateOatFile)228 TEST_F(DexoptTest, ValidateOatFile) {
229   std::string dex1 = GetScratchDir() + "/Dex1.jar";
230   std::string multidex1 = GetScratchDir() + "/MultiDex1.jar";
231   std::string dex2 = GetScratchDir() + "/Dex2.jar";
232   std::string oat_location = GetScratchDir() + "/Oat.oat";
233 
234   Copy(GetDexSrc1(), dex1);
235   Copy(GetMultiDexSrc1(), multidex1);
236   Copy(GetDexSrc2(), dex2);
237 
238   std::string error_msg;
239   std::vector<std::string> args;
240   args.push_back("--dex-file=" + dex1);
241   args.push_back("--dex-file=" + multidex1);
242   args.push_back("--dex-file=" + dex2);
243   args.push_back("--oat-file=" + oat_location);
244   ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
245 
246   std::unique_ptr<OatFile> oat(OatFile::Open(/*zip_fd=*/ -1,
247                                              oat_location.c_str(),
248                                              oat_location.c_str(),
249                                              /*executable=*/ false,
250                                              /*low_4gb=*/ false,
251                                              &error_msg));
252   ASSERT_TRUE(oat != nullptr) << error_msg;
253 
254   {
255     // Test opening the oat file also with explicit dex filenames.
256     std::vector<std::string> dex_filenames{ dex1, multidex1, dex2 };
257     std::unique_ptr<OatFile> oat2(OatFile::Open(/*zip_fd=*/ -1,
258                                                 oat_location.c_str(),
259                                                 oat_location.c_str(),
260                                                 /*executable=*/ false,
261                                                 /*low_4gb=*/ false,
262                                                 ArrayRef<const std::string>(dex_filenames),
263                                                 /*reservation=*/ nullptr,
264                                                 &error_msg));
265     ASSERT_TRUE(oat2 != nullptr) << error_msg;
266   }
267 
268   // Originally all the dex checksums should be up to date.
269   EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg;
270 
271   // Invalidate the dex1 checksum.
272   Copy(GetDexSrc2(), dex1);
273   EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg));
274 
275   // Restore the dex1 checksum.
276   Copy(GetDexSrc1(), dex1);
277   EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg;
278 
279   // Invalidate the non-main multidex checksum.
280   Copy(GetMultiDexSrc2(), multidex1);
281   EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg));
282 
283   // Restore the multidex checksum.
284   Copy(GetMultiDexSrc1(), multidex1);
285   EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg;
286 
287   // Invalidate the dex2 checksum.
288   Copy(GetDexSrc1(), dex2);
289   EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg));
290 
291   // restore the dex2 checksum.
292   Copy(GetDexSrc2(), dex2);
293   EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg;
294 
295   // Replace the multidex file with a non-multidex file.
296   Copy(GetDexSrc1(), multidex1);
297   EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg));
298 
299   // Restore the multidex file
300   Copy(GetMultiDexSrc1(), multidex1);
301   EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg;
302 
303   // Replace dex1 with a multidex file.
304   Copy(GetMultiDexSrc1(), dex1);
305   EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg));
306 
307   // Restore the dex1 file.
308   Copy(GetDexSrc1(), dex1);
309   EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg;
310 
311   // Remove the dex2 file.
312   EXPECT_EQ(0, unlink(dex2.c_str()));
313   EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg));
314 
315   // Restore the dex2 file.
316   Copy(GetDexSrc2(), dex2);
317   EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg;
318 
319   // Remove the multidex file.
320   EXPECT_EQ(0, unlink(multidex1.c_str()));
321   EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg));
322 }
323 
TEST_F(DexoptTest,Checksums)324 TEST_F(DexoptTest, Checksums) {
325   Runtime* runtime = Runtime::Current();
326   ASSERT_TRUE(runtime != nullptr);
327   ASSERT_FALSE(runtime->GetHeap()->GetBootImageSpaces().empty());
328 
329   std::vector<std::string> bcp = runtime->GetBootClassPath();
330   std::vector<std::string> bcp_locations = runtime->GetBootClassPathLocations();
331   std::vector<const DexFile*> dex_files = runtime->GetClassLinker()->GetBootClassPath();
332 
333   std::string error_msg;
334   auto create_and_verify = [&]() {
335     std::string checksums = gc::space::ImageSpace::GetBootClassPathChecksums(
336         ArrayRef<gc::space::ImageSpace* const>(runtime->GetHeap()->GetBootImageSpaces()),
337         ArrayRef<const DexFile* const>(dex_files));
338     return gc::space::ImageSpace::VerifyBootClassPathChecksums(
339         checksums,
340         android::base::Join(bcp_locations, ':'),
341         runtime->GetImageLocation(),
342         ArrayRef<const std::string>(bcp_locations),
343         ArrayRef<const std::string>(bcp),
344         kRuntimeISA,
345         gc::space::ImageSpaceLoadingOrder::kSystemFirst,
346         &error_msg);
347   };
348 
349   ASSERT_TRUE(create_and_verify()) << error_msg;
350 
351   std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
352   for (const std::string& src : { GetDexSrc1(), GetDexSrc2() }) {
353     std::vector<std::unique_ptr<const DexFile>> new_dex_files;
354     const ArtDexFileLoader dex_file_loader;
355     ASSERT_TRUE(dex_file_loader.Open(src.c_str(),
356                                      src,
357                                      /*verify=*/ true,
358                                      /*verify_checksum=*/ false,
359                                      &error_msg,
360                                      &new_dex_files))
361         << error_msg;
362 
363     bcp.push_back(src);
364     bcp_locations.push_back(src);
365     for (std::unique_ptr<const DexFile>& df : new_dex_files) {
366       dex_files.push_back(df.get());
367       opened_dex_files.push_back(std::move(df));
368     }
369 
370     ASSERT_TRUE(create_and_verify()) << error_msg;
371   }
372 }
373 
374 template <bool kImage, bool kRelocate, bool kImageDex2oat>
375 class ImageSpaceLoadingTest : public CommonRuntimeTest {
376  protected:
SetUpRuntimeOptions(RuntimeOptions * options)377   void SetUpRuntimeOptions(RuntimeOptions* options) override {
378     std::string image_location = GetCoreArtLocation();
379     if (!kImage) {
380       missing_image_base_ = std::make_unique<ScratchFile>();
381       image_location = missing_image_base_->GetFilename() + ".art";
382     }
383     options->emplace_back(android::base::StringPrintf("-Ximage:%s", image_location.c_str()),
384                           nullptr);
385     options->emplace_back(kRelocate ? "-Xrelocate" : "-Xnorelocate", nullptr);
386     options->emplace_back(kImageDex2oat ? "-Ximage-dex2oat" : "-Xnoimage-dex2oat", nullptr);
387 
388     // We want to test the relocation behavior of ImageSpace. As such, don't pretend we're a
389     // compiler.
390     callbacks_.reset();
391 
392     // Clear DEX2OATBOOTCLASSPATH environment variable used for boot image compilation.
393     // We don't want that environment variable to affect the behavior of this test.
394     CHECK(old_dex2oat_bcp_ == nullptr);
395     const char* old_dex2oat_bcp = getenv("DEX2OATBOOTCLASSPATH");
396     if (old_dex2oat_bcp != nullptr) {
397       old_dex2oat_bcp_.reset(strdup(old_dex2oat_bcp));
398       CHECK(old_dex2oat_bcp_ != nullptr);
399       unsetenv("DEX2OATBOOTCLASSPATH");
400     }
401   }
402 
TearDown()403   void TearDown() override {
404     if (old_dex2oat_bcp_ != nullptr) {
405       int result = setenv("DEX2OATBOOTCLASSPATH", old_dex2oat_bcp_.get(), /* replace */ 0);
406       CHECK_EQ(result, 0);
407       old_dex2oat_bcp_.reset();
408     }
409     missing_image_base_.reset();
410   }
411 
412  private:
413   std::unique_ptr<ScratchFile> missing_image_base_;
414   UniqueCPtr<const char[]> old_dex2oat_bcp_;
415 };
416 
417 using ImageSpaceDex2oatTest = ImageSpaceLoadingTest<false, true, true>;
TEST_F(ImageSpaceDex2oatTest,Test)418 TEST_F(ImageSpaceDex2oatTest, Test) {
419   EXPECT_FALSE(Runtime::Current()->GetHeap()->GetBootImageSpaces().empty());
420 }
421 
422 using ImageSpaceNoDex2oatTest = ImageSpaceLoadingTest<true, true, false>;
TEST_F(ImageSpaceNoDex2oatTest,Test)423 TEST_F(ImageSpaceNoDex2oatTest, Test) {
424   EXPECT_FALSE(Runtime::Current()->GetHeap()->GetBootImageSpaces().empty());
425 }
426 
427 using ImageSpaceNoRelocateNoDex2oatTest = ImageSpaceLoadingTest<true, false, false>;
TEST_F(ImageSpaceNoRelocateNoDex2oatTest,Test)428 TEST_F(ImageSpaceNoRelocateNoDex2oatTest, Test) {
429   EXPECT_FALSE(Runtime::Current()->GetHeap()->GetBootImageSpaces().empty());
430 }
431 
432 class NoAccessAndroidDataTest : public ImageSpaceLoadingTest<false, true, true> {
433  protected:
NoAccessAndroidDataTest()434   NoAccessAndroidDataTest() : quiet_(LogSeverity::FATAL) {}
435 
SetUpRuntimeOptions(RuntimeOptions * options)436   void SetUpRuntimeOptions(RuntimeOptions* options) override {
437     const char* android_data = getenv("ANDROID_DATA");
438     CHECK(android_data != nullptr);
439     old_android_data_ = android_data;
440     bad_android_data_ = old_android_data_ + "/no-android-data";
441     int result = setenv("ANDROID_DATA", bad_android_data_.c_str(), /* replace */ 1);
442     CHECK_EQ(result, 0) << strerror(errno);
443     result = mkdir(bad_android_data_.c_str(), /* mode */ 0700);
444     CHECK_EQ(result, 0) << strerror(errno);
445     // Create a regular file "dalvik_cache". GetDalvikCache() shall get EEXIST
446     // when trying to create a directory with the same name and creating a
447     // subdirectory for a particular architecture shall fail.
448     bad_dalvik_cache_ = bad_android_data_ + "/dalvik-cache";
449     int fd = creat(bad_dalvik_cache_.c_str(), /* mode */ 0);
450     CHECK_NE(fd, -1) << strerror(errno);
451     result = close(fd);
452     CHECK_EQ(result, 0) << strerror(errno);
453     ImageSpaceLoadingTest<false, true, true>::SetUpRuntimeOptions(options);
454   }
455 
TearDown()456   void TearDown() override {
457     ImageSpaceLoadingTest<false, true, true>::TearDown();
458     int result = unlink(bad_dalvik_cache_.c_str());
459     CHECK_EQ(result, 0) << strerror(errno);
460     result = rmdir(bad_android_data_.c_str());
461     CHECK_EQ(result, 0) << strerror(errno);
462     result = setenv("ANDROID_DATA", old_android_data_.c_str(), /* replace */ 1);
463     CHECK_EQ(result, 0) << strerror(errno);
464   }
465 
466  private:
467   ScopedLogSeverity quiet_;
468   std::string old_android_data_;
469   std::string bad_android_data_;
470   std::string bad_dalvik_cache_;
471 };
472 
TEST_F(NoAccessAndroidDataTest,Test)473 TEST_F(NoAccessAndroidDataTest, Test) {
474   EXPECT_TRUE(Runtime::Current()->GetHeap()->GetBootImageSpaces().empty());
475 }
476 
477 }  // namespace space
478 }  // namespace gc
479 }  // namespace art
480