• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "code_item_accessors-inl.h"
18 
19 #include <sys/mman.h>
20 #include <memory>
21 #include <vector>
22 
23 #include "base/mem_map.h"
24 #include "dex_file_loader.h"
25 #include "gtest/gtest.h"
26 
27 namespace art {
28 
29 class CodeItemAccessorsTest : public testing::Test {};
30 
CreateFakeDex(bool compact_dex,std::vector<uint8_t> * data)31 std::unique_ptr<const DexFile> CreateFakeDex(bool compact_dex, std::vector<uint8_t>* data) {
32   data->resize(MemMap::GetPageSize());
33   if (compact_dex) {
34     CompactDexFile::Header* header =
35         const_cast<CompactDexFile::Header*>(CompactDexFile::Header::At(data->data()));
36     CompactDexFile::WriteMagic(header->magic_.data());
37     CompactDexFile::WriteCurrentVersion(header->magic_.data());
38     header->data_off_ = 0;
39     header->data_size_ = data->size();
40     header->file_size_ = data->size();
41   } else {
42     auto* header = reinterpret_cast<DexFile::Header*>(data->data());
43     StandardDexFile::WriteMagic(data->data());
44     StandardDexFile::WriteCurrentVersion(data->data());
45     header->header_size_ = sizeof(*header);
46     header->file_size_ = data->size();
47   }
48   DexFileLoader dex_file_loader(data->data(), data->size(), "location");
49   std::string error_msg;
50   std::unique_ptr<const DexFile> dex(dex_file_loader.Open(/*location_checksum=*/123,
51                                                           /*oat_dex_file=*/nullptr,
52                                                           /*verify=*/false,
53                                                           /*verify_checksum=*/false,
54                                                           &error_msg));
55   CHECK(dex != nullptr) << error_msg;
56   return dex;
57 }
58 
TEST(CodeItemAccessorsTest,TestDexInstructionsAccessor)59 TEST(CodeItemAccessorsTest, TestDexInstructionsAccessor) {
60   std::vector<uint8_t> standard_dex_data;
61   std::unique_ptr<const DexFile> standard_dex(CreateFakeDex(/*compact_dex=*/false,
62                                                             &standard_dex_data));
63   ASSERT_TRUE(standard_dex != nullptr);
64   std::vector<uint8_t> compact_dex_data;
65   std::unique_ptr<const DexFile> compact_dex(CreateFakeDex(/*compact_dex=*/true,
66                                                            &compact_dex_data));
67   ASSERT_TRUE(compact_dex != nullptr);
68   static constexpr uint16_t kRegisterSize = 2;
69   static constexpr uint16_t kInsSize = 1;
70   static constexpr uint16_t kOutsSize = 3;
71   static constexpr uint16_t kTriesSize = 4;
72   // debug_info_off_ is not accessible from the helpers yet.
73   static constexpr size_t kInsnsSizeInCodeUnits = 5;
74 
75   auto verify_code_item = [&](const DexFile* dex,
76                               const dex::CodeItem* item,
77                               const uint16_t* insns) {
78     CodeItemInstructionAccessor insns_accessor(*dex, item);
79     EXPECT_TRUE(insns_accessor.HasCodeItem());
80     ASSERT_EQ(insns_accessor.InsnsSizeInCodeUnits(), kInsnsSizeInCodeUnits);
81     EXPECT_EQ(insns_accessor.Insns(), insns);
82 
83     CodeItemDataAccessor data_accessor(*dex, item);
84     EXPECT_TRUE(data_accessor.HasCodeItem());
85     EXPECT_EQ(data_accessor.InsnsSizeInCodeUnits(), kInsnsSizeInCodeUnits);
86     EXPECT_EQ(data_accessor.Insns(), insns);
87     EXPECT_EQ(data_accessor.RegistersSize(), kRegisterSize);
88     EXPECT_EQ(data_accessor.InsSize(), kInsSize);
89     EXPECT_EQ(data_accessor.OutsSize(), kOutsSize);
90     EXPECT_EQ(data_accessor.TriesSize(), kTriesSize);
91   };
92 
93   StandardDexFile::CodeItem* dex_code_item =
94       reinterpret_cast<StandardDexFile::CodeItem*>(const_cast<uint8_t*>(standard_dex->Begin()));
95   dex_code_item->registers_size_ = kRegisterSize;
96   dex_code_item->ins_size_ = kInsSize;
97   dex_code_item->outs_size_ = kOutsSize;
98   dex_code_item->tries_size_ = kTriesSize;
99   dex_code_item->insns_size_in_code_units_ = kInsnsSizeInCodeUnits;
100   verify_code_item(standard_dex.get(), dex_code_item, dex_code_item->insns_);
101 
102   CompactDexFile::CodeItem* cdex_code_item =
103       reinterpret_cast<CompactDexFile::CodeItem*>(const_cast<uint8_t*>(compact_dex->Begin() +
104           CompactDexFile::CodeItem::kMaxPreHeaderSize * sizeof(uint16_t)));
105   std::vector<uint16_t> preheader;
106   cdex_code_item->Create(kRegisterSize,
107                          kInsSize,
108                          kOutsSize,
109                          kTriesSize,
110                          kInsnsSizeInCodeUnits,
111                          cdex_code_item->GetPreHeader());
112 
113   verify_code_item(compact_dex.get(), cdex_code_item, cdex_code_item->insns_);
114 }
115 
116 }  // namespace art
117