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
32namespace JSC {
33
34#if USE(JSVALUE64)
35
36StructureIDTable::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
48void 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
100void StructureIDTable::resize(size_t newCapacity)
101{
102 if (newCapacity > s_maximumNumberOfStructures)
103 newCapacity = s_maximumNumberOfStructures;
104
105 // Create the new table.
106 auto newTable = makeUniqueArray<StructureOrOffset>(newCapacity);
107
108 // Copy the contents of the old table to the new table.
109 memcpy(newTable.get(), table(), m_capacity * sizeof(StructureOrOffset));
110
111 // Store fence to make sure we've copied everything before doing the swap.
112 WTF::storeStoreFence();
113
114 // Swap the old and new tables.
115 swap(m_table, newTable);
116
117 // Put the old table (now labeled as new) into the list of old tables.
118 m_oldTables.append(WTFMove(newTable));
119
120 // Update the capacity.
121 m_capacity = newCapacity;
122
123 makeFreeListFromRange(m_size, m_capacity - 1);
124}
125
126void StructureIDTable::flushOldTables()
127{
128 m_oldTables.clear();
129}
130
131StructureID StructureIDTable::allocateID(Structure* structure)
132{
133 if (UNLIKELY(!m_firstFreeOffset)) {
134 RELEASE_ASSERT(m_capacity <= s_maximumNumberOfStructures);
135 ASSERT(m_size == m_capacity);
136 resize(m_capacity * 2);
137 ASSERT(m_size < m_capacity);
138 RELEASE_ASSERT(m_firstFreeOffset);
139 }
140
141 // entropyBits must not be zero. This ensures that if a corrupted
142 // structureID is encountered (with incorrect entropyBits), the decoded
143 // structure pointer for that ID will be always be a bad pointer with
144 // high bits set.
145 constexpr uint32_t entropyBitsMask = (1 << s_numberOfEntropyBits) - 1;
146 uint32_t entropyBits = m_weakRandom.getUint32() & entropyBitsMask;
147 if (UNLIKELY(!entropyBits)) {
148 constexpr uint32_t numberOfValuesToPickFrom = entropyBitsMask;
149 entropyBits = (m_weakRandom.getUint32() % numberOfValuesToPickFrom) + 1;
150 }
151
152 uint32_t structureIndex = m_firstFreeOffset;
153 m_firstFreeOffset = table()[m_firstFreeOffset].offset;
154 if (!m_firstFreeOffset)
155 m_lastFreeOffset = 0;
156
157 StructureID result = (structureIndex << s_numberOfEntropyBits) | entropyBits;
158 table()[structureIndex].encodedStructureBits = encode(structure, result);
159 m_size++;
160 ASSERT(!isNuked(result));
161 return result;
162}
163
164void StructureIDTable::deallocateID(Structure* structure, StructureID structureID)
165{
166 ASSERT(structureID != s_unusedID);
167 uint32_t structureIndex = structureID >> s_numberOfEntropyBits;
168 ASSERT(structureIndex && structureIndex < s_maximumNumberOfStructures);
169 RELEASE_ASSERT(table()[structureIndex].encodedStructureBits == encode(structure, structureID));
170 m_size--;
171 if (!m_firstFreeOffset) {
172 table()[structureIndex].offset = 0;
173 m_firstFreeOffset = structureIndex;
174 m_lastFreeOffset = structureIndex;
175 return;
176 }
177
178 bool insertAtHead = m_weakRandom.getUint32() & 1;
179 if (insertAtHead) {
180 table()[structureIndex].offset = m_firstFreeOffset;
181 m_firstFreeOffset = structureIndex;
182 } else {
183 table()[structureIndex].offset = 0;
184 table()[m_lastFreeOffset].offset = structureIndex;
185 m_lastFreeOffset = structureIndex;
186 }
187}
188
189#endif // USE(JSVALUE64)
190
191} // namespace JSC
192