Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 23 | namespace art { |
| 24 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 25 | static bool SameBits(MemoryRegion region, const BitVector& bit_vector) { |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 26 | 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 Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 34 | using Kind = DexRegisterLocation::Kind; |
| 35 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 36 | TEST(StackMapTest, Test1) { |
| 37 | ArenaPool pool; |
| 38 | ArenaAllocator arena(&pool); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 39 | StackMapStream stream(&arena); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 40 | |
| 41 | ArenaBitVector sp_mask(&arena, 0, false); |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 42 | size_t number_of_dex_registers = 2; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 43 | stream.AddStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 44 | stream.AddDexRegisterEntry(0, Kind::kInStack, 0); // Short location. |
| 45 | stream.AddDexRegisterEntry(1, Kind::kConstant, -2); // Short location. |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 46 | |
| 47 | size_t size = stream.ComputeNeededSize(); |
| 48 | void* memory = arena.Alloc(size, kArenaAllocMisc); |
| 49 | MemoryRegion region(memory, size); |
| 50 | stream.FillIn(region); |
| 51 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 52 | CodeInfo code_info(region); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 53 | ASSERT_EQ(0u, code_info.GetStackMaskSize()); |
| 54 | ASSERT_EQ(1u, code_info.GetNumberOfStackMaps()); |
| 55 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 56 | uint32_t number_of_location_catalog_entries = |
| 57 | code_info.GetNumberOfDexRegisterLocationCatalogEntries(); |
| 58 | ASSERT_EQ(2u, number_of_location_catalog_entries); |
| 59 | DexRegisterLocationCatalog location_catalog = code_info.GetDexRegisterLocationCatalog(); |
| 60 | // The Dex register location catalog contains: |
| 61 | // - one 1-byte short Dex register location, and |
| 62 | // - one 5-byte large Dex register location. |
| 63 | size_t expected_location_catalog_size = 1u + 5u; |
| 64 | ASSERT_EQ(expected_location_catalog_size, location_catalog.Size()); |
| 65 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 66 | StackMap stack_map = code_info.GetStackMapAt(0); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 67 | ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0))); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 68 | ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64))); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 69 | ASSERT_EQ(0u, stack_map.GetDexPc(code_info)); |
| 70 | ASSERT_EQ(64u, stack_map.GetNativePcOffset(code_info)); |
| 71 | ASSERT_EQ(0x3u, stack_map.GetRegisterMask(code_info)); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 72 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 73 | MemoryRegion stack_mask = stack_map.GetStackMask(code_info); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 74 | ASSERT_TRUE(SameBits(stack_mask, sp_mask)); |
| 75 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 76 | ASSERT_TRUE(stack_map.HasDexRegisterMap(code_info)); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 77 | DexRegisterMap dex_register_map = |
| 78 | code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers); |
| 79 | ASSERT_TRUE(dex_register_map.IsDexRegisterLive(0)); |
| 80 | ASSERT_TRUE(dex_register_map.IsDexRegisterLive(1)); |
| 81 | ASSERT_EQ(2u, dex_register_map.GetNumberOfLiveDexRegisters(number_of_dex_registers)); |
| 82 | // The Dex register map contains: |
| 83 | // - one 1-byte live bit mask, and |
| 84 | // - one 1-byte set of location catalog entry indices composed of two 2-bit values. |
| 85 | size_t expected_dex_register_map_size = 1u + 1u; |
| 86 | ASSERT_EQ(expected_dex_register_map_size, dex_register_map.Size()); |
| 87 | |
| 88 | ASSERT_EQ(Kind::kInStack, |
| 89 | dex_register_map.GetLocationKind(0, number_of_dex_registers, code_info)); |
| 90 | ASSERT_EQ(Kind::kConstant, |
| 91 | dex_register_map.GetLocationKind(1, number_of_dex_registers, code_info)); |
| 92 | ASSERT_EQ(Kind::kInStack, |
| 93 | dex_register_map.GetLocationInternalKind(0, number_of_dex_registers, code_info)); |
| 94 | ASSERT_EQ(Kind::kConstantLargeValue, |
| 95 | dex_register_map.GetLocationInternalKind(1, number_of_dex_registers, code_info)); |
| 96 | ASSERT_EQ(0, dex_register_map.GetStackOffsetInBytes(0, number_of_dex_registers, code_info)); |
| 97 | ASSERT_EQ(-2, dex_register_map.GetConstant(1, number_of_dex_registers, code_info)); |
| 98 | |
| 99 | size_t index0 = dex_register_map.GetLocationCatalogEntryIndex( |
| 100 | 0, number_of_dex_registers, number_of_location_catalog_entries); |
| 101 | size_t index1 = dex_register_map.GetLocationCatalogEntryIndex( |
| 102 | 1, number_of_dex_registers, number_of_location_catalog_entries); |
| 103 | ASSERT_EQ(0u, index0); |
| 104 | ASSERT_EQ(1u, index1); |
| 105 | DexRegisterLocation location0 = location_catalog.GetDexRegisterLocation(index0); |
| 106 | DexRegisterLocation location1 = location_catalog.GetDexRegisterLocation(index1); |
| 107 | ASSERT_EQ(Kind::kInStack, location0.GetKind()); |
| 108 | ASSERT_EQ(Kind::kConstant, location1.GetKind()); |
| 109 | ASSERT_EQ(Kind::kInStack, location0.GetInternalKind()); |
| 110 | ASSERT_EQ(Kind::kConstantLargeValue, location1.GetInternalKind()); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 111 | ASSERT_EQ(0, location0.GetValue()); |
| 112 | ASSERT_EQ(-2, location1.GetValue()); |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 113 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 114 | ASSERT_FALSE(stack_map.HasInlineInfo(code_info)); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | TEST(StackMapTest, Test2) { |
| 118 | ArenaPool pool; |
| 119 | ArenaAllocator arena(&pool); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 120 | StackMapStream stream(&arena); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 121 | |
| 122 | ArenaBitVector sp_mask1(&arena, 0, true); |
| 123 | sp_mask1.SetBit(2); |
| 124 | sp_mask1.SetBit(4); |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 125 | size_t number_of_dex_registers = 2; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 126 | stream.AddStackMapEntry(0, 64, 0x3, &sp_mask1, number_of_dex_registers, 2); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 127 | stream.AddDexRegisterEntry(0, Kind::kInStack, 0); // Short location. |
| 128 | stream.AddDexRegisterEntry(1, Kind::kConstant, -2); // Large location. |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 129 | stream.AddInlineInfoEntry(42); |
| 130 | stream.AddInlineInfoEntry(82); |
| 131 | |
| 132 | ArenaBitVector sp_mask2(&arena, 0, true); |
| 133 | sp_mask2.SetBit(3); |
| 134 | sp_mask1.SetBit(8); |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 135 | stream.AddStackMapEntry(1, 128, 0xFF, &sp_mask2, number_of_dex_registers, 0); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 136 | stream.AddDexRegisterEntry(0, Kind::kInRegister, 18); // Short location. |
| 137 | stream.AddDexRegisterEntry(1, Kind::kInFpuRegister, 3); // Short location. |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 138 | |
| 139 | size_t size = stream.ComputeNeededSize(); |
| 140 | void* memory = arena.Alloc(size, kArenaAllocMisc); |
| 141 | MemoryRegion region(memory, size); |
| 142 | stream.FillIn(region); |
| 143 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 144 | CodeInfo code_info(region); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 145 | ASSERT_EQ(1u, code_info.GetStackMaskSize()); |
| 146 | ASSERT_EQ(2u, code_info.GetNumberOfStackMaps()); |
| 147 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 148 | uint32_t number_of_location_catalog_entries = |
| 149 | code_info.GetNumberOfDexRegisterLocationCatalogEntries(); |
| 150 | ASSERT_EQ(4u, number_of_location_catalog_entries); |
| 151 | DexRegisterLocationCatalog location_catalog = code_info.GetDexRegisterLocationCatalog(); |
| 152 | // The Dex register location catalog contains: |
| 153 | // - three 1-byte short Dex register locations, and |
| 154 | // - one 5-byte large Dex register location. |
| 155 | size_t expected_location_catalog_size = 3u * 1u + 5u; |
| 156 | ASSERT_EQ(expected_location_catalog_size, location_catalog.Size()); |
| 157 | |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 158 | // First stack map. |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 159 | { |
| 160 | StackMap stack_map = code_info.GetStackMapAt(0); |
| 161 | ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0))); |
| 162 | ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64))); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 163 | ASSERT_EQ(0u, stack_map.GetDexPc(code_info)); |
| 164 | ASSERT_EQ(64u, stack_map.GetNativePcOffset(code_info)); |
| 165 | ASSERT_EQ(0x3u, stack_map.GetRegisterMask(code_info)); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 166 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 167 | MemoryRegion stack_mask = stack_map.GetStackMask(code_info); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 168 | ASSERT_TRUE(SameBits(stack_mask, sp_mask1)); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 169 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 170 | ASSERT_TRUE(stack_map.HasDexRegisterMap(code_info)); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 171 | DexRegisterMap dex_register_map = |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 172 | code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 173 | ASSERT_TRUE(dex_register_map.IsDexRegisterLive(0)); |
| 174 | ASSERT_TRUE(dex_register_map.IsDexRegisterLive(1)); |
| 175 | ASSERT_EQ(2u, dex_register_map.GetNumberOfLiveDexRegisters(number_of_dex_registers)); |
| 176 | // The Dex register map contains: |
| 177 | // - one 1-byte live bit mask, and |
| 178 | // - one 1-byte set of location catalog entry indices composed of two 2-bit values. |
| 179 | size_t expected_dex_register_map_size = 1u + 1u; |
| 180 | ASSERT_EQ(expected_dex_register_map_size, dex_register_map.Size()); |
| 181 | |
| 182 | ASSERT_EQ(Kind::kInStack, |
| 183 | dex_register_map.GetLocationKind(0, number_of_dex_registers, code_info)); |
| 184 | ASSERT_EQ(Kind::kConstant, |
| 185 | dex_register_map.GetLocationKind(1, number_of_dex_registers, code_info)); |
| 186 | ASSERT_EQ(Kind::kInStack, |
| 187 | dex_register_map.GetLocationInternalKind(0, number_of_dex_registers, code_info)); |
| 188 | ASSERT_EQ(Kind::kConstantLargeValue, |
| 189 | dex_register_map.GetLocationInternalKind(1, number_of_dex_registers, code_info)); |
| 190 | ASSERT_EQ(0, dex_register_map.GetStackOffsetInBytes(0, number_of_dex_registers, code_info)); |
| 191 | ASSERT_EQ(-2, dex_register_map.GetConstant(1, number_of_dex_registers, code_info)); |
| 192 | |
| 193 | size_t index0 = dex_register_map.GetLocationCatalogEntryIndex( |
| 194 | 0, number_of_dex_registers, number_of_location_catalog_entries); |
| 195 | size_t index1 = dex_register_map.GetLocationCatalogEntryIndex( |
| 196 | 1, number_of_dex_registers, number_of_location_catalog_entries); |
| 197 | ASSERT_EQ(0u, index0); |
| 198 | ASSERT_EQ(1u, index1); |
| 199 | DexRegisterLocation location0 = location_catalog.GetDexRegisterLocation(index0); |
| 200 | DexRegisterLocation location1 = location_catalog.GetDexRegisterLocation(index1); |
| 201 | ASSERT_EQ(Kind::kInStack, location0.GetKind()); |
| 202 | ASSERT_EQ(Kind::kConstant, location1.GetKind()); |
| 203 | ASSERT_EQ(Kind::kInStack, location0.GetInternalKind()); |
| 204 | ASSERT_EQ(Kind::kConstantLargeValue, location1.GetInternalKind()); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 205 | ASSERT_EQ(0, location0.GetValue()); |
| 206 | ASSERT_EQ(-2, location1.GetValue()); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 207 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 208 | ASSERT_TRUE(stack_map.HasInlineInfo(code_info)); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 209 | InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map); |
| 210 | ASSERT_EQ(2u, inline_info.GetDepth()); |
| 211 | ASSERT_EQ(42u, inline_info.GetMethodReferenceIndexAtDepth(0)); |
| 212 | ASSERT_EQ(82u, inline_info.GetMethodReferenceIndexAtDepth(1)); |
| 213 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 214 | |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 215 | // Second stack map. |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 216 | { |
| 217 | StackMap stack_map = code_info.GetStackMapAt(1); |
| 218 | ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(1u))); |
| 219 | ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(128u))); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 220 | ASSERT_EQ(1u, stack_map.GetDexPc(code_info)); |
| 221 | ASSERT_EQ(128u, stack_map.GetNativePcOffset(code_info)); |
| 222 | ASSERT_EQ(0xFFu, stack_map.GetRegisterMask(code_info)); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 223 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 224 | MemoryRegion stack_mask = stack_map.GetStackMask(code_info); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 225 | ASSERT_TRUE(SameBits(stack_mask, sp_mask2)); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 226 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 227 | ASSERT_TRUE(stack_map.HasDexRegisterMap(code_info)); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 228 | DexRegisterMap dex_register_map = |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 229 | code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 230 | ASSERT_TRUE(dex_register_map.IsDexRegisterLive(0)); |
| 231 | ASSERT_TRUE(dex_register_map.IsDexRegisterLive(1)); |
| 232 | ASSERT_EQ(2u, dex_register_map.GetNumberOfLiveDexRegisters(number_of_dex_registers)); |
| 233 | // The Dex register map contains: |
| 234 | // - one 1-byte live bit mask, and |
| 235 | // - one 1-byte set of location catalog entry indices composed of two 2-bit values. |
| 236 | size_t expected_dex_register_map_size = 1u + 1u; |
| 237 | ASSERT_EQ(expected_dex_register_map_size, dex_register_map.Size()); |
| 238 | |
| 239 | ASSERT_EQ(Kind::kInRegister, |
| 240 | dex_register_map.GetLocationKind(0, number_of_dex_registers, code_info)); |
| 241 | ASSERT_EQ(Kind::kInFpuRegister, |
| 242 | dex_register_map.GetLocationKind(1, number_of_dex_registers, code_info)); |
| 243 | ASSERT_EQ(Kind::kInRegister, |
| 244 | dex_register_map.GetLocationInternalKind(0, number_of_dex_registers, code_info)); |
| 245 | ASSERT_EQ(Kind::kInFpuRegister, |
| 246 | dex_register_map.GetLocationInternalKind(1, number_of_dex_registers, code_info)); |
| 247 | ASSERT_EQ(18, dex_register_map.GetMachineRegister(0, number_of_dex_registers, code_info)); |
| 248 | ASSERT_EQ(3, dex_register_map.GetMachineRegister(1, number_of_dex_registers, code_info)); |
| 249 | |
| 250 | size_t index0 = dex_register_map.GetLocationCatalogEntryIndex( |
| 251 | 0, number_of_dex_registers, number_of_location_catalog_entries); |
| 252 | size_t index1 = dex_register_map.GetLocationCatalogEntryIndex( |
| 253 | 1, number_of_dex_registers, number_of_location_catalog_entries); |
| 254 | ASSERT_EQ(2u, index0); |
| 255 | ASSERT_EQ(3u, index1); |
| 256 | DexRegisterLocation location0 = location_catalog.GetDexRegisterLocation(index0); |
| 257 | DexRegisterLocation location1 = location_catalog.GetDexRegisterLocation(index1); |
| 258 | ASSERT_EQ(Kind::kInRegister, location0.GetKind()); |
| 259 | ASSERT_EQ(Kind::kInFpuRegister, location1.GetKind()); |
| 260 | ASSERT_EQ(Kind::kInRegister, location0.GetInternalKind()); |
| 261 | ASSERT_EQ(Kind::kInFpuRegister, location1.GetInternalKind()); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 262 | ASSERT_EQ(18, location0.GetValue()); |
| 263 | ASSERT_EQ(3, location1.GetValue()); |
Roland Levillain | 12baf47 | 2015-03-05 12:41:42 +0000 | [diff] [blame] | 264 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 265 | ASSERT_FALSE(stack_map.HasInlineInfo(code_info)); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 266 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 267 | } |
| 268 | |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 269 | TEST(StackMapTest, TestNonLiveDexRegisters) { |
| 270 | ArenaPool pool; |
| 271 | ArenaAllocator arena(&pool); |
| 272 | StackMapStream stream(&arena); |
| 273 | |
| 274 | ArenaBitVector sp_mask(&arena, 0, false); |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 275 | uint32_t number_of_dex_registers = 2; |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 276 | stream.AddStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 277 | stream.AddDexRegisterEntry(0, Kind::kNone, 0); // No location. |
| 278 | stream.AddDexRegisterEntry(1, Kind::kConstant, -2); // Large location. |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 279 | |
| 280 | size_t size = stream.ComputeNeededSize(); |
| 281 | void* memory = arena.Alloc(size, kArenaAllocMisc); |
| 282 | MemoryRegion region(memory, size); |
| 283 | stream.FillIn(region); |
| 284 | |
| 285 | CodeInfo code_info(region); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 286 | ASSERT_EQ(0u, code_info.GetStackMaskSize()); |
| 287 | ASSERT_EQ(1u, code_info.GetNumberOfStackMaps()); |
| 288 | |
| 289 | uint32_t number_of_location_catalog_entries = |
| 290 | code_info.GetNumberOfDexRegisterLocationCatalogEntries(); |
| 291 | ASSERT_EQ(1u, number_of_location_catalog_entries); |
| 292 | DexRegisterLocationCatalog location_catalog = code_info.GetDexRegisterLocationCatalog(); |
| 293 | // The Dex register location catalog contains: |
| 294 | // - one 5-byte large Dex register location. |
| 295 | size_t expected_location_catalog_size = 5u; |
| 296 | ASSERT_EQ(expected_location_catalog_size, location_catalog.Size()); |
| 297 | |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 298 | StackMap stack_map = code_info.GetStackMapAt(0); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 299 | ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0))); |
| 300 | ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64))); |
| 301 | ASSERT_EQ(0u, stack_map.GetDexPc(code_info)); |
| 302 | ASSERT_EQ(64u, stack_map.GetNativePcOffset(code_info)); |
| 303 | ASSERT_EQ(0x3u, stack_map.GetRegisterMask(code_info)); |
| 304 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 305 | ASSERT_TRUE(stack_map.HasDexRegisterMap(code_info)); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 306 | DexRegisterMap dex_register_map = |
| 307 | code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers); |
| 308 | ASSERT_FALSE(dex_register_map.IsDexRegisterLive(0)); |
| 309 | ASSERT_TRUE(dex_register_map.IsDexRegisterLive(1)); |
| 310 | ASSERT_EQ(1u, dex_register_map.GetNumberOfLiveDexRegisters(number_of_dex_registers)); |
| 311 | // The Dex register map contains: |
| 312 | // - one 1-byte live bit mask. |
| 313 | // No space is allocated for the sole location catalog entry index, as it is useless. |
| 314 | size_t expected_dex_register_map_size = 1u + 0u; |
| 315 | ASSERT_EQ(expected_dex_register_map_size, dex_register_map.Size()); |
| 316 | |
| 317 | ASSERT_EQ(Kind::kNone, |
| 318 | dex_register_map.GetLocationKind(0, number_of_dex_registers, code_info)); |
| 319 | ASSERT_EQ(Kind::kConstant, |
| 320 | dex_register_map.GetLocationKind(1, number_of_dex_registers, code_info)); |
| 321 | ASSERT_EQ(Kind::kNone, |
| 322 | dex_register_map.GetLocationInternalKind(0, number_of_dex_registers, code_info)); |
| 323 | ASSERT_EQ(Kind::kConstantLargeValue, |
| 324 | dex_register_map.GetLocationInternalKind(1, number_of_dex_registers, code_info)); |
| 325 | ASSERT_EQ(-2, dex_register_map.GetConstant(1, number_of_dex_registers, code_info)); |
| 326 | |
| 327 | size_t index0 = dex_register_map.GetLocationCatalogEntryIndex( |
| 328 | 0, number_of_dex_registers, number_of_location_catalog_entries); |
| 329 | size_t index1 = dex_register_map.GetLocationCatalogEntryIndex( |
| 330 | 1, number_of_dex_registers, number_of_location_catalog_entries); |
| 331 | ASSERT_EQ(DexRegisterLocationCatalog::kNoLocationEntryIndex, index0); |
| 332 | ASSERT_EQ(0u, index1); |
| 333 | DexRegisterLocation location0 = location_catalog.GetDexRegisterLocation(index0); |
| 334 | DexRegisterLocation location1 = location_catalog.GetDexRegisterLocation(index1); |
| 335 | ASSERT_EQ(Kind::kNone, location0.GetKind()); |
| 336 | ASSERT_EQ(Kind::kConstant, location1.GetKind()); |
| 337 | ASSERT_EQ(Kind::kNone, location0.GetInternalKind()); |
| 338 | ASSERT_EQ(Kind::kConstantLargeValue, location1.GetInternalKind()); |
| 339 | ASSERT_EQ(0, location0.GetValue()); |
| 340 | ASSERT_EQ(-2, location1.GetValue()); |
| 341 | |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 342 | ASSERT_FALSE(stack_map.HasInlineInfo(code_info)); |
| 343 | } |
| 344 | |
| 345 | // Generate a stack map whose dex register offset is |
| 346 | // StackMap::kNoDexRegisterMapSmallEncoding, and ensure we do |
| 347 | // not treat it as kNoDexRegisterMap. |
| 348 | TEST(StackMapTest, DexRegisterMapOffsetOverflow) { |
| 349 | ArenaPool pool; |
| 350 | ArenaAllocator arena(&pool); |
| 351 | StackMapStream stream(&arena); |
| 352 | |
| 353 | ArenaBitVector sp_mask(&arena, 0, false); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 354 | uint32_t number_of_dex_registers = 1024; |
| 355 | // Create the first stack map (and its Dex register map). |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 356 | stream.AddStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 357 | uint32_t number_of_dex_live_registers_in_dex_register_map_0 = number_of_dex_registers - 8; |
| 358 | for (uint32_t i = 0; i < number_of_dex_live_registers_in_dex_register_map_0; ++i) { |
| 359 | // Use two different Dex register locations to populate this map, |
| 360 | // as using a single value (in the whole CodeInfo object) would |
| 361 | // make this Dex register mapping data empty (see |
| 362 | // art::DexRegisterMap::SingleEntrySizeInBits). |
| 363 | stream.AddDexRegisterEntry(i, Kind::kConstant, i % 2); // Short location. |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 364 | } |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 365 | // Create the second stack map (and its Dex register map). |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 366 | stream.AddStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0); |
| 367 | for (uint32_t i = 0; i < number_of_dex_registers; ++i) { |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 368 | stream.AddDexRegisterEntry(i, Kind::kConstant, 0); // Short location. |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | size_t size = stream.ComputeNeededSize(); |
| 372 | void* memory = arena.Alloc(size, kArenaAllocMisc); |
| 373 | MemoryRegion region(memory, size); |
| 374 | stream.FillIn(region); |
| 375 | |
| 376 | CodeInfo code_info(region); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 377 | // The location catalog contains two entries (DexRegisterLocation(kConstant, 0) |
| 378 | // and DexRegisterLocation(kConstant, 1)), therefore the location catalog index |
| 379 | // has a size of 1 bit. |
| 380 | uint32_t number_of_location_catalog_entries = |
| 381 | code_info.GetNumberOfDexRegisterLocationCatalogEntries(); |
| 382 | ASSERT_EQ(2u, number_of_location_catalog_entries); |
| 383 | ASSERT_EQ(1u, DexRegisterMap::SingleEntrySizeInBits(number_of_location_catalog_entries)); |
| 384 | |
| 385 | // The first Dex register map contains: |
| 386 | // - a live register bit mask for 1024 registers (that is, 128 bytes of |
| 387 | // data); and |
| 388 | // - Dex register mapping information for 1016 1-bit Dex (live) register |
| 389 | // locations (that is, 127 bytes of data). |
| 390 | // Hence it has a size of 255 bytes, and therefore... |
| 391 | ASSERT_EQ(128u, DexRegisterMap::GetLiveBitMaskSize(number_of_dex_registers)); |
| 392 | StackMap stack_map0 = code_info.GetStackMapAt(0); |
| 393 | DexRegisterMap dex_register_map0 = |
| 394 | code_info.GetDexRegisterMapOf(stack_map0, number_of_dex_registers); |
| 395 | ASSERT_EQ(127u, dex_register_map0.GetLocationMappingDataSize(number_of_dex_registers, |
| 396 | number_of_location_catalog_entries)); |
| 397 | ASSERT_EQ(255u, dex_register_map0.Size()); |
| 398 | |
| 399 | StackMap stack_map1 = code_info.GetStackMapAt(1); |
| 400 | ASSERT_TRUE(stack_map1.HasDexRegisterMap(code_info)); |
| 401 | // ...the offset of the second Dex register map (relative to the |
| 402 | // beginning of the Dex register maps region) is 255 (i.e., |
| 403 | // kNoDexRegisterMapSmallEncoding). |
Nicolas Geoffray | 896f8f7 | 2015-03-30 15:44:25 +0100 | [diff] [blame] | 404 | ASSERT_NE(stack_map1.GetDexRegisterMapOffset(code_info), StackMap::kNoDexRegisterMap); |
| 405 | ASSERT_EQ(stack_map1.GetDexRegisterMapOffset(code_info), 0xFFu); |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 408 | TEST(StackMapTest, TestShareDexRegisterMap) { |
| 409 | ArenaPool pool; |
| 410 | ArenaAllocator arena(&pool); |
| 411 | StackMapStream stream(&arena); |
| 412 | |
| 413 | ArenaBitVector sp_mask(&arena, 0, false); |
| 414 | uint32_t number_of_dex_registers = 2; |
| 415 | // First stack map. |
| 416 | stream.AddStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 417 | stream.AddDexRegisterEntry(0, Kind::kInRegister, 0); // Short location. |
| 418 | stream.AddDexRegisterEntry(1, Kind::kConstant, -2); // Large location. |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 419 | // Second stack map, which should share the same dex register map. |
| 420 | stream.AddStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 421 | stream.AddDexRegisterEntry(0, Kind::kInRegister, 0); // Short location. |
| 422 | stream.AddDexRegisterEntry(1, Kind::kConstant, -2); // Large location. |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 423 | // Third stack map (doesn't share the dex register map). |
| 424 | stream.AddStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 425 | stream.AddDexRegisterEntry(0, Kind::kInRegister, 2); // Short location. |
| 426 | stream.AddDexRegisterEntry(1, Kind::kConstant, -2); // Large location. |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 427 | |
| 428 | size_t size = stream.ComputeNeededSize(); |
| 429 | void* memory = arena.Alloc(size, kArenaAllocMisc); |
| 430 | MemoryRegion region(memory, size); |
| 431 | stream.FillIn(region); |
| 432 | |
| 433 | CodeInfo ci(region); |
| 434 | // Verify first stack map. |
| 435 | StackMap sm0 = ci.GetStackMapAt(0); |
| 436 | DexRegisterMap dex_registers0 = ci.GetDexRegisterMapOf(sm0, number_of_dex_registers); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 437 | ASSERT_EQ(0, dex_registers0.GetMachineRegister(0, number_of_dex_registers, ci)); |
| 438 | ASSERT_EQ(-2, dex_registers0.GetConstant(1, number_of_dex_registers, ci)); |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 439 | |
| 440 | // Verify second stack map. |
| 441 | StackMap sm1 = ci.GetStackMapAt(1); |
| 442 | DexRegisterMap dex_registers1 = ci.GetDexRegisterMapOf(sm1, number_of_dex_registers); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 443 | ASSERT_EQ(0, dex_registers1.GetMachineRegister(0, number_of_dex_registers, ci)); |
| 444 | ASSERT_EQ(-2, dex_registers1.GetConstant(1, number_of_dex_registers, ci)); |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 445 | |
| 446 | // Verify third stack map. |
| 447 | StackMap sm2 = ci.GetStackMapAt(2); |
| 448 | DexRegisterMap dex_registers2 = ci.GetDexRegisterMapOf(sm2, number_of_dex_registers); |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 449 | ASSERT_EQ(2, dex_registers2.GetMachineRegister(0, number_of_dex_registers, ci)); |
| 450 | ASSERT_EQ(-2, dex_registers2.GetConstant(1, number_of_dex_registers, ci)); |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 451 | |
| 452 | // Verify dex register map offsets. |
| 453 | ASSERT_EQ(sm0.GetDexRegisterMapOffset(ci), sm1.GetDexRegisterMapOffset(ci)); |
| 454 | ASSERT_NE(sm0.GetDexRegisterMapOffset(ci), sm2.GetDexRegisterMapOffset(ci)); |
| 455 | ASSERT_NE(sm1.GetDexRegisterMapOffset(ci), sm2.GetDexRegisterMapOffset(ci)); |
| 456 | } |
| 457 | |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 458 | TEST(StackMapTest, TestNoDexRegisterMap) { |
| 459 | ArenaPool pool; |
| 460 | ArenaAllocator arena(&pool); |
| 461 | StackMapStream stream(&arena); |
| 462 | |
| 463 | ArenaBitVector sp_mask(&arena, 0, false); |
| 464 | uint32_t number_of_dex_registers = 0; |
| 465 | stream.AddStackMapEntry(0, 64, 0x3, &sp_mask, number_of_dex_registers, 0); |
| 466 | |
| 467 | size_t size = stream.ComputeNeededSize(); |
| 468 | void* memory = arena.Alloc(size, kArenaAllocMisc); |
| 469 | MemoryRegion region(memory, size); |
| 470 | stream.FillIn(region); |
| 471 | |
| 472 | CodeInfo code_info(region); |
| 473 | ASSERT_EQ(0u, code_info.GetStackMaskSize()); |
| 474 | ASSERT_EQ(1u, code_info.GetNumberOfStackMaps()); |
| 475 | |
| 476 | uint32_t number_of_location_catalog_entries = |
| 477 | code_info.GetNumberOfDexRegisterLocationCatalogEntries(); |
| 478 | ASSERT_EQ(0u, number_of_location_catalog_entries); |
| 479 | DexRegisterLocationCatalog location_catalog = code_info.GetDexRegisterLocationCatalog(); |
| 480 | ASSERT_EQ(0u, location_catalog.Size()); |
| 481 | |
| 482 | StackMap stack_map = code_info.GetStackMapAt(0); |
| 483 | ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForDexPc(0))); |
| 484 | ASSERT_TRUE(stack_map.Equals(code_info.GetStackMapForNativePcOffset(64))); |
| 485 | ASSERT_EQ(0u, stack_map.GetDexPc(code_info)); |
| 486 | ASSERT_EQ(64u, stack_map.GetNativePcOffset(code_info)); |
| 487 | ASSERT_EQ(0x3u, stack_map.GetRegisterMask(code_info)); |
| 488 | |
| 489 | ASSERT_FALSE(stack_map.HasDexRegisterMap(code_info)); |
| 490 | ASSERT_FALSE(stack_map.HasInlineInfo(code_info)); |
| 491 | } |
| 492 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 493 | } // namespace art |