blob: 02b95e68d75abe8d7918213f2701b28ffe873b10 [file] [log] [blame]
Mathieu Chartier8892c6b2018-01-09 15:10:17 -08001/*
2 * Copyright (C) 2018 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 <vector>
18#include <sys/mman.h>
19
20#include "base/logging.h"
21#include "dex/compact_dex_debug_info.h"
22#include "gtest/gtest.h"
23#include "mem_map.h"
24
25namespace art {
26
27TEST(CompactDexDebugInfoTest, TestBuildAndAccess) {
28 MemMap::Init();
29
30 const size_t kDebugInfoMinOffset = 1234567;
31 std::vector<uint32_t> offsets = {
32 0, 17, 2, 3, 11, 0, 0, 0, 0, 1, 0, 1552, 100, 122, 44, 1234567, 0, 0,
33 std::numeric_limits<uint32_t>::max() - kDebugInfoMinOffset, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
34 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12,
35 };
36 // Add some large offset since the debug info section will never be that close to the beginning
37 // of the file.
38 for (uint32_t& offset : offsets) {
39 if (offset != 0u) {
40 offset += kDebugInfoMinOffset;
41 }
42 }
43
44 std::vector<uint8_t> data;
45 uint32_t base_offset = 0;
46 uint32_t table_offset = 0;
47 CompactDexDebugInfoOffsetTable::Build(offsets,
48 /*out*/ &data,
49 /*out*/ &base_offset,
50 /*out*/ &table_offset);
51 EXPECT_GE(base_offset, kDebugInfoMinOffset);
52 EXPECT_LT(table_offset, data.size());
53 ASSERT_GT(data.size(), 0u);
54 const size_t before_size = offsets.size() * sizeof(offsets.front());
55 EXPECT_LT(data.size(), before_size);
56
57 // Note that the accessor requires the data to be aligned. Use memmap to accomplish this.
58 std::string error_msg;
59 // Leave some extra room since we don't copy the table at the start (for testing).
60 constexpr size_t kExtraOffset = 4 * 128;
61 std::unique_ptr<MemMap> fake_dex(MemMap::MapAnonymous("fake dex",
62 nullptr,
63 data.size() + kExtraOffset,
64 PROT_READ | PROT_WRITE,
65 /*low_4gb*/ false,
66 /*reuse*/ false,
67 &error_msg));
68 ASSERT_TRUE(fake_dex != nullptr) << error_msg;
69 std::copy(data.begin(), data.end(), fake_dex->Begin() + kExtraOffset);
70
71 CompactDexDebugInfoOffsetTable::Accessor accessor(fake_dex->Begin() + kExtraOffset,
72 base_offset,
73 table_offset);
74 for (size_t i = 0; i < offsets.size(); ++i) {
75 EXPECT_EQ(offsets[i], accessor.GetDebugInfoOffset(i));
76 }
77
78 // Sort to produce a try and produce a smaller table. This happens because the leb diff is smaller
79 // for sorted increasing order.
80 std::sort(offsets.begin(), offsets.end());
81 std::vector<uint8_t> sorted_data;
82 CompactDexDebugInfoOffsetTable::Build(offsets,
83 /*out*/ &sorted_data,
84 /*out*/ &base_offset,
85 /*out*/ &table_offset);
86 EXPECT_LT(sorted_data.size(), data.size());
87 {
88 ScopedLogSeverity sls(LogSeverity::INFO);
89 LOG(INFO) << "raw size " << before_size
90 << " table size " << data.size()
91 << " sorted table size " << sorted_data.size();
92 }
93}
94
95} // namespace art