Exemplo n.º 1
0
        public void running_a_scan_when_no_pointers_are_referenced_resets_the_arena()
        {
            var bump    = (Allocator.ArenaSize / 4) + 1; // three fit in each arena, with some spare
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new Allocator(512, Mega.Bytes(1), mem);

            subject.Alloc(bump);
            var ar1 = subject.CurrentArena();

            subject.Alloc(bump); subject.Alloc(bump); // fill up first arena

            var x2  = subject.Alloc(bump).Value;
            var ar2 = subject.CurrentArena();

            Assert.That(ar1, Is.Not.EqualTo(ar2), "Failed to trigger a new arena");

            // Check that both arenas are non-empty:
            Assert.That(subject.GetArenaOccupation(ar1).Value, Is.Not.Zero);
            Assert.That(subject.GetArenaOccupation(ar2).Value, Is.Not.Zero);

            // Run the scan (only including the second arena)
            var list = new Vector <long>(subject, mem);

            list.Push(x2);
            subject.ScanAndSweep(list);

            // Check nothing has been cleared
            Assert.That(subject.GetArenaOccupation(ar1).Value, Is.Zero, "Unreferenced arena was not reset");
            Assert.That(subject.GetArenaOccupation(ar2).Value, Is.Not.Zero);
        }
Exemplo n.º 2
0
        public void running_a_scan_when_any_pointers_are_referenced_keeps_the_arena()
        {
            // scan takes a list of known references, and assumes anything
            // that's not in the list in not referenced. Any arena with nothing
            // referenced is reset.

            var mem     = new MemorySimulator(Mega.Bytes(1));
            var bump    = (Allocator.ArenaSize / 4) + 1; // three fit in each arena, with some spare
            var subject = new Allocator(512, Mega.Bytes(1), mem);

            var x1  = subject.Alloc(bump).Value;
            var ar1 = subject.CurrentArena();

            subject.Alloc(bump); subject.Alloc(bump); // fill up first arena

            var x2  = subject.Alloc(bump).Value;
            var ar2 = subject.CurrentArena();

            Assert.That(ar1, Is.Not.EqualTo(ar2), "Failed to trigger a new arena");

            // Check that both arenas are non-empty:
            Assert.That(subject.GetArenaOccupation(ar1).Value, Is.Not.Zero);
            Assert.That(subject.GetArenaOccupation(ar2).Value, Is.Not.Zero);

            // Run the scan (note we don't need all pointers, just one from each arena)
            var list = new Vector <long>(subject, mem);

            list.Push(x1);
            list.Push(x2);
            subject.ScanAndSweep(list);

            // Check nothing has been cleared
            Assert.That(subject.GetArenaOccupation(ar1).Value, Is.Not.Zero);
            Assert.That(subject.GetArenaOccupation(ar2).Value, Is.Not.Zero);
        }
Exemplo n.º 3
0
        public void a_second_allocation_returns_different_memory()
        {
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new Allocator(100, Mega.Bytes(10), mem);

            long ptr1 = subject.Alloc(byteCount: 256).Value;
            long ptr2 = subject.Alloc(byteCount: 256).Value;

            Assert.That(ptr1, Is.Not.EqualTo(ptr2));
        }
Exemplo n.º 4
0
        public void allocating_enough_memory_changes_arena()
        {
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new Allocator(100, Mega.Bytes(1), mem);

            int  first  = subject.CurrentArena();
            long ptr1   = subject.Alloc(Allocator.ArenaSize).Value;
            long ptr2   = subject.Alloc(Kilo.Bytes(1)).Value;
            int  second = subject.CurrentArena();

            Assert.That(ptr1, Is.Not.EqualTo(ptr2));
            Assert.That(first, Is.Not.EqualTo(second));
        }
Exemplo n.º 5
0
        public void deallocating_an_old_allocation_does_nothing()
        {
            // Older items just hang around until the entire arena is abandoned
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new Allocator(100, Mega.Bytes(10), mem);

            long ptr1 = subject.Alloc(byteCount: 256).Value;
            long ptr2 = subject.Alloc(byteCount: 256).Value;

            subject.Deref(ptr1);
            long ptr3 = subject.Alloc(byteCount: 512).Value;

            Assert.That(ptr3, Is.GreaterThan(ptr2));
        }
