blob: 112771847cbfbcc679666c2aa257fceaea6a3038 [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"
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070018
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000019#include "art_method.h"
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070020#include "base/arena_bit_vector.h"
David Sehr3215fff2018-04-03 17:10:12 -070021#include "base/malloc_arena_pool.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010022#include "stack_map_stream.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010023
24#include "gtest/gtest.h"
25
26namespace art {
27
David Srbecky09ed0982016-02-12 21:58:43 +000028// Check that the stack mask of given stack map is identical
29// to the given bit vector. Returns true if they are same.
30static bool CheckStackMask(
David Srbecky45aa5982016-03-18 02:15:09 +000031 const CodeInfo& code_info,
David Srbecky09ed0982016-02-12 21:58:43 +000032 const StackMap& stack_map,
David Srbecky09ed0982016-02-12 21:58:43 +000033 const BitVector& bit_vector) {
David Srbecky052f8ca2018-04-26 15:42:54 +010034 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(stack_map);
David Srbecky4b59d102018-05-29 21:46:10 +000035 if (bit_vector.GetNumberOfBits() > stack_mask.size_in_bits()) {
David Srbecky09ed0982016-02-12 21:58:43 +000036 return false;
37 }
David Srbecky4b59d102018-05-29 21:46:10 +000038 for (size_t i = 0; i < stack_mask.size_in_bits(); ++i) {
David Srbecky45aa5982016-03-18 02:15:09 +000039 if (stack_mask.LoadBit(i) != bit_vector.IsBitSet(i)) {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010040 return false;
41 }
42 }
43 return true;
44}
45
Roland Levillaina552e1c2015-03-26 15:01:03 +000046using Kind = DexRegisterLocation::Kind;
47
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010048TEST(StackMapTest, Test1) {
David Sehr3215fff2018-04-03 17:10:12 -070049 MallocArenaPool pool;
Vladimir Marko174b2e22017-10-12 13:34:49 +010050 ArenaStack arena_stack(&pool);
51 ScopedArenaAllocator allocator(&arena_stack);
Vladimir Marko69d310e2017-10-09 14:12:23 +010052 StackMapStream stream(&allocator, kRuntimeISA);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010053
Vladimir Marko69d310e2017-10-09 14:12:23 +010054 ArenaBitVector sp_mask(&allocator, 0, false);
Roland Levillain12baf472015-03-05 12:41:42 +000055 size_t number_of_dex_registers = 2;
Calin Juravle4f46ac52015-04-23 18:47:21 +010056 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010057 stream.AddDexRegisterEntry(Kind::kInStack, 0); // Short location.
58 stream.AddDexRegisterEntry(Kind::kConstant, -2); // Short location.
Calin Juravle4f46ac52015-04-23 18:47:21 +010059 stream.EndStackMapEntry();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010060
Calin Juravle4f46ac52015-04-23 18:47:21 +010061 size_t size = stream.PrepareForFillIn();
Vladimir Marko69d310e2017-10-09 14:12:23 +010062 void* memory = allocator.Alloc(size, kArenaAllocMisc);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010063 MemoryRegion region(memory, size);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070064 stream.FillInCodeInfo(region);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010065
Nicolas Geoffray39468442014-09-02 15:17:15 +010066 CodeInfo code_info(region);
David Srbecky052f8ca2018-04-26 15:42:54 +010067 ASSERT_EQ(1u, code_info.GetNumberOfStackMaps());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010068
David Srbecky052f8ca2018-04-26 15:42:54 +010069 uint32_t number_of_catalog_entries = code_info.GetNumberOfLocationCatalogEntries();
David Srbecky09ed0982016-02-12 21:58:43 +000070 ASSERT_EQ(2u, number_of_catalog_entries);
David Srbecky052f8ca2018-04-26 15:42:54 +010071 DexRegisterLocationCatalog location_catalog = code_info.GetDexRegisterLocationCatalog();
Roland Levillaina552e1c2015-03-26 15:01:03 +000072 // The Dex register location catalog contains:
73 // - one 1-byte short Dex register location, and
74 // - one 5-byte large Dex register location.
75 size_t expected_location_catalog_size = 1u + 5u;
76 ASSERT_EQ(expected_location_catalog_size, location_catalog.Size());
77
David Srbecky052f8ca2018-04-26 15:42:54 +010078 StackMap stack_map = code_info.GetStackMapAt(0);
79 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0)));
80 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64)));
81 ASSERT_EQ(0u, stack_map.GetDexPc());
82 ASSERT_EQ(64u, stack_map.GetNativePcOffset(kRuntimeISA));
83 ASSERT_EQ(0x3u, code_info.GetRegisterMaskOf(stack_map));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010084
David Srbecky052f8ca2018-04-26 15:42:54 +010085 ASSERT_TRUE(CheckStackMask(code_info, stack_map, sp_mask));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010086
David Srbecky052f8ca2018-04-26 15:42:54 +010087 ASSERT_TRUE(stack_map.HasDexRegisterMap());
Roland Levillaina552e1c2015-03-26 15:01:03 +000088 DexRegisterMap dex_register_map =
David Srbecky052f8ca2018-04-26 15:42:54 +010089 code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
Roland Levillaina552e1c2015-03-26 15:01:03 +000090 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(0));
91 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(1));
92 ASSERT_EQ(2u, dex_register_map.GetNumberOfLiveDexRegisters(number_of_dex_registers));
93 // The Dex register map contains:
94 // - one 1-byte live bit mask, and
95 // - one 1-byte set of location catalog entry indices composed of two 2-bit values.
96 size_t expected_dex_register_map_size = 1u + 1u;
97 ASSERT_EQ(expected_dex_register_map_size, dex_register_map.Size());
98
David Srbecky21d45b42018-05-30 06:35:05 +010099 ASSERT_EQ(Kind::kInStack, dex_register_map.GetLocationKind(0));
100 ASSERT_EQ(Kind::kConstant, dex_register_map.GetLocationKind(1));
101 ASSERT_EQ(Kind::kInStack, dex_register_map.GetLocationInternalKind(0));
102 ASSERT_EQ(Kind::kConstantLargeValue, dex_register_map.GetLocationInternalKind(1));
103 ASSERT_EQ(0, dex_register_map.GetStackOffsetInBytes(0));
104 ASSERT_EQ(-2, dex_register_map.GetConstant(1));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000105
David Srbecky21d45b42018-05-30 06:35:05 +0100106 size_t index0 = dex_register_map.GetLocationCatalogEntryIndex(0, number_of_catalog_entries);
107 size_t index1 = dex_register_map.GetLocationCatalogEntryIndex(1, number_of_catalog_entries);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000108 ASSERT_EQ(0u, index0);
109 ASSERT_EQ(1u, index1);
110 DexRegisterLocation location0 = location_catalog.GetDexRegisterLocation(index0);
111 DexRegisterLocation location1 = location_catalog.GetDexRegisterLocation(index1);
112 ASSERT_EQ(Kind::kInStack, location0.GetKind());
113 ASSERT_EQ(Kind::kConstant, location1.GetKind());
114 ASSERT_EQ(Kind::kInStack, location0.GetInternalKind());
115 ASSERT_EQ(Kind::kConstantLargeValue, location1.GetInternalKind());
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000116 ASSERT_EQ(0, location0.GetValue());
117 ASSERT_EQ(-2, location1.GetValue());
Roland Levillain12baf472015-03-05 12:41:42 +0000118
David Srbecky052f8ca2018-04-26 15:42:54 +0100119 ASSERT_FALSE(stack_map.HasInlineInfo());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100120}
121
122TEST(StackMapTest, Test2) {
David Sehr3215fff2018-04-03 17:10:12 -0700123 MallocArenaPool pool;
Vladimir Marko174b2e22017-10-12 13:34:49 +0100124 ArenaStack arena_stack(&pool);
125 ScopedArenaAllocator allocator(&arena_stack);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100126 StackMapStream stream(&allocator, kRuntimeISA);
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000127 ArtMethod art_method;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100128
Vladimir Marko69d310e2017-10-09 14:12:23 +0100129 ArenaBitVector sp_mask1(&allocator, 0, true);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100130 sp_mask1.SetBit(2);
131 sp_mask1.SetBit(4);
Roland Levillain12baf472015-03-05 12:41:42 +0000132 size_t number_of_dex_registers = 2;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100133 size_t number_of_dex_registers_in_inline_info = 0;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100134 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask1, number_of_dex_registers, 2);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100135 stream.AddDexRegisterEntry(Kind::kInStack, 0); // Short location.
136 stream.AddDexRegisterEntry(Kind::kConstant, -2); // Large location.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000137 stream.BeginInlineInfoEntry(&art_method, 3, number_of_dex_registers_in_inline_info);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100138 stream.EndInlineInfoEntry();
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000139 stream.BeginInlineInfoEntry(&art_method, 2, number_of_dex_registers_in_inline_info);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100140 stream.EndInlineInfoEntry();
Calin Juravle4f46ac52015-04-23 18:47:21 +0100141 stream.EndStackMapEntry();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100142
Vladimir Marko69d310e2017-10-09 14:12:23 +0100143 ArenaBitVector sp_mask2(&allocator, 0, true);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100144 sp_mask2.SetBit(3);
David Brazdilf10a25f2015-06-02 14:29:52 +0100145 sp_mask2.SetBit(8);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100146 stream.BeginStackMapEntry(1, 128, 0xFF, &sp_mask2, number_of_dex_registers, 0);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100147 stream.AddDexRegisterEntry(Kind::kInRegister, 18); // Short location.
148 stream.AddDexRegisterEntry(Kind::kInFpuRegister, 3); // Short location.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100149 stream.EndStackMapEntry();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100150
Vladimir Marko69d310e2017-10-09 14:12:23 +0100151 ArenaBitVector sp_mask3(&allocator, 0, true);
David Brazdild9cb68e2015-08-25 13:52:43 +0100152 sp_mask3.SetBit(1);
153 sp_mask3.SetBit(5);
154 stream.BeginStackMapEntry(2, 192, 0xAB, &sp_mask3, number_of_dex_registers, 0);
155 stream.AddDexRegisterEntry(Kind::kInRegister, 6); // Short location.
156 stream.AddDexRegisterEntry(Kind::kInRegisterHigh, 8); // Short location.
157 stream.EndStackMapEntry();
158
Vladimir Marko69d310e2017-10-09 14:12:23 +0100159 ArenaBitVector sp_mask4(&allocator, 0, true);
David Brazdild9cb68e2015-08-25 13:52:43 +0100160 sp_mask4.SetBit(6);
161 sp_mask4.SetBit(7);
162 stream.BeginStackMapEntry(3, 256, 0xCD, &sp_mask4, number_of_dex_registers, 0);
163 stream.AddDexRegisterEntry(Kind::kInFpuRegister, 3); // Short location, same in stack map 2.
164 stream.AddDexRegisterEntry(Kind::kInFpuRegisterHigh, 1); // Short location.
165 stream.EndStackMapEntry();
166
Calin Juravle4f46ac52015-04-23 18:47:21 +0100167 size_t size = stream.PrepareForFillIn();
Vladimir Marko69d310e2017-10-09 14:12:23 +0100168 void* memory = allocator.Alloc(size, kArenaAllocMisc);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100169 MemoryRegion region(memory, size);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700170 stream.FillInCodeInfo(region);
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100171
Nicolas Geoffray39468442014-09-02 15:17:15 +0100172 CodeInfo code_info(region);
David Srbecky052f8ca2018-04-26 15:42:54 +0100173 ASSERT_EQ(4u, code_info.GetNumberOfStackMaps());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100174
David Srbecky052f8ca2018-04-26 15:42:54 +0100175 uint32_t number_of_catalog_entries = code_info.GetNumberOfLocationCatalogEntries();
David Srbecky09ed0982016-02-12 21:58:43 +0000176 ASSERT_EQ(7u, number_of_catalog_entries);
David Srbecky052f8ca2018-04-26 15:42:54 +0100177 DexRegisterLocationCatalog location_catalog = code_info.GetDexRegisterLocationCatalog();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000178 // The Dex register location catalog contains:
David Brazdild9cb68e2015-08-25 13:52:43 +0100179 // - six 1-byte short Dex register locations, and
Roland Levillaina552e1c2015-03-26 15:01:03 +0000180 // - one 5-byte large Dex register location.
David Brazdild9cb68e2015-08-25 13:52:43 +0100181 size_t expected_location_catalog_size = 6u * 1u + 5u;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000182 ASSERT_EQ(expected_location_catalog_size, location_catalog.Size());
183
Roland Levillain12baf472015-03-05 12:41:42 +0000184 // First stack map.
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000185 {
David Srbecky052f8ca2018-04-26 15:42:54 +0100186 StackMap stack_map = code_info.GetStackMapAt(0);
187 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0)));
188 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64)));
189 ASSERT_EQ(0u, stack_map.GetDexPc());
190 ASSERT_EQ(64u, stack_map.GetNativePcOffset(kRuntimeISA));
191 ASSERT_EQ(0x3u, code_info.GetRegisterMaskOf(stack_map));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100192
David Srbecky052f8ca2018-04-26 15:42:54 +0100193 ASSERT_TRUE(CheckStackMask(code_info, stack_map, sp_mask1));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100194
David Srbecky052f8ca2018-04-26 15:42:54 +0100195 ASSERT_TRUE(stack_map.HasDexRegisterMap());
Roland Levillaina552e1c2015-03-26 15:01:03 +0000196 DexRegisterMap dex_register_map =
David Srbecky052f8ca2018-04-26 15:42:54 +0100197 code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000198 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(0));
199 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(1));
200 ASSERT_EQ(2u, dex_register_map.GetNumberOfLiveDexRegisters(number_of_dex_registers));
201 // The Dex register map contains:
202 // - one 1-byte live bit mask, and
203 // - one 1-byte set of location catalog entry indices composed of two 2-bit values.
204 size_t expected_dex_register_map_size = 1u + 1u;
205 ASSERT_EQ(expected_dex_register_map_size, dex_register_map.Size());
206
David Srbecky21d45b42018-05-30 06:35:05 +0100207 ASSERT_EQ(Kind::kInStack, dex_register_map.GetLocationKind(0));
208 ASSERT_EQ(Kind::kConstant, dex_register_map.GetLocationKind(1));
209 ASSERT_EQ(Kind::kInStack, dex_register_map.GetLocationInternalKind(0));
210 ASSERT_EQ(Kind::kConstantLargeValue, dex_register_map.GetLocationInternalKind(1));
211 ASSERT_EQ(0, dex_register_map.GetStackOffsetInBytes(0));
212 ASSERT_EQ(-2, dex_register_map.GetConstant(1));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000213
David Srbecky21d45b42018-05-30 06:35:05 +0100214 size_t index0 = dex_register_map.GetLocationCatalogEntryIndex(0, number_of_catalog_entries);
215 size_t index1 = dex_register_map.GetLocationCatalogEntryIndex(1, number_of_catalog_entries);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000216 ASSERT_EQ(0u, index0);
217 ASSERT_EQ(1u, index1);
218 DexRegisterLocation location0 = location_catalog.GetDexRegisterLocation(index0);
219 DexRegisterLocation location1 = location_catalog.GetDexRegisterLocation(index1);
220 ASSERT_EQ(Kind::kInStack, location0.GetKind());
221 ASSERT_EQ(Kind::kConstant, location1.GetKind());
222 ASSERT_EQ(Kind::kInStack, location0.GetInternalKind());
223 ASSERT_EQ(Kind::kConstantLargeValue, location1.GetInternalKind());
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000224 ASSERT_EQ(0, location0.GetValue());
225 ASSERT_EQ(-2, location1.GetValue());
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100226
David Srbecky052f8ca2018-04-26 15:42:54 +0100227 ASSERT_TRUE(stack_map.HasInlineInfo());
228 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map);
229 ASSERT_EQ(2u, inline_info.GetDepth());
230 ASSERT_EQ(3u, inline_info.GetDexPcAtDepth(0));
231 ASSERT_EQ(2u, inline_info.GetDexPcAtDepth(1));
232 ASSERT_TRUE(inline_info.EncodesArtMethodAtDepth(0));
233 ASSERT_TRUE(inline_info.EncodesArtMethodAtDepth(1));
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000234 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100235
Roland Levillain12baf472015-03-05 12:41:42 +0000236 // Second stack map.
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000237 {
David Srbecky052f8ca2018-04-26 15:42:54 +0100238 StackMap stack_map = code_info.GetStackMapAt(1);
239 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(1u)));
240 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(128u)));
241 ASSERT_EQ(1u, stack_map.GetDexPc());
242 ASSERT_EQ(128u, stack_map.GetNativePcOffset(kRuntimeISA));
243 ASSERT_EQ(0xFFu, code_info.GetRegisterMaskOf(stack_map));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100244
David Srbecky052f8ca2018-04-26 15:42:54 +0100245 ASSERT_TRUE(CheckStackMask(code_info, stack_map, sp_mask2));
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100246
David Srbecky052f8ca2018-04-26 15:42:54 +0100247 ASSERT_TRUE(stack_map.HasDexRegisterMap());
Roland Levillaina552e1c2015-03-26 15:01:03 +0000248 DexRegisterMap dex_register_map =
David Srbecky052f8ca2018-04-26 15:42:54 +0100249 code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000250 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(0));
251 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(1));
252 ASSERT_EQ(2u, dex_register_map.GetNumberOfLiveDexRegisters(number_of_dex_registers));
253 // The Dex register map contains:
254 // - one 1-byte live bit mask, and
255 // - one 1-byte set of location catalog entry indices composed of two 2-bit values.
256 size_t expected_dex_register_map_size = 1u + 1u;
257 ASSERT_EQ(expected_dex_register_map_size, dex_register_map.Size());
258
David Srbecky21d45b42018-05-30 06:35:05 +0100259 ASSERT_EQ(Kind::kInRegister, dex_register_map.GetLocationKind(0));
260 ASSERT_EQ(Kind::kInFpuRegister, dex_register_map.GetLocationKind(1));
261 ASSERT_EQ(Kind::kInRegister, dex_register_map.GetLocationInternalKind(0));
262 ASSERT_EQ(Kind::kInFpuRegister, dex_register_map.GetLocationInternalKind(1));
263 ASSERT_EQ(18, dex_register_map.GetMachineRegister(0));
264 ASSERT_EQ(3, dex_register_map.GetMachineRegister(1));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000265
David Srbecky21d45b42018-05-30 06:35:05 +0100266 size_t index0 = dex_register_map.GetLocationCatalogEntryIndex(0, number_of_catalog_entries);
267 size_t index1 = dex_register_map.GetLocationCatalogEntryIndex(1, number_of_catalog_entries);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000268 ASSERT_EQ(2u, index0);
269 ASSERT_EQ(3u, index1);
270 DexRegisterLocation location0 = location_catalog.GetDexRegisterLocation(index0);
271 DexRegisterLocation location1 = location_catalog.GetDexRegisterLocation(index1);
272 ASSERT_EQ(Kind::kInRegister, location0.GetKind());
273 ASSERT_EQ(Kind::kInFpuRegister, location1.GetKind());
274 ASSERT_EQ(Kind::kInRegister, location0.GetInternalKind());
275 ASSERT_EQ(Kind::kInFpuRegister, location1.GetInternalKind());
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000276 ASSERT_EQ(18, location0.GetValue());
277 ASSERT_EQ(3, location1.GetValue());
Roland Levillain12baf472015-03-05 12:41:42 +0000278
David Srbecky052f8ca2018-04-26 15:42:54 +0100279 ASSERT_FALSE(stack_map.HasInlineInfo());
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000280 }
David Brazdild9cb68e2015-08-25 13:52:43 +0100281
282 // Third stack map.
283 {
David Srbecky052f8ca2018-04-26 15:42:54 +0100284 StackMap stack_map = code_info.GetStackMapAt(2);
285 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(2u)));
286 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(192u)));
287 ASSERT_EQ(2u, stack_map.GetDexPc());
288 ASSERT_EQ(192u, stack_map.GetNativePcOffset(kRuntimeISA));
289 ASSERT_EQ(0xABu, code_info.GetRegisterMaskOf(stack_map));
David Brazdild9cb68e2015-08-25 13:52:43 +0100290
David Srbecky052f8ca2018-04-26 15:42:54 +0100291 ASSERT_TRUE(CheckStackMask(code_info, stack_map, sp_mask3));
David Brazdild9cb68e2015-08-25 13:52:43 +0100292
David Srbecky052f8ca2018-04-26 15:42:54 +0100293 ASSERT_TRUE(stack_map.HasDexRegisterMap());
David Brazdild9cb68e2015-08-25 13:52:43 +0100294 DexRegisterMap dex_register_map =
David Srbecky052f8ca2018-04-26 15:42:54 +0100295 code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
David Brazdild9cb68e2015-08-25 13:52:43 +0100296 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(0));
297 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(1));
298 ASSERT_EQ(2u, dex_register_map.GetNumberOfLiveDexRegisters(number_of_dex_registers));
299 // The Dex register map contains:
300 // - one 1-byte live bit mask, and
301 // - one 1-byte set of location catalog entry indices composed of two 2-bit values.
302 size_t expected_dex_register_map_size = 1u + 1u;
303 ASSERT_EQ(expected_dex_register_map_size, dex_register_map.Size());
304
David Srbecky21d45b42018-05-30 06:35:05 +0100305 ASSERT_EQ(Kind::kInRegister, dex_register_map.GetLocationKind(0));
306 ASSERT_EQ(Kind::kInRegisterHigh, dex_register_map.GetLocationKind(1));
307 ASSERT_EQ(Kind::kInRegister, dex_register_map.GetLocationInternalKind(0));
308 ASSERT_EQ(Kind::kInRegisterHigh, dex_register_map.GetLocationInternalKind(1));
309 ASSERT_EQ(6, dex_register_map.GetMachineRegister(0));
310 ASSERT_EQ(8, dex_register_map.GetMachineRegister(1));
David Brazdild9cb68e2015-08-25 13:52:43 +0100311
David Srbecky21d45b42018-05-30 06:35:05 +0100312 size_t index0 = dex_register_map.GetLocationCatalogEntryIndex(0, number_of_catalog_entries);
313 size_t index1 = dex_register_map.GetLocationCatalogEntryIndex(1, number_of_catalog_entries);
David Brazdild9cb68e2015-08-25 13:52:43 +0100314 ASSERT_EQ(4u, index0);
315 ASSERT_EQ(5u, index1);
316 DexRegisterLocation location0 = location_catalog.GetDexRegisterLocation(index0);
317 DexRegisterLocation location1 = location_catalog.GetDexRegisterLocation(index1);
318 ASSERT_EQ(Kind::kInRegister, location0.GetKind());
319 ASSERT_EQ(Kind::kInRegisterHigh, location1.GetKind());
320 ASSERT_EQ(Kind::kInRegister, location0.GetInternalKind());
321 ASSERT_EQ(Kind::kInRegisterHigh, location1.GetInternalKind());
322 ASSERT_EQ(6, location0.GetValue());
323 ASSERT_EQ(8, location1.GetValue());
324
David Srbecky052f8ca2018-04-26 15:42:54 +0100325 ASSERT_FALSE(stack_map.HasInlineInfo());
David Brazdild9cb68e2015-08-25 13:52:43 +0100326 }
327
328 // Fourth stack map.
329 {
David Srbecky052f8ca2018-04-26 15:42:54 +0100330 StackMap stack_map = code_info.GetStackMapAt(3);
331 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(3u)));
332 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(256u)));
333 ASSERT_EQ(3u, stack_map.GetDexPc());
334 ASSERT_EQ(256u, stack_map.GetNativePcOffset(kRuntimeISA));
335 ASSERT_EQ(0xCDu, code_info.GetRegisterMaskOf(stack_map));
David Brazdild9cb68e2015-08-25 13:52:43 +0100336
David Srbecky052f8ca2018-04-26 15:42:54 +0100337 ASSERT_TRUE(CheckStackMask(code_info, stack_map, sp_mask4));
David Brazdild9cb68e2015-08-25 13:52:43 +0100338
David Srbecky052f8ca2018-04-26 15:42:54 +0100339 ASSERT_TRUE(stack_map.HasDexRegisterMap());
David Brazdild9cb68e2015-08-25 13:52:43 +0100340 DexRegisterMap dex_register_map =
David Srbecky052f8ca2018-04-26 15:42:54 +0100341 code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
David Brazdild9cb68e2015-08-25 13:52:43 +0100342 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(0));
343 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(1));
344 ASSERT_EQ(2u, dex_register_map.GetNumberOfLiveDexRegisters(number_of_dex_registers));
345 // The Dex register map contains:
346 // - one 1-byte live bit mask, and
347 // - one 1-byte set of location catalog entry indices composed of two 2-bit values.
348 size_t expected_dex_register_map_size = 1u + 1u;
349 ASSERT_EQ(expected_dex_register_map_size, dex_register_map.Size());
350
David Srbecky21d45b42018-05-30 06:35:05 +0100351 ASSERT_EQ(Kind::kInFpuRegister, dex_register_map.GetLocationKind(0));
352 ASSERT_EQ(Kind::kInFpuRegisterHigh, dex_register_map.GetLocationKind(1));
353 ASSERT_EQ(Kind::kInFpuRegister, dex_register_map.GetLocationInternalKind(0));
354 ASSERT_EQ(Kind::kInFpuRegisterHigh, dex_register_map.GetLocationInternalKind(1));
355 ASSERT_EQ(3, dex_register_map.GetMachineRegister(0));
356 ASSERT_EQ(1, dex_register_map.GetMachineRegister(1));
David Brazdild9cb68e2015-08-25 13:52:43 +0100357
David Srbecky21d45b42018-05-30 06:35:05 +0100358 size_t index0 = dex_register_map.GetLocationCatalogEntryIndex(0, number_of_catalog_entries);
359 size_t index1 = dex_register_map.GetLocationCatalogEntryIndex(1, number_of_catalog_entries);
David Brazdild9cb68e2015-08-25 13:52:43 +0100360 ASSERT_EQ(3u, index0); // Shared with second stack map.
361 ASSERT_EQ(6u, index1);
362 DexRegisterLocation location0 = location_catalog.GetDexRegisterLocation(index0);
363 DexRegisterLocation location1 = location_catalog.GetDexRegisterLocation(index1);
364 ASSERT_EQ(Kind::kInFpuRegister, location0.GetKind());
365 ASSERT_EQ(Kind::kInFpuRegisterHigh, location1.GetKind());
366 ASSERT_EQ(Kind::kInFpuRegister, location0.GetInternalKind());
367 ASSERT_EQ(Kind::kInFpuRegisterHigh, location1.GetInternalKind());
368 ASSERT_EQ(3, location0.GetValue());
369 ASSERT_EQ(1, location1.GetValue());
370
David Srbecky052f8ca2018-04-26 15:42:54 +0100371 ASSERT_FALSE(stack_map.HasInlineInfo());
David Brazdild9cb68e2015-08-25 13:52:43 +0100372 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100373}
374
Mathieu Chartier32289082017-02-09 15:57:37 -0800375TEST(StackMapTest, TestDeduplicateInlineInfoDexRegisterMap) {
David Sehr3215fff2018-04-03 17:10:12 -0700376 MallocArenaPool pool;
Vladimir Marko174b2e22017-10-12 13:34:49 +0100377 ArenaStack arena_stack(&pool);
378 ScopedArenaAllocator allocator(&arena_stack);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100379 StackMapStream stream(&allocator, kRuntimeISA);
Mathieu Chartier32289082017-02-09 15:57:37 -0800380 ArtMethod art_method;
381
Vladimir Marko69d310e2017-10-09 14:12:23 +0100382 ArenaBitVector sp_mask1(&allocator, 0, true);
Mathieu Chartier32289082017-02-09 15:57:37 -0800383 sp_mask1.SetBit(2);
384 sp_mask1.SetBit(4);
385 const size_t number_of_dex_registers = 2;
386 const size_t number_of_dex_registers_in_inline_info = 2;
387 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask1, number_of_dex_registers, 1);
388 stream.AddDexRegisterEntry(Kind::kInStack, 0); // Short location.
389 stream.AddDexRegisterEntry(Kind::kConstant, -2); // Large location.
390 stream.BeginInlineInfoEntry(&art_method, 3, number_of_dex_registers_in_inline_info);
391 stream.AddDexRegisterEntry(Kind::kInStack, 0); // Short location.
392 stream.AddDexRegisterEntry(Kind::kConstant, -2); // Large location.
393 stream.EndInlineInfoEntry();
394 stream.EndStackMapEntry();
395
396 size_t size = stream.PrepareForFillIn();
Vladimir Marko69d310e2017-10-09 14:12:23 +0100397 void* memory = allocator.Alloc(size, kArenaAllocMisc);
Mathieu Chartier32289082017-02-09 15:57:37 -0800398 MemoryRegion region(memory, size);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700399 stream.FillInCodeInfo(region);
Mathieu Chartier32289082017-02-09 15:57:37 -0800400
401 CodeInfo code_info(region);
David Srbecky052f8ca2018-04-26 15:42:54 +0100402 ASSERT_EQ(1u, code_info.GetNumberOfStackMaps());
Mathieu Chartier32289082017-02-09 15:57:37 -0800403
David Srbecky052f8ca2018-04-26 15:42:54 +0100404 uint32_t number_of_catalog_entries = code_info.GetNumberOfLocationCatalogEntries();
Mathieu Chartier32289082017-02-09 15:57:37 -0800405 ASSERT_EQ(2u, number_of_catalog_entries);
David Srbecky052f8ca2018-04-26 15:42:54 +0100406 DexRegisterLocationCatalog location_catalog = code_info.GetDexRegisterLocationCatalog();
Mathieu Chartier32289082017-02-09 15:57:37 -0800407 // The Dex register location catalog contains:
408 // - one 1-byte short Dex register locations, and
409 // - one 5-byte large Dex register location.
410 const size_t expected_location_catalog_size = 1u + 5u;
411 ASSERT_EQ(expected_location_catalog_size, location_catalog.Size());
412
413 // First stack map.
414 {
David Srbecky052f8ca2018-04-26 15:42:54 +0100415 StackMap stack_map = code_info.GetStackMapAt(0);
416 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0)));
417 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64)));
418 ASSERT_EQ(0u, stack_map.GetDexPc());
419 ASSERT_EQ(64u, stack_map.GetNativePcOffset(kRuntimeISA));
420 ASSERT_EQ(0x3u, code_info.GetRegisterMaskOf(stack_map));
Mathieu Chartier32289082017-02-09 15:57:37 -0800421
David Srbecky052f8ca2018-04-26 15:42:54 +0100422 ASSERT_TRUE(CheckStackMask(code_info, stack_map, sp_mask1));
Mathieu Chartier32289082017-02-09 15:57:37 -0800423
David Srbecky052f8ca2018-04-26 15:42:54 +0100424 ASSERT_TRUE(stack_map.HasDexRegisterMap());
425 DexRegisterMap map(code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers));
Mathieu Chartier32289082017-02-09 15:57:37 -0800426 ASSERT_TRUE(map.IsDexRegisterLive(0));
427 ASSERT_TRUE(map.IsDexRegisterLive(1));
428 ASSERT_EQ(2u, map.GetNumberOfLiveDexRegisters(number_of_dex_registers));
429 // The Dex register map contains:
430 // - one 1-byte live bit mask, and
431 // - one 1-byte set of location catalog entry indices composed of two 2-bit values.
432 size_t expected_map_size = 1u + 1u;
433 ASSERT_EQ(expected_map_size, map.Size());
434
David Srbecky21d45b42018-05-30 06:35:05 +0100435 ASSERT_EQ(Kind::kInStack, map.GetLocationKind(0));
436 ASSERT_EQ(Kind::kConstant, map.GetLocationKind(1));
437 ASSERT_EQ(Kind::kInStack, map.GetLocationInternalKind(0));
438 ASSERT_EQ(Kind::kConstantLargeValue, map.GetLocationInternalKind(1));
439 ASSERT_EQ(0, map.GetStackOffsetInBytes(0));
440 ASSERT_EQ(-2, map.GetConstant(1));
Mathieu Chartier32289082017-02-09 15:57:37 -0800441
David Srbecky21d45b42018-05-30 06:35:05 +0100442 const size_t index0 = map.GetLocationCatalogEntryIndex(0, number_of_catalog_entries);
443 const size_t index1 = map.GetLocationCatalogEntryIndex(1, number_of_catalog_entries);
Mathieu Chartier32289082017-02-09 15:57:37 -0800444 ASSERT_EQ(0u, index0);
445 ASSERT_EQ(1u, index1);
446 DexRegisterLocation location0 = location_catalog.GetDexRegisterLocation(index0);
447 DexRegisterLocation location1 = location_catalog.GetDexRegisterLocation(index1);
448 ASSERT_EQ(Kind::kInStack, location0.GetKind());
449 ASSERT_EQ(Kind::kConstant, location1.GetKind());
450 ASSERT_EQ(Kind::kInStack, location0.GetInternalKind());
451 ASSERT_EQ(Kind::kConstantLargeValue, location1.GetInternalKind());
452 ASSERT_EQ(0, location0.GetValue());
453 ASSERT_EQ(-2, location1.GetValue());
454
455 // Test that the inline info dex register map deduplicated to the same offset as the stack map
456 // one.
David Srbecky052f8ca2018-04-26 15:42:54 +0100457 ASSERT_TRUE(stack_map.HasInlineInfo());
458 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map);
459 EXPECT_EQ(inline_info.GetDexRegisterMapOffsetAtDepth(0),
460 stack_map.GetDexRegisterMapOffset());
Mathieu Chartier32289082017-02-09 15:57:37 -0800461 }
462}
463
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000464TEST(StackMapTest, TestNonLiveDexRegisters) {
David Sehr3215fff2018-04-03 17:10:12 -0700465 MallocArenaPool pool;
Vladimir Marko174b2e22017-10-12 13:34:49 +0100466 ArenaStack arena_stack(&pool);
467 ScopedArenaAllocator allocator(&arena_stack);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100468 StackMapStream stream(&allocator, kRuntimeISA);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000469
Vladimir Marko69d310e2017-10-09 14:12:23 +0100470 ArenaBitVector sp_mask(&allocator, 0, false);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000471 uint32_t number_of_dex_registers = 2;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100472 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100473 stream.AddDexRegisterEntry(Kind::kNone, 0); // No location.
474 stream.AddDexRegisterEntry(Kind::kConstant, -2); // Large location.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100475 stream.EndStackMapEntry();
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000476
Calin Juravle4f46ac52015-04-23 18:47:21 +0100477 size_t size = stream.PrepareForFillIn();
Vladimir Marko69d310e2017-10-09 14:12:23 +0100478 void* memory = allocator.Alloc(size, kArenaAllocMisc);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000479 MemoryRegion region(memory, size);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700480 stream.FillInCodeInfo(region);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000481
482 CodeInfo code_info(region);
David Srbecky052f8ca2018-04-26 15:42:54 +0100483 ASSERT_EQ(1u, code_info.GetNumberOfStackMaps());
Roland Levillaina552e1c2015-03-26 15:01:03 +0000484
David Srbecky052f8ca2018-04-26 15:42:54 +0100485 uint32_t number_of_catalog_entries = code_info.GetNumberOfLocationCatalogEntries();
David Srbecky09ed0982016-02-12 21:58:43 +0000486 ASSERT_EQ(1u, number_of_catalog_entries);
David Srbecky052f8ca2018-04-26 15:42:54 +0100487 DexRegisterLocationCatalog location_catalog = code_info.GetDexRegisterLocationCatalog();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000488 // The Dex register location catalog contains:
489 // - one 5-byte large Dex register location.
490 size_t expected_location_catalog_size = 5u;
491 ASSERT_EQ(expected_location_catalog_size, location_catalog.Size());
492
David Srbecky052f8ca2018-04-26 15:42:54 +0100493 StackMap stack_map = code_info.GetStackMapAt(0);
494 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0)));
495 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64)));
496 ASSERT_EQ(0u, stack_map.GetDexPc());
497 ASSERT_EQ(64u, stack_map.GetNativePcOffset(kRuntimeISA));
498 ASSERT_EQ(0x3u, code_info.GetRegisterMaskOf(stack_map));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000499
David Srbecky052f8ca2018-04-26 15:42:54 +0100500 ASSERT_TRUE(stack_map.HasDexRegisterMap());
Roland Levillaina552e1c2015-03-26 15:01:03 +0000501 DexRegisterMap dex_register_map =
David Srbecky052f8ca2018-04-26 15:42:54 +0100502 code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000503 ASSERT_FALSE(dex_register_map.IsDexRegisterLive(0));
504 ASSERT_TRUE(dex_register_map.IsDexRegisterLive(1));
505 ASSERT_EQ(1u, dex_register_map.GetNumberOfLiveDexRegisters(number_of_dex_registers));
506 // The Dex register map contains:
507 // - one 1-byte live bit mask.
508 // No space is allocated for the sole location catalog entry index, as it is useless.
509 size_t expected_dex_register_map_size = 1u + 0u;
510 ASSERT_EQ(expected_dex_register_map_size, dex_register_map.Size());
511
David Srbecky21d45b42018-05-30 06:35:05 +0100512 ASSERT_EQ(Kind::kNone, dex_register_map.GetLocationKind(0));
513 ASSERT_EQ(Kind::kConstant, dex_register_map.GetLocationKind(1));
514 ASSERT_EQ(Kind::kNone, dex_register_map.GetLocationInternalKind(0));
515 ASSERT_EQ(Kind::kConstantLargeValue, dex_register_map.GetLocationInternalKind(1));
516 ASSERT_EQ(-2, dex_register_map.GetConstant(1));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000517
David Srbecky21d45b42018-05-30 06:35:05 +0100518 size_t index0 = dex_register_map.GetLocationCatalogEntryIndex(0, number_of_catalog_entries);
519 size_t index1 = dex_register_map.GetLocationCatalogEntryIndex(1, number_of_catalog_entries);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000520 ASSERT_EQ(DexRegisterLocationCatalog::kNoLocationEntryIndex, index0);
521 ASSERT_EQ(0u, index1);
522 DexRegisterLocation location0 = location_catalog.GetDexRegisterLocation(index0);
523 DexRegisterLocation location1 = location_catalog.GetDexRegisterLocation(index1);
524 ASSERT_EQ(Kind::kNone, location0.GetKind());
525 ASSERT_EQ(Kind::kConstant, location1.GetKind());
526 ASSERT_EQ(Kind::kNone, location0.GetInternalKind());
527 ASSERT_EQ(Kind::kConstantLargeValue, location1.GetInternalKind());
528 ASSERT_EQ(0, location0.GetValue());
529 ASSERT_EQ(-2, location1.GetValue());
530
David Srbecky052f8ca2018-04-26 15:42:54 +0100531 ASSERT_FALSE(stack_map.HasInlineInfo());
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000532}
533
534// Generate a stack map whose dex register offset is
535// StackMap::kNoDexRegisterMapSmallEncoding, and ensure we do
536// not treat it as kNoDexRegisterMap.
537TEST(StackMapTest, DexRegisterMapOffsetOverflow) {
David Sehr3215fff2018-04-03 17:10:12 -0700538 MallocArenaPool pool;
Vladimir Marko174b2e22017-10-12 13:34:49 +0100539 ArenaStack arena_stack(&pool);
540 ScopedArenaAllocator allocator(&arena_stack);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100541 StackMapStream stream(&allocator, kRuntimeISA);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000542
Vladimir Marko69d310e2017-10-09 14:12:23 +0100543 ArenaBitVector sp_mask(&allocator, 0, false);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000544 uint32_t number_of_dex_registers = 1024;
545 // Create the first stack map (and its Dex register map).
Calin Juravle4f46ac52015-04-23 18:47:21 +0100546 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000547 uint32_t number_of_dex_live_registers_in_dex_register_map_0 = number_of_dex_registers - 8;
548 for (uint32_t i = 0; i < number_of_dex_live_registers_in_dex_register_map_0; ++i) {
549 // Use two different Dex register locations to populate this map,
550 // as using a single value (in the whole CodeInfo object) would
551 // make this Dex register mapping data empty (see
552 // art::DexRegisterMap::SingleEntrySizeInBits).
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100553 stream.AddDexRegisterEntry(Kind::kConstant, i % 2); // Short location.
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000554 }
Calin Juravle4f46ac52015-04-23 18:47:21 +0100555 stream.EndStackMapEntry();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000556 // Create the second stack map (and its Dex register map).
Calin Juravle4f46ac52015-04-23 18:47:21 +0100557 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000558 for (uint32_t i = 0; i < number_of_dex_registers; ++i) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100559 stream.AddDexRegisterEntry(Kind::kConstant, 0); // Short location.
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000560 }
Calin Juravle4f46ac52015-04-23 18:47:21 +0100561 stream.EndStackMapEntry();
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000562
Calin Juravle4f46ac52015-04-23 18:47:21 +0100563 size_t size = stream.PrepareForFillIn();
Vladimir Marko69d310e2017-10-09 14:12:23 +0100564 void* memory = allocator.Alloc(size, kArenaAllocMisc);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000565 MemoryRegion region(memory, size);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700566 stream.FillInCodeInfo(region);
Nicolas Geoffray004c2302015-03-20 10:06:38 +0000567
568 CodeInfo code_info(region);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000569 // The location catalog contains two entries (DexRegisterLocation(kConstant, 0)
570 // and DexRegisterLocation(kConstant, 1)), therefore the location catalog index
571 // has a size of 1 bit.
David Srbecky052f8ca2018-04-26 15:42:54 +0100572 uint32_t number_of_catalog_entries = code_info.GetNumberOfLocationCatalogEntries();
David Srbecky09ed0982016-02-12 21:58:43 +0000573 ASSERT_EQ(2u, number_of_catalog_entries);
574 ASSERT_EQ(1u, DexRegisterMap::SingleEntrySizeInBits(number_of_catalog_entries));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000575
576 // The first Dex register map contains:
577 // - a live register bit mask for 1024 registers (that is, 128 bytes of
578 // data); and
579 // - Dex register mapping information for 1016 1-bit Dex (live) register
580 // locations (that is, 127 bytes of data).
581 // Hence it has a size of 255 bytes, and therefore...
582 ASSERT_EQ(128u, DexRegisterMap::GetLiveBitMaskSize(number_of_dex_registers));
David Srbecky052f8ca2018-04-26 15:42:54 +0100583 StackMap stack_map0 = code_info.GetStackMapAt(0);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000584 DexRegisterMap dex_register_map0 =
David Srbecky052f8ca2018-04-26 15:42:54 +0100585 code_info.GetDexRegisterMapOf(stack_map0, number_of_dex_registers);
David Srbecky21d45b42018-05-30 06:35:05 +0100586 ASSERT_EQ(127u, dex_register_map0.GetLocationMappingDataSize(number_of_catalog_entries));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000587 ASSERT_EQ(255u, dex_register_map0.Size());
588
David Srbecky052f8ca2018-04-26 15:42:54 +0100589 StackMap stack_map1 = code_info.GetStackMapAt(1);
590 ASSERT_TRUE(stack_map1.HasDexRegisterMap());
Roland Levillaina552e1c2015-03-26 15:01:03 +0000591 // ...the offset of the second Dex register map (relative to the
592 // beginning of the Dex register maps region) is 255 (i.e.,
593 // kNoDexRegisterMapSmallEncoding).
David Srbecky052f8ca2018-04-26 15:42:54 +0100594 ASSERT_NE(stack_map1.GetDexRegisterMapOffset(),
595 StackMap::kNoValue);
596 ASSERT_EQ(stack_map1.GetDexRegisterMapOffset(), 0xFFu);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000597}
598
Calin Juravle6ae70962015-03-18 16:31:28 +0000599TEST(StackMapTest, TestShareDexRegisterMap) {
David Sehr3215fff2018-04-03 17:10:12 -0700600 MallocArenaPool pool;
Vladimir Marko174b2e22017-10-12 13:34:49 +0100601 ArenaStack arena_stack(&pool);
602 ScopedArenaAllocator allocator(&arena_stack);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100603 StackMapStream stream(&allocator, kRuntimeISA);
Calin Juravle6ae70962015-03-18 16:31:28 +0000604
Vladimir Marko69d310e2017-10-09 14:12:23 +0100605 ArenaBitVector sp_mask(&allocator, 0, false);
Calin Juravle6ae70962015-03-18 16:31:28 +0000606 uint32_t number_of_dex_registers = 2;
607 // First stack map.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100608 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100609 stream.AddDexRegisterEntry(Kind::kInRegister, 0); // Short location.
610 stream.AddDexRegisterEntry(Kind::kConstant, -2); // Large location.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100611 stream.EndStackMapEntry();
Calin Juravle6ae70962015-03-18 16:31:28 +0000612 // Second stack map, which should share the same dex register map.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100613 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100614 stream.AddDexRegisterEntry(Kind::kInRegister, 0); // Short location.
615 stream.AddDexRegisterEntry(Kind::kConstant, -2); // Large location.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100616 stream.EndStackMapEntry();
Calin Juravle6ae70962015-03-18 16:31:28 +0000617 // Third stack map (doesn't share the dex register map).
Calin Juravle4f46ac52015-04-23 18:47:21 +0100618 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100619 stream.AddDexRegisterEntry(Kind::kInRegister, 2); // Short location.
620 stream.AddDexRegisterEntry(Kind::kConstant, -2); // Large location.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100621 stream.EndStackMapEntry();
Calin Juravle6ae70962015-03-18 16:31:28 +0000622
Calin Juravle4f46ac52015-04-23 18:47:21 +0100623 size_t size = stream.PrepareForFillIn();
Vladimir Marko69d310e2017-10-09 14:12:23 +0100624 void* memory = allocator.Alloc(size, kArenaAllocMisc);
Calin Juravle6ae70962015-03-18 16:31:28 +0000625 MemoryRegion region(memory, size);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700626 stream.FillInCodeInfo(region);
Calin Juravle6ae70962015-03-18 16:31:28 +0000627
628 CodeInfo ci(region);
David Brazdilf677ebf2015-05-29 16:29:43 +0100629
Calin Juravle6ae70962015-03-18 16:31:28 +0000630 // Verify first stack map.
David Srbecky052f8ca2018-04-26 15:42:54 +0100631 StackMap sm0 = ci.GetStackMapAt(0);
632 DexRegisterMap dex_registers0 = ci.GetDexRegisterMapOf(sm0, number_of_dex_registers);
David Srbecky21d45b42018-05-30 06:35:05 +0100633 ASSERT_EQ(0, dex_registers0.GetMachineRegister(0));
634 ASSERT_EQ(-2, dex_registers0.GetConstant(1));
Calin Juravle6ae70962015-03-18 16:31:28 +0000635
636 // Verify second stack map.
David Srbecky052f8ca2018-04-26 15:42:54 +0100637 StackMap sm1 = ci.GetStackMapAt(1);
638 DexRegisterMap dex_registers1 = ci.GetDexRegisterMapOf(sm1, number_of_dex_registers);
David Srbecky21d45b42018-05-30 06:35:05 +0100639 ASSERT_EQ(0, dex_registers1.GetMachineRegister(0));
640 ASSERT_EQ(-2, dex_registers1.GetConstant(1));
Calin Juravle6ae70962015-03-18 16:31:28 +0000641
642 // Verify third stack map.
David Srbecky052f8ca2018-04-26 15:42:54 +0100643 StackMap sm2 = ci.GetStackMapAt(2);
644 DexRegisterMap dex_registers2 = ci.GetDexRegisterMapOf(sm2, number_of_dex_registers);
David Srbecky21d45b42018-05-30 06:35:05 +0100645 ASSERT_EQ(2, dex_registers2.GetMachineRegister(0));
646 ASSERT_EQ(-2, dex_registers2.GetConstant(1));
Calin Juravle6ae70962015-03-18 16:31:28 +0000647
648 // Verify dex register map offsets.
David Srbecky052f8ca2018-04-26 15:42:54 +0100649 ASSERT_EQ(sm0.GetDexRegisterMapOffset(),
650 sm1.GetDexRegisterMapOffset());
651 ASSERT_NE(sm0.GetDexRegisterMapOffset(),
652 sm2.GetDexRegisterMapOffset());
653 ASSERT_NE(sm1.GetDexRegisterMapOffset(),
654 sm2.GetDexRegisterMapOffset());
Calin Juravle6ae70962015-03-18 16:31:28 +0000655}
656
Roland Levillaina552e1c2015-03-26 15:01:03 +0000657TEST(StackMapTest, TestNoDexRegisterMap) {
David Sehr3215fff2018-04-03 17:10:12 -0700658 MallocArenaPool pool;
Vladimir Marko174b2e22017-10-12 13:34:49 +0100659 ArenaStack arena_stack(&pool);
660 ScopedArenaAllocator allocator(&arena_stack);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100661 StackMapStream stream(&allocator, kRuntimeISA);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000662
Vladimir Marko69d310e2017-10-09 14:12:23 +0100663 ArenaBitVector sp_mask(&allocator, 0, false);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000664 uint32_t number_of_dex_registers = 0;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100665 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0);
666 stream.EndStackMapEntry();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000667
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000668 number_of_dex_registers = 1;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800669 stream.BeginStackMapEntry(1, 68, 0x4, &sp_mask, number_of_dex_registers, 0);
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000670 stream.EndStackMapEntry();
671
Calin Juravle4f46ac52015-04-23 18:47:21 +0100672 size_t size = stream.PrepareForFillIn();
Vladimir Marko69d310e2017-10-09 14:12:23 +0100673 void* memory = allocator.Alloc(size, kArenaAllocMisc);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000674 MemoryRegion region(memory, size);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700675 stream.FillInCodeInfo(region);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000676
677 CodeInfo code_info(region);
David Srbecky052f8ca2018-04-26 15:42:54 +0100678 ASSERT_EQ(2u, code_info.GetNumberOfStackMaps());
Roland Levillaina552e1c2015-03-26 15:01:03 +0000679
David Srbecky052f8ca2018-04-26 15:42:54 +0100680 uint32_t number_of_catalog_entries = code_info.GetNumberOfLocationCatalogEntries();
David Srbecky09ed0982016-02-12 21:58:43 +0000681 ASSERT_EQ(0u, number_of_catalog_entries);
David Srbecky052f8ca2018-04-26 15:42:54 +0100682 DexRegisterLocationCatalog location_catalog = code_info.GetDexRegisterLocationCatalog();
Roland Levillaina552e1c2015-03-26 15:01:03 +0000683 ASSERT_EQ(0u, location_catalog.Size());
684
David Srbecky052f8ca2018-04-26 15:42:54 +0100685 StackMap stack_map = code_info.GetStackMapAt(0);
686 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0)));
687 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64)));
688 ASSERT_EQ(0u, stack_map.GetDexPc());
689 ASSERT_EQ(64u, stack_map.GetNativePcOffset(kRuntimeISA));
690 ASSERT_EQ(0x3u, code_info.GetRegisterMaskOf(stack_map));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000691
David Srbecky052f8ca2018-04-26 15:42:54 +0100692 ASSERT_FALSE(stack_map.HasDexRegisterMap());
693 ASSERT_FALSE(stack_map.HasInlineInfo());
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000694
David Srbecky052f8ca2018-04-26 15:42:54 +0100695 stack_map = code_info.GetStackMapAt(1);
696 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(1)));
697 ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(68)));
698 ASSERT_EQ(1u, stack_map.GetDexPc());
699 ASSERT_EQ(68u, stack_map.GetNativePcOffset(kRuntimeISA));
700 ASSERT_EQ(0x4u, code_info.GetRegisterMaskOf(stack_map));
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000701
David Srbecky052f8ca2018-04-26 15:42:54 +0100702 ASSERT_FALSE(stack_map.HasDexRegisterMap());
703 ASSERT_FALSE(stack_map.HasInlineInfo());
Roland Levillaina552e1c2015-03-26 15:01:03 +0000704}
705
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100706TEST(StackMapTest, InlineTest) {
David Sehr3215fff2018-04-03 17:10:12 -0700707 MallocArenaPool pool;
Vladimir Marko174b2e22017-10-12 13:34:49 +0100708 ArenaStack arena_stack(&pool);
709 ScopedArenaAllocator allocator(&arena_stack);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100710 StackMapStream stream(&allocator, kRuntimeISA);
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000711 ArtMethod art_method;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100712
Vladimir Marko69d310e2017-10-09 14:12:23 +0100713 ArenaBitVector sp_mask1(&allocator, 0, true);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100714 sp_mask1.SetBit(2);
715 sp_mask1.SetBit(4);
716
717 // First stack map.
718 stream.BeginStackMapEntry(0, 64, 0x3, &sp_mask1, 2, 2);
719 stream.AddDexRegisterEntry(Kind::kInStack, 0);
720 stream.AddDexRegisterEntry(Kind::kConstant, 4);
721
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000722 stream.BeginInlineInfoEntry(&art_method, 2, 1);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100723 stream.AddDexRegisterEntry(Kind::kInStack, 8);
724 stream.EndInlineInfoEntry();
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000725 stream.BeginInlineInfoEntry(&art_method, 3, 3);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100726 stream.AddDexRegisterEntry(Kind::kInStack, 16);
727 stream.AddDexRegisterEntry(Kind::kConstant, 20);
728 stream.AddDexRegisterEntry(Kind::kInRegister, 15);
729 stream.EndInlineInfoEntry();
730
731 stream.EndStackMapEntry();
732
733 // Second stack map.
734 stream.BeginStackMapEntry(2, 22, 0x3, &sp_mask1, 2, 3);
735 stream.AddDexRegisterEntry(Kind::kInStack, 56);
736 stream.AddDexRegisterEntry(Kind::kConstant, 0);
737
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000738 stream.BeginInlineInfoEntry(&art_method, 2, 1);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100739 stream.AddDexRegisterEntry(Kind::kInStack, 12);
740 stream.EndInlineInfoEntry();
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000741 stream.BeginInlineInfoEntry(&art_method, 3, 3);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100742 stream.AddDexRegisterEntry(Kind::kInStack, 80);
743 stream.AddDexRegisterEntry(Kind::kConstant, 10);
744 stream.AddDexRegisterEntry(Kind::kInRegister, 5);
745 stream.EndInlineInfoEntry();
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000746 stream.BeginInlineInfoEntry(&art_method, 5, 0);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100747 stream.EndInlineInfoEntry();
748
749 stream.EndStackMapEntry();
750
751 // Third stack map.
752 stream.BeginStackMapEntry(4, 56, 0x3, &sp_mask1, 2, 0);
753 stream.AddDexRegisterEntry(Kind::kNone, 0);
754 stream.AddDexRegisterEntry(Kind::kConstant, 4);
755 stream.EndStackMapEntry();
756
757 // Fourth stack map.
758 stream.BeginStackMapEntry(6, 78, 0x3, &sp_mask1, 2, 3);
759 stream.AddDexRegisterEntry(Kind::kInStack, 56);
760 stream.AddDexRegisterEntry(Kind::kConstant, 0);
761
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000762 stream.BeginInlineInfoEntry(&art_method, 2, 0);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100763 stream.EndInlineInfoEntry();
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000764 stream.BeginInlineInfoEntry(&art_method, 5, 1);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100765 stream.AddDexRegisterEntry(Kind::kInRegister, 2);
766 stream.EndInlineInfoEntry();
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000767 stream.BeginInlineInfoEntry(&art_method, 10, 2);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100768 stream.AddDexRegisterEntry(Kind::kNone, 0);
769 stream.AddDexRegisterEntry(Kind::kInRegister, 3);
770 stream.EndInlineInfoEntry();
771
772 stream.EndStackMapEntry();
773
774 size_t size = stream.PrepareForFillIn();
Vladimir Marko69d310e2017-10-09 14:12:23 +0100775 void* memory = allocator.Alloc(size, kArenaAllocMisc);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100776 MemoryRegion region(memory, size);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700777 stream.FillInCodeInfo(region);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100778
779 CodeInfo ci(region);
780
781 {
782 // Verify first stack map.
David Srbecky052f8ca2018-04-26 15:42:54 +0100783 StackMap sm0 = ci.GetStackMapAt(0);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100784
David Srbecky052f8ca2018-04-26 15:42:54 +0100785 DexRegisterMap dex_registers0 = ci.GetDexRegisterMapOf(sm0, 2);
David Srbecky21d45b42018-05-30 06:35:05 +0100786 ASSERT_EQ(0, dex_registers0.GetStackOffsetInBytes(0));
787 ASSERT_EQ(4, dex_registers0.GetConstant(1));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100788
David Srbecky052f8ca2018-04-26 15:42:54 +0100789 InlineInfo if0 = ci.GetInlineInfoOf(sm0);
790 ASSERT_EQ(2u, if0.GetDepth());
791 ASSERT_EQ(2u, if0.GetDexPcAtDepth(0));
792 ASSERT_TRUE(if0.EncodesArtMethodAtDepth(0));
793 ASSERT_EQ(3u, if0.GetDexPcAtDepth(1));
794 ASSERT_TRUE(if0.EncodesArtMethodAtDepth(1));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100795
David Srbecky052f8ca2018-04-26 15:42:54 +0100796 DexRegisterMap dex_registers1 = ci.GetDexRegisterMapAtDepth(0, if0, 1);
David Srbecky21d45b42018-05-30 06:35:05 +0100797 ASSERT_EQ(8, dex_registers1.GetStackOffsetInBytes(0));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100798
David Srbecky052f8ca2018-04-26 15:42:54 +0100799 DexRegisterMap dex_registers2 = ci.GetDexRegisterMapAtDepth(1, if0, 3);
David Srbecky21d45b42018-05-30 06:35:05 +0100800 ASSERT_EQ(16, dex_registers2.GetStackOffsetInBytes(0));
801 ASSERT_EQ(20, dex_registers2.GetConstant(1));
802 ASSERT_EQ(15, dex_registers2.GetMachineRegister(2));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100803 }
804
805 {
806 // Verify second stack map.
David Srbecky052f8ca2018-04-26 15:42:54 +0100807 StackMap sm1 = ci.GetStackMapAt(1);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100808
David Srbecky052f8ca2018-04-26 15:42:54 +0100809 DexRegisterMap dex_registers0 = ci.GetDexRegisterMapOf(sm1, 2);
David Srbecky21d45b42018-05-30 06:35:05 +0100810 ASSERT_EQ(56, dex_registers0.GetStackOffsetInBytes(0));
811 ASSERT_EQ(0, dex_registers0.GetConstant(1));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100812
David Srbecky052f8ca2018-04-26 15:42:54 +0100813 InlineInfo if1 = ci.GetInlineInfoOf(sm1);
814 ASSERT_EQ(3u, if1.GetDepth());
815 ASSERT_EQ(2u, if1.GetDexPcAtDepth(0));
816 ASSERT_TRUE(if1.EncodesArtMethodAtDepth(0));
817 ASSERT_EQ(3u, if1.GetDexPcAtDepth(1));
818 ASSERT_TRUE(if1.EncodesArtMethodAtDepth(1));
819 ASSERT_EQ(5u, if1.GetDexPcAtDepth(2));
820 ASSERT_TRUE(if1.EncodesArtMethodAtDepth(2));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100821
David Srbecky052f8ca2018-04-26 15:42:54 +0100822 DexRegisterMap dex_registers1 = ci.GetDexRegisterMapAtDepth(0, if1, 1);
David Srbecky21d45b42018-05-30 06:35:05 +0100823 ASSERT_EQ(12, dex_registers1.GetStackOffsetInBytes(0));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100824
David Srbecky052f8ca2018-04-26 15:42:54 +0100825 DexRegisterMap dex_registers2 = ci.GetDexRegisterMapAtDepth(1, if1, 3);
David Srbecky21d45b42018-05-30 06:35:05 +0100826 ASSERT_EQ(80, dex_registers2.GetStackOffsetInBytes(0));
827 ASSERT_EQ(10, dex_registers2.GetConstant(1));
828 ASSERT_EQ(5, dex_registers2.GetMachineRegister(2));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100829
David Srbecky052f8ca2018-04-26 15:42:54 +0100830 ASSERT_FALSE(if1.HasDexRegisterMapAtDepth(2));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100831 }
832
833 {
834 // Verify third stack map.
David Srbecky052f8ca2018-04-26 15:42:54 +0100835 StackMap sm2 = ci.GetStackMapAt(2);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100836
David Srbecky052f8ca2018-04-26 15:42:54 +0100837 DexRegisterMap dex_registers0 = ci.GetDexRegisterMapOf(sm2, 2);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100838 ASSERT_FALSE(dex_registers0.IsDexRegisterLive(0));
David Srbecky21d45b42018-05-30 06:35:05 +0100839 ASSERT_EQ(4, dex_registers0.GetConstant(1));
David Srbecky052f8ca2018-04-26 15:42:54 +0100840 ASSERT_FALSE(sm2.HasInlineInfo());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100841 }
842
843 {
844 // Verify fourth stack map.
David Srbecky052f8ca2018-04-26 15:42:54 +0100845 StackMap sm3 = ci.GetStackMapAt(3);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100846
David Srbecky052f8ca2018-04-26 15:42:54 +0100847 DexRegisterMap dex_registers0 = ci.GetDexRegisterMapOf(sm3, 2);
David Srbecky21d45b42018-05-30 06:35:05 +0100848 ASSERT_EQ(56, dex_registers0.GetStackOffsetInBytes(0));
849 ASSERT_EQ(0, dex_registers0.GetConstant(1));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100850
David Srbecky052f8ca2018-04-26 15:42:54 +0100851 InlineInfo if2 = ci.GetInlineInfoOf(sm3);
852 ASSERT_EQ(3u, if2.GetDepth());
853 ASSERT_EQ(2u, if2.GetDexPcAtDepth(0));
854 ASSERT_TRUE(if2.EncodesArtMethodAtDepth(0));
855 ASSERT_EQ(5u, if2.GetDexPcAtDepth(1));
856 ASSERT_TRUE(if2.EncodesArtMethodAtDepth(1));
857 ASSERT_EQ(10u, if2.GetDexPcAtDepth(2));
858 ASSERT_TRUE(if2.EncodesArtMethodAtDepth(2));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100859
David Srbecky052f8ca2018-04-26 15:42:54 +0100860 ASSERT_FALSE(if2.HasDexRegisterMapAtDepth(0));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100861
David Srbecky052f8ca2018-04-26 15:42:54 +0100862 DexRegisterMap dex_registers1 = ci.GetDexRegisterMapAtDepth(1, if2, 1);
David Srbecky21d45b42018-05-30 06:35:05 +0100863 ASSERT_EQ(2, dex_registers1.GetMachineRegister(0));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100864
David Srbecky052f8ca2018-04-26 15:42:54 +0100865 DexRegisterMap dex_registers2 = ci.GetDexRegisterMapAtDepth(2, if2, 2);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100866 ASSERT_FALSE(dex_registers2.IsDexRegisterLive(0));
David Srbecky21d45b42018-05-30 06:35:05 +0100867 ASSERT_EQ(3, dex_registers2.GetMachineRegister(1));
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100868 }
869}
870
David Srbeckyd02b23f2018-05-29 23:27:22 +0100871TEST(StackMapTest, PackedNativePcTest) {
872 uint32_t packed_thumb2 =
873 StackMap::PackNativePc(kThumb2InstructionAlignment, InstructionSet::kThumb2);
874 uint32_t packed_arm64 =
875 StackMap::PackNativePc(kArm64InstructionAlignment, InstructionSet::kArm64);
876 uint32_t packed_x86 =
877 StackMap::PackNativePc(kX86InstructionAlignment, InstructionSet::kX86);
878 uint32_t packed_x86_64 =
879 StackMap::PackNativePc(kX86_64InstructionAlignment, InstructionSet::kX86_64);
880 uint32_t packed_mips =
881 StackMap::PackNativePc(kMipsInstructionAlignment, InstructionSet::kMips);
882 uint32_t packed_mips64 =
883 StackMap::PackNativePc(kMips64InstructionAlignment, InstructionSet::kMips64);
884 EXPECT_EQ(StackMap::UnpackNativePc(packed_thumb2, InstructionSet::kThumb2),
885 kThumb2InstructionAlignment);
886 EXPECT_EQ(StackMap::UnpackNativePc(packed_arm64, InstructionSet::kArm64),
887 kArm64InstructionAlignment);
888 EXPECT_EQ(StackMap::UnpackNativePc(packed_x86, InstructionSet::kX86),
889 kX86InstructionAlignment);
890 EXPECT_EQ(StackMap::UnpackNativePc(packed_x86_64, InstructionSet::kX86_64),
891 kX86_64InstructionAlignment);
892 EXPECT_EQ(StackMap::UnpackNativePc(packed_mips, InstructionSet::kMips),
893 kMipsInstructionAlignment);
894 EXPECT_EQ(StackMap::UnpackNativePc(packed_mips64, InstructionSet::kMips64),
895 kMips64InstructionAlignment);
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800896}
897
David Srbecky45aa5982016-03-18 02:15:09 +0000898TEST(StackMapTest, TestDeduplicateStackMask) {
David Sehr3215fff2018-04-03 17:10:12 -0700899 MallocArenaPool pool;
Vladimir Marko174b2e22017-10-12 13:34:49 +0100900 ArenaStack arena_stack(&pool);
901 ScopedArenaAllocator allocator(&arena_stack);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100902 StackMapStream stream(&allocator, kRuntimeISA);
David Srbecky45aa5982016-03-18 02:15:09 +0000903
Vladimir Marko69d310e2017-10-09 14:12:23 +0100904 ArenaBitVector sp_mask(&allocator, 0, true);
David Srbecky45aa5982016-03-18 02:15:09 +0000905 sp_mask.SetBit(1);
906 sp_mask.SetBit(4);
907 stream.BeginStackMapEntry(0, 4, 0x3, &sp_mask, 0, 0);
908 stream.EndStackMapEntry();
909 stream.BeginStackMapEntry(0, 8, 0x3, &sp_mask, 0, 0);
910 stream.EndStackMapEntry();
911
912 size_t size = stream.PrepareForFillIn();
Vladimir Marko69d310e2017-10-09 14:12:23 +0100913 void* memory = allocator.Alloc(size, kArenaAllocMisc);
David Srbecky45aa5982016-03-18 02:15:09 +0000914 MemoryRegion region(memory, size);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700915 stream.FillInCodeInfo(region);
David Srbecky45aa5982016-03-18 02:15:09 +0000916
917 CodeInfo code_info(region);
David Srbecky052f8ca2018-04-26 15:42:54 +0100918 ASSERT_EQ(2u, code_info.GetNumberOfStackMaps());
David Srbecky45aa5982016-03-18 02:15:09 +0000919
David Srbecky052f8ca2018-04-26 15:42:54 +0100920 StackMap stack_map1 = code_info.GetStackMapForNativePcOffset(4);
921 StackMap stack_map2 = code_info.GetStackMapForNativePcOffset(8);
922 EXPECT_EQ(stack_map1.GetStackMaskIndex(),
923 stack_map2.GetStackMaskIndex());
David Srbecky45aa5982016-03-18 02:15:09 +0000924}
925
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800926TEST(StackMapTest, TestInvokeInfo) {
David Sehr3215fff2018-04-03 17:10:12 -0700927 MallocArenaPool pool;
Vladimir Marko174b2e22017-10-12 13:34:49 +0100928 ArenaStack arena_stack(&pool);
929 ScopedArenaAllocator allocator(&arena_stack);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100930 StackMapStream stream(&allocator, kRuntimeISA);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800931
Vladimir Marko69d310e2017-10-09 14:12:23 +0100932 ArenaBitVector sp_mask(&allocator, 0, true);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800933 sp_mask.SetBit(1);
934 stream.BeginStackMapEntry(0, 4, 0x3, &sp_mask, 0, 0);
935 stream.AddInvoke(kSuper, 1);
936 stream.EndStackMapEntry();
937 stream.BeginStackMapEntry(0, 8, 0x3, &sp_mask, 0, 0);
938 stream.AddInvoke(kStatic, 3);
939 stream.EndStackMapEntry();
940 stream.BeginStackMapEntry(0, 16, 0x3, &sp_mask, 0, 0);
941 stream.AddInvoke(kDirect, 65535);
942 stream.EndStackMapEntry();
943
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700944 const size_t code_info_size = stream.PrepareForFillIn();
Vladimir Marko69d310e2017-10-09 14:12:23 +0100945 MemoryRegion code_info_region(allocator.Alloc(code_info_size, kArenaAllocMisc), code_info_size);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700946 stream.FillInCodeInfo(code_info_region);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800947
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700948 const size_t method_info_size = stream.ComputeMethodInfoSize();
Vladimir Marko69d310e2017-10-09 14:12:23 +0100949 MemoryRegion method_info_region(allocator.Alloc(method_info_size, kArenaAllocMisc),
950 method_info_size);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700951 stream.FillInMethodInfo(method_info_region);
952
953 CodeInfo code_info(code_info_region);
954 MethodInfo method_info(method_info_region.begin());
David Srbecky052f8ca2018-04-26 15:42:54 +0100955 ASSERT_EQ(3u, code_info.GetNumberOfStackMaps());
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800956
David Srbecky052f8ca2018-04-26 15:42:54 +0100957 InvokeInfo invoke1(code_info.GetInvokeInfoForNativePcOffset(4));
958 InvokeInfo invoke2(code_info.GetInvokeInfoForNativePcOffset(8));
959 InvokeInfo invoke3(code_info.GetInvokeInfoForNativePcOffset(16));
960 InvokeInfo invoke_invalid(code_info.GetInvokeInfoForNativePcOffset(12));
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800961 EXPECT_FALSE(invoke_invalid.IsValid()); // No entry for that index.
962 EXPECT_TRUE(invoke1.IsValid());
963 EXPECT_TRUE(invoke2.IsValid());
964 EXPECT_TRUE(invoke3.IsValid());
David Srbecky052f8ca2018-04-26 15:42:54 +0100965 EXPECT_EQ(invoke1.GetInvokeType(), kSuper);
966 EXPECT_EQ(invoke1.GetMethodIndex(method_info), 1u);
967 EXPECT_EQ(invoke1.GetNativePcOffset(kRuntimeISA), 4u);
968 EXPECT_EQ(invoke2.GetInvokeType(), kStatic);
969 EXPECT_EQ(invoke2.GetMethodIndex(method_info), 3u);
970 EXPECT_EQ(invoke2.GetNativePcOffset(kRuntimeISA), 8u);
971 EXPECT_EQ(invoke3.GetInvokeType(), kDirect);
972 EXPECT_EQ(invoke3.GetMethodIndex(method_info), 65535u);
973 EXPECT_EQ(invoke3.GetNativePcOffset(kRuntimeISA), 16u);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800974}
975
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100976} // namespace art