blob: 1bd12a6f094a2c773efa240cfc4b7ae55e41a6d7 [file] [log] [blame]
Mathieu Chartier69147f12017-11-06 20:02:24 -08001/*
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
David Sehr0225f8e2018-01-31 08:52:24 +000019#include <sys/mman.h>
Mathieu Chartier69147f12017-11-06 20:02:24 -080020#include <memory>
21
22#include "common_runtime_test.h"
David Sehr013fd802018-01-11 22:55:24 -080023#include "art_dex_file_loader.h"
Mathieu Chartier69147f12017-11-06 20:02:24 -080024#include "dex_file_loader.h"
25#include "mem_map.h"
26
27namespace art {
28
29class CodeItemAccessorsTest : public CommonRuntimeTest {};
30
31std::unique_ptr<const DexFile> CreateFakeDex(bool compact_dex) {
32 std::string error_msg;
33 std::unique_ptr<MemMap> map(
34 MemMap::MapAnonymous(/*name*/ "map",
35 /*addr*/ nullptr,
36 /*byte_count*/ kPageSize,
37 PROT_READ | PROT_WRITE,
38 /*low_4gb*/ false,
39 /*reuse*/ false,
40 &error_msg));
41 CHECK(map != nullptr) << error_msg;
42 if (compact_dex) {
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -080043 CompactDexFile::Header* header =
44 const_cast<CompactDexFile::Header*>(CompactDexFile::Header::At(map->Begin()));
45 CompactDexFile::WriteMagic(header->magic_);
46 CompactDexFile::WriteCurrentVersion(header->magic_);
47 header->data_off_ = 0;
48 header->data_size_ = map->Size();
Mathieu Chartier69147f12017-11-06 20:02:24 -080049 } else {
50 StandardDexFile::WriteMagic(map->Begin());
51 StandardDexFile::WriteCurrentVersion(map->Begin());
52 }
David Sehr013fd802018-01-11 22:55:24 -080053 const ArtDexFileLoader dex_file_loader;
54 std::unique_ptr<const DexFile> dex(dex_file_loader.Open("location",
55 /*location_checksum*/ 123,
56 std::move(map),
57 /*verify*/false,
58 /*verify_checksum*/false,
59 &error_msg));
Mathieu Chartier69147f12017-11-06 20:02:24 -080060 CHECK(dex != nullptr) << error_msg;
61 return dex;
62}
63
64TEST(CodeItemAccessorsTest, TestDexInstructionsAccessor) {
65 MemMap::Init();
66 std::unique_ptr<const DexFile> standard_dex(CreateFakeDex(/*compact_dex*/false));
67 ASSERT_TRUE(standard_dex != nullptr);
68 std::unique_ptr<const DexFile> compact_dex(CreateFakeDex(/*compact_dex*/true));
69 ASSERT_TRUE(compact_dex != nullptr);
Mathieu Chartier8740c662018-01-11 14:50:02 -080070 static constexpr uint16_t kRegisterSize = 2;
71 static constexpr uint16_t kInsSize = 1;
Mathieu Chartier69147f12017-11-06 20:02:24 -080072 static constexpr uint16_t kOutsSize = 3;
73 static constexpr uint16_t kTriesSize = 4;
74 // debug_info_off_ is not accessible from the helpers yet.
75 static constexpr size_t kInsnsSizeInCodeUnits = 5;
76
77 auto verify_code_item = [&](const DexFile* dex,
78 const DexFile::CodeItem* item,
79 const uint16_t* insns) {
Mathieu Chartier698ebbc2018-01-05 11:00:42 -080080 CodeItemInstructionAccessor insns_accessor(*dex, item);
Mathieu Chartier69147f12017-11-06 20:02:24 -080081 EXPECT_TRUE(insns_accessor.HasCodeItem());
82 ASSERT_EQ(insns_accessor.InsnsSizeInCodeUnits(), kInsnsSizeInCodeUnits);
83 EXPECT_EQ(insns_accessor.Insns(), insns);
84
Mathieu Chartier698ebbc2018-01-05 11:00:42 -080085 CodeItemDataAccessor data_accessor(*dex, item);
Mathieu Chartier69147f12017-11-06 20:02:24 -080086 EXPECT_TRUE(data_accessor.HasCodeItem());
87 EXPECT_EQ(data_accessor.InsnsSizeInCodeUnits(), kInsnsSizeInCodeUnits);
88 EXPECT_EQ(data_accessor.Insns(), insns);
89 EXPECT_EQ(data_accessor.RegistersSize(), kRegisterSize);
90 EXPECT_EQ(data_accessor.InsSize(), kInsSize);
91 EXPECT_EQ(data_accessor.OutsSize(), kOutsSize);
92 EXPECT_EQ(data_accessor.TriesSize(), kTriesSize);
93 };
94
Mathieu Chartier73f21d42018-01-02 14:26:50 -080095 StandardDexFile::CodeItem* dex_code_item =
96 reinterpret_cast<StandardDexFile::CodeItem*>(const_cast<uint8_t*>(standard_dex->Begin()));
Mathieu Chartier69147f12017-11-06 20:02:24 -080097 dex_code_item->registers_size_ = kRegisterSize;
98 dex_code_item->ins_size_ = kInsSize;
99 dex_code_item->outs_size_ = kOutsSize;
100 dex_code_item->tries_size_ = kTriesSize;
101 dex_code_item->insns_size_in_code_units_ = kInsnsSizeInCodeUnits;
Mathieu Chartier73f21d42018-01-02 14:26:50 -0800102 verify_code_item(standard_dex.get(), dex_code_item, dex_code_item->insns_);
Mathieu Chartier69147f12017-11-06 20:02:24 -0800103
Mathieu Chartier73f21d42018-01-02 14:26:50 -0800104 CompactDexFile::CodeItem* cdex_code_item =
Mathieu Chartier8740c662018-01-11 14:50:02 -0800105 reinterpret_cast<CompactDexFile::CodeItem*>(const_cast<uint8_t*>(compact_dex->Begin() +
106 CompactDexFile::CodeItem::kMaxPreHeaderSize * sizeof(uint16_t)));
107 std::vector<uint16_t> preheader;
108 cdex_code_item->Create(kRegisterSize,
109 kInsSize,
110 kOutsSize,
111 kTriesSize,
112 kInsnsSizeInCodeUnits,
113 cdex_code_item->GetPreHeader());
114
Mathieu Chartier69147f12017-11-06 20:02:24 -0800115 verify_code_item(compact_dex.get(), cdex_code_item, cdex_code_item->insns_);
116}
117
118} // namespace art