Files
palemoon27/xpcom/tests/gtest/TestPLDHash.cpp
T
roytam1 8a6eb144ac import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1126544 - Update sixgill to handle more constructs and manage memory better (e487a0cd05)
- Bug 1182407 - Use unpack feature of tooltool wherever possible. r=mshal (6bde76a413)
- Bug 1123386 - Part 4: Update the tooltool manifests for the OSX and Linux static analysis builds in order to upgrade clang; r=rail (517ae90efa)
- Bug 1204722 - Make sure that unboxed arrays created from literals are compatible with the type of the literal's elements, r=jandem. (400663edb4)
- Bug 1117259 - Disable the no-unused-local-typedef warning if clang supports it; r=gps (16e2e49fc6)
- Bug 1126813 - Turn on the -Wrange-loop-analysis warning if available; r=gps (1c783e1ed2)
- Bug 1191688 - Add -nologo option to rc.exe. r=ted (9c5dbe2b89)
- Bug 1150312 - Remove MOZ_SHARK. r=glandium (ba6db939d4)
- Bug 1186636 - Add a pref to configure -moz prefixed gradients support. r=dholbert (9c41ae7460)
- missing parts of  Bug 1077282: Cleanup uses of GreD vs GreBinD, introcuded by v2 signature changes on OSX. Based on initial patch by rstrong. r=bsmedberg (af60bfc743)
- de-palemoon (d8b7bae74f)
- Bug 932100 - Part 1: Remove load-time dependency on user32. r=bsmedberg (9864a0ed0c)
- missing part of Bug 932100 - Part 2: Move DLL blocklist code to mozglue. r=bsmedberg, r=glandium (6497ad86bd)
- Bug 1194890 - Ensure that any user32 imports to mozglue are delay loaded; r=glandium (afa0a8d14e)
- Bug 1082792 - Build firefox.exe with -MD in ASAN builds; r=glandium (89771bb4c0)
- missing VPX/WEBM stuff (ec425938c9)
- Bug 1184452 - Correctly reject @font-face descriptors that have garbage after them. r=heycam (f530fc858e)
- Bug 1189922. Add a preference to enable global whitelisting of the CSSUnprefixingService. r=dholbert (e2997cb125)
- Bug 1198732 - IonMonkey: MIPS32: Fix calculate frame size in generateEnterJIT. r=nbp (454d75946d)
- Bug 1199057 - IonMonkey: MIPS32: Plumb new.target on the stack and make it accessible to JSNatives. r=nbp (01d5cb04c2)
- Bug 1204306 - IonMonkey: MIPS32: Clean up MacroAssembler functions that aliased to Assembler. r=arai (600dc73280)
- Bug 1199080 - IonMonkey: MIPS32: Fix treating saved frame bit as part# o the frame type. r=nbp (5e4e5ba250)
- Bug 1099448 - Don't accept box properties with invalid calc() or rgb() etc. function values. r=dbaron (5737e8c300)
- Bug 1203142 - Insert /**/ separate between two adjacent '-' symbols when serializing token streams. r=simon.sapin (06fb613d6c)
- Bug 1057680 - Add support for font-stretch values in the font shorthand. r=jdaggett (8d8e24751d)
- Bug 1155485 - Mark nsFrameManagerBase::mPresShell as MOZ_NON_OWNING_REF; r=roc (e79e28bbd0)
- Bug 1121760 (part 5) - Remove PL_DHashMarkTableImmutable(). r=poiru. (8d8c7d9d65)
- Bug 1121760 (part 6) - Move all remaining PL_DHash*() functions into PLDHashTable. r=poiru. (d36ec167cc)
2022-03-03 07:57:18 +08:00

