Skip to content

Commit

Permalink
add a warning when nearby allocation fails
Browse files Browse the repository at this point in the history
  • Loading branch information
cheat-engine committed Oct 4, 2022
1 parent 7cfe3eb commit 26923f1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
55 changes: 50 additions & 5 deletions Cheat Engine/autoassembler.pas
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ 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
Expand All @@ -84,7 +85,7 @@ implementation
uses simpleaobscanner, StrUtils, LuaHandler, memscan, disassembler{$ifdef windows}, networkInterface{$endif},
networkInterfaceApi, LuaCaller, SynHighlighterAA, Parsers, Globals, memoryQuery,
MemoryBrowserFormUnit, MemoryRecordUnit{$ifdef windows}, vmxfunctions{$endif}, autoassemblerexeptionhandler,
UnexpectedExceptionsHelper, types, autoassemblercode;
UnexpectedExceptionsHelper, types, autoassemblercode, System.UITypes;
{$endif}


Expand Down Expand Up @@ -175,6 +176,36 @@ implementation
AutoAssemblerPrologues: TAutoAssemblerPrologues;
AutoAssemblerProloguesPostAOBSCAN: TAutoAssemblerPrologues;


type
TAllocWarn=class
public
preferedaddress: ptruint;
procedure AllocFailedQuestion;
procedure warn;
end;


procedure TAllocWarn.AllocFailedQuestion;
var r:TModalResult;
begin
r:=MessageDlg('This script uses nearby allocation but it is impossible to allocate nearby '+preferedaddress.ToHexString+'. Please rewrite the script to function without nearby allocation. Try executing the script anyhow and allocate on a region outside reach of 2GB? The target will crash if the script was not designed with this failure in mind', mtError,[mbyes,mbno,mbYesToAll, mbNoToAll],0);
if r in [mryes,mrYesToAll] then NearbyAllocationFailureFatal:=false;

if r=mrYesToAll then
WarnOnNearbyAllocationFailure:=false;
end;

procedure TAllocWarn.warn;
begin
if MainThreadID=GetCurrentThreadId then
AllocFailedQuestion
else
tthread.Synchronize(nil, AllocFailedQuestion);
end;



function registerAutoAssemblerPrologue(m: TAutoAssemblerPrologue; postAOBSCAN: boolean=false): integer;
var i: integer;
prologues: PAutoAssemblerPrologues;
Expand Down Expand Up @@ -3197,11 +3228,25 @@ type tdefine=record

if allocs[j].address=0 then
begin
raise EAssemblerException.create(format(rsFailureAlloc, [prefered,allocs[j].varname, code.text]));
// if allocs[j].address=0 then
if WarnOnNearbyAllocationFailure then
begin
with TAllocWarn.create do
begin
preferedaddress:=prefered;
warn;
free;
end;
end;

// allocs[j].address:=ptrUint(virtualallocex(processhandle,nil,x, MEM_RESERVE or MEM_COMMIT,protection));
// OutputDebugString(rsFailureToAllocateMemory+' 2');
if NearbyAllocationFailureFatal then
raise EAssemblerException.create(format(rsFailureAlloc, [prefered,allocs[j].varname, code.text]))
else
begin
if SystemSupportsWritableExecutableMemory then
allocs[j].address:=ptrUint(virtualallocex(processhandle,nil,x, MEM_RESERVE or MEM_COMMIT,PAGE_EXECUTE_READWRITE))
else
allocs[j].address:=ptrUint(virtualallocex(processhandle,nil,x, MEM_RESERVE or MEM_COMMIT,PAGE_READWRITE));
end;
end;

if allocs[j].address=0 then raise EAssemblerException.create(rsFailureToAllocateMemory);
Expand Down
3 changes: 3 additions & 0 deletions Cheat Engine/globals.pas
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ interface

allocsAddToUnexpectedExceptionList: boolean;

WarnOnNearbyAllocationFailure: boolean=true; //not saved in settings. Inconvenience the user every fucking time they restart (Learn to use 14 byte jmps people)
NearbyAllocationFailureFatal: boolean=true;

{$ifdef darwin}
speedhack_HookMachAbsoluteTime:boolean;
{$endif}
Expand Down

0 comments on commit 26923f1

Please sign in to comment.