Skip to content

Commit

Permalink
add base vmp
Browse files Browse the repository at this point in the history
  • Loading branch information
Blaukovitch committed Aug 13, 2024
1 parent 1c6d1f3 commit 787dbd9
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
29 changes: 29 additions & 0 deletions client/src/CV8Resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include "JSBindings.h"


extern void StaticRequire(const v8::FunctionCallbackInfo<v8::Value>& info)
{
V8_GET_ISOLATE_CONTEXT_RESOURCE();
Expand All @@ -37,6 +38,11 @@ extern void StaticRequire(const v8::FunctionCallbackInfo<v8::Value>& info)

IImportHandler* handler = nullptr;
bool isWorker = *static_cast<bool*>(isolate->GetData(isolate->GetNumberOfDataSlots() - 1));

#ifdef NDEBUG
VMProtectBeginMutation("StaticRequire");
#endif

if (isWorker)
{
auto worker = static_cast<CWorker*>(ctx->GetAlignedPointerFromEmbedderData(2));
Expand All @@ -50,6 +56,9 @@ extern void StaticRequire(const v8::FunctionCallbackInfo<v8::Value>& info)
if (!_exports.IsEmpty()) info.GetReturnValue().Set(_exports.ToLocalChecked());
else
V8Helpers::Throw(isolate, "No such module " + name);
#ifdef NDEBUG
VMProtectEnd();
#endif
}

void StartFile(const v8::FunctionCallbackInfo<v8::Value>& info)
Expand All @@ -69,12 +78,18 @@ void StartFile(const v8::FunctionCallbackInfo<v8::Value>& info)

void CV8ResourceImpl::ProcessDynamicImports()
{
#ifdef NDEBUG
VMProtectBeginMutation("ProcessDynamicImports");
#endif
if (dynamicImports.empty()) return;
for (auto& importFn : dynamicImports)
{
importFn();
}
dynamicImports.clear();
#ifdef NDEBUG
VMProtectEnd();
#endif
}

extern std::string bootstrap_code =
Expand Down Expand Up @@ -234,6 +249,10 @@ bool CV8ResourceImpl::Stop()

void CV8ResourceImpl::OnEvent(const alt::CEvent* e)
{
#ifdef NDEBUG
VMProtectBeginMutation("CV8ResourceImpl::OnEvent");
#endif

auto nscope = resource->PushNativesScope();

v8::Locker locker(isolate);
Expand Down Expand Up @@ -308,6 +327,9 @@ void CV8ResourceImpl::OnEvent(const alt::CEvent* e)
runtime.OnDisconnect();
}
}
#ifdef NDEBUG
VMProtectEnd();
#endif
}

void CV8ResourceImpl::HandleRPCAnswer(const alt::CScriptRPCAnswerEvent* ev)
Expand Down Expand Up @@ -463,6 +485,9 @@ std::vector<V8Helpers::EventCallback*> CV8ResourceImpl::GetRmlHandlers(alt::IRml

void CV8ResourceImpl::OnTick()
{
#ifdef NDEBUG
VMProtectBeginMutation("CV8ResourceImpl::OnTick");
#endif
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
Expand Down Expand Up @@ -526,6 +551,10 @@ void CV8ResourceImpl::OnTick()
}

promiseRejections.ProcessQueue(this);

#ifdef NDEBUG
VMProtectEnd();
#endif
}

void CV8ResourceImpl::OnPromiseRejectedWithNoHandler(v8::PromiseRejectMessage& data)
Expand Down
57 changes: 57 additions & 0 deletions client/src/IImportHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@

static inline v8::MaybeLocal<v8::Module> CompileESM(v8::Isolate* isolate, const std::string& name, const std::string& src)
{
#ifdef NDEBUG
VMProtectBeginMutation("CompileESM");
#endif

v8::Local<v8::String> sourceCode = V8Helpers::JSValue(src);

v8::ScriptOrigin scriptOrigin(isolate, V8Helpers::JSValue(name), 0, 0, false, -1, v8::Local<v8::Value>(), false, false, true, v8::Local<v8::PrimitiveArray>());

v8::ScriptCompiler::Source source{ sourceCode, scriptOrigin };
return v8::ScriptCompiler::CompileModule(isolate, &source);
#ifdef NDEBUG
VMProtectEnd();
#endif
}

static inline bool IsSystemModule(v8::Isolate* isolate, const std::string& name)
Expand Down Expand Up @@ -110,25 +117,43 @@ std::string IImportHandler::GetModulePath(v8::Local<v8::Module> moduleHandle)
const IImportHandler::ModuleData IImportHandler::GetModuleData(const std::string& name)
{
auto result = modules.find(name);

#ifdef NDEBUG
VMProtectBeginMutation("IImportHandler::GetModuleData");
#endif

if(result == modules.end()) return ModuleData{};
return result->second;
#ifdef NDEBUG
VMProtectEnd();
#endif
}

v8::Local<v8::Module> IImportHandler::GetModuleFromPath(std::string modulePath)
{
v8::Isolate* isolate = v8::Isolate::GetCurrent();
#ifdef NDEBUG
VMProtectBeginMutation("IImportHandler::GetModuleFromPath");
#endif
for(auto& md : modules)
{
if(md.first == modulePath) return md.second.mod.Get(isolate);
}

return v8::Local<v8::Module>{};
#ifdef NDEBUG
VMProtectEnd();
#endif
}