Exemplo n.º 6
0
        public static void Set(string tag)
        {
            if (Address == IntPtr.Zero)
            {
                Allocator alloc = new Allocator();
                Address = alloc.Alloc(Size);
                alloc.Free();

                if (Address == IntPtr.Zero)
                {
                    return;
                }

                Buffer.BlockCopy(BitConverter.GetBytes((int)(Address + 18)), 0, Shellcode, 1, 4);
                Buffer.BlockCopy(BitConverter.GetBytes((int)(Address + 18)), 0, Shellcode, 6, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(Memory.Engine + signatures.dwSetClanTag), 0, Shellcode, 11, 4);
            }

            if (!LocalPlayer.InGame)
            {
                return;
            }

            byte[] tag_bytes = Encoding.UTF8.GetBytes(tag + "\0");
            byte[] reset     = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

            Buffer.BlockCopy(reset, 0, Shellcode, 18, reset.Length);
            Buffer.BlockCopy(tag_bytes, 0, Shellcode, 18, tag.Length > 15 ? 15 : tag.Length);
            CreateThread.Create(Address, Shellcode);
        }
Exemplo n.º 7
0
        public void TestMethod1()
        {
            var al = new Allocator(new MyAllocator());
            var vi = al.Alloc <int>();

            vi = 42;
        }
Exemplo n.º 8
0
        [Test] // a stress test of sorts
        public void can_handle_large_allocation_spaces()
        {
            var mem     = new OffsetMemorySimulator(Mega.Bytes(1), Giga.Bytes(1)); // only need enough room for arena tables
            var subject = new Allocator(Giga.Bytes(1), Giga.Bytes(2), mem);        // big for an embedded system. 3GB total.

            var result = subject.Alloc(Allocator.ArenaSize / 2);

            Assert.That(result.Value - Giga.Bytes(1), Is.EqualTo(131072)); // allocated at bottom of given space, excluding arena tables

            for (int i = 0; i < 1000; i++)
            {
                result = subject.Alloc(Allocator.ArenaSize - 1);
            }

            Assert.That(subject.CurrentArena(), Is.GreaterThanOrEqualTo(1000));
            Assert.That(result.Value, Is.GreaterThan(Giga.Bytes(1)));
        }
Exemplo n.º 9
0
        public void can_allocate_memory_from_a_pool_and_get_a_pointer()
        {
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new Allocator(100, Mega.Bytes(10), mem);

            long ptr = subject.Alloc(byteCount: Kilo.Bytes(1)).Value;

            Assert.That(ptr, Is.GreaterThanOrEqualTo(100));
        }
Exemplo n.º 10
0
        [Test] // The vector class can be used to store larger chunks of data
        public void requesting_a_block_larger_than_a_single_area_fails()
        {
            // Doing this to keep things very simple
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new Allocator(512, Mega.Bytes(1), mem);

            var result = subject.Alloc(Allocator.ArenaSize * 2);

            Assert.That(result.Success, Is.False);
        }
Exemplo n.º 11
0
        public void deallocating_everything_in_an_arena_resets_it()
        {
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new Allocator(100, Mega.Bytes(1), mem);

            long ptr1 = subject.Alloc(512).Value;
            long ptr2 = subject.Alloc(512).Value;
            long ptr3 = subject.Alloc(512).Value;

            subject.Deref(ptr1);
            subject.Deref(ptr2);

            long ptr4 = subject.Alloc(512).Value;

            subject.Deref(ptr3);
            subject.Deref(ptr4);

            // should be reset now, and next alloc goes back to start
            long ptrFinal = subject.Alloc(512).Value;

            Assert.That(ptrFinal, Is.EqualTo(ptr1));
        }
Exemplo n.º 12
0
        public void can_directly_deallocate_a_pointer()
        {
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new Allocator(100, Mega.Bytes(10), mem);

            long ptr = subject.Alloc(byteCount: 256).Value;

            subject.Deref(ptr);

            var ar   = subject.CurrentArena();
            var refs = subject.ArenaRefCount(ar);

            Assert.That(refs.Value, Is.Zero);
        }
Exemplo n.º 13
0
        public void memory_exhaustion_results_in_an_error_code()
        {
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new Allocator(10, Mega.Bytes(1), mem);

            Result <long> result = default;

            for (int i = 0; i < 17; i++)
            {
                result = subject.Alloc(Allocator.ArenaSize - 1);
            }

            Assert.That(result.Success, Is.False);
        }
Exemplo n.º 14
0
        public void can_add_and_remove_current_referenced_pointers()
        {
            // This increments and decrements ref counts?
            // Free is the equivalent of directly setting ref count to zero? Or is it just a synonym for Deref?
            // We can keep an overall refcount for the arena and ignore the individual references (except for the head, as an optimisation)
            // We don't protect from double-free

            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new Allocator(100, Mega.Bytes(1), mem);

            long ptr = subject.Alloc(byteCount: 256).Value;

            subject.Reference(ptr);
            subject.Reference(ptr);
            subject.Deref(ptr);

            Assert.Pass();
        }
