Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce MemoryPointer #2196

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/logic/AMBuilder
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ for cxx in builder.targets:
'DatabaseConfBuilder.cpp',
'LumpManager.cpp',
'smn_entitylump.cpp',
'MemoryPointer.cpp'
]

if binary.compiler.target.arch == 'x86_64':
Expand Down
67 changes: 67 additions & 0 deletions core/logic/MemoryPointer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* vim: set ts=4 sw=4 tw=99 noet :
* =============================================================================
* SourceMod
* Copyright (C) 2024 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/

#include "MemoryPointer.h"
#include <sourcehook.h>
#include <sh_memory.h>
#include <algorithm>

MemoryPointer::MemoryPointer(cell_t size) : m_ptr(malloc(size)), m_owned(true), m_size(size)
{
}

MemoryPointer::MemoryPointer(void* ptr, cell_t size) : m_ptr(ptr), m_owned(false), m_size(size)
{
}

MemoryPointer::~MemoryPointer()
{
if (m_owned && m_ptr)
{
free(m_ptr);
m_ptr = nullptr;
}
Kenzzer marked this conversation as resolved.
Show resolved Hide resolved
}

void MemoryPointer::Delete()
{
delete this;
}

void* MemoryPointer::Get()
{
return m_ptr;
}

cell_t MemoryPointer::GetSize()
{
return m_size;
}
54 changes: 54 additions & 0 deletions core/logic/MemoryPointer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* vim: set ts=4 sw=4 tw=99 noet :
* =============================================================================
* SourceMod
* Copyright (C) 2024 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/

#pragma once

#include <memory>
#include <cstdint>
#include <sp_vm_types.h>
#include <IMemoryPointer.h>

class MemoryPointer : public SourceMod::IMemoryPointer
{
public:
MemoryPointer(cell_t size);
MemoryPointer(void* ptr, cell_t size);

// SourceMod::IMemoryPointer
virtual ~MemoryPointer();
virtual void Delete() override;
virtual void* Get() override;
virtual cell_t GetSize() override;
protected:
void* m_ptr;
bool m_owned;
cell_t m_size;
};
187 changes: 186 additions & 1 deletion core/logic/smn_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <ITranslator.h>
#include <DebugReporter.h>
#include <FrameIterator.h>
#include <MemoryPointer.h>

#include <sourcehook.h>
#include <sh_memory.h>
Expand All @@ -67,6 +68,7 @@ using namespace SourcePawn;

HandleType_t g_PlIter;
HandleType_t g_FrameIter;
HandleType_t g_MemoryPtr;

IForward *g_OnLogAction = NULL;

Expand All @@ -86,6 +88,14 @@ class CoreNativeHelpers :
g_PlIter = handlesys->CreateType("PluginIterator", this, 0, NULL, NULL, g_pCoreIdent, NULL);
g_FrameIter = handlesys->CreateType("FrameIterator", this, 0, NULL, NULL, g_pCoreIdent, NULL);

HandleAccess mp_hacc;
TypeAccess mp_tacc;
mp_hacc.access[HandleAccess_Read] = 0;
mp_hacc.access[HandleAccess_Delete] = HANDLE_RESTRICT_OWNER;
mp_hacc.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
mp_tacc.access[HTypeAccess_Create] = true;
g_MemoryPtr = handlesys->CreateType("MemoryPointer", this, 0, &mp_tacc, &mp_hacc, g_pCoreIdent, NULL);

g_OnLogAction = forwardsys->CreateForward("OnLogAction",
ET_Hook,
5,
Expand All @@ -100,7 +110,11 @@ class CoreNativeHelpers :
}
void OnHandleDestroy(HandleType_t type, void *object)
{
if (type == g_FrameIter)
if (type == g_MemoryPtr)
{
((IMemoryPointer *)object)->Delete();
Kenzzer marked this conversation as resolved.
Show resolved Hide resolved
}
else if (type == g_FrameIter)
{
delete (SafeFrameIterator *) object;
}
Expand All @@ -115,6 +129,7 @@ class CoreNativeHelpers :
forwardsys->ReleaseForward(g_OnLogAction);
handlesys->RemoveType(g_PlIter, g_pCoreIdent);
handlesys->RemoveType(g_FrameIter, g_pCoreIdent);
handlesys->RemoveType(g_MemoryPtr, g_pCoreIdent);
}
} g_CoreNativeHelpers;

Expand Down Expand Up @@ -971,6 +986,169 @@ static cell_t IsNullString(IPluginContext *pContext, const cell_t *params)
return str == nullptr;
}

static cell_t MemoryPointer_Create(IPluginContext *pContext, const cell_t *params)
{
auto ptr = new MemoryPointer(params[1]);

Handle_t handle = handlesys->CreateHandle(g_MemoryPtr, ptr, pContext->GetIdentity(), g_pCoreIdent, NULL);
if (handle == BAD_HANDLE)
{
delete ptr;
return BAD_HANDLE;
}

return handle;
}

