forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHash.cpp
More file actions
431 lines (368 loc) · 12.5 KB
/
Hash.cpp
File metadata and controls
431 lines (368 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "ParserPch.h"
#if PROFILE_DICTIONARY
#include "DictionaryStats.h"
#endif
const HashTbl::KWD HashTbl::g_mptkkwd[tkLimKwd] =
{
{ knopNone,0,knopNone,0 },
#define KEYWORD(tk,f,prec2,nop2,prec1,nop1,name) \
{ nop2,kopl##prec2,nop1,kopl##prec1 },
#define TOK_DCL(tk,prec2,nop2,prec1,nop1) \
{ nop2,kopl##prec2,nop1,kopl##prec1 },
#include "keywords.h"
};
const HashTbl::ReservedWordInfo HashTbl::s_reservedWordInfo[tkID] =
{
{ nullptr, fidNil },
#define KEYWORD(tk,f,prec2,nop2,prec1,nop1,name) \
{ reinterpret_cast<const StaticSym*>(&g_ssym_##name), f },
#include "keywords.h"
};
BOOL HashTbl::Init(uint cidHash)
{
// cidHash must be a power of two
Assert(cidHash > 0 && 0 == (cidHash & (cidHash - 1)));
int32 cb;
/* Allocate and clear the hash bucket table */
m_luMask = cidHash - 1;
m_luCount = 0;
// (Bug 1117873 - Windows OS Bugs)
// Prefast: Verify that cidHash * sizeof(Ident *) does not cause an integer overflow
// NoReleaseAllocator( ) takes int32 - so check for LONG_MAX
// Win8 730594 - Use intsafe function to check for overflow.
uint cbTemp;
if (FAILED(UIntMult(cidHash, sizeof(Ident *), &cbTemp)) || cbTemp > LONG_MAX)
return FALSE;
cb = cbTemp;
if (nullptr == (m_prgpidName = (Ident **)m_noReleaseAllocator.Alloc(cb)))
return FALSE;
memset(m_prgpidName, 0, cb);
#if PROFILE_DICTIONARY
stats = DictionaryStats::Create(typeid(this).name(), cidHash);
#endif
return TRUE;
}
void HashTbl::Grow()
{
// Grow the bucket size by grow factor
// Has the side-effect of inverting the order the pids appear in their respective buckets.
uint cidHash = m_luMask + 1;
uint n_cidHash = cidHash * GrowFactor;
Assert(n_cidHash > 0 && 0 == (n_cidHash & (n_cidHash - 1)));
// Win8 730594 - Use intsafe function to check for overflow.
uint cbTemp;
if (FAILED(UIntMult(n_cidHash, sizeof(Ident *), &cbTemp)) || cbTemp > LONG_MAX)
// It is fine to exit early here, we will just have a potentially densely populated hash table
return;
int32 cb = cbTemp;
uint n_luMask = n_cidHash - 1;
IdentPtr *n_prgpidName = (IdentPtr *)m_noReleaseAllocator.Alloc(cb);
if (n_prgpidName == nullptr)
// It is fine to exit early here, we will just have a potentially densely populated hash table
return;
// Clear the array
memset(n_prgpidName, 0, cb);
// Place each entry its new bucket.
for (uint i = 0; i < cidHash; i++)
{
for (IdentPtr pid = m_prgpidName[i], next = pid ? pid->m_pidNext : nullptr; pid; pid = next, next = pid ? pid->m_pidNext : nullptr)
{
uint32 luHash = pid->m_luHash;
uint32 luIndex = luHash & n_luMask;
pid->m_pidNext = n_prgpidName[luIndex];
n_prgpidName[luIndex] = pid;
}
}
Assert(CountAndVerifyItems(n_prgpidName, n_cidHash, n_luMask) == m_luCount);
// Update the table fields.
m_prgpidName = n_prgpidName;
m_luMask= n_luMask;
#if PROFILE_DICTIONARY
if(stats)
{
int emptyBuckets = 0;
for (uint i = 0; i < n_cidHash; i++)
{
if(m_prgpidName[i] == nullptr)
{
emptyBuckets++;
}
}
stats->Resize(n_cidHash, emptyBuckets);
}
#endif
}
#if DEBUG
uint HashTbl::CountAndVerifyItems(IdentPtr *buckets, uint bucketCount, uint mask)
{
uint count = 0;
for (uint i = 0; i < bucketCount; i++)
for (IdentPtr pid = buckets[i]; pid; pid = pid->m_pidNext)
{
Assert((pid->m_luHash & mask) == i);
count++;
}
return count;
}
#endif
#pragma warning(push)
#pragma warning(disable:4740) // flow in or out of inline asm code suppresses global optimization
// Decide if token is keyword by string matching -
// This method is used during colorizing when scanner isn't interested in storing the actual id and does not care about conversion of escape sequences
tokens Ident::TkFromNameLen(uint32 luHash, _In_reads_(cch) LPCOLESTR prgch, uint32 cch, bool isStrictMode, ushort * pgrfid, ushort * ptk)
{
// look for a keyword
#include "kwds_sw.h"
#define KEYWORD(tk,f,prec2,nop2,prec1,nop1,name) \
LEqual_##name: \
if (cch == g_ssym_##name.cch && \
0 == memcmp(g_ssym_##name.sz, prgch, cch * sizeof(OLECHAR))) \
{ \
if (f) \
*pgrfid |= f; \
*ptk = tk; \
return ((f & fidKwdRsvd) || (isStrictMode && (f & fidKwdFutRsvd))) ? tk : tkID; \
} \
goto LDefault;
#include "keywords.h"
LDefault:
return tkID;
}
#pragma warning(pop)
#if DBG
tokens Ident::TkFromNameLen(_In_reads_(cch) LPCOLESTR prgch, uint32 cch, bool isStrictMode)
{
uint32 luHash = CaseSensitiveComputeHash(prgch, prgch + cch);
ushort grfid;
ushort tk;
return TkFromNameLen(luHash, prgch, cch, isStrictMode, &grfid, &tk);
}
#endif
tokens Ident::Tk(bool isStrictMode)
{
const tokens token = (tokens)m_tk;
if (token == tkLim)
{
m_tk = tkNone;
const uint32 luHash = this->m_luHash;
const LPCOLESTR prgch = Psz();
const uint32 cch = Cch();
return TkFromNameLen(luHash, prgch, cch, isStrictMode, &this->m_grfid, &this->m_tk);
}
else if (token == tkNone || !(m_grfid & fidKwdRsvd))
{
if ( !isStrictMode || !(m_grfid & fidKwdFutRsvd))
{
return tkID;
}
}
return token;
}
void Ident::SetTk(tokens token, ushort grfid)
{
Assert(token != tkNone && token < tkID);
if (m_tk == tkLim)
{
m_tk = (ushort)token;
m_grfid |= grfid;
}
else
{
Assert(m_tk == token);
Assert((m_grfid & grfid) == grfid);
}
}
void Ident::TrySetIsUsedInLdElem(ParseNode * pnode)
{
if (pnode && pnode->nop == knopStr)
{
pnode->AsParseNodeStr()->pid->SetIsUsedInLdElem(true);
}
}
IdentPtr HashTbl::PidFromTk(tokens token)
{
Assert(token > tkNone && token < tkID);
__analysis_assume(token > tkNone && token < tkID);
// Create a pid so we can create a name node
IdentPtr rpid = m_rpid[token];
if (nullptr == rpid)
{
StaticSym const * sym = s_reservedWordInfo[token].sym;
Assert(sym != nullptr);
rpid = this->PidHashNameLenWithHash(sym->sz, sym->sz + sym->cch, sym->cch, sym->luHash);
rpid->SetTk(token, s_reservedWordInfo[token].grfid);
m_rpid[token] = rpid;
}
return rpid;
}
template <typename CharType>
IdentPtr HashTbl::PidHashNameLen(CharType const * prgch, CharType const * end, uint32 cch)
{
// NOTE: We use case sensitive hash during compilation, but the runtime
// uses case insensitive hashing so it can do case insensitive lookups.
uint32 luHash = CaseSensitiveComputeHash(prgch, end);
return PidHashNameLenWithHash(prgch, end, cch, luHash);
}
template IdentPtr HashTbl::PidHashNameLen<utf8char_t>(utf8char_t const * prgch, utf8char_t const * end, uint32 cch);
template IdentPtr HashTbl::PidHashNameLen<char>(char const * prgch, char const * end, uint32 cch);
template IdentPtr HashTbl::PidHashNameLen<char16>(char16 const * prgch, char16 const * end, uint32 cch);
template <typename CharType>
IdentPtr HashTbl::PidHashNameLen(CharType const * prgch, uint32 cch)
{
Assert(sizeof(CharType) == 2);
return PidHashNameLen(prgch, prgch + cch, cch);
};
template IdentPtr HashTbl::PidHashNameLen<utf8char_t>(utf8char_t const * prgch, uint32 cch);
template IdentPtr HashTbl::PidHashNameLen<char>(char const * prgch, uint32 cch);
template IdentPtr HashTbl::PidHashNameLen<char16>(char16 const * prgch, uint32 cch);
template <typename CharType>
IdentPtr HashTbl::PidHashNameLenWithHash(_In_reads_(cch) CharType const * prgch, CharType const * end, int32 cch, uint32 luHash)
{
Assert(cch >= 0);
Assert(cch == 0 || prgch != nullptr);
Assert(luHash == CaseSensitiveComputeHash(prgch, end));
IdentPtr * ppid = nullptr;
IdentPtr pid;
LONG cb;
int32 bucketCount;
#if PROFILE_DICTIONARY
int depth = 0;
#endif
pid = this->FindExistingPid(prgch, end, cch, luHash, &ppid, &bucketCount
#if PROFILE_DICTIONARY
, depth
#endif
);
if (pid)
{
return pid;
}
if (bucketCount > BucketLengthLimit && m_luCount > m_luMask)
{
Grow();
// ppid is now invalid because the Grow() moves the entries around.
// Find the correct ppid by repeating the find of the end of the bucket
// the new item will be placed in.
// Note this is similar to the main find loop but does not count nor does it
// look at the entries because we already proved above the entry is not in the
// table, we just want to find the end of the bucket.
ppid = &m_prgpidName[luHash & m_luMask];
while (*ppid)
ppid = &(*ppid)->m_pidNext;
}
#if PROFILE_DICTIONARY
++depth;
if (stats)
stats->Insert(depth);
#endif
//Windows OS Bug 1795286 : CENTRAL PREFAST RUN: inetcore\scriptengines\src\src\core\hash.cpp :
// 'sizeof((*pid))+((cch+1))*sizeof(OLECHAR)' may be smaller than
// '((cch+1))*sizeof(OLECHAR)'. This can be caused by integer overflows
// or underflows. This could yield an incorrect buffer all
/* Allocate space for the identifier */
ULONG Len;
if (FAILED(ULongAdd(cch, 1, &Len)) ||
FAILED(ULongMult(Len, sizeof(OLECHAR), &Len)) ||
FAILED(ULongAdd(Len, sizeof(*pid), &Len)) ||
FAILED(ULongToLong(Len, &cb)))
{
cb = 0;
OutOfMemory();
}
if (nullptr == (pid = (IdentPtr)m_noReleaseAllocator.Alloc(cb)))
OutOfMemory();
/* Insert the identifier into the hash list */
*ppid = pid;
// Increment the number of entries in the table.
m_luCount++;
/* Fill in the identifier record */
pid->m_pidNext = nullptr;
pid->m_tk = tkLim;
pid->m_grfid = fidNil;
pid->m_luHash = luHash;
pid->m_cch = cch;
pid->m_pidRefStack = nullptr;
pid->m_propertyId = Js::Constants::NoProperty;
pid->assignmentState = NotAssigned;
pid->isUsedInLdElem = false;
HashTbl::CopyString(pid->m_sz, prgch, end);
return pid;
}
template <typename CharType>
IdentPtr HashTbl::FindExistingPid(
CharType const * prgch,
CharType const * end,
int32 cch,
uint32 luHash,
IdentPtr **pppInsert,
int32 *pBucketCount
#if PROFILE_DICTIONARY
, int& depth
#endif
)
{
int32 bucketCount;
IdentPtr pid;
/* Search the hash table for an existing match */
IdentPtr *ppid = &m_prgpidName[luHash & m_luMask];
for (bucketCount = 0; nullptr != (pid = *ppid); ppid = &pid->m_pidNext, bucketCount++)
{
if (pid->m_luHash == luHash && (int)pid->m_cch == cch &&
HashTbl::CharsAreEqual(pid->m_sz, prgch, end))
{
return pid;
}
#if PROFILE_DICTIONARY
++depth;
#endif
}
if (pBucketCount)
{
*pBucketCount = bucketCount;
}
if (pppInsert)
{
*pppInsert = ppid;
}
return nullptr;
}
template IdentPtr HashTbl::FindExistingPid<utf8char_t>(
utf8char_t const * prgch, utf8char_t const * end, int32 cch, uint32 luHash, IdentPtr **pppInsert, int32 *pBucketCount
#if PROFILE_DICTIONARY
, int& depth
#endif
);
template IdentPtr HashTbl::FindExistingPid<char>(
char const * prgch, char const * end, int32 cch, uint32 luHash, IdentPtr **pppInsert, int32 *pBucketCount
#if PROFILE_DICTIONARY
, int& depth
#endif
);
template IdentPtr HashTbl::FindExistingPid<char16>(
char16 const * prgch, char16 const * end, int32 cch, uint32 luHash, IdentPtr **pppInsert, int32 *pBucketCount
#if PROFILE_DICTIONARY
, int& depth
#endif
);
bool HashTbl::Contains(_In_reads_(cch) LPCOLESTR prgch, int32 cch)
{
uint32 luHash = CaseSensitiveComputeHash(prgch, prgch + cch);
for (auto pid = m_prgpidName[luHash & m_luMask]; pid; pid = pid->m_pidNext)
{
if (pid->m_luHash == luHash && (int)pid->m_cch == cch &&
HashTbl::CharsAreEqual(pid->m_sz, prgch + cch, prgch))
{
return true;
}
}
return false;
}
#include "HashFunc.cpp"
__declspec(noreturn) void HashTbl::OutOfMemory()
{
throw ParseExceptionObject(ERRnoMemory);
}