Skip to content

Commit d846e70

Browse files
committed
forget_memory opcode added.
1 parent 91e78cd commit d846e70

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- **2001 ([get_script_filename](https://library.sannybuilder.com/#/sa/CLEO/2001))**
1515
- **2002 ([cleo_return_with](https://library.sannybuilder.com/#/sa/CLEO/2002))**
1616
- **2003 ([cleo_return_fail](https://library.sannybuilder.com/#/sa/CLEO/2003))**
17+
- **2004 ([forget_memory](https://library.sannybuilder.com/#/sa/CLEO/2004))**
1718
- 'argument count' parameter of **0AB1 (cleo_call)** is now optional. `cleo_call @LABEL args 0` can be written as `cleo_call @LABEL`
1819
- 'argument count' parameter of **0AB2 (cleo_return)** is now optional. `cleo_return 0` can be written as `cleo_return`
1920
- opcodes **0AAB**, **0AE4**, **0AE5**, **0AE6**, **0AE7** and **0AE8** moved to the [FileSystemOperations](https://github.com/cleolibrary/CLEO5/tree/master/cleo_plugins/FileSystemOperations) plugin

source/CCustomOpcodeSystem.cpp

+52-6
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ namespace CLEO
130130
OpcodeResult __stdcall opcode_2001(CRunningScript* thread); // get_script_filename
131131
OpcodeResult __stdcall opcode_2002(CRunningScript* thread); // cleo_return_with
132132
OpcodeResult __stdcall opcode_2003(CRunningScript* thread); // cleo_return_fail
133+
OpcodeResult __stdcall opcode_2004(CRunningScript* thread); // forget_memory
133134

134135
typedef void(*FuncScriptDeleteDelegateT) (CRunningScript *script);
135136
struct ScriptDeleteDelegate {
@@ -406,6 +407,7 @@ namespace CLEO
406407
CLEO_RegisterOpcode(0x2001, opcode_2001); // get_script_filename
407408
CLEO_RegisterOpcode(0x2002, opcode_2002); // cleo_return_with
408409
CLEO_RegisterOpcode(0x2003, opcode_2003); // cleo_return_fail
410+
CLEO_RegisterOpcode(0x2004, opcode_2004); // forget_memory
409411
}
410412

411413
void CCustomOpcodeSystem::Inject(CCodeInjector& inj)
@@ -2472,10 +2474,21 @@ namespace CLEO
24722474
//0AC8=2,%2d% = allocate_memory_size %1d%
24732475
OpcodeResult __stdcall opcode_0AC8(CRunningScript *thread)
24742476
{
2475-
DWORD size;
2476-
*thread >> size;
2477-
void *mem = malloc(size);
2478-
if (mem) GetInstance().OpcodeSystem.m_pAllocations.insert(mem);
2477+
DWORD size; *thread >> size;
2478+
2479+
void* mem = malloc(size);
2480+
if (mem)
2481+
{
2482+
DWORD oldProtect;
2483+
VirtualProtect(mem, size, PAGE_EXECUTE_READWRITE, &oldProtect);
2484+
2485+
GetInstance().OpcodeSystem.m_pAllocations.insert(mem);
2486+
}
2487+
else
2488+
{
2489+
LOG_WARNING(thread, "[0AC8] failed to allocate of %d bytes memory in script %s", size, ((CCustomScript*)thread)->GetInfoStr().c_str());
2490+
}
2491+
24792492
*thread << mem;
24802493
SetScriptCondResult(thread, mem != nullptr);
24812494
return OR_CONTINUE;
@@ -2484,14 +2497,24 @@ namespace CLEO
24842497
//0AC9=1,free_allocated_memory %1d%
24852498
OpcodeResult __stdcall opcode_0AC9(CRunningScript *thread)
24862499
{
2487-
void *mem;
2488-
*thread >> mem;
2500+
void *mem; *thread >> mem;
2501+
2502+
if ((size_t)mem <= CCustomOpcodeSystem::MinValidAddress)
2503+
{
2504+
SHOW_ERROR("[0AC9] used with invalid '0x%X' pointer argument in script %s\nScript suspended.", mem, ((CCustomScript*)thread)->GetInfoStr().c_str());
2505+
return CCustomOpcodeSystem::ErrorSuspendScript(thread);
2506+
}
2507+
2508+
// allocated with 0AC8
24892509
auto & allocs = GetInstance().OpcodeSystem.m_pAllocations;
24902510
if (allocs.find(mem) != allocs.end())
24912511
{
24922512
free(mem);
24932513
allocs.erase(mem);
2514+
return OR_CONTINUE; // done
24942515
}
2516+
2517+
LOG_WARNING(thread, "[0AC9] used with pointer to unknown or already freed memory in script %s", ((CCustomScript*)thread)->GetInfoStr().c_str());
24952518
return OR_CONTINUE;
24962519
}
24972520

@@ -3109,6 +3132,29 @@ namespace CLEO
31093132
SetScriptCondResult(thread, false);
31103133
return GetInstance().OpcodeSystem.CleoReturnGeneric(0x2003, thread);
31113134
}
3135+
3136+
//2004=1,forget_memory %1d%
3137+
OpcodeResult __stdcall opcode_2004(CRunningScript* thread)
3138+
{
3139+
void* mem; *thread >> mem;
3140+
3141+
if ((size_t)mem <= CCustomOpcodeSystem::MinValidAddress)
3142+
{
3143+
SHOW_ERROR("[2004] used with invalid '0x%X' pointer argument in script %s\nScript suspended.", mem, ((CCustomScript*)thread)->GetInfoStr().c_str());
3144+
return CCustomOpcodeSystem::ErrorSuspendScript(thread);
3145+
}
3146+
3147+
// allocated with 0AC8
3148+
auto& allocs = GetInstance().OpcodeSystem.m_pAllocations;
3149+
if (allocs.find(mem) != allocs.end())
3150+
{
3151+
allocs.erase(mem);
3152+
return OR_CONTINUE; // done
3153+
}
3154+
3155+
LOG_WARNING(thread, "[2004] used with pointer to unknown or already freed memory in script %s", ((CCustomScript*)thread)->GetInfoStr().c_str());
3156+
return OR_CONTINUE;
3157+
}
31123158
}
31133159

31143160

source/CCustomOpcodeSystem.h

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ namespace CLEO
5353
friend OpcodeResult __stdcall opcode_0AA3(CRunningScript *pScript);
5454
friend OpcodeResult __stdcall opcode_0AC8(CRunningScript *pScript);
5555
friend OpcodeResult __stdcall opcode_0AC9(CRunningScript *pScript);
56+
friend OpcodeResult __stdcall opcode_2004(CRunningScript* pScript);
5657

5758
std::set<DWORD> m_hFiles;
5859
std::set<HMODULE> m_hNativeLibs;

0 commit comments

Comments
 (0)