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);
        }
Esempio n. 2
0
        /// <summary>
        /// If the memory source is of type ExternalMemory, give it a new instance of this process.
        /// </summary>
        public static void SwapExternalMemorySource(ref Reloaded.Memory.Sources.IMemory memorySource, Process newProcess = null)
        {
            // While running tests with xUnit, dotnet can seemingly restart itself causing for the old handle set in
            // the IMemoryGenerator class to be invalid.

            // This is a hack which replaces the ExternalMemory instance such that the tests run on the correct handle.
            if (memorySource.GetType() == typeof(ExternalMemory))
            {
                if (newProcess != null)
                {
                    memorySource = new Reloaded.Memory.Sources.ExternalMemory(newProcess);
                }
                else
                {
                    memorySource = new Reloaded.Memory.Sources.ExternalMemory(Process.GetCurrentProcess());
                }
            }
        }
        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);
        }