Exemplo n.º 15
0
        public void add_and_removing_elements_works_with_pre_existing_allocations()
        {
            var mem   = new MemorySimulator(Mega.Bytes(1));
            var alloc = new Allocator(0, Mega.Bytes(1), mem);

            alloc.Alloc(174);
            var subject = new Vector <SampleElement>(alloc, mem);

            Assert.That(subject.Length(), Is.Zero);

            subject.Push(Sample1());
            subject.Push(Sample2());

            Assert.That(subject.Length(), Is.EqualTo(2));

            subject.Pop();
            subject.Push(Sample3());

            Assert.That(subject.Get(1).Value, Is.EqualTo(Sample3()), "Get 1 is wrong");
            Assert.That(subject.Get(0).Value, Is.EqualTo(Sample1()), "Get 0 is wrong");
        }
Exemplo n.º 16
0
        public void can_read_the_current_allocation_pressure()
        {
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new Allocator(10, Mega.Bytes(1), mem); // this is 1048576, which doesn't divide nicely into arenas...

            // Check the empty state is sane
            subject.GetState(out var allocatedBytes, out var unallocatedBytes, out var occupiedArenas, out var emptyArenas, out var refCount, out var largestBlock);

            Assert.That(allocatedBytes, Is.Zero);
            Assert.That(unallocatedBytes, Is.EqualTo(1048560)); // ... so we end up with slightly less space that's usable. Max loss is < 64K
            Assert.That(largestBlock, Is.EqualTo(Allocator.ArenaSize));

            Assert.That(occupiedArenas, Is.Zero);
            Assert.That(emptyArenas, Is.EqualTo(16)); // arenas per megabyte
            Assert.That(refCount, Is.Zero);

            // Do some allocation
            var allocd = 0L;
            var size   = Allocator.ArenaSize / 6;

            for (int i = 0; i < 14; i++)
            {
                allocd += size;
                subject.Alloc(size);
            }

            // Check filled state is sane
            subject.GetState(out allocatedBytes, out unallocatedBytes, out occupiedArenas, out emptyArenas, out refCount, out largestBlock);

            Assert.That(allocatedBytes, Is.EqualTo(allocd));
            Assert.That(unallocatedBytes, Is.EqualTo(1048560 - allocd));
            Assert.That(largestBlock, Is.EqualTo(Allocator.ArenaSize));

            Assert.That(occupiedArenas, Is.EqualTo(3));
            Assert.That(emptyArenas, Is.EqualTo(13));
            Assert.That(refCount, Is.EqualTo(14));
        }
Exemplo n.º 17
0
        public static void Exec(string szCmd, bool highPriority = false)
        {
            if (Address == IntPtr.Zero)
            {
                Allocator Alloc = new Allocator();
                Address = Alloc.Alloc(Size);
                if (Address == IntPtr.Zero)
                {
                    return;
                }
            }
            if (szCmd.Length > 255)
            {
                szCmd = szCmd.Substring(0, 255);
            }

            var szCmd_bytes = Encoding.UTF8.GetBytes(szCmd + "\0");

            Memory.WriteProcessMemory(Memory.pHandle, Address, szCmd_bytes, szCmd_bytes.Length, 0);
            IntPtr Thread = DllImport.CreateRemoteThread(Memory.pHandle, (IntPtr)null, IntPtr.Zero, new IntPtr(Memory.Engine + CalcedOffsets.ClientCMD), Address, 0, (IntPtr)null);

            DllImport.CloseHandle(Thread);
            DllImport.WaitForSingleObject(Thread, 0xFFFFFFFF);
        }
Exemplo n.º 18
0
 public CPtr AllocObject(object value)
 {
     return(mObject.Alloc(value));
 }
Exemplo n.º 19
0
 public CPtr AllocDateTime(DateTime dt)
 {
     return(mDateTime.Alloc(dt));
 }
Exemplo n.º 20
0
 public CPtr AllocString(string value)
 {
     return(mStr.Alloc(value));
 }
Exemplo n.º 21
0
 public CPtr AllocDouble(double value)
 {
     return(mDouble.Alloc(value));
 }
Exemplo n.º 22
0
 public CPtr AllocFloat(float value)
 {
     return(mFloat.Alloc(value));
 }
Exemplo n.º 23
0
 public CPtr AllocInt64(int value)
 {
     return(mInt64.Alloc(value));
 }
Exemplo n.º 24
0
 public CPtr AllocInt32(int value)
 {
     return(mInt32.Alloc(value));
 }
Exemplo n.º 25
0
 public CPtr AllocBool(bool value)
 {
     return(mBoolean.Alloc(value));
 }
Exemplo n.º 26
0
 private static IntPtr MemAllocCallback(UIntPtr size) => Allocator.Alloc((int)size.ToUInt32());