Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order iteration over all chromium hash tables. #717

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kannanvijayan
Copy link

This is a squashed rebase against master of Ori's original PR here: #616

For RUN-1730 (https://linear.app/replay/issue/RUN-1730/make-all-chromium-hashset-and-hashmap-iteration-order-deterministic)

@kannanvijayan kannanvijayan force-pushed the kannan/run-1730-make-all-chromium-hashset-and-hashmap-iteration-order branch 3 times, most recently from 56b5d1c to bd5312b Compare June 13, 2023 16:01
@kannanvijayan
Copy link
Author

Attaching the scratch printf patch so I don't lose it later.

diff --git a/third_party/blink/renderer/modules/storage/cached_storage_area.cc b/third_party/blink/renderer/modules/storage/cached_storage_area.cc
index 3699b4eda5142..df7b147b28937 100644
--- a/third_party/blink/renderer/modules/storage/cached_storage_area.cc
+++ b/third_party/blink/renderer/modules/storage/cached_storage_area.cc
@@ -108,10 +108,11 @@ bool CachedStorageArea::SetItem(const String& key,
                       optional_old_value, source_string,
                       MakeSuccessCallback(source));
   }
-  if (!IsSessionStorage())
+  if (!IsSessionStorage()) {
     EnqueuePendingMutation(key, value, old_value, source_string);
-  else if (old_value != value)
+  } else if (old_value != value) {
     EnqueueStorageEvent(key, old_value, value, page_url, source_id);
+  }
   return true;
 }
 
