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