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