blob: c5891f9d46f6bfc9d7666d44661c77cc31394016 [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>
David Sehre1123402018-02-01 02:46:18 -080021#include <vector>
Mathieu Chartier69147f12017-11-06 20:02:24 -080022
Mathieu Chartier69147f12017-11-06 20:02:24 -080023#include "dex_file_loader.h"
David Sehre1123402018-02-01 02:46:18 -080024#include "gtest/gtest.h"
Mathieu Chartier69147f12017-11-06 20:02:24 -080025
26namespace art {
27
David Sehre1123402018-02-01 02:46:18 -080028class CodeItemAccessorsTest : public testing::Test {};
Mathieu Chartier69147f12017-11-06 20:02:24 -080029
David Sehre1123402018-02-01 02:46:18 -080030std::unique_ptr<const DexFile> CreateFakeDex(bool compact_dex, std::vector<uint8_t>* data) {
31 data->resize(kPageSize);
Mathieu Chartier69147f12017-11-06 20:02:24 -080032 if (compact_dex) {
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -080033 CompactDexFile::Header* header =
David Sehre1123402018-02-01 02:46:18 -080034 const_cast<CompactDexFile::Header*>(CompactDexFile::Header::At(data->data()));
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -080035 CompactDexFile::WriteMagic(header->magic_);
36 CompactDexFile::WriteCurrentVersion(header->magic_);
37 header->data_off_ = 0;
David Sehre1123402018-02-01 02:46:18 -080038 header->data_size_ = data->size();
Mathieu Chartier69147f12017-11-06 20:02:24 -080039 } else {
David Sehre1123402018-02-01 02:46:18 -080040 StandardDexFile::WriteMagic(data->data());
41 StandardDexFile::WriteCurrentVersion(data->data());
Mathieu Chartier69147f12017-11-06 20:02:24 -080042 }
David Sehre1123402018-02-01 02:46:18 -080043 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",
Andreas Gampe0de385f2018-10-11 11:11:13 -070048 /*location_checksum=*/ 123,
49 /*oat_dex_file=*/nullptr,
50 /*verify=*/false,
51 /*verify_checksum=*/false,
David Sehr013fd802018-01-11 22:55:24 -080052 &error_msg));
Mathieu Chartier69147f12017-11-06 20:02:24 -080053 CHECK(dex != nullptr) << error_msg;
54 return dex;
55}
56
57TEST(CodeItemAccessorsTest, TestDexInstructionsAccessor) {
David Sehre1123402018-02-01 02:46:18 -080058 std::vector<uint8_t> standard_dex_data;
Andreas Gampe0de385f2018-10-11 11:11:13 -070059 std::unique_ptr<const DexFile> standard_dex(CreateFakeDex(/*compact_dex=*/false,
David Sehre1123402018-02-01 02:46:18 -080060 &standard_dex_data));
Mathieu Chartier69147f12017-11-06 20:02:24 -080061 ASSERT_TRUE(standard_dex != nullptr);
David Sehre1123402018-02-01 02:46:18 -080062 std::vector<uint8_t> compact_dex_data;
Andreas Gampe0de385f2018-10-11 11:11:13 -070063 std::unique_ptr<const DexFile> compact_dex(CreateFakeDex(/*compact_dex=*/true,
David Sehre1123402018-02-01 02:46:18 -080064 &compact_dex_data));
Mathieu Chartier69147f12017-11-06 20:02:24 -080065 ASSERT_TRUE(compact_dex != nullptr);
Mathieu Chartier8740c662018-01-11 14:50:02 -080066 static constexpr uint16_t kRegisterSize = 2;
67 static constexpr uint16_t kInsSize = 1;
Mathieu Chartier69147f12017-11-06 20:02:24 -080068 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,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080074 const dex::CodeItem* item,
Mathieu Chartier69147f12017-11-06 20:02:24 -080075 const uint16_t* insns) {
Mathieu Chartier698ebbc2018-01-05 11:00:42 -080076 CodeItemInstructionAccessor insns_accessor(*dex, item);
Mathieu Chartier69147f12017-11-06 20:02:24 -080077 EXPECT_TRUE(insns_accessor.HasCodeItem());
78 ASSERT_EQ(insns_accessor.InsnsSizeInCodeUnits(), kInsnsSizeInCodeUnits);
79 EXPECT_EQ(insns_accessor.Insns(), insns);
80
Mathieu Chartier698ebbc2018-01-05 11:00:42 -080081 CodeItemDataAccessor data_accessor(*dex, item);
Mathieu Chartier69147f12017-11-06 20:02:24 -080082 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
Mathieu Chartier73f21d42018-01-02 14:26:50 -080091 StandardDexFile::CodeItem* dex_code_item =
92 reinterpret_cast<StandardDexFile::CodeItem*>(const_cast<uint8_t*>(standard_dex->Begin()));
Mathieu Chartier69147f12017-11-06 20:02:24 -080093 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;
Mathieu Chartier73f21d42018-01-02 14:26:50 -080098 verify_code_item(standard_dex.get(), dex_code_item, dex_code_item->insns_);
Mathieu Chartier69147f12017-11-06 20:02:24 -080099
Mathieu Chartier73f21d42018-01-02 14:26:50 -0800100 CompactDexFile::CodeItem* cdex_code_item =
Mathieu Chartier8740c662018-01-11 14:50:02 -0800101 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
Mathieu Chartier69147f12017-11-06 20:02:24 -0800111 verify_code_item(compact_dex.get(), cdex_code_item, cdex_code_item->insns_);
112}
113
114} // namespace art