blob: e04fa988871ab5a510bcb8193756d5a5902935ba [file] [log] [blame]
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001/*
2 * Copyright (C) 2014 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 "stack_map.h"
18#include "stack_map_stream.h"
19#include "utils/arena_bit_vector.h"
20
21#include "gtest/gtest.h"
22
23namespace art {
24
Roland Levillaina2d8ec62015-03-12 15:25:29 +000025static bool SameBits(MemoryRegion region, const BitVector& bit_vector) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010026 for (size_t i = 0; i < region.size_in_bits(); ++i) {
27 if (region.LoadBit(i) != bit_vector.IsBitSet(i)) {
28 return false;
29 }
30 }
31 return true;
32}
33
Roland Levillaina552e1c2015-03-26 15:01:03 +000034using Kind = DexRegisterLocation::Kind;
35
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010036TEST(StackMapTest, Test1) {
37 ArenaPool pool;
38 ArenaAllocator arena(&pool);
Nicolas Geoffray39468442014-09-02 15:17:15 +010039 StackMapStream stream(&arena);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010040
41 ArenaBitVector sp_mask(&arena, 0, false);
Roland Levillain12baf472015-03-05 12:41:42 +000042 size_t number_of_dex_registers = 2;
Calin Juravle4f46ac52015-04-23 18:47:21 +010043 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010044 stream.AddDexRegisterEntry(Kind::kInStack, 0); // Short location.
45 stream.AddDexRegisterEntry(Kind::kConstant, -2); // Short location.
Calin Juravle4f46ac52015-04-23 18:47:21 +010046 stream.EndStackMapEntry();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010047
Calin Juravle4f46ac52015-04-23 18:47:21 +010048 size_t size = stream.PrepareForFillIn();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010049 void* memory = arena.Alloc(size, kArenaAllocMisc);
50 MemoryRegion region(memory, size);
51 stream.FillIn(region);
52
Nicolas Geoffray39468442014-09-02 15:17:15 +010053 CodeInfo code_info(region);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010054 ASSERT_EQ(0u, code_info.GetStackMaskSize());
55 ASSERT_EQ(1u, code_info.GetNumberOfStackMaps());
56
Roland Levillaina552e1c2015-03-26 15:01:03 +000057 uint32_t number_of_location_catalog_entries =
58 code_info.GetNumberOfDexRegisterLocationCatalogEntries();
59 ASSERT_EQ(2u, number_of_location_catalog_entries);
60 DexRegisterLocationCatalog location_catalog = code_info.GetDexRegisterLocationCatalog();
61 // The Dex register location catalog contains:
62 // - one 1-byte short Dex register location, and
63 // - one 5-byte large Dex register location.
64 size_t expected_location_catalog_size = 1u + 5u;
65 ASSERT_EQ(expected_location_catalog_size, location_catalog.Size());
66
Nicolas Geoffray39468442014-09-02 15:17:15 +010067 StackMap stack_map = code_info.GetStackMapAt(0);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010068 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010069 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64)));
Nicolas Geoffray004c2302015-03-20 10:06:38 +000070 ASSERT_EQ(0u, stack_map.GetDexPc(code_info));
71 ASSERT_EQ(64u, stack_map.GetNativePcOffset(code_info));
72 ASSERT_EQ(0x3u, stack_map.GetRegisterMask(code_info));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010073
Nicolas Geoffray004c2302015-03-20 10:06:38 +000074 MemoryRegion stack_mask = stack_map.GetStackMask(code_info);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010075 ASSERT_TRUE(SameBits(stack_mask, sp_mask));
76
Nicolas Geoffray004c2302015-03-20 10:06:38 +000077 ASSERT_TRUE(stack_map.HasDexRegisterMap(code_info));
Roland Levillaina552e1c2015-03-26 15:01:03 +000078 DexRegisterMap dex_register_map =
79 code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
80 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(0));
81 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(1));
82 ASSERT_EQ(2u, dex_register_map.GetNumberOfLiveDexRegisters(number_of_dex_registers));
83 // The Dex register map contains:
84 // - one 1-byte live bit mask, and
85 // - one 1-byte set of location catalog entry indices composed of two 2-bit values.
86 size_t expected_dex_register_map_size = 1u + 1u;
87 ASSERT_EQ(expected_dex_register_map_size, dex_register_map.Size());
88
89 ASSERT_EQ(Kind::kInStack,
90 dex_register_map.GetLocationKind(0, number_of_dex_registers, code_info));
91 ASSERT_EQ(Kind::kConstant,
92 dex_register_map.GetLocationKind(1, number_of_dex_registers, code_info));
93 ASSERT_EQ(Kind::kInStack,
94 dex_register_map.GetLocationInternalKind(0, number_of_dex_registers, code_info));
95 ASSERT_EQ(Kind::kConstantLargeValue,
96 dex_register_map.GetLocationInternalKind(1, number_of_dex_registers, code_info));
97 ASSERT_EQ(0, dex_register_map.GetStackOffsetInBytes(0, number_of_dex_registers, code_info));
98 ASSERT_EQ(-2, dex_register_map.GetConstant(1, number_of_dex_registers, code_info));
99
100 size_t index0 = dex_register_map.GetLocationCatalogEntryIndex(
101 0, number_of_dex_registers, number_of_location_catalog_entries);
102 size_t index1 = dex_register_map.GetLocationCatalogEntryIndex(
103 1, number_of_dex_registers, number_of_location_catalog_entries);
104 ASSERT_EQ(0u, index0);
105 ASSERT_EQ(1u, index1);
106 DexRegisterLocation location0 = location_catalog.GetDexRegisterLocation(index0);
107 DexRegisterLocation location1 = location_catalog.GetDexRegisterLocation(index1);
108 ASSERT_EQ(Kind::kInStack, location0.GetKind());
109 ASSERT_EQ(Kind::kConstant, location1.GetKind());
110 ASSERT_EQ(Kind::kInStack, location0.GetInternalKind());
111 ASSERT_EQ(Kind::kConstantLargeValue, location1.GetInternalKind());
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000112 ASSERT_EQ(0, location0.GetValue());
113 ASSERT_EQ(-2, location1.GetValue());
Roland Levillain12baf472015-03-05 12:41:42 +0000114
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000115 ASSERT_FALSE(stack_map.HasInlineInfo(code_info));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100116}
117
118TEST(StackMapTest, Test2) {
119 ArenaPool pool;
120 ArenaAllocator arena(&pool);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100121 StackMapStream stream(&arena);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100122
123 ArenaBitVector sp_mask1(&arena, 0, true);
124 sp_mask1.SetBit(2);
125 sp_mask1.SetBit(4);
Roland Levillain12baf472015-03-05 12:41:42 +0000126 size_t number_of_dex_registers = 2;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100127 size_t number_of_dex_registers_in_inline_info = 0;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100128 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask1, number_of_dex_registers, 2);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100129 stream.AddDexRegisterEntry(Kind::kInStack, 0); // Short location.
130 stream.AddDexRegisterEntry(Kind::kConstant, -2); // Large location.
131 stream.BeginInlineInfoEntry(82, 3, number_of_dex_registers_in_inline_info);
132 stream.EndInlineInfoEntry();
133 stream.BeginInlineInfoEntry(42, 2, number_of_dex_registers_in_inline_info);
134 stream.EndInlineInfoEntry();
Calin Juravle4f46ac52015-04-23 18:47:21 +0100135 stream.EndStackMapEntry();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100136
137 ArenaBitVector sp_mask2(&arena, 0, true);
138 sp_mask2.SetBit(3);
139 sp_mask1.SetBit(8);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100140 stream.BeginStackMapEntry(1, 128, 0xFF, &sp_mask2, number_of_dex_registers, 0);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100141 stream.AddDexRegisterEntry(Kind::kInRegister, 18); // Short location.
142 stream.AddDexRegisterEntry(Kind::kInFpuRegister, 3); // Short location.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100143 stream.EndStackMapEntry();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100144
Calin Juravle4f46ac52015-04-23 18:47:21 +0100145 size_t size = stream.PrepareForFillIn();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100146 void* memory = arena.Alloc(size, kArenaAllocMisc);
147 MemoryRegion region(memory, size);
148 stream.FillIn(region);
149
Nicolas Geoffray39468442014-09-02 15:17:15 +0100150 CodeInfo code_info(region);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100151 ASSERT_EQ(1u, code_info.GetStackMaskSize());
152 ASSERT_EQ(2u, code_info.GetNumberOfStackMaps());
153
Roland Levillaina552e1c2015-03-26 15:01:03 +0000154 uint32_t number_of_location_catalog_entries =
155 code_info.GetNumberOfDexRegisterLocationCatalogEntries();
156 ASSERT_EQ(4u, number_of_location_catalog_entries);
157 DexRegisterLocationCatalog location_catalog = code_info.GetDexRegisterLocationCatalog();
158 // The Dex register location catalog contains:
159 // - three 1-byte short Dex register locations, and
160 // - one 5-byte large Dex register location.
161 size_t expected_location_catalog_size = 3u * 1u + 5u;
162 ASSERT_EQ(expected_location_catalog_size, location_catalog.Size());
163
Roland Levillain12baf472015-03-05 12:41:42 +0000164 // First stack map.
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000165 {
166 StackMap stack_map = code_info.GetStackMapAt(0);
167 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0)));
168 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64)));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000169 ASSERT_EQ(0u, stack_map.GetDexPc(code_info));
170 ASSERT_EQ(64u, stack_map.GetNativePcOffset(code_info));
171 ASSERT_EQ(0x3u, stack_map.GetRegisterMask(code_info));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100172
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000173 MemoryRegion stack_mask = stack_map.GetStackMask(code_info);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000174 ASSERT_TRUE(SameBits(stack_mask, sp_mask1));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100175
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000176 ASSERT_TRUE(stack_map.HasDexRegisterMap(code_info));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000177 DexRegisterMap dex_register_map =
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000178 code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000179 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(0));
180 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(1));
181 ASSERT_EQ(2u, dex_register_map.GetNumberOfLiveDexRegisters(number_of_dex_registers));
182 // The Dex register map contains:
183 // - one 1-byte live bit mask, and
184 // - one 1-byte set of location catalog entry indices composed of two 2-bit values.
185 size_t expected_dex_register_map_size = 1u + 1u;
186 ASSERT_EQ(expected_dex_register_map_size, dex_register_map.Size());
187
188 ASSERT_EQ(Kind::kInStack,
189 dex_register_map.GetLocationKind(0, number_of_dex_registers, code_info));
190 ASSERT_EQ(Kind::kConstant,
191 dex_register_map.GetLocationKind(1, number_of_dex_registers, code_info));
192 ASSERT_EQ(Kind::kInStack,
193 dex_register_map.GetLocationInternalKind(0, number_of_dex_registers, code_info));
194 ASSERT_EQ(Kind::kConstantLargeValue,
195 dex_register_map.GetLocationInternalKind(1, number_of_dex_registers, code_info));
196 ASSERT_EQ(0, dex_register_map.GetStackOffsetInBytes(0, number_of_dex_registers, code_info));
197 ASSERT_EQ(-2, dex_register_map.GetConstant(1, number_of_dex_registers, code_info));
198
199 size_t index0 = dex_register_map.GetLocationCatalogEntryIndex(
200 0, number_of_dex_registers, number_of_location_catalog_entries);
201 size_t index1 = dex_register_map.GetLocationCatalogEntryIndex(
202 1, number_of_dex_registers, number_of_location_catalog_entries);
203 ASSERT_EQ(0u, index0);
204 ASSERT_EQ(1u, index1);
205 DexRegisterLocation location0 = location_catalog.GetDexRegisterLocation(index0);
206 DexRegisterLocation location1 = location_catalog.GetDexRegisterLocation(index1);
207 ASSERT_EQ(Kind::kInStack, location0.GetKind());
208 ASSERT_EQ(Kind::kConstant, location1.GetKind());
209 ASSERT_EQ(Kind::kInStack, location0.GetInternalKind());
210 ASSERT_EQ(Kind::kConstantLargeValue, location1.GetInternalKind());
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000211 ASSERT_EQ(0, location0.GetValue());
212 ASSERT_EQ(-2, location1.GetValue());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100213
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000214 ASSERT_TRUE(stack_map.HasInlineInfo(code_info));
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000215 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map);
216 ASSERT_EQ(2u, inline_info.GetDepth());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100217 ASSERT_EQ(82u, inline_info.GetMethodIndexAtDepth(0));
218 ASSERT_EQ(42u, inline_info.GetMethodIndexAtDepth(1));
219 ASSERT_EQ(3u, inline_info.GetDexPcAtDepth(0));
220 ASSERT_EQ(2u, inline_info.GetDexPcAtDepth(1));
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000221 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100222
Roland Levillain12baf472015-03-05 12:41:42 +0000223 // Second stack map.
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000224 {
225 StackMap stack_map = code_info.GetStackMapAt(1);
226 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(1u)));
227 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(128u)));
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000228 ASSERT_EQ(1u, stack_map.GetDexPc(code_info));
229 ASSERT_EQ(128u, stack_map.GetNativePcOffset(code_info));
230 ASSERT_EQ(0xFFu, stack_map.GetRegisterMask(code_info));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100231
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000232 MemoryRegion stack_mask = stack_map.GetStackMask(code_info);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000233 ASSERT_TRUE(SameBits(stack_mask, sp_mask2));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100234
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000235 ASSERT_TRUE(stack_map.HasDexRegisterMap(code_info));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000236 DexRegisterMap dex_register_map =
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000237 code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000238 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(0));
239 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(1));
240 ASSERT_EQ(2u, dex_register_map.GetNumberOfLiveDexRegisters(number_of_dex_registers));
241 // The Dex register map contains:
242 // - one 1-byte live bit mask, and
243 // - one 1-byte set of location catalog entry indices composed of two 2-bit values.
244 size_t expected_dex_register_map_size = 1u + 1u;
245 ASSERT_EQ(expected_dex_register_map_size, dex_register_map.Size());
246
247 ASSERT_EQ(Kind::kInRegister,
248 dex_register_map.GetLocationKind(0, number_of_dex_registers, code_info));
249 ASSERT_EQ(Kind::kInFpuRegister,
250 dex_register_map.GetLocationKind(1, number_of_dex_registers, code_info));
251 ASSERT_EQ(Kind::kInRegister,
252 dex_register_map.GetLocationInternalKind(0, number_of_dex_registers, code_info));
253 ASSERT_EQ(Kind::kInFpuRegister,
254 dex_register_map.GetLocationInternalKind(1, number_of_dex_registers, code_info));
255 ASSERT_EQ(18, dex_register_map.GetMachineRegister(0, number_of_dex_registers, code_info));
256 ASSERT_EQ(3, dex_register_map.GetMachineRegister(1, number_of_dex_registers, code_info));
257
258 size_t index0 = dex_register_map.GetLocationCatalogEntryIndex(
259 0, number_of_dex_registers, number_of_location_catalog_entries);
260 size_t index1 = dex_register_map.GetLocationCatalogEntryIndex(
261 1, number_of_dex_registers, number_of_location_catalog_entries);
262 ASSERT_EQ(2u, index0);
263 ASSERT_EQ(3u, index1);
264 DexRegisterLocation location0 = location_catalog.GetDexRegisterLocation(index0);
265 DexRegisterLocation location1 = location_catalog.GetDexRegisterLocation(index1);
266 ASSERT_EQ(Kind::kInRegister, location0.GetKind());
267 ASSERT_EQ(Kind::kInFpuRegister, location1.GetKind());
268 ASSERT_EQ(Kind::kInRegister, location0.GetInternalKind());
269 ASSERT_EQ(Kind::kInFpuRegister, location1.GetInternalKind());
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000270 ASSERT_EQ(18, location0.GetValue());
271 ASSERT_EQ(3, location1.GetValue());
Roland Levillain12baf472015-03-05 12:41:42 +0000272
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000273 ASSERT_FALSE(stack_map.HasInlineInfo(code_info));
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000274 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100275}
276
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000277TEST(StackMapTest, TestNonLiveDexRegisters) {
278 ArenaPool pool;
279 ArenaAllocator arena(&pool);
280 StackMapStream stream(&arena);
281
282 ArenaBitVector sp_mask(&arena, 0, false);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000283 uint32_t number_of_dex_registers = 2;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100284 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100285 stream.AddDexRegisterEntry(Kind::kNone, 0); // No location.
286 stream.AddDexRegisterEntry(Kind::kConstant, -2); // Large location.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100287 stream.EndStackMapEntry();
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000288
Calin Juravle4f46ac52015-04-23 18:47:21 +0100289 size_t size = stream.PrepareForFillIn();
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000290 void* memory = arena.Alloc(size, kArenaAllocMisc);
291 MemoryRegion region(memory, size);
292 stream.FillIn(region);
293
294 CodeInfo code_info(region);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000295 ASSERT_EQ(0u, code_info.GetStackMaskSize());
296 ASSERT_EQ(1u, code_info.GetNumberOfStackMaps());
297
298 uint32_t number_of_location_catalog_entries =
299 code_info.GetNumberOfDexRegisterLocationCatalogEntries();
300 ASSERT_EQ(1u, number_of_location_catalog_entries);
301 DexRegisterLocationCatalog location_catalog = code_info.GetDexRegisterLocationCatalog();
302 // The Dex register location catalog contains:
303 // - one 5-byte large Dex register location.
304 size_t expected_location_catalog_size = 5u;
305 ASSERT_EQ(expected_location_catalog_size, location_catalog.Size());
306
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000307 StackMap stack_map = code_info.GetStackMapAt(0);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000308 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0)));
309 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64)));
310 ASSERT_EQ(0u, stack_map.GetDexPc(code_info));
311 ASSERT_EQ(64u, stack_map.GetNativePcOffset(code_info));
312 ASSERT_EQ(0x3u, stack_map.GetRegisterMask(code_info));
313
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000314 ASSERT_TRUE(stack_map.HasDexRegisterMap(code_info));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000315 DexRegisterMap dex_register_map =
316 code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
317 ASSERT_FALSE(dex_register_map.IsDexRegisterLive(0));
318 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(1));
319 ASSERT_EQ(1u, dex_register_map.GetNumberOfLiveDexRegisters(number_of_dex_registers));
320 // The Dex register map contains:
321 // - one 1-byte live bit mask.
322 // No space is allocated for the sole location catalog entry index, as it is useless.
323 size_t expected_dex_register_map_size = 1u + 0u;
324 ASSERT_EQ(expected_dex_register_map_size, dex_register_map.Size());
325
326 ASSERT_EQ(Kind::kNone,
327 dex_register_map.GetLocationKind(0, number_of_dex_registers, code_info));
328 ASSERT_EQ(Kind::kConstant,
329 dex_register_map.GetLocationKind(1, number_of_dex_registers, code_info));
330 ASSERT_EQ(Kind::kNone,
331 dex_register_map.GetLocationInternalKind(0, number_of_dex_registers, code_info));
332 ASSERT_EQ(Kind::kConstantLargeValue,
333 dex_register_map.GetLocationInternalKind(1, number_of_dex_registers, code_info));
334 ASSERT_EQ(-2, dex_register_map.GetConstant(1, number_of_dex_registers, code_info));
335
336 size_t index0 = dex_register_map.GetLocationCatalogEntryIndex(
337 0, number_of_dex_registers, number_of_location_catalog_entries);
338 size_t index1 = dex_register_map.GetLocationCatalogEntryIndex(
339 1, number_of_dex_registers, number_of_location_catalog_entries);
340 ASSERT_EQ(DexRegisterLocationCatalog::kNoLocationEntryIndex, index0);
341 ASSERT_EQ(0u, index1);
342 DexRegisterLocation location0 = location_catalog.GetDexRegisterLocation(index0);
343 DexRegisterLocation location1 = location_catalog.GetDexRegisterLocation(index1);
344 ASSERT_EQ(Kind::kNone, location0.GetKind());
345 ASSERT_EQ(Kind::kConstant, location1.GetKind());
346 ASSERT_EQ(Kind::kNone, location0.GetInternalKind());
347 ASSERT_EQ(Kind::kConstantLargeValue, location1.GetInternalKind());
348 ASSERT_EQ(0, location0.GetValue());
349 ASSERT_EQ(-2, location1.GetValue());
350
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000351 ASSERT_FALSE(stack_map.HasInlineInfo(code_info));
352}
353
354// Generate a stack map whose dex register offset is
355// StackMap::kNoDexRegisterMapSmallEncoding, and ensure we do
356// not treat it as kNoDexRegisterMap.
357TEST(StackMapTest, DexRegisterMapOffsetOverflow) {
358 ArenaPool pool;
359 ArenaAllocator arena(&pool);
360 StackMapStream stream(&arena);
361
362 ArenaBitVector sp_mask(&arena, 0, false);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000363 uint32_t number_of_dex_registers = 1024;
364 // Create the first stack map (and its Dex register map).
Calin Juravle4f46ac52015-04-23 18:47:21 +0100365 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000366 uint32_t number_of_dex_live_registers_in_dex_register_map_0 = number_of_dex_registers - 8;
367 for (uint32_t i = 0; i < number_of_dex_live_registers_in_dex_register_map_0; ++i) {
368 // Use two different Dex register locations to populate this map,
369 // as using a single value (in the whole CodeInfo object) would
370 // make this Dex register mapping data empty (see
371 // art::DexRegisterMap::SingleEntrySizeInBits).
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100372 stream.AddDexRegisterEntry(Kind::kConstant, i % 2); // Short location.
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000373 }
Calin Juravle4f46ac52015-04-23 18:47:21 +0100374 stream.EndStackMapEntry();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000375 // Create the second stack map (and its Dex register map).
Calin Juravle4f46ac52015-04-23 18:47:21 +0100376 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000377 for (uint32_t i = 0; i < number_of_dex_registers; ++i) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100378 stream.AddDexRegisterEntry(Kind::kConstant, 0); // Short location.
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000379 }
Calin Juravle4f46ac52015-04-23 18:47:21 +0100380 stream.EndStackMapEntry();
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000381
Calin Juravle4f46ac52015-04-23 18:47:21 +0100382 size_t size = stream.PrepareForFillIn();
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000383 void* memory = arena.Alloc(size, kArenaAllocMisc);
384 MemoryRegion region(memory, size);
385 stream.FillIn(region);
386
387 CodeInfo code_info(region);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000388 // The location catalog contains two entries (DexRegisterLocation(kConstant, 0)
389 // and DexRegisterLocation(kConstant, 1)), therefore the location catalog index
390 // has a size of 1 bit.
391 uint32_t number_of_location_catalog_entries =
392 code_info.GetNumberOfDexRegisterLocationCatalogEntries();
393 ASSERT_EQ(2u, number_of_location_catalog_entries);
394 ASSERT_EQ(1u, DexRegisterMap::SingleEntrySizeInBits(number_of_location_catalog_entries));
395
396 // The first Dex register map contains:
397 // - a live register bit mask for 1024 registers (that is, 128 bytes of
398 // data); and
399 // - Dex register mapping information for 1016 1-bit Dex (live) register
400 // locations (that is, 127 bytes of data).
401 // Hence it has a size of 255 bytes, and therefore...
402 ASSERT_EQ(128u, DexRegisterMap::GetLiveBitMaskSize(number_of_dex_registers));
403 StackMap stack_map0 = code_info.GetStackMapAt(0);
404 DexRegisterMap dex_register_map0 =
405 code_info.GetDexRegisterMapOf(stack_map0, number_of_dex_registers);
406 ASSERT_EQ(127u, dex_register_map0.GetLocationMappingDataSize(number_of_dex_registers,
407 number_of_location_catalog_entries));
408 ASSERT_EQ(255u, dex_register_map0.Size());
409
410 StackMap stack_map1 = code_info.GetStackMapAt(1);
411 ASSERT_TRUE(stack_map1.HasDexRegisterMap(code_info));
412 // ...the offset of the second Dex register map (relative to the
413 // beginning of the Dex register maps region) is 255 (i.e.,
414 // kNoDexRegisterMapSmallEncoding).
Nicolas Geoffray896f8f72015-03-30 15:44:25 +0100415 ASSERT_NE(stack_map1.GetDexRegisterMapOffset(code_info), StackMap::kNoDexRegisterMap);
416 ASSERT_EQ(stack_map1.GetDexRegisterMapOffset(code_info), 0xFFu);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000417}
418
Calin Juravle6ae70962015-03-18 16:31:28 +0000419TEST(StackMapTest, TestShareDexRegisterMap) {
420 ArenaPool pool;
421 ArenaAllocator arena(&pool);
422 StackMapStream stream(&arena);
423
424 ArenaBitVector sp_mask(&arena, 0, false);
425 uint32_t number_of_dex_registers = 2;
426 // First stack map.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100427 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100428 stream.AddDexRegisterEntry(Kind::kInRegister, 0); // Short location.
429 stream.AddDexRegisterEntry(Kind::kConstant, -2); // Large location.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100430 stream.EndStackMapEntry();
Calin Juravle6ae70962015-03-18 16:31:28 +0000431 // Second stack map, which should share the same dex register map.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100432 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100433 stream.AddDexRegisterEntry(Kind::kInRegister, 0); // Short location.
434 stream.AddDexRegisterEntry(Kind::kConstant, -2); // Large location.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100435 stream.EndStackMapEntry();
Calin Juravle6ae70962015-03-18 16:31:28 +0000436 // Third stack map (doesn't share the dex register map).
Calin Juravle4f46ac52015-04-23 18:47:21 +0100437 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100438 stream.AddDexRegisterEntry(Kind::kInRegister, 2); // Short location.
439 stream.AddDexRegisterEntry(Kind::kConstant, -2); // Large location.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100440 stream.EndStackMapEntry();
Calin Juravle6ae70962015-03-18 16:31:28 +0000441
Calin Juravle4f46ac52015-04-23 18:47:21 +0100442 size_t size = stream.PrepareForFillIn();
Calin Juravle6ae70962015-03-18 16:31:28 +0000443 void* memory = arena.Alloc(size, kArenaAllocMisc);
444 MemoryRegion region(memory, size);
445 stream.FillIn(region);
446
447 CodeInfo ci(region);
448 // Verify first stack map.
449 StackMap sm0 = ci.GetStackMapAt(0);
450 DexRegisterMap dex_registers0 = ci.GetDexRegisterMapOf(sm0, number_of_dex_registers);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000451 ASSERT_EQ(0, dex_registers0.GetMachineRegister(0, number_of_dex_registers, ci));
452 ASSERT_EQ(-2, dex_registers0.GetConstant(1, number_of_dex_registers, ci));
Calin Juravle6ae70962015-03-18 16:31:28 +0000453
454 // Verify second stack map.
455 StackMap sm1 = ci.GetStackMapAt(1);
456 DexRegisterMap dex_registers1 = ci.GetDexRegisterMapOf(sm1, number_of_dex_registers);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000457 ASSERT_EQ(0, dex_registers1.GetMachineRegister(0, number_of_dex_registers, ci));
458 ASSERT_EQ(-2, dex_registers1.GetConstant(1, number_of_dex_registers, ci));
Calin Juravle6ae70962015-03-18 16:31:28 +0000459
460 // Verify third stack map.
461 StackMap sm2 = ci.GetStackMapAt(2);
462 DexRegisterMap dex_registers2 = ci.GetDexRegisterMapOf(sm2, number_of_dex_registers);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000463 ASSERT_EQ(2, dex_registers2.GetMachineRegister(0, number_of_dex_registers, ci));
464 ASSERT_EQ(-2, dex_registers2.GetConstant(1, number_of_dex_registers, ci));
Calin Juravle6ae70962015-03-18 16:31:28 +0000465
466 // Verify dex register map offsets.
467 ASSERT_EQ(sm0.GetDexRegisterMapOffset(ci), sm1.GetDexRegisterMapOffset(ci));
468 ASSERT_NE(sm0.GetDexRegisterMapOffset(ci), sm2.GetDexRegisterMapOffset(ci));
469 ASSERT_NE(sm1.GetDexRegisterMapOffset(ci), sm2.GetDexRegisterMapOffset(ci));
470}
471
Roland Levillaina552e1c2015-03-26 15:01:03 +0000472TEST(StackMapTest, TestNoDexRegisterMap) {
473 ArenaPool pool;
474 ArenaAllocator arena(&pool);
475 StackMapStream stream(&arena);
476
477 ArenaBitVector sp_mask(&arena, 0, false);
478 uint32_t number_of_dex_registers = 0;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100479 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
480 stream.EndStackMapEntry();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000481
Calin Juravle4f46ac52015-04-23 18:47:21 +0100482 size_t size = stream.PrepareForFillIn();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000483 void* memory = arena.Alloc(size, kArenaAllocMisc);
484 MemoryRegion region(memory, size);
485 stream.FillIn(region);
486
487 CodeInfo code_info(region);
488 ASSERT_EQ(0u, code_info.GetStackMaskSize());
489 ASSERT_EQ(1u, code_info.GetNumberOfStackMaps());
490
491 uint32_t number_of_location_catalog_entries =
492 code_info.GetNumberOfDexRegisterLocationCatalogEntries();
493 ASSERT_EQ(0u, number_of_location_catalog_entries);
494 DexRegisterLocationCatalog location_catalog = code_info.GetDexRegisterLocationCatalog();
495 ASSERT_EQ(0u, location_catalog.Size());
496
497 StackMap stack_map = code_info.GetStackMapAt(0);
498 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0)));
499 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64)));
500 ASSERT_EQ(0u, stack_map.GetDexPc(code_info));
501 ASSERT_EQ(64u, stack_map.GetNativePcOffset(code_info));
502 ASSERT_EQ(0x3u, stack_map.GetRegisterMask(code_info));
503
504 ASSERT_FALSE(stack_map.HasDexRegisterMap(code_info));
505 ASSERT_FALSE(stack_map.HasInlineInfo(code_info));
506}
507
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100508TEST(StackMapTest, InlineTest) {
509 ArenaPool pool;
510 ArenaAllocator arena(&pool);
511 StackMapStream stream(&arena);
512
513 ArenaBitVector sp_mask1(&arena, 0, true);
514 sp_mask1.SetBit(2);
515 sp_mask1.SetBit(4);
516
517 // First stack map.
518 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask1, 2, 2);
519 stream.AddDexRegisterEntry(Kind::kInStack, 0);
520 stream.AddDexRegisterEntry(Kind::kConstant, 4);
521
522 stream.BeginInlineInfoEntry(42, 2, 1);
523 stream.AddDexRegisterEntry(Kind::kInStack, 8);
524 stream.EndInlineInfoEntry();
525 stream.BeginInlineInfoEntry(82, 3, 3);
526 stream.AddDexRegisterEntry(Kind::kInStack, 16);
527 stream.AddDexRegisterEntry(Kind::kConstant, 20);
528 stream.AddDexRegisterEntry(Kind::kInRegister, 15);
529 stream.EndInlineInfoEntry();
530
531 stream.EndStackMapEntry();
532
533 // Second stack map.
534 stream.BeginStackMapEntry(2, 22, 0x3, &sp_mask1, 2, 3);
535 stream.AddDexRegisterEntry(Kind::kInStack, 56);
536 stream.AddDexRegisterEntry(Kind::kConstant, 0);
537
538 stream.BeginInlineInfoEntry(42, 2, 1);
539 stream.AddDexRegisterEntry(Kind::kInStack, 12);
540 stream.EndInlineInfoEntry();
541 stream.BeginInlineInfoEntry(82, 3, 3);
542 stream.AddDexRegisterEntry(Kind::kInStack, 80);
543 stream.AddDexRegisterEntry(Kind::kConstant, 10);
544 stream.AddDexRegisterEntry(Kind::kInRegister, 5);
545 stream.EndInlineInfoEntry();
546 stream.BeginInlineInfoEntry(52, 5, 0);
547 stream.EndInlineInfoEntry();
548
549 stream.EndStackMapEntry();
550
551 // Third stack map.
552 stream.BeginStackMapEntry(4, 56, 0x3, &sp_mask1, 2, 0);
553 stream.AddDexRegisterEntry(Kind::kNone, 0);
554 stream.AddDexRegisterEntry(Kind::kConstant, 4);
555 stream.EndStackMapEntry();
556
557 // Fourth stack map.
558 stream.BeginStackMapEntry(6, 78, 0x3, &sp_mask1, 2, 3);
559 stream.AddDexRegisterEntry(Kind::kInStack, 56);
560 stream.AddDexRegisterEntry(Kind::kConstant, 0);
561
562 stream.BeginInlineInfoEntry(42, 2, 0);
563 stream.EndInlineInfoEntry();
564 stream.BeginInlineInfoEntry(52, 5, 1);
565 stream.AddDexRegisterEntry(Kind::kInRegister, 2);
566 stream.EndInlineInfoEntry();
567 stream.BeginInlineInfoEntry(52, 10, 2);
568 stream.AddDexRegisterEntry(Kind::kNone, 0);
569 stream.AddDexRegisterEntry(Kind::kInRegister, 3);
570 stream.EndInlineInfoEntry();
571
572 stream.EndStackMapEntry();
573
574 size_t size = stream.PrepareForFillIn();
575 void* memory = arena.Alloc(size, kArenaAllocMisc);
576 MemoryRegion region(memory, size);
577 stream.FillIn(region);
578
579 CodeInfo ci(region);
580
581 {
582 // Verify first stack map.
583 StackMap sm0 = ci.GetStackMapAt(0);
584
585 DexRegisterMap dex_registers0 = ci.GetDexRegisterMapOf(sm0, 2);
586 ASSERT_EQ(0, dex_registers0.GetStackOffsetInBytes(0, 2, ci));
587 ASSERT_EQ(4, dex_registers0.GetConstant(1, 2, ci));
588
589 InlineInfo if0 = ci.GetInlineInfoOf(sm0);
590 ASSERT_EQ(2u, if0.GetDepth());
591 ASSERT_EQ(2u, if0.GetDexPcAtDepth(0));
592 ASSERT_EQ(42u, if0.GetMethodIndexAtDepth(0));
593 ASSERT_EQ(3u, if0.GetDexPcAtDepth(1));
594 ASSERT_EQ(82u, if0.GetMethodIndexAtDepth(1));
595
596 DexRegisterMap dex_registers1 = ci.GetDexRegisterMapAtDepth(0, if0, 1);
597 ASSERT_EQ(8, dex_registers1.GetStackOffsetInBytes(0, 1, ci));
598
599 DexRegisterMap dex_registers2 = ci.GetDexRegisterMapAtDepth(1, if0, 3);
600 ASSERT_EQ(16, dex_registers2.GetStackOffsetInBytes(0, 3, ci));
601 ASSERT_EQ(20, dex_registers2.GetConstant(1, 3, ci));
602 ASSERT_EQ(15, dex_registers2.GetMachineRegister(2, 3, ci));
603 }
604
605 {
606 // Verify second stack map.
607 StackMap sm1 = ci.GetStackMapAt(1);
608
609 DexRegisterMap dex_registers0 = ci.GetDexRegisterMapOf(sm1, 2);
610 ASSERT_EQ(56, dex_registers0.GetStackOffsetInBytes(0, 2, ci));
611 ASSERT_EQ(0, dex_registers0.GetConstant(1, 2, ci));
612
613 InlineInfo if1 = ci.GetInlineInfoOf(sm1);
614 ASSERT_EQ(3u, if1.GetDepth());
615 ASSERT_EQ(2u, if1.GetDexPcAtDepth(0));
616 ASSERT_EQ(42u, if1.GetMethodIndexAtDepth(0));
617 ASSERT_EQ(3u, if1.GetDexPcAtDepth(1));
618 ASSERT_EQ(82u, if1.GetMethodIndexAtDepth(1));
619 ASSERT_EQ(5u, if1.GetDexPcAtDepth(2));
620 ASSERT_EQ(52u, if1.GetMethodIndexAtDepth(2));
621
622 DexRegisterMap dex_registers1 = ci.GetDexRegisterMapAtDepth(0, if1, 1);
623 ASSERT_EQ(12, dex_registers1.GetStackOffsetInBytes(0, 1, ci));
624
625 DexRegisterMap dex_registers2 = ci.GetDexRegisterMapAtDepth(1, if1, 3);
626 ASSERT_EQ(80, dex_registers2.GetStackOffsetInBytes(0, 3, ci));
627 ASSERT_EQ(10, dex_registers2.GetConstant(1, 3, ci));
628 ASSERT_EQ(5, dex_registers2.GetMachineRegister(2, 3, ci));
629
630 ASSERT_FALSE(if1.HasDexRegisterMapAtDepth(2));
631 }
632
633 {
634 // Verify third stack map.
635 StackMap sm2 = ci.GetStackMapAt(2);
636
637 DexRegisterMap dex_registers0 = ci.GetDexRegisterMapOf(sm2, 2);
638 ASSERT_FALSE(dex_registers0.IsDexRegisterLive(0));
639 ASSERT_EQ(4, dex_registers0.GetConstant(1, 2, ci));
640 ASSERT_FALSE(sm2.HasInlineInfo(ci));
641 }
642
643 {
644 // Verify fourth stack map.
645 StackMap sm3 = ci.GetStackMapAt(3);
646
647 DexRegisterMap dex_registers0 = ci.GetDexRegisterMapOf(sm3, 2);
648 ASSERT_EQ(56, dex_registers0.GetStackOffsetInBytes(0, 2, ci));
649 ASSERT_EQ(0, dex_registers0.GetConstant(1, 2, ci));
650
651 InlineInfo if2 = ci.GetInlineInfoOf(sm3);
652 ASSERT_EQ(3u, if2.GetDepth());
653 ASSERT_EQ(2u, if2.GetDexPcAtDepth(0));
654 ASSERT_EQ(42u, if2.GetMethodIndexAtDepth(0));
655 ASSERT_EQ(5u, if2.GetDexPcAtDepth(1));
656 ASSERT_EQ(52u, if2.GetMethodIndexAtDepth(1));
657 ASSERT_EQ(10u, if2.GetDexPcAtDepth(2));
658 ASSERT_EQ(52u, if2.GetMethodIndexAtDepth(2));
659
660 ASSERT_FALSE(if2.HasDexRegisterMapAtDepth(0));
661
662 DexRegisterMap dex_registers1 = ci.GetDexRegisterMapAtDepth(1, if2, 1);
663 ASSERT_EQ(2, dex_registers1.GetMachineRegister(0, 1, ci));
664
665 DexRegisterMap dex_registers2 = ci.GetDexRegisterMapAtDepth(2, if2, 2);
666 ASSERT_FALSE(dex_registers2.IsDexRegisterLive(0));
667 ASSERT_EQ(3, dex_registers2.GetMachineRegister(1, 2, ci));
668 }
669}
670
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100671} // namespace art