static cell_t MemoryPointer_Store(IPluginContext *pContext, const cell_t *params)
{
Handle_t hndl = (Handle_t)params[1];
HandleError err;
IMemoryPointer *ptr;

HandleSecurity sec;
sec.pIdentity = g_pCoreIdent;
sec.pOwner = pContext->GetIdentity();

if ((err=handlesys->ReadHandle(hndl, g_MemoryPtr, &sec, (void **)&ptr)) != HandleError_None)
{
return pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
}

unsigned int bytesSize = 0;
switch (params[3])
{
case NumberType_Int8:
bytesSize = sizeof(std::uint8_t);
break;
case NumberType_Int16:
bytesSize = sizeof(std::uint16_t);
break;
case NumberType_Int32:
bytesSize = sizeof(std::uint32_t);
break;
default:
return pContext->ThrowNativeError("Invalid number types %d", params[3]);
}

ptr->Store(params[2], bytesSize, params[4], params[5] != 0);

return 0;
}

static cell_t MemoryPointer_Load(IPluginContext *pContext, const cell_t *params)
{
Handle_t hndl = (Handle_t)params[1];
HandleError err;
IMemoryPointer *ptr;

HandleSecurity sec;
sec.pIdentity = g_pCoreIdent;
sec.pOwner = pContext->GetIdentity();

if ((err=handlesys->ReadHandle(hndl, g_MemoryPtr, &sec, (void **)&ptr)) != HandleError_None)
{
return pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
}

unsigned int bytesSize = 0;
switch (params[2])
{
case NumberType_Int8:
bytesSize = sizeof(std::uint8_t);
break;
case NumberType_Int16:
bytesSize = sizeof(std::uint16_t);
break;
case NumberType_Int32:
bytesSize = sizeof(std::uint32_t);
break;
default:
return pContext->ThrowNativeError("Invalid number types %d", params[2]);
}

return ptr->Load(bytesSize, params[3]);
}

static cell_t MemoryPointer_StoreMemoryPointer(IPluginContext *pContext, const cell_t *params)
{
Handle_t hndl = (Handle_t)params[1];
HandleError err;
IMemoryPointer *ptr;

HandleSecurity sec;
sec.pIdentity = g_pCoreIdent;
sec.pOwner = pContext->GetIdentity();

if ((err=handlesys->ReadHandle(hndl, g_MemoryPtr, &sec, (void **)&ptr)) != HandleError_None)
{
return pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
}

hndl = (Handle_t)params[2];
IMemoryPointer *store;
if ((err=handlesys->ReadHandle(hndl, g_MemoryPtr, &sec, (void **)&store)) != HandleError_None)
{
return pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
}

ptr->StorePtr(store->Get(), params[3], params[4] != 0);
return 0;
}

static cell_t MemoryPointer_LoadMemoryPointer(IPluginContext *pContext, const cell_t *params)
{
Handle_t hndl = (Handle_t)params[1];
HandleError err;
IMemoryPointer *ptr;

HandleSecurity sec;
sec.pIdentity = g_pCoreIdent;
sec.pOwner = pContext->GetIdentity();

if ((err=handlesys->ReadHandle(hndl, g_MemoryPtr, &sec, (void **)&ptr)) != HandleError_None)
{
return pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
}

auto newPtr = new MemoryPointer(ptr->LoadPtr(params[2]), 0);

Handle_t newHandle = handlesys->CreateHandle(g_MemoryPtr, newPtr, pContext->GetIdentity(), g_pCoreIdent, NULL);
if (newHandle == BAD_HANDLE)
{
delete newPtr;
return BAD_HANDLE;
}

return newHandle;
}

static cell_t MemoryPointer_Offset(IPluginContext *pContext, const cell_t *params)
{
Handle_t hndl = (Handle_t)params[1];
HandleError err;
IMemoryPointer *ptr;

HandleSecurity sec;
sec.pIdentity = g_pCoreIdent;
sec.pOwner = pContext->GetIdentity();

if ((err=handlesys->ReadHandle(hndl, g_MemoryPtr, &sec, (void **)&ptr)) != HandleError_None)
{
return pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
}

auto newPtr = new MemoryPointer((void*)(((intptr_t)ptr->Get()) + params[2]), 0);
Handle_t newHandle = handlesys->CreateHandle(g_MemoryPtr, newPtr, pContext->GetIdentity(), g_pCoreIdent, NULL);
if (newHandle == BAD_HANDLE)
{
delete newPtr;
return BAD_HANDLE;
}

return newHandle;
}

static cell_t FrameIterator_Create(IPluginContext *pContext, const cell_t *params)
{
IFrameIterator *it = pContext->CreateFrameIterator();
Expand Down Expand Up @@ -1160,6 +1338,13 @@ REGISTER_NATIVES(coreNatives)
{"IsNullVector", IsNullVector},
{"IsNullString", IsNullString},
{"LogStackTrace", LogStackTrace},

{"MemoryPointer.MemoryPointer", MemoryPointer_Create},
{"MemoryPointer.Store", MemoryPointer_Store},
{"MemoryPointer.Load", MemoryPointer_Load},
{"MemoryPointer.StoreMemoryPointer", MemoryPointer_StoreMemoryPointer},
{"MemoryPointer.LoadMemoryPointer", MemoryPointer_LoadMemoryPointer},
{"MemoryPointer.Offset", MemoryPointer_Offset},

{"FrameIterator.FrameIterator", FrameIterator_Create},
{"FrameIterator.Next", FrameIterator_Next},
Expand Down
Loading