Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 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 <memory> |
| 20 | |
| 21 | #include "common_runtime_test.h" |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 22 | #include "art_dex_file_loader.h" |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 23 | #include "dex_file_loader.h" |
| 24 | #include "mem_map.h" |
| 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | class CodeItemAccessorsTest : public CommonRuntimeTest {}; |
| 29 | |
| 30 | std::unique_ptr<const DexFile> CreateFakeDex(bool compact_dex) { |
| 31 | std::string error_msg; |
| 32 | std::unique_ptr<MemMap> map( |
| 33 | MemMap::MapAnonymous(/*name*/ "map", |
| 34 | /*addr*/ nullptr, |
| 35 | /*byte_count*/ kPageSize, |
| 36 | PROT_READ | PROT_WRITE, |
| 37 | /*low_4gb*/ false, |
| 38 | /*reuse*/ false, |
| 39 | &error_msg)); |
| 40 | CHECK(map != nullptr) << error_msg; |
| 41 | if (compact_dex) { |
| 42 | CompactDexFile::WriteMagic(map->Begin()); |
| 43 | CompactDexFile::WriteCurrentVersion(map->Begin()); |
| 44 | } else { |
| 45 | StandardDexFile::WriteMagic(map->Begin()); |
| 46 | StandardDexFile::WriteCurrentVersion(map->Begin()); |
| 47 | } |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 48 | const ArtDexFileLoader dex_file_loader; |
| 49 | std::unique_ptr<const DexFile> dex(dex_file_loader.Open("location", |
| 50 | /*location_checksum*/ 123, |
| 51 | std::move(map), |
| 52 | /*verify*/false, |
| 53 | /*verify_checksum*/false, |
| 54 | &error_msg)); |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 55 | CHECK(dex != nullptr) << error_msg; |
| 56 | return dex; |
| 57 | } |
| 58 | |
| 59 | TEST(CodeItemAccessorsTest, TestDexInstructionsAccessor) { |
| 60 | MemMap::Init(); |
| 61 | std::unique_ptr<const DexFile> standard_dex(CreateFakeDex(/*compact_dex*/false)); |
| 62 | ASSERT_TRUE(standard_dex != nullptr); |
| 63 | std::unique_ptr<const DexFile> compact_dex(CreateFakeDex(/*compact_dex*/true)); |
| 64 | ASSERT_TRUE(compact_dex != nullptr); |
| 65 | static constexpr uint16_t kRegisterSize = 1; |
| 66 | static constexpr uint16_t kInsSize = 2; |
| 67 | static constexpr uint16_t kOutsSize = 3; |
| 68 | static constexpr uint16_t kTriesSize = 4; |
| 69 | // debug_info_off_ is not accessible from the helpers yet. |
| 70 | static constexpr size_t kInsnsSizeInCodeUnits = 5; |
| 71 | |
| 72 | auto verify_code_item = [&](const DexFile* dex, |
| 73 | const DexFile::CodeItem* item, |
| 74 | const uint16_t* insns) { |
Mathieu Chartier | 698ebbc | 2018-01-05 11:00:42 -0800 | [diff] [blame] | 75 | CodeItemInstructionAccessor insns_accessor(*dex, item); |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 76 | EXPECT_TRUE(insns_accessor.HasCodeItem()); |
| 77 | ASSERT_EQ(insns_accessor.InsnsSizeInCodeUnits(), kInsnsSizeInCodeUnits); |
| 78 | EXPECT_EQ(insns_accessor.Insns(), insns); |
| 79 | |
Mathieu Chartier | 698ebbc | 2018-01-05 11:00:42 -0800 | [diff] [blame] | 80 | CodeItemDataAccessor data_accessor(*dex, item); |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 81 | EXPECT_TRUE(data_accessor.HasCodeItem()); |
| 82 | EXPECT_EQ(data_accessor.InsnsSizeInCodeUnits(), kInsnsSizeInCodeUnits); |
| 83 | EXPECT_EQ(data_accessor.Insns(), insns); |
| 84 | EXPECT_EQ(data_accessor.RegistersSize(), kRegisterSize); |
| 85 | EXPECT_EQ(data_accessor.InsSize(), kInsSize); |
| 86 | EXPECT_EQ(data_accessor.OutsSize(), kOutsSize); |
| 87 | EXPECT_EQ(data_accessor.TriesSize(), kTriesSize); |
| 88 | }; |
| 89 | |
Mathieu Chartier | 73f21d4 | 2018-01-02 14:26:50 -0800 | [diff] [blame] | 90 | StandardDexFile::CodeItem* dex_code_item = |
| 91 | reinterpret_cast<StandardDexFile::CodeItem*>(const_cast<uint8_t*>(standard_dex->Begin())); |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 92 | dex_code_item->registers_size_ = kRegisterSize; |
| 93 | dex_code_item->ins_size_ = kInsSize; |
| 94 | dex_code_item->outs_size_ = kOutsSize; |
| 95 | dex_code_item->tries_size_ = kTriesSize; |
| 96 | dex_code_item->insns_size_in_code_units_ = kInsnsSizeInCodeUnits; |
Mathieu Chartier | 73f21d4 | 2018-01-02 14:26:50 -0800 | [diff] [blame] | 97 | verify_code_item(standard_dex.get(), dex_code_item, dex_code_item->insns_); |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 98 | |
Mathieu Chartier | 73f21d4 | 2018-01-02 14:26:50 -0800 | [diff] [blame] | 99 | CompactDexFile::CodeItem* cdex_code_item = |
| 100 | reinterpret_cast<CompactDexFile::CodeItem*>(const_cast<uint8_t*>(compact_dex->Begin())); |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 101 | cdex_code_item->registers_size_ = kRegisterSize; |
| 102 | cdex_code_item->ins_size_ = kInsSize; |
| 103 | cdex_code_item->outs_size_ = kOutsSize; |
| 104 | cdex_code_item->tries_size_ = kTriesSize; |
| 105 | cdex_code_item->insns_size_in_code_units_ = kInsnsSizeInCodeUnits; |
| 106 | verify_code_item(compact_dex.get(), cdex_code_item, cdex_code_item->insns_); |
| 107 | } |
| 108 | |
| 109 | } // namespace art |