From 96094881be4a8fcf3e3b85cebd2f5a2a497908b1 Mon Sep 17 00:00:00 2001 From: RemoteNet Date: Thu, 24 Oct 2024 18:57:31 +0300 Subject: [PATCH 1/2] Fix duplicate key exception in GetRemoteObject Fixes #17 Fix the issue of duplicate key exception when using `GetRemoteObject` multiple times on the same Candidate/address. * **Add `IsFrozen` method in `MsvcFrozenItemsCollection` class** - Add a method `IsFrozen` to check if an object is already frozen. - Implement the `IsFrozen` method to return true if the object is in `_frozenItemsToDestructors`. * **Modify `MakeObjectResponse` method in `MsvcDiver` class** - Check if an object is already frozen before attempting to freeze it. - Use the `IsFrozen` method from `MsvcFrozenItemsCollection` to check if an object is already frozen. - Return the already frozen object if it is found to be frozen. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/theXappy/RemoteNET/issues/17?shareId=XXXX-XXXX-XXXX-XXXX). --- src/ScubaDiver/MsvcDiver.cs | 14 ++++++++++++++ src/ScubaDiver/MsvcFrozenItemsCollection.cs | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/src/ScubaDiver/MsvcDiver.cs b/src/ScubaDiver/MsvcDiver.cs index 974bc484..5ce2a1f5 100644 --- a/src/ScubaDiver/MsvcDiver.cs +++ b/src/ScubaDiver/MsvcDiver.cs @@ -744,6 +744,20 @@ protected override string MakeObjectResponse(ScubaDiverMessage arg) try { + // Check if the object is already frozen + if (_freezer.IsFrozen(objAddr)) + { + Logger.Debug($"[MsvcDiver][MakeObjectResponse] Object at 0x{objAddr:X16} is already frozen."); + ObjectDump od = new ObjectDump() + { + Type = typeName, + RetrivalAddress = objAddr, + PinnedAddress = objAddr, + HashCode = 0x0bad0bad + }; + return JsonConvert.SerializeObject(od); + } + // TODO: Wrong for x86 long vftable = Marshal.ReadInt64(new IntPtr((long)objAddr)); Rtti.TypeInfo typeInfo = ResolveTypeFromVftableAddress((nuint)vftable); diff --git a/src/ScubaDiver/MsvcFrozenItemsCollection.cs b/src/ScubaDiver/MsvcFrozenItemsCollection.cs index ed3bf395..1bfe4efb 100644 --- a/src/ScubaDiver/MsvcFrozenItemsCollection.cs +++ b/src/ScubaDiver/MsvcFrozenItemsCollection.cs @@ -32,6 +32,13 @@ public ulong Pin(ulong objAddress) } } + public bool IsFrozen(ulong objAddress) + { + lock (_lock) + { + return _frozenItemsToDestructors.ContainsKey(objAddress); + } + } /// /// Unpins an object From 47672199406eed388a054743b2d713fd0faa9a4a Mon Sep 17 00:00:00 2001 From: RemoteNet Date: Thu, 24 Oct 2024 19:04:26 +0300 Subject: [PATCH 2/2] fix var name --- src/ScubaDiver/MsvcDiver.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ScubaDiver/MsvcDiver.cs b/src/ScubaDiver/MsvcDiver.cs index 5ce2a1f5..01b12730 100644 --- a/src/ScubaDiver/MsvcDiver.cs +++ b/src/ScubaDiver/MsvcDiver.cs @@ -748,14 +748,14 @@ protected override string MakeObjectResponse(ScubaDiverMessage arg) if (_freezer.IsFrozen(objAddr)) { Logger.Debug($"[MsvcDiver][MakeObjectResponse] Object at 0x{objAddr:X16} is already frozen."); - ObjectDump od = new ObjectDump() + ObjectDump alreadyFrozenObjDump = new ObjectDump() { Type = typeName, RetrivalAddress = objAddr, PinnedAddress = objAddr, HashCode = 0x0bad0bad }; - return JsonConvert.SerializeObject(od); + return JsonConvert.SerializeObject(alreadyFrozenObjDump); } // TODO: Wrong for x86