• 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   static constexpr uint16_t kRegisterSize = 2;
65   static constexpr uint16_t kInsSize = 1;
66   static constexpr uint16_t kOutsSize = 3;
67   static constexpr uint16_t kTriesSize = 4;
68   // debug_info_off_ is not accessible from the helpers yet.
69   static constexpr size_t kInsnsSizeInCodeUnits = 5;
70 
71   auto verify_code_item = [&](const DexFile* dex,
72                               const dex::CodeItem* item,
73                               const uint16_t* insns) {
74     CodeItemInstructionAccessor insns_accessor(*dex, item);
75     EXPECT_TRUE(insns_accessor.HasCodeItem());
76     ASSERT_EQ(insns_accessor.InsnsSizeInCodeUnits(), kInsnsSizeInCodeUnits);
77     EXPECT_EQ(insns_accessor.Insns(), insns);
78 
79     CodeItemDataAccessor data_accessor(*dex, item);
80     EXPECT_TRUE(data_accessor.HasCodeItem());
81     EXPECT_EQ(data_accessor.InsnsSizeInCodeUnits(), kInsnsSizeInCodeUnits);
82     EXPECT_EQ(data_accessor.Insns(), insns);
83     EXPECT_EQ(data_accessor.RegistersSize(), kRegisterSize);
84     EXPECT_EQ(data_accessor.InsSize(), kInsSize);
85     EXPECT_EQ(data_accessor.OutsSize(), kOutsSize);
86     EXPECT_EQ(data_accessor.TriesSize(), kTriesSize);
87   };
88 
89   StandardDexFile::CodeItem* dex_code_item =
90       reinterpret_cast<StandardDexFile::CodeItem*>(const_cast<uint8_t*>(standard_dex->Begin()));
91   dex_code_item->registers_size_ = kRegisterSize;
92   dex_code_item->ins_size_ = kInsSize;
93   dex_code_item->outs_size_ = kOutsSize;
94   dex_code_item->tries_size_ = kTriesSize;
95   dex_code_item->insns_size_in_code_units_ = kInsnsSizeInCodeUnits;
96   verify_code_item(standard_dex.get(), dex_code_item, dex_code_item->insns_);
97 }
98 
99 }  // namespace art
100