Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


How to (locally) allocate fixed size?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
kojocrash
How do I cheat?
Reputation: 0

Joined: 27 Dec 2022
Posts: 6

PostPosted: Thu Dec 29, 2022 8:54 pm    Post subject: How to (locally) allocate fixed size? Reply with quote

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?
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 457

Joined: 09 May 2003
Posts: 25262
Location: The netherlands

PostPosted: Thu Dec 29, 2022 10:58 pm    Post subject: Reply with quote

Code:

ms=createMemoryStream()
ms.Size=10
printf("locally allocated 10 bytes at %x", ms.Memory)


use ms.destroy() to free the memory

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
kojocrash
How do I cheat?
Reputation: 0

Joined: 27 Dec 2022
Posts: 6

PostPosted: Fri Dec 30, 2022 5:47 am    Post subject: Reply with quote

Thanks!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites