1 | /* |
2 | * Copyright (C) 2013-2019 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "StructureIDTable.h" |
28 | |
29 | #include <limits.h> |
30 | #include <wtf/Atomics.h> |
31 | |
32 | namespace JSC { |
33 | |
34 | #if USE(JSVALUE64) |
35 | |
36 | StructureIDTable::StructureIDTable() |
37 | : m_table(makeUniqueArray<StructureOrOffset>(s_initialSize)) |
38 | , m_size(1) |
39 | , m_capacity(s_initialSize) |
40 | { |
41 | // We pre-allocate the first offset so that the null Structure |
42 | // can still be represented as the StructureID '0'. |
43 | table()[0].encodedStructureBits = 0; |
44 | |
45 | makeFreeListFromRange(1, m_capacity - 1); |
46 | } |
47 | |
48 | void StructureIDTable::makeFreeListFromRange(uint32_t first, uint32_t last) |
49 | { |
50 | ASSERT(!m_firstFreeOffset); |
51 | ASSERT(!m_lastFreeOffset); |
52 | |
53 | // Put all the new IDs on the free list sequentially. |
54 | uint32_t head = first; |
55 | uint32_t tail = last; |
56 | for (uint32_t i = first; i < last; ++i) |
57 | table()[i].offset = i + 1; |
58 | table()[last].offset = 0; |
59 | |
60 | // Randomize the free list. |
61 | uint32_t size = last - first + 1; |
62 | uint32_t maxIterations = (size * 2) / 3; |
63 | for (uint32_t count = 0; count < maxIterations; ++count) { |
64 | // Move a random pick either to the head or the tail of the free list. |
65 | uint32_t random = m_weakRandom.getUint32(); |
66 | uint32_t nodeBefore = first + (random % size); |
67 | uint32_t pick = table()[nodeBefore].offset; |
68 | if (pick) { |
69 | uint32_t nodeAfter = table()[pick].offset; |
70 | table()[nodeBefore].offset = nodeAfter; |
71 | if ((random & 1) || !nodeAfter) { |
72 | // Move to the head. |
73 | table()[pick].offset = head; |
74 | head = pick; |
75 | if (!nodeAfter) |
76 | tail = nodeBefore; |
77 | } else { |
78 | // Move to the tail. |
79 | table()[pick].offset = 0; |
80 | table()[tail].offset = pick; |
81 | tail = pick; |
82 | } |
83 | } |
84 | } |
85 | |
86 | // Cut list in half and swap halves. |
87 | uint32_t cut = first + (m_weakRandom.getUint32() % size); |
88 | uint32_t afterCut = table()[cut].offset; |
89 | if (afterCut) { |
90 | table()[tail].offset = head; |
91 | tail = cut; |
92 | head = afterCut; |
93 | table()[cut].offset = 0; |
94 | } |
95 | |
96 | m_firstFreeOffset = head; |
97 | m_lastFreeOffset = tail; |
98 | } |
99 | |
100 | void StructureIDTable::resize(size_t newCapacity) |
101 | { |
102 | if (newCapacity > s_maximumNumberOfStructures) |
103 | newCapacity = s_maximumNumberOfStructures; |
104 | |
105 | // If m_size is already s_maximumNumberOfStructures, newCapacity becomes s_maximumNumberOfStructures in the above code. |
106 | // In that case, we should crash because of exhaust of StructureIDs. |
107 | RELEASE_ASSERT_WITH_MESSAGE(m_size < newCapacity, "Crash intentionally because of exhaust of StructureIDs." ); |
108 | |
109 | // Create the new table. |
110 | auto newTable = makeUniqueArray<StructureOrOffset>(newCapacity); |
111 | |
112 | // Copy the contents of the old table to the new table. |
113 | memcpy(newTable.get(), table(), m_capacity * sizeof(StructureOrOffset)); |
114 | |
115 | // Store fence to make sure we've copied everything before doing the swap. |
116 | WTF::storeStoreFence(); |
117 | |
118 | // Swap the old and new tables. |
119 | swap(m_table, newTable); |
120 | |
121 | // Put the old table (now labeled as new) into the list of old tables. |
122 | m_oldTables.append(WTFMove(newTable)); |
123 | |
124 | // Update the capacity. |
125 | m_capacity = newCapacity; |
126 | |
127 | makeFreeListFromRange(m_size, m_capacity - 1); |
128 | } |
129 | |
130 | void StructureIDTable::flushOldTables() |
131 | { |
132 | m_oldTables.clear(); |
133 | } |
134 | |
135 | StructureID StructureIDTable::allocateID(Structure* structure) |
136 | { |
137 | if (UNLIKELY(!m_firstFreeOffset)) { |
138 | RELEASE_ASSERT(m_capacity <= s_maximumNumberOfStructures); |
139 | ASSERT(m_size == m_capacity); |
140 | resize(m_capacity * 2); |
141 | ASSERT(m_size < m_capacity); |
142 | RELEASE_ASSERT(m_firstFreeOffset); |
143 | } |
144 | |
145 | // entropyBits must not be zero. This ensures that if a corrupted |
146 | // structureID is encountered (with incorrect entropyBits), the decoded |
147 | // structure pointer for that ID will be always be a bad pointer with |
148 | // high bits set. |
149 | constexpr uint32_t entropyBitsMask = (1 << s_numberOfEntropyBits) - 1; |
150 | uint32_t entropyBits = m_weakRandom.getUint32() & entropyBitsMask; |
151 | if (UNLIKELY(!entropyBits)) { |
152 | constexpr uint32_t numberOfValuesToPickFrom = entropyBitsMask; |
153 | entropyBits = (m_weakRandom.getUint32() % numberOfValuesToPickFrom) + 1; |
154 | } |
155 | |
156 | uint32_t structureIndex = m_firstFreeOffset; |
157 | m_firstFreeOffset = table()[m_firstFreeOffset].offset; |
158 | if (!m_firstFreeOffset) |
159 | m_lastFreeOffset = 0; |
160 | |
161 | StructureID result = (structureIndex << s_numberOfEntropyBits) | entropyBits; |
162 | table()[structureIndex].encodedStructureBits = encode(structure, result); |
163 | m_size++; |
164 | ASSERT(!isNuked(result)); |
165 | return result; |
166 | } |
167 | |
168 | void StructureIDTable::deallocateID(Structure* structure, StructureID structureID) |
169 | { |
170 | ASSERT(structureID != s_unusedID); |
171 | uint32_t structureIndex = structureID >> s_numberOfEntropyBits; |
172 | ASSERT(structureIndex && structureIndex < s_maximumNumberOfStructures); |
173 | RELEASE_ASSERT(table()[structureIndex].encodedStructureBits == encode(structure, structureID)); |
174 | m_size--; |
175 | if (!m_firstFreeOffset) { |
176 | table()[structureIndex].offset = 0; |
177 | m_firstFreeOffset = structureIndex; |
178 | m_lastFreeOffset = structureIndex; |
179 | return; |
180 | } |
181 | |
182 | bool insertAtHead = m_weakRandom.getUint32() & 1; |
183 | if (insertAtHead) { |
184 | table()[structureIndex].offset = m_firstFreeOffset; |
185 | m_firstFreeOffset = structureIndex; |
186 | } else { |
187 | table()[structureIndex].offset = 0; |
188 | table()[m_lastFreeOffset].offset = structureIndex; |
189 | m_lastFreeOffset = structureIndex; |
190 | } |
191 | } |
192 | |
193 | #endif // USE(JSVALUE64) |
194 | |
195 | } // namespace JSC |
196 | |