@@ -180,10 +181,33 @@ void CachedStorageArea::Clear(Source* source) {
 
 String CachedStorageArea::RegisterSource(Source* source) {
   String id = String::Number(base::RandUint64());
-  areas_->insert(source, id);
+  auto inserted = areas_->insert(source, id);
+  const WeakMember<Source>& weak_source = inserted.stored_value->key;
+  recordreplay::Print(
+    "KVKV CachedStorageArea(%p)::RegisterSource source=%p, weak_source=%p\n",
+    this, source, (void*) &weak_source
+  );
   return id;
 }
 
+void* KVKV_MAP_POINTERS[4] = {0};
+
+void KvkvCheckCachedStorageAreaPointer(const void* ptr, const char* string, ...) {
+  // format the string using the variable arguments
+  va_list args;
+  va_start(args, string);
+  char buffer[256];
+  vsnprintf(buffer, sizeof(buffer), string, args);
+  va_end(args);
+
+  for (size_t i = 0; i < 4; i++) {
+    if (KVKV_MAP_POINTERS[i] == ptr) {
+      recordreplay::Print("KVKV - HeapHashMap::Trace[%s] - %p\n", buffer, ptr);
+      break;
+    }
+  }
+}
+
 CachedStorageArea::CachedStorageArea(
     AreaType type,
     const BlinkStorageKey& storage_key,
@@ -196,6 +220,12 @@ CachedStorageArea::CachedStorageArea(
       storage_namespace_(storage_namespace),
       is_session_storage_for_prerendering_(is_session_storage_for_prerendering),
       areas_(MakeGarbageCollected<HeapHashMap<WeakMember<Source>, String>>()) {
+  for (size_t i = 0; i < 4; ++i) {
+    if (!KVKV_MAP_POINTERS[i]) {
+      KVKV_MAP_POINTERS[i] = areas_.Get();
+      break;
+    }
+  }
   BindStorageArea(std::move(storage_area), local_dom_window);
   base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
       this, "DOMStorage",
@@ -662,12 +692,33 @@ void CachedStorageArea::EnqueueStorageEvent(const String& key,
                                             const String& new_value,
                                             const String& url,
                                             const String& storage_area_id) {
+  recordreplay::Print(
+    "KVKV: CachedStorageArea(%p)::EnqueueStorageEvent: BEGIN areas_=%p\n",
+    this, areas_.Get()
+  );
   // Ignore key-change events which aren't actually changing the value.
   if (!key.IsNull() && new_value == old_value)
     return;
 
   HeapVector<Member<Source>, 1> areas_vector;
   for (const auto& area : *areas_) {
+    recordreplay::Print(
+      "KVKV: CachedStorageArea(%p)::EnqueueStorageEvent: Pushing source %p\n",
+      this,
+      area.key.Get()
+    );
+    if (area.key.Get() == nullptr) {
+      recordreplay::Print(
+        "KVKV: CachedStorageArea(%p)::EnqueueStorageEvent: "
+        "key=%s old_value=%s new_value=%s url=%s storage_area_id=%s\n",
+        this,
+        key.Utf8().data(),
+        old_value.Utf8().data(),
+        new_value.Utf8().data(),
+        url.Utf8().data(),
+        storage_area_id.Utf8().data()
+      );
+    }
     areas_vector.push_back(area.key);
   }
   std::sort(areas_vector.begin(), areas_vector.end(),
@@ -677,7 +728,12 @@ void CachedStorageArea::EnqueueStorageEvent(const String& key,
     auto iter = areas_->find(source);
     CHECK(iter != areas_->end());
     if (iter->value != storage_area_id) {
+      recordreplay::Print(
+        "KVKV: CachedStorageArea(%p)::EnqueueStorageEvent: source: %p - %s\n",
+        this, source.Get(), iter->value.Utf8().data()
+      );
       bool keep = source->EnqueueStorageEvent(key, old_value, new_value, url);
+      recordreplay::Print("KVKV: after source keep=%d\n", (int) keep);
       if (!keep)
         areas_to_remove_.push_back(source);
     }
diff --git a/third_party/blink/renderer/modules/storage/storage_area.cc b/third_party/blink/renderer/modules/storage/storage_area.cc
index 9aed892f89c4e..97a052eeca035 100644
--- a/third_party/blink/renderer/modules/storage/storage_area.cc
+++ b/third_party/blink/renderer/modules/storage/storage_area.cc
@@ -29,6 +29,7 @@
 #include "base/memory/scoped_refptr.h"
 #include "base/metrics/histogram_macros.h"
 #include "base/trace_event/trace_event.h"
+#include "base/record_replay.h"
 #include "third_party/blink/public/common/action_after_pagehide.h"
 #include "third_party/blink/public/common/features.h"
 #include "third_party/blink/public/platform/task_type.h"
@@ -65,6 +66,19 @@ StorageArea* StorageArea::CreateForInspectorAgent(
                                            /* should_enqueue_events */ false);
 }
 
+static StorageArea* KVKV_STORAGE_AREAS[4];
+
+void KvkvCheckStorageAreas(const void* ptr, const char* msg) {
+  if (ptr == nullptr) {
+    return;
+  }
+  for (size_t i = 0; i < 4; i++) {
+    if (KVKV_STORAGE_AREAS[i] == ptr) {
+      recordreplay::Print("KVKV CheckStorageAreas[%s]: Found %p\n", msg, ptr);
+    }
+  }
+}
+
 StorageArea::StorageArea(LocalDOMWindow* window,
                          scoped_refptr<CachedStorageArea> storage_area,
                          StorageType storage_type,
@@ -73,6 +87,12 @@ StorageArea::StorageArea(LocalDOMWindow* window,
       cached_area_(std::move(storage_area)),
       storage_type_(storage_type),
       should_enqueue_events_(should_enqueue_events) {
+  for (size_t i = 0; i < 4; i++) {
+    if (!KVKV_STORAGE_AREAS[i]) {
+      KVKV_STORAGE_AREAS[i] = this;
+      break;
+    }
+  }
   // Pointer registration is needed for sorting in CachedStorageArea::EnqueueStorageEvent.
   recordreplay::RegisterPointer("StorageArea", static_cast<CachedStorageArea::Source*>(this));
   DCHECK(window);
diff --git a/third_party/blink/renderer/platform/heap/collection_support/heap_hash_map.h b/third_party/blink/renderer/platform/heap/collection_support/heap_hash_map.h
index f194569fb0f04..34ea239542486 100644
--- a/third_party/blink/renderer/platform/heap/collection_support/heap_hash_map.h
+++ b/third_party/blink/renderer/platform/heap/collection_support/heap_hash_map.h
@@ -13,6 +13,8 @@
 
 namespace blink {
 
+void KvkvCheckCachedStorageAreaPointer(const void* ptr, const char *msg, ...);
+
 template <typename KeyArg,
           typename MappedArg,
           typename HashArg = typename DefaultHash<KeyArg>::Hash,
@@ -35,6 +37,8 @@ class HeapHashMap final : public GarbageCollected<HeapHashMap<KeyArg,
   HeapHashMap() { CheckType(); }
 
   void Trace(Visitor* visitor) const {
+    KvkvCheckCachedStorageAreaPointer((const void*) this, "HeapHashMap::Trace");
+
     HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg,
             HeapAllocator>::Trace(visitor);
   }
diff --git a/third_party/blink/renderer/platform/wtf/hash_table.h b/third_party/blink/renderer/platform/wtf/hash_table.h
index a346e37c8dcf2..fce45ed684eff 100644
--- a/third_party/blink/renderer/platform/wtf/hash_table.h
+++ b/third_party/blink/renderer/platform/wtf/hash_table.h
@@ -25,6 +25,7 @@
 
 #include <memory>
 #include <vector>
+#include <cstdio>
 
 #include "base/check_op.h"
 #include "base/dcheck_is_on.h"
@@ -95,6 +96,10 @@
 #endif
 #endif
 
+namespace blink {
+  void KvkvCheckCachedStorageAreaPointer(const void* ptr, const char* string, ...);
+}
+
 namespace WTF {
 
 // This is for tracing inside collections that have special support for weak
@@ -2279,11 +2284,19 @@ struct WeakProcessingHashTableHelper<kWeakHandling,
                       const void* parameter) {
     HashTableType* table =
         reinterpret_cast<HashTableType*>(const_cast<void*>(parameter));
+    KvkvCheckCachedStorageAreaPointer(
+      table,
+      "WeakProcessing: %p (%d)",
+      table->table_,
+      (int) table->table_size_
+    );
+    fprintf(stderr, "KVKV WeakProcessingHashTableHelper::Process BEGIN\n");
     // During incremental marking, the table may be freed after the callback has
     // been registered.
     if (!table->table_)
       return;
 
+    fprintf(stderr, "KVKV WeakProcessingHashTableHelper::Process START\n");
     // Weak processing: If the backing was accessible through an iterator and
     // thus marked strongly this loop will find all buckets as non-empty.
     for (ValueType* element = table->table_ + table->table_size_ - 1;
@@ -2292,7 +2305,9 @@ struct WeakProcessingHashTableHelper<kWeakHandling,
         if (!TraceInCollectionTrait<kWeakHandling, ValueType, Traits>::IsAlive(
                 info, *element)) {
           table->RegisterModification();
+          table->idxorder_[table->idxmap_[element - table->table_]] = -1;
           HashTableType::DeleteBucket(*element);  // Also calls the destructor.
+          fprintf(stderr, "KVKV WeakProcessingHashTableHelper::Process deleteBucket\n");
           table->deleted_count_++;
           table->key_count_--;
           // We don't rehash the backing until the next add or delete,
@@ -2300,6 +2315,7 @@ struct WeakProcessingHashTableHelper<kWeakHandling,
         }
       }
     }
+    fprintf(stderr, "KVKV WeakProcessingHashTableHelper::Process END\n");
   }
 };

@kannanvijayan kannanvijayan force-pushed the kannan/run-1730-make-all-chromium-hashset-and-hashmap-iteration-order branch from bd5312b to 831f48d Compare June 30, 2023 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant