Branch: refs/heads/main
  Home:   https://github.com/WebKit/WebKit
  Commit: 815f292434d34a9f4f93866e851d9697c323aff6
      
https://github.com/WebKit/WebKit/commit/815f292434d34a9f4f93866e851d9697c323aff6
  Author: Geoffrey Garen <[email protected]>
  Date:   2025-12-07 (Sun, 07 Dec 2025)

  Changed paths:
    M Source/WTF/wtf/HashTable.h
    M Source/WTF/wtf/WeakPtr.h
    M Tools/TestWebKitAPI/Tests/WTF/HashSet.cpp

  Log Message:
  -----------
  Optimize hash table key validation & close the HashTranslator loophole
https://bugs.webkit.org/show_bug.cgi?id=303648
rdar://165932284

Reviewed by Darin Adler.

Changed hash table key validation to happen after insertion.

This has a few benefits:

1. Key validation happens less frequently. Lookup is far more common than
insertion, on average.

2. Key validation no longer does type conversion. Some conversions have a cost,
like P* => RefPtr<P> or, worse, std::span<const char> => String.

Hopefully these optimizations will enable key validation in more cases -- though
I haven't tested end-to-end performance yet.

3. HashTranslator no longer skips key validation. It does take some doing to
sneak a bad key through a HashTranslator. But it's possible. For example:

    HashSet<String> hashSet;
    hashSet.add<StringViewHashTranslator>(
        StringView(String())); // Oops!
    hashSet.add<StringViewHashTranslator>(
        StringView(String(WTF::HashTableDeletedValue))); // Oops!

This patch introduces a small behavior change: In release builds only, if you
try to look up the empty or deleted key, you'll get "not present" now instead
of a crash.

Added API tests to cover these behavior changes.

* Source/WTF/wtf/HashTable.h:
(WTF::HashTable::isValidKey):
(WTF::HashTable::validateKey):
(WTF::Malloc>::inlineLookup):
(WTF::Malloc>::lookupForReinsert):
(WTF::Malloc>::fullLookupForWriting):
(WTF::Malloc>::addUniqueForInitialization):
(WTF::Malloc>::add):
(WTF::Malloc>::addPassingHashCode):
(WTF::checkHashTableKey): Deleted.
* Source/WTF/wtf/WeakPtr.h:
(WTF::WeakPtrHashTraits::isDeletedValue): Deleted.
* Tools/TestWebKitAPI/Tests/WTF/HashSet.cpp:
(TestWebKitAPI::TEST(WTF_HashSetDeathTest, StringViewHashTranslatorEmptyValue)):
(TestWebKitAPI::TEST(WTF_HashSetDeathTest, 
StringViewHashTranslatorDeletedValue)):
(TestWebKitAPI::TEST(WTF_HashSetDeathTest, 
ASCIICaseInsensitiveStringViewHashTranslatorEmptyValue)):
(TestWebKitAPI::TEST(WTF_HashSetDeathTest, 
ASCIICaseInsensitiveStringViewHashTranslatorDeletedValue)):
(TestWebKitAPI::TEST(WTF_HashSetDeathTest, 
HashTranslatorASCIILiteralEmptyValue)):
(TestWebKitAPI::TEST(WTF_HashSetDeathTest, 
HashTranslatorASCIILiteralDeletedValue)):
(TestWebKitAPI::TEST(WTF_HashSetDeathTest, 
HashTranslatorASCIILiteralCaseInsensitiveEmptyValue)):
(TestWebKitAPI::TEST(WTF_HashSet, 
HashTranslatorASCIILiteralCaseInsensitiveDeletedValue)):

Canonical link: https://commits.webkit.org/304074@main



To unsubscribe from these emails, change your notification settings at 
https://github.com/WebKit/WebKit/settings/notifications

Reply via email to