Skip to content

Commit

Permalink
don't allocate different protection regions in whole new blocks. Adju…
Browse files Browse the repository at this point in the history
…st the protection after alloc
  • Loading branch information
cheat-engine committed Mar 27, 2022
1 parent 776a890 commit 8125188
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions Cheat Engine/autoassembler.pas
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ procedure unregisterAutoAssemblerPrologue(id: integer);

var oldaamessage: boolean;

function autoassemble2(code: tstrings;popupmessages: boolean;syntaxcheckonly:boolean; targetself: boolean; disableinfo: TDisableInfo=nil; memrec: TMemoryRecord=nil):boolean;

implementation

{$ifdef jni}
Expand Down Expand Up @@ -159,7 +161,7 @@ implementation
rsNoPreferedRangeAllocWarning = 'None of the ALLOC statements specify a '
+'prefered address. Did you take into account that the JMP instruction is'
+' going to be 14 bytes long?';
rsFailureAlloc = 'Failure allocating memory near %.8x';
rsFailureAlloc = 'Failure allocating memory near %.8x for variable named %s in script %s';

//type
// TregisteredAutoAssemblerCommands = TFPGList<TRegisteredAutoAssemblerCommand>;
Expand Down Expand Up @@ -3092,15 +3094,28 @@ type tdefine=record
begin
//does this entry have a prefered location or a non default protection

if (allocs[i].prefered<>0) or (allocs[i].protection<>PAGE_EXECUTE_READWRITE) then
if allocs[i].protection<>protection then
begin
//increment x to the next pagebase
if (x and $fff>0) then
begin
y:=$1000- (x and $fff);
inc(x,y);
inc(allocs[i-1].size,y); //adjust the previous entry's size
end;

protection:=allocs[i].protection;
end;

if (allocs[i].prefered<>0) then
begin
//if yes, is it the same as the previous entry? (or was the previous one that doesn't care?)
if prefered=0 then
prefered:=allocs[i].prefered;

if (prefered<>allocs[i].prefered) or (protection<>allocs[i].protection) then
if (prefered<>allocs[i].prefered) then
begin
//different prefered address or protection
//different prefered address

if x>0 then //it has some previous entries with compatible locations
begin
Expand All @@ -3119,7 +3134,7 @@ type tdefine=record
if (prefered=0) and (oldprefered<>0) then
prefered:=oldprefered;

allocs[j].address:=ptrUint(virtualallocex(processhandle,pointer(prefered),x, MEM_RESERVE or MEM_COMMIT,protection));
allocs[j].address:=ptrUint(virtualallocex(processhandle,pointer(prefered),x, MEM_RESERVE or MEM_COMMIT,PAGE_EXECUTE_READWRITE));
if allocs[j].address=0 then
begin
OutputDebugString(rsFailureToAllocateMemory+' 1');
Expand All @@ -3134,7 +3149,7 @@ type tdefine=record

if allocs[j].address=0 then
begin
raise EAssemblerException.create(format(rsFailureAlloc, [prefered]));
raise EAssemblerException.create(format(rsFailureAlloc, [prefered,allocs[j].varname, code.text]));
// if allocs[j].address=0 then

// allocs[j].address:=ptrUint(virtualallocex(processhandle,nil,x, MEM_RESERVE or MEM_COMMIT,protection));
Expand All @@ -3159,6 +3174,7 @@ type tdefine=record

//no prefered location specified, OR same prefered location


inc(x,allocs[i].size);
end; //after the loop

Expand All @@ -3183,7 +3199,7 @@ type tdefine=record
prefered:=oldprefered;


allocs[j].address:=ptrUint(virtualallocex(processhandle,pointer(prefered),x, MEM_RESERVE or MEM_COMMIT,protection));
allocs[j].address:=ptrUint(virtualallocex(processhandle,pointer(prefered),x, MEM_RESERVE or MEM_COMMIT,PAGE_EXECUTE_READWRITE));
if allocs[j].address=0 then
begin
OutputDebugString(rsFailureToAllocateMemory+' 3 (prefered='+inttohex(prefered,8)+')');
Expand All @@ -3196,7 +3212,7 @@ type tdefine=record
allocs[j].address:=lastChanceAllocPrefered(prefered,x, protection);

if allocs[j].address=0 then
raise EAssemblerException.create(format(rsFailureAlloc, [prefered]));
raise EAssemblerException.create(format(rsFailureAlloc, [prefered,allocs[j].varname, code.text]));
// allocs[j].address:=ptrUint(virtualallocex(processhandle,nil,x, MEM_RESERVE or MEM_COMMIT,protection));

if allocs[j].address=0 then raise EAssemblerException.create(rsFailureToAllocateMemory);
Expand All @@ -3206,6 +3222,13 @@ type tdefine=record


end;

//apply protections:
for i:=0 to length(allocs)-1 do
begin
if allocs[i].protection<>PAGE_EXECUTE_READWRITE then
VirtualProtectEx(processhandle, pointer(allocs[i].address), allocs[i].size, allocs[i].protection,protection);
end;
end;

{$ifdef windows}
Expand Down

0 comments on commit 8125188

Please sign in to comment.