v8::MaybeLocal<v8::Value> IImportHandler::Require(const std::string& name)
{
v8::Isolate* isolate = v8::Isolate::GetCurrent();
auto it = requiresMap.find(name);

#ifdef NDEBUG
VMProtectBeginMutation("Require");
#endif
if(it != requiresMap.end()) return it->second.Get(isolate);

auto v8module = V8Module::All()[isolate].find(name);
Expand All @@ -151,13 +176,20 @@ v8::MaybeLocal<v8::Value> IImportHandler::Require(const std::string& name)
}

return v8::MaybeLocal<v8::Value>();
#ifdef NDEBUG
VMProtectEnd();
#endif
}

v8::MaybeLocal<v8::Module> IImportHandler::ResolveFile(const std::string& name, v8::Local<v8::Module> referrer, alt::IResource* resource)
{
v8::Isolate* isolate = v8::Isolate::GetCurrent();
auto path = alt::ICore::Instance().Resolve(resource, name, GetModulePath(referrer));

#ifdef NDEBUG
VMProtectBeginMutation("ResolveFile");
#endif

if(!path.pkg) return v8::MaybeLocal<v8::Module>();

std::string fileName = path.fileName;
Expand Down Expand Up @@ -208,6 +240,10 @@ v8::MaybeLocal<v8::Module> IImportHandler::ResolveFile(const std::string& name,
}

return maybeModule;

#ifdef NDEBUG
VMProtectEnd();
#endif
}

v8::MaybeLocal<v8::Module> IImportHandler::ResolveModule(const std::string& _name, v8::Local<v8::Module> referrer, alt::IResource* resource)
Expand All @@ -217,6 +253,10 @@ v8::MaybeLocal<v8::Module> IImportHandler::ResolveModule(const std::string& _nam

std::string name = _name;

#ifdef NDEBUG
VMProtectBeginMutation("ResolveModule");
#endif

if(name.starts_with("alt:")) name = name.substr(4);
if(name == "alt-client") name = "alt";

Expand Down Expand Up @@ -277,25 +317,39 @@ v8::MaybeLocal<v8::Module> IImportHandler::ResolveModule(const std::string& _nam
}*/

return maybeModule;
#ifdef NDEBUG
VMProtectEnd();
#endif
}

v8::MaybeLocal<v8::Module> IImportHandler::ResolveCode(const std::string& name, const std::string& code, const V8Helpers::SourceLocation& location)
{
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::MaybeLocal<v8::Module> maybeModule;
std::stringstream nameStream;
#ifdef NDEBUG
VMProtectBeginMutation("Import::ResolveCode");
#endif

if(name.empty()) nameStream << "[module " << location.GetFileName() << ":" << location.GetLineNumber() << "]";
else
nameStream << name;
maybeModule = CompileESM(isolate, nameStream.str(), code);

return maybeModule;
#ifdef NDEBUG
VMProtectEnd();
#endif
}

v8::MaybeLocal<v8::Module> IImportHandler::ResolveBytecode(const std::string& name, uint8_t* buffer, size_t size)
{
v8::Isolate* isolate = v8::Isolate::GetCurrent();

#ifdef NDEBUG
VMProtectBeginMutation("ResolveBytecode");
#endif

// Copy source code size
int sourceCodeSize = 0;
memcpy(&sourceCodeSize, buffer + sizeof(bytecodeMagic), sizeof(int));
Expand Down Expand Up @@ -325,4 +379,7 @@ v8::MaybeLocal<v8::Module> IImportHandler::ResolveBytecode(const std::string& na
}
else
return module;
#ifdef NDEBUG
VMProtectEnd();
#endif
}
2 changes: 2 additions & 0 deletions shared/V8Class.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include "Log.h"

#include "../../vmp/VMProtectSDK.h"

class V8Class
{
using InitCallback = std::function<void(v8::Local<v8::FunctionTemplate>)>;
Expand Down
7 changes: 7 additions & 0 deletions shared/V8ResourceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@ using namespace alt;
extern V8Class v8Vector3, v8Vector2, v8RGBA, v8BaseObject, v8Quaternion;
bool V8ResourceImpl::Start()
{
#ifdef NDEBUG
VMProtectBeginMutation("V8ResourceImpl::Start");
#endif

vector3Class.Reset(isolate, v8Vector3.JSValue(isolate, GetContext()));
vector2Class.Reset(isolate, v8Vector2.JSValue(isolate, GetContext()));
quaternionClass.Reset(isolate, v8Quaternion.JSValue(isolate, GetContext()));
rgbaClass.Reset(isolate, v8RGBA.JSValue(isolate, GetContext()));
baseObjectClass.Reset(isolate, v8BaseObject.JSValue(isolate, GetContext()));

return true;
#ifdef NDEBUG
VMProtectEnd();
#endif
}

bool V8ResourceImpl::Stop()
Expand Down

0 comments on commit 787dbd9

Please sign in to comment.