347 lines
11 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "pldhash.h"
#include "gtest/gtest.h"
// This test mostly focuses on edge cases. But more coverage of normal
// operations wouldn't be a bad thing.
#ifdef XP_UNIX
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
// This global variable is defined in toolkit/xre/nsSigHandlers.cpp.
extern unsigned int _gdb_sleep_duration;
#endif
// We can test that certain operations cause expected aborts by forking
// and then checking that the child aborted in the expected way (i.e. via
// MOZ_CRASH). We skip this for the following configurations.
// - On Windows, because it doesn't have fork().
// - On non-DEBUG builds, because the crashes cause the crash reporter to pop
// up when running this test locally, which is surprising and annoying.
// - On ASAN builds, because ASAN alters the way a MOZ_CRASHing process
// terminates, which makes it harder to test if the right thing has occurred.
void
TestCrashyOperation(void (*aCrashyOperation)())
{
#if defined(XP_UNIX) && defined(DEBUG) && !defined(MOZ_ASAN)
// We're about to trigger a crash. When it happens don't pause to allow GDB
// to be attached.
unsigned int old_gdb_sleep_duration = _gdb_sleep_duration;
_gdb_sleep_duration = 0;
int pid = fork();
ASSERT_NE(pid, -1);
if (pid == 0) {
// Child: perform the crashy operation.
aCrashyOperation();
fprintf(stderr, "TestCrashyOperation: didn't crash?!\n");
ASSERT_TRUE(false); // shouldn't reach here
}
// Parent: check that child crashed as expected.
int status;
ASSERT_NE(waitpid(pid, &status, 0), -1);
// The path taken here depends on the platform and configuration.
ASSERT_TRUE(WIFEXITED(status) || WTERMSIG(status));
if (WIFEXITED(status)) {
// This occurs if the ah_crap_handler() is run, i.e. we caught the crash.
// It returns the number of the caught signal.
int signum = WEXITSTATUS(status);
if (signum != SIGSEGV && signum != SIGBUS) {
fprintf(stderr, "TestCrashyOperation 'exited' failure: %d\n", signum);
ASSERT_TRUE(false);
}
} else if (WIFSIGNALED(status)) {
// This one occurs if we didn't catch the crash. The exit code is the
// number of the terminating signal.
int signum = WTERMSIG(status);
if (signum != SIGSEGV && signum != SIGBUS) {
fprintf(stderr, "TestCrashyOperation 'signaled' failure: %d\n", signum);
ASSERT_TRUE(false);
}
}
_gdb_sleep_duration = old_gdb_sleep_duration;
#endif
}
void
InitCapacityOk_InitialLengthTooBig()
{
PLDHashTable t(PLDHashTable::StubOps(), sizeof(PLDHashEntryStub),
PLDHashTable::kMaxInitialLength + 1);
}
void
InitCapacityOk_InitialEntryStoreTooBig()
{
// Try the smallest disallowed power-of-two entry store size, which is 2^32
// bytes (which overflows to 0). (Note that the 2^23 *length* gets converted
// to a 2^24 *capacity*.)
PLDHashTable t(PLDHashTable::StubOps(), (uint32_t)1 << 23, (uint32_t)1 << 8);
}
TEST(PLDHashTableTest, InitCapacityOk)
{
// Try the largest allowed capacity. With kMaxCapacity==1<<26, this
// would allocate (if we added an element) 0.5GB of entry store on 32-bit
// platforms and 1GB on 64-bit platforms.
PLDHashTable t1(PLDHashTable::StubOps(), sizeof(PLDHashEntryStub),
PLDHashTable::kMaxInitialLength);
// Try the largest allowed power-of-two entry store size, which is 2^31 bytes
// (Note that the 2^23 *length* gets converted to a 2^24 *capacity*.)
PLDHashTable t2(PLDHashTable::StubOps(), (uint32_t)1 << 23, (uint32_t)1 << 7);
// Try a too-large capacity (which aborts).
TestCrashyOperation(InitCapacityOk_InitialLengthTooBig);
// Try a large capacity combined with a large entry size that when multiplied
// overflow (causing abort).
TestCrashyOperation(InitCapacityOk_InitialEntryStoreTooBig);
// Ideally we'd also try a large-but-ok capacity that almost but doesn't
// quite overflow, but that would result in allocating slightly less than 4
// GiB of entry storage. That would be very likely to fail on 32-bit
// platforms, so such a test wouldn't be reliable.
}
TEST(PLDHashTableTest, LazyStorage)
{
PLDHashTable t(PLDHashTable::StubOps(), sizeof(PLDHashEntryStub));
// PLDHashTable allocates entry storage lazily. Check that all the non-add
// operations work appropriately when the table is empty and the storage
// hasn't yet been allocated.
ASSERT_EQ(t.Capacity(), 0u);
ASSERT_EQ(t.EntrySize(), sizeof(PLDHashEntryStub));
ASSERT_EQ(t.EntryCount(), 0u);
ASSERT_EQ(t.Generation(), 0u);
ASSERT_TRUE(!t.Search((const void*)1));
// No result to check here, but call it to make sure it doesn't crash.
t.Remove((const void*)2);
for (auto iter = t.Iter(); !iter.Done(); iter.Next()) {
ASSERT_TRUE(false); // shouldn't hit this on an empty table
}
ASSERT_EQ(t.ShallowSizeOfExcludingThis(moz_malloc_size_of), 0u);
}
// A trivial hash function is good enough here. It's also super-fast for
// test_pldhash_grow_to_max_capacity() because we insert the integers 0..,
// which means it's collision-free.
static PLDHashNumber
TrivialHash(PLDHashTable *table, const void *key)
{
return (PLDHashNumber)(size_t)key;
}
static void
TrivialInitEntry(PLDHashEntryHdr* aEntry, const void* aKey)
{
auto entry = static_cast<PLDHashEntryStub*>(aEntry);
entry->key = aKey;
}
static const PLDHashTableOps trivialOps = {
TrivialHash,
PLDHashTable::MatchEntryStub,
PLDHashTable::MoveEntryStub,
PLDHashTable::ClearEntryStub,
TrivialInitEntry
};
TEST(PLDHashTableTest, MoveSemantics)
{
PLDHashTable t1(&trivialOps, sizeof(PLDHashEntryStub));
t1.Add((const void*)88);
PLDHashTable t2(&trivialOps, sizeof(PLDHashEntryStub));
t2.Add((const void*)99);
t1 = mozilla::Move(t1); // self-move
t1 = mozilla::Move(t2); // empty overwritten with empty
PLDHashTable t3(&trivialOps, sizeof(PLDHashEntryStub));
PLDHashTable t4(&trivialOps, sizeof(PLDHashEntryStub));
t3.Add((const void*)88);
t3 = mozilla::Move(t4); // non-empty overwritten with empty
PLDHashTable t5(&trivialOps, sizeof(PLDHashEntryStub));
PLDHashTable t6(&trivialOps, sizeof(PLDHashEntryStub));
t6.Add((const void*)88);
t5 = mozilla::Move(t6); // empty overwritten with non-empty
PLDHashTable t7(&trivialOps, sizeof(PLDHashEntryStub));
PLDHashTable t8(mozilla::Move(t7)); // new table constructed with uninited
PLDHashTable t9(&trivialOps, sizeof(PLDHashEntryStub));
t9.Add((const void*)88);
PLDHashTable t10(mozilla::Move(t9)); // new table constructed with inited
}
TEST(PLDHashTableTest, Clear)
{
PLDHashTable t1(&trivialOps, sizeof(PLDHashEntryStub));
t1.Clear();
ASSERT_EQ(t1.EntryCount(), 0u);
t1.ClearAndPrepareForLength(100);
ASSERT_EQ(t1.EntryCount(), 0u);
t1.Add((const void*)77);
t1.Add((const void*)88);
t1.Add((const void*)99);
ASSERT_EQ(t1.EntryCount(), 3u);
t1.Clear();
ASSERT_EQ(t1.EntryCount(), 0u);
t1.Add((const void*)55);
t1.Add((const void*)66);
t1.Add((const void*)77);
t1.Add((const void*)88);
t1.Add((const void*)99);
ASSERT_EQ(t1.EntryCount(), 5u);
t1.ClearAndPrepareForLength(8192);
ASSERT_EQ(t1.EntryCount(), 0u);
}
TEST(PLDHashTableTest, Iterator)
{
PLDHashTable t(&trivialOps, sizeof(PLDHashEntryStub));
// Explicitly test the move constructor. We do this because, due to copy
// elision, compilers might optimize away move constructor calls for normal
// iterator use.
{
PLDHashTable::Iterator iter1(&t);
PLDHashTable::Iterator iter2(mozilla::Move(iter1));
}
// Iterate through the empty table.
for (PLDHashTable::Iterator iter(&t); !iter.Done(); iter.Next()) {
(void) iter.Get();
ASSERT_TRUE(false); // shouldn't hit this
}
// Add three entries.
t.Add((const void*)77);
t.Add((const void*)88);
t.Add((const void*)99);
// Check the iterator goes through each entry once.
bool saw77 = false, saw88 = false, saw99 = false;
int n = 0;
for (auto iter(t.Iter()); !iter.Done(); iter.Next()) {
auto entry = static_cast<PLDHashEntryStub*>(iter.Get());
if (entry->key == (const void*)77) {
saw77 = true;
}
if (entry->key == (const void*)88) {
saw88 = true;
}
if (entry->key == (const void*)99) {
saw99 = true;
}
n++;
}
ASSERT_TRUE(saw77 && saw88 && saw99 && n == 3);
t.Clear();
// First, we insert 64 items, which results in a capacity of 128, and a load
// factor of 50%.
for (intptr_t i = 0; i < 64; i++) {
t.Add((const void*)i);
}
ASSERT_EQ(t.EntryCount(), 64u);
ASSERT_EQ(t.Capacity(), 128u);
// The first removing iterator does no removing; capacity and entry count are
// unchanged.
for (PLDHashTable::Iterator iter(&t); !iter.Done(); iter.Next()) {
(void) iter.Get();
}
ASSERT_EQ(t.EntryCount(), 64u);
ASSERT_EQ(t.Capacity(), 128u);
// The second removing iterator removes 16 items. This reduces the load
// factor to 37.5% (48 / 128), which isn't low enough to shrink the table.
for (auto iter = t.Iter(); !iter.Done(); iter.Next()) {
auto entry = static_cast<PLDHashEntryStub*>(iter.Get());
if ((intptr_t)(entry->key) % 4 == 0) {
iter.Remove();
}
}
ASSERT_EQ(t.EntryCount(), 48u);
ASSERT_EQ(t.Capacity(), 128u);
// The third removing iterator removes another 16 items. This reduces
// the load factor to 25% (32 / 128), so the table is shrunk.
for (auto iter = t.Iter(); !iter.Done(); iter.Next()) {
auto entry = static_cast<PLDHashEntryStub*>(iter.Get());
if ((intptr_t)(entry->key) % 2 == 0) {
iter.Remove();
}
}
ASSERT_EQ(t.EntryCount(), 32u);
ASSERT_EQ(t.Capacity(), 64u);
// The fourth removing iterator removes all remaining items. This reduces
// the capacity to the minimum.
for (auto iter = t.Iter(); !iter.Done(); iter.Next()) {
iter.Remove();
}
ASSERT_EQ(t.EntryCount(), 0u);
ASSERT_EQ(t.Capacity(), unsigned(PLDHashTable::kMinCapacity));
}
// See bug 931062, we skip this test on Android due to OOM. Also, it's slow,
// and so should always be last.
#ifndef MOZ_WIDGET_ANDROID
TEST(PLDHashTableTest, GrowToMaxCapacity)
{
// This is infallible.
PLDHashTable* t =
new PLDHashTable(&trivialOps, sizeof(PLDHashEntryStub), 128);
// Keep inserting elements until failure occurs because the table is full.
size_t numInserted = 0;
while (true) {
if (!t->Add((const void*)numInserted, mozilla::fallible)) {
break;
}
numInserted++;
}
// We stop when the element count is 96.875% of PL_DHASH_MAX_SIZE (see
// MaxLoadOnGrowthFailure()).
if (numInserted !=
PLDHashTable::kMaxCapacity - (PLDHashTable::kMaxCapacity >> 5)) {
delete t;
ASSERT_TRUE(false);
}
delete t;
}
#endif