Beispiel #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);
        }
        public void SafeReadWriteRaw(Reloaded.Memory.Sources.IMemory memorySource)
        {
            // Prepare
            int arrayElements = 13432;

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

            /* Start Test */

            // Generate random int struct to read/write to.
            var randomByteArray = RandomByteArray.GenerateRandomByteArray(arrayElements);

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

            // Throws corrupted state exception if operations fail until restore.
            memorySource.SafeWriteRaw(pointer, randomByteArray.Array);
            memorySource.SafeReadRaw(pointer, out byte[] randomByteArrayCopy, randomByteArray.Array.Length);

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

            // Compare before exiting test.
            Assert.Equal(randomByteArray.Array, randomByteArrayCopy);

            // Cleanup
            memorySource.Free(pointer);
        }
Beispiel #3
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);
        }
        public void SafeReadWrite(Reloaded.Memory.Sources.IMemory memorySource)
        {
            // Prepare
            int structSize = Struct.GetSize <RandomIntStruct>();

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

            /* Start Test */

            // Generate random int struct to read/write to.
            var randomIntStruct = RandomIntStruct.BuildRandomStruct();

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

            // Throws corrupted state exception if operations fail until restore.
            memorySource.SafeWrite(pointer, ref randomIntStruct, false);
            memorySource.SafeRead(pointer, out RandomIntStruct randomIntStructOldOverload, false);
            memorySource.SafeRead(pointer, out RandomIntStruct randomIntStructNewOverload);
            Assert.Equal(randomIntStructOldOverload, randomIntStructNewOverload);

            // New overloads in 1.4.0
            memorySource.SafeWrite(pointer, ref randomIntStruct);
            memorySource.SafeRead(pointer, out randomIntStructOldOverload, false);
            memorySource.SafeRead(pointer, out randomIntStructNewOverload);
            Assert.Equal(randomIntStructOldOverload, randomIntStructNewOverload);

            // We test both read functions against each write function.

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

            // Compare before exiting test.
            Assert.Equal(randomIntStruct, randomIntStructNewOverload);

            // Cleanup
            memorySource.Free(pointer);
        }
Beispiel #5
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);
        }
Beispiel #6
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);
        }
Beispiel #7
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);
        }
Beispiel #8
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);
        }