Exemple #1
0
        public void ReadWriteWithMarshalling(Reloaded.Memory.Sources.IMemory memorySource)
        {
            // Prepare
            IMemoryTools.SwapExternalMemorySource(ref memorySource, _helloWorldProcess);
            IntPtr pointer = memorySource.Allocate(0x100);

            /* Start Test */

            // Random marshal struct read/write.
            for (int x = 0; x < 100; x++)
            {
                MarshallingStruct randomIntStruct = MarshallingStruct.BuildRandomStruct();
                memorySource.Write(pointer, ref randomIntStruct, true);
                memorySource.Read(pointer, out MarshallingStruct randomValueCopy, true);

                // Test for equality.
                Assert.Equal(randomIntStruct, randomValueCopy);

                // Test references:
                // If marshalling did not take place, write function would have written pointer to string and read it back in.
                // If marshalling did take place, a new string was created with the value of the string found in memory.
                // Set marshal parameter to false in read/write operation above to test this.
                Assert.False(object.ReferenceEquals(randomIntStruct.Name, randomValueCopy.Name));
            }

            /* End Test */

            // Cleanup
            memorySource.Free(pointer);
        }
Exemple #2
0
        public void AllocateMemory(Reloaded.Memory.Sources.IMemory memorySource)
        {
            // Prepare
            IMemoryTools.SwapExternalMemorySource(ref memorySource, _helloWorldProcess);

            // Test
            IntPtr pointer = memorySource.Allocate(0xFFFF);

            Assert.NotEqual((IntPtr)0, pointer);
            memorySource.Free(pointer);
        }
Exemple #3
0
        public void ChangePermissionFail(Reloaded.Memory.Sources.IMemory memorySource)
        {
            /* Start Test */

            // Run the change permission function to deny read/write access.
            try { memorySource.ChangePermission((IntPtr)(-1), 0x100, Kernel32.Kernel32.MEM_PROTECTION.PAGE_NOACCESS); }
            catch (NotImplementedException)          { return; } // ChangePermission is optional to implement
            catch (MemoryPermissionException) { return; }        // Thrown as expected.

            // Cleanup on fail.
            memorySource.ChangePermission((IntPtr)(-1), 0x100, Kernel32.Kernel32.MEM_PROTECTION.PAGE_EXECUTE_READWRITE);
            Assert.True(false, "This method should throw CannotChangePermissionsException");
        }
Exemple #4
0
        public void ReadWritePrimitives(Reloaded.Memory.Sources.IMemory memorySource)
        {
            // Prepare
            IMemoryTools.SwapExternalMemorySource(ref memorySource, _helloWorldProcess);
            IntPtr pointer = memorySource.Allocate(0x100);

            /* Start Test */

            // Random integer read/write.
            for (int x = 0; x < 100; x++)
            {
                int randomValue = new Random().Next();
                memorySource.Write(pointer, ref randomValue);
                memorySource.Read(pointer, out int randomValueCopy);
                Assert.Equal(randomValue, randomValueCopy);
            }

            /* End Test */

            // Cleanup
            memorySource.Free(pointer);
        }
Exemple #5
0
        public void ChangePermissions(Reloaded.Memory.Sources.IMemory memorySource)
        {
            // Prepare
            IMemoryTools.SwapExternalMemorySource(ref memorySource, _helloWorldProcess);
            IntPtr pointer = memorySource.Allocate(0x100);

            /* Start Test */

            // Run the change permission function to deny read/write access.
            try { memorySource.ChangePermission(pointer, 0x100, Kernel32.Kernel32.MEM_PROTECTION.PAGE_NOACCESS); }
            catch (NotImplementedException) { return; } // ChangePermission is optional to implement

            // NETCore removed handling of Corrupted State Exceptions https://github.com/dotnet/coreclr/issues/9045
            // We cannot properly test the method.

            // Restore or NETCore execution engine will complain.
            try { memorySource.ChangePermission(pointer, 0x100, Kernel32.Kernel32.MEM_PROTECTION.PAGE_EXECUTE_READWRITE); }
            catch (NotImplementedException) { return; } // ChangePermission is optional to implement

            // Cleanup
            memorySource.Free(pointer);
        }
Exemple #6
0
        public void ReadWriteStructs(Reloaded.Memory.Sources.IMemory memorySource)
        {
            // Prepare
            IMemoryTools.SwapExternalMemorySource(ref memorySource, _helloWorldProcess);
            IntPtr pointer = memorySource.Allocate(0x100);

            /* Start Test */

            // Random struct read/write.
            for (int x = 0; x < 100; x++)
            {
                RandomIntStruct randomIntStruct = RandomIntStruct.BuildRandomStruct();
                memorySource.Write(pointer, ref randomIntStruct);
                memorySource.Read(pointer, out RandomIntStruct randomValueCopy);
                Assert.Equal(randomIntStruct, randomValueCopy);
            }

            /* End Test */

            // Cleanup
            memorySource.Free(pointer);
        }
Exemple #7
0
        public void ReadWriteRawData(Reloaded.Memory.Sources.IMemory memorySource)
        {
            // Prepare
            int allocationSize = 0x100;

            IMemoryTools.SwapExternalMemorySource(ref memorySource, _helloWorldProcess);
            IntPtr pointer = memorySource.Allocate(allocationSize);

            /* Start Test */

            // Random integer read/write.
            for (int x = 0; x < 100; x++)
            {
                RandomByteArray randomArray = RandomByteArray.GenerateRandomByteArray(allocationSize);
                memorySource.WriteRaw(pointer, randomArray.Array);
                memorySource.ReadRaw(pointer, out byte[] randomValueCopy, randomArray.Array.Length);
                Assert.Equal(randomArray.Array, randomValueCopy);
            }

            /* End Test */

            // Cleanup
            memorySource.Free(pointer);
        }