kojocrash How do I cheat? Reputation: 0
Joined: 27 Dec 2022 Posts: 6
|
Posted: Thu Dec 29, 2022 8:54 pm Post subject: How to (locally) allocate fixed size? |
|
|
I am trying to allocate X bytes into cheat engine's local memory but all the methods I've tried always allocates 4096 bytes instead. Usually this wouldn't be a problem for me, but my extension will be doing thousands of allocations and I'd like to keep the allocation size to a minimum. I am not sure if I am misunderstanding the concept of allocation, but looking at memory view, the newly allocated region has 4096 bytes of "00"s followed and preceeded by "??".
Here's the code I used to test different methods:
Code: | --[[ RESULTS
Local Shared Memory:
Mem A: 2950000 (allocated more than needed = true)
Mem B: 2960000 (allocated more than needed = true) (diff = 0x10000)
Mem C: 2970000 (allocated more than needed = true) (diff = 0x10000)
Mem D: 2980000 (allocated more than needed = true) (diff = 0x10000)
Regular Allocation:
Mem A: 232997f0000 (allocated more than needed = true)
Mem B: 23299800000 (allocated more than needed = true) (diff = 0x10000)
Mem C: 23299810000 (allocated more than needed = true) (diff = 0x10000)
Mem D: 23299820000 (allocated more than needed = true) (diff = 0x10000)
Kernel Memory:
Mem A: 0 (allocated more than needed = false)
Mem B: 0 (allocated more than needed = false) (diff = 0)
Mem C: 0 (allocated more than needed = false) (diff = 0)
Mem D: 0 (allocated more than needed = false) (diff = 0)
Copy Memory:
Mem A: 2950000 (allocated more than needed = true)
Mem B: 2960000 (allocated more than needed = true) (diff = 0x10000)
Mem C: 2970000 (allocated more than needed = true) (diff = 0x10000)
Mem D: 2980000 (allocated more than needed = true) (diff = 0x10000)
Shared Memory:
Mem A: 232997f0000 (allocated more than needed = true)
Mem B: 23299810000 (allocated more than needed = true) (diff = 0x20000)
Mem C: 23299840000 (allocated more than needed = true) (diff = 0x30000)
Mem D: 23299860000 (allocated more than needed = true) (diff = 0x20000)
Error:deallocateSharedMemory is not implemented (It's not even in the list of available functions)
Script Error
]]
local function allocatedMoreThanNeeded(memAddr)
return readBytes(memAddr + 8) ~= nil
end
local function allocatedMoreThanNeededLocal(memAddr)
return readBytesLocal(memAddr + 8) ~= nil
end
function testA()
local aMem = allocateSharedMemoryLocal("testingA", 8)
local bMem = allocateSharedMemoryLocal("testingB", 8)
local cMem = allocateSharedMemoryLocal("testingC", 8)
local dMem = allocateSharedMemoryLocal("testingD", 8)
print(("Mem A: %x (allocated more than needed = %s)"):format(aMem, tostring(allocatedMoreThanNeededLocal(aMem))))
print(("Mem B: %x (allocated more than needed = %s) (diff = %#x)"):format(bMem, tostring(allocatedMoreThanNeededLocal(bMem)), bMem - aMem))
print(("Mem C: %x (allocated more than needed = %s) (diff = %#x)"):format(cMem, tostring(allocatedMoreThanNeededLocal(cMem)), cMem - bMem))
print(("Mem D: %x (allocated more than needed = %s) (diff = %#x)"):format(dMem, tostring(allocatedMoreThanNeededLocal(dMem)), dMem - cMem))
deallocateSharedMemoryLocal(aMem)
deallocateSharedMemoryLocal(bMem)
deallocateSharedMemoryLocal(cMem)
deallocateSharedMemoryLocal(dMem)
end
function testB()
local aMem = allocateMemory(8)
local bMem = allocateMemory(8)
local cMem = allocateMemory(8)
local dMem = allocateMemory(8)
print(("Mem A: %x (allocated more than needed = %s)"):format(aMem, tostring(allocatedMoreThanNeeded(aMem))))
print(("Mem B: %x (allocated more than needed = %s) (diff = %#x)"):format(bMem, tostring(allocatedMoreThanNeeded(bMem)), bMem - aMem))
print(("Mem C: %x (allocated more than needed = %s) (diff = %#x)"):format(cMem, tostring(allocatedMoreThanNeeded(cMem)), cMem - bMem))
print(("Mem D: %x (allocated more than needed = %s) (diff = %#x)"):format(dMem, tostring(allocatedMoreThanNeeded(dMem)), dMem - cMem))
deAlloc(aMem)
deAlloc(bMem)
deAlloc(cMem)
deAlloc(dMem)
end
function testC()
local aMem = allocateKernelMemory(8)
local bMem = allocateKernelMemory(8)
local cMem = allocateKernelMemory(8)
local dMem = allocateKernelMemory(8)
print(("Mem A: %x (allocated more than needed = %s)"):format(aMem, tostring(allocatedMoreThanNeeded(aMem))))
print(("Mem B: %x (allocated more than needed = %s) (diff = %#x)"):format(bMem, tostring(allocatedMoreThanNeeded(bMem)), bMem - aMem))
print(("Mem C: %x (allocated more than needed = %s) (diff = %#x)"):format(cMem, tostring(allocatedMoreThanNeeded(cMem)), cMem - bMem))
print(("Mem D: %x (allocated more than needed = %s) (diff = %#x)"):format(dMem, tostring(allocatedMoreThanNeeded(dMem)), dMem - cMem))
freeKernelMemory(aMem)
freeKernelMemory(bMem)
freeKernelMemory(cMem)
freeKernelMemory(dMem)
end
function testD()
aMem = copyMemory(process, 8, nil, 1)
bMem = copyMemory(process, 8, nil, 1)
cMem = copyMemory(process, 8, nil, 1)
dMem = copyMemory(process, 8, nil, 1)
print(("Mem A: %x (allocated more than needed = %s)"):format(aMem, tostring(allocatedMoreThanNeededLocal(aMem))))
print(("Mem B: %x (allocated more than needed = %s) (diff = %#x)"):format(bMem, tostring(allocatedMoreThanNeededLocal(bMem)), bMem - aMem))
print(("Mem C: %x (allocated more than needed = %s) (diff = %#x)"):format(cMem, tostring(allocatedMoreThanNeededLocal(cMem)), cMem - bMem))
print(("Mem D: %x (allocated more than needed = %s) (diff = %#x)"):format(dMem, tostring(allocatedMoreThanNeededLocal(dMem)), dMem - cMem))
-- idk how to deallocate this
end
function testE()
local aMem = allocateSharedMemory("testingA", 8)
local bMem = allocateSharedMemory("testingB", 8)
local cMem = allocateSharedMemory("testingC", 8)
local dMem = allocateSharedMemory("testingD", 8)
print(("Mem A: %x (allocated more than needed = %s)"):format(aMem, tostring(allocatedMoreThanNeeded(aMem))))
print(("Mem B: %x (allocated more than needed = %s) (diff = %#x)"):format(bMem, tostring(allocatedMoreThanNeeded(bMem)), bMem - aMem))
print(("Mem C: %x (allocated more than needed = %s) (diff = %#x)"):format(cMem, tostring(allocatedMoreThanNeeded(cMem)), cMem - bMem))
print(("Mem D: %x (allocated more than needed = %s) (diff = %#x)"):format(dMem, tostring(allocatedMoreThanNeeded(dMem)), dMem - cMem))
deallocateSharedMemory(aMem)
deallocateSharedMemory(bMem)
deallocateSharedMemory(cMem)
deallocateSharedMemory(dMem)
end
print("Local Shared Memory:")
testA()
print("\nRegular Allocation:")
testB()
print("\nKernel Memory:")
testC()
print("\nCopy Memory:")
testD()
print("\nShared Memory:")
testE()
|
I also tried allocating with an auto assemble script (loaded into cheat table), and it works exactly as I wanted, except for the fact that it uses the target process's memory rather than cheat engine's:
Code: | /* RESULTS
Address 1: 23299870000
Address 2: 23299870008
Address Diff: 8 bytes
*/
[enable]
alloc(mycode,8)
registersymbol(mycode)
alloc(mycode2,8)
registersymbol(mycode2)
{$lua}
local function delayed(func, delay)
if type(func) ~= 'function' then return end
if type(delay) ~= 'number' then delay = 1000 end
local t = createTimer()
t.Interval = delay
t.OnTimer = function(t)
t.destroy()
func()
end
end
delayed(function()
local addr1 = getAddressSafe("mycode")
local addr2 = getAddressSafe("mycode2")
print(("Address 1: %x"):format(addr1 or 0))
print(("Address 2: %x"):format(addr2 or 0))
print(("Address Diff: %s bytes"):format(addr2 - addr1))
end, 100)
{$asm}
[disable]
dealloc(*)
unregistersymbol(*) |
Is there a way to do the same thing in Cheat Engine's memory?
|
|