Skip to content

Commit

Permalink
core 1.0.2.0
Browse files Browse the repository at this point in the history
add chakraUtil for detach makefunc
  • Loading branch information
karikera committed Feb 23, 2021
1 parent 5c6b238 commit e7e878d
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 1,900 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ It's the core for [bdsx](https://github.com/bdsx/bdsx)
├ ken (https://github.com/karikera/ken) - personal library project
└ bdsx-core (https://github.com/bdsx/bdsx-core)

2. Update git submodules.
2. Update git submodules for each project.

3. Build bdsx.sln with Visual Studio 2019.

Expand Down
9 changes: 2 additions & 7 deletions bdsx/bdsx.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ echo #define BUILD_TIME "%25date%-%time2:.=%"&gt;"gen/buildtime.h"</Command>
<ClCompile Include="sehandler.cpp" />
<ClCompile Include="voidpointer.cpp" />
<ClCompile Include="jshook.cpp" />
<ClCompile Include="makefunc.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="jsctx.cpp" />
<ClCompile Include="nativepointer.cpp" />
Expand All @@ -171,7 +170,6 @@ echo #define BUILD_TIME "%25date%-%time2:.=%"&gt;"gen/buildtime.h"</Command>
<ClInclude Include="sehandler.h" />
<ClInclude Include="voidpointer.h" />
<ClInclude Include="jshook.h" />
<ClInclude Include="makefunc.h" />
<ClInclude Include="jsctx.h" />
<ClInclude Include="nativepointer.h" />
<ClInclude Include="netfilter.h" />
Expand All @@ -194,21 +192,18 @@ echo #define BUILD_TIME "%25date%-%time2:.=%"&gt;"gen/buildtime.h"</Command>
<ItemGroup>
<ResourceCompile Include="bdsx.rc" />
</ItemGroup>
<ItemGroup>
<MASM Include="getout.asm" />
</ItemGroup>
<ItemGroup>
<CustomBuild Include="version.bat">
<FileType>Document</FileType>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">gen\version.h;$(SolutionDir)..\bdsx\bdsx\version.json</Outputs>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">gen\version.h;$(SolutionDir)..\bdsx\bdsx\version-bdsx.json</Outputs>
<Outputs Condition="'$(Configuration)|$(Platform)'=='ReleaseTest|x64'">gen\version.h;$(SolutionDir)..\bdsx\bdsx\version.json</Outputs>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">gen\version.h;$(SolutionDir)..\bdsx\bdsx\version.json</Outputs>
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">@call "%(Identity)"

@set resver=%25BDSX_CORE_VERSION:.=,%
@echo #define BDSX_CORE_VERSION "%25BDSX_CORE_VERSION%"&gt;gen\version.h
@echo #define BDSX_CORE_VERSION_RES %resver%&gt;&gt;gen\version.h
@echo {"coreVersion":"%25BDSX_CORE_VERSION%"}&gt;"$(SolutionDir)..\bdsx\bdsx\version.json"
@echo "%25BDSX_CORE_VERSION%"&gt;"$(SolutionDir)..\bdsx\bdsx\version-bdsx.json"
</Command>
<Command Condition="'$(Configuration)|$(Platform)'=='ReleaseTest|x64'">@call "%(Identity)"

Expand Down
5 changes: 0 additions & 5 deletions bdsx/bdsx.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<ClCompile Include="nodegate.cpp" />
<ClCompile Include="stdafx.cpp" />
<ClCompile Include="netfilter.cpp" />
<ClCompile Include="makefunc.cpp" />
<ClCompile Include="jshook.cpp" />
<ClCompile Include="cachedpdb.cpp" />
<ClCompile Include="voidpointer.cpp" />
Expand All @@ -27,7 +26,6 @@
<ClInclude Include="nodegate.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="netfilter.h" />
<ClInclude Include="makefunc.h" />
<ClInclude Include="jshook.h" />
<ClInclude Include="cachedpdb.h" />
<ClInclude Include="voidpointer.h" />
Expand All @@ -48,9 +46,6 @@
<ItemGroup>
<ResourceCompile Include="bdsx.rc" />
</ItemGroup>
<ItemGroup>
<MASM Include="getout.asm" />
</ItemGroup>
<ItemGroup>
<CustomBuild Include="version.bat" />
</ItemGroup>
Expand Down
20 changes: 0 additions & 20 deletions bdsx/getout.asm

This file was deleted.

149 changes: 137 additions & 12 deletions bdsx/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "staticpointer.h"
#include "nativepointer.h"
#include "structurepointer.h"
#include "makefunc.h"
#include "cachedpdb.h"
#include "sehandler.h"
#include "jsctx.h"
Expand All @@ -25,6 +24,9 @@
#include <shellapi.h>
#include <DbgHelp.h>

#define USE_EDGEMODE_JSRT
#include <jsrt.h>

#undef main

using namespace kr;
Expand Down Expand Up @@ -64,9 +66,6 @@ Initializer<Socket> __init;

int nodeStart(int argc, char** argv) noexcept
{
size_t a = 0;
uint32_t v = (uint32_t)((a >> 32) ^ a);

__try
{
return nodegate::start(argc, argv);
Expand Down Expand Up @@ -189,9 +188,9 @@ BOOL WINAPI DllMain(

namespace
{
void tester(void* sharedptr, int value) noexcept
float tester(double a, float b, int64_t c) noexcept
{
int a = 0;
return (float)a;
}

void trycatch(void* param, void(*func)(void*), void(*_catch)(void*, pcstr)) noexcept
Expand Down Expand Up @@ -242,8 +241,85 @@ namespace
{
throw str;
}
void throwJsErrorCode(JsErrorCode err) throws(kr::JsException)
{
throw kr::JsException(kr::TSZ16() << u"JsErrorCode: 0x" << kr::hexf((int)err));
}

namespace stackutil
{
uintptr_t last_allocate = 1;
void* stack_alloc(size_t size) noexcept
{
StackAllocator* alloc = StackAllocator::getInstance();
void* p = alloc->allocate(8 + size);
*(uintptr_t*)p = last_allocate;
last_allocate = (uintptr_t)p;
return (byte*)p + 8;
}
void stack_free_all() noexcept
{
uintptr_t p = last_allocate;
if (p & 1) return;

StackAllocator* alloc = StackAllocator::getInstance();
for (;;)
{
void* orip = (void*)(p & ~1);
p = *(uintptr_t*)orip;
alloc->free(orip);
if (p & 1)
{
last_allocate = p;
break;
}
}
}

char* to_ansi(const char16_t* str, size_t size) noexcept
{
Utf16ToAnsi dec = Text16(str, size);
size_t outsize = dec.size();
char* out = (char*)stack_alloc(outsize + 1);
dec.copyTo(out);
out[outsize] = '\0';
return out;
}

char* to_utf8(const char16_t* str, size_t size) noexcept
{
Utf16ToUtf8 dec = Text16(str, size);
size_t outsize = dec.size();
char* out = (char*)stack_alloc(outsize + 1);
dec.copyTo(out);
out[outsize] = '\0';

return out;
}

void* pointer_js2class(JsValueRef value) noexcept
{
return JsValue((JsRawData)value).getNativeObject<VoidPointer>();
}

JsErrorCode from_ansi(pcstr str, JsValueRef* out) noexcept
{
TSZ16 buf;
buf << (AnsiToUtf16)(Text)str;
return JsPointerToString(wide(buf.data()), buf.size(), out);
}

JsErrorCode from_utf8(pcstr str, JsValueRef* out) noexcept
{
TSZ16 buf;
buf << (Utf8ToUtf16)(Text)str;
return JsPointerToString(wide(buf.data()), buf.size(), out);
}

}
}


void nodegate::initNativeModule(void* exports_raw) noexcept
{
g_ctx.create();
Expand Down Expand Up @@ -275,23 +351,73 @@ void nodegate::initNativeModule(void* exports_raw) noexcept
exports.set(u"cgate", cgate);

cgate.set(u"bdsxCoreVersion", CONCAT(u, BDSX_CORE_VERSION));
cgate.set(u"GetProcAddress", VoidPointer::make(GetProcAddress));
cgate.set(u"GetModuleHandleW", VoidPointer::make(GetModuleHandleW));
cgate.set(u"GetProcAddressPtr", VoidPointer::make(GetProcAddress));
cgate.setMethod(u"GetProcAddress", [](VoidPointer* module, Text16 name) {
return VoidPointer::make(GetProcAddress((HMODULE)module->getAddressRaw(), TSZ() << (Utf16ToUtf8)name));
});
cgate.setMethod(u"GetProcAddressByOrdinal", [](VoidPointer* module, int ordinal) {
return VoidPointer::make(GetProcAddress((HMODULE)module->getAddressRaw(), (LPCSTR)(intptr_t)ordinal));
});
cgate.set(u"GetModuleHandleWPtr", VoidPointer::make(GetModuleHandleW));
cgate.setMethod(u"GetModuleHandleW", [](JsValue name) {
return VoidPointer::make(GetModuleHandleW(name == nullptr ? nullptr : wide(name.cast<Text16>().data())));
});
cgate.setMethod(u"nodeLoopOnce", nodegate::loopOnce);
cgate.set(u"nodeLoop", VoidPointer::make(nodegate::loop));
cgate.set(u"tester", VoidPointer::make(tester));

cgate.setMethod(u"allocExecutableMemory", [](uint64_t size) {
cgate.setMethod(u"allocExecutableMemory", [](uint64_t size, uint64_t align) {
hook::ExecutableAllocator* alloc = hook::ExecutableAllocator::getInstance();
StaticPointer* ptr = StaticPointer::newInstance();
ptr->setAddressRaw(alloc->alloc(size));
ptr->setAddressRaw(alloc->alloc(size, max(align, 1)));
return ptr;
});
}

{
JsValue chakraUtil = JsNewObject;
exports.set(u"chakraUtil", chakraUtil);

chakraUtil.set(u"stack_alloc", VoidPointer::make(stackutil::stack_alloc));
chakraUtil.set(u"stack_free_all", VoidPointer::make(stackutil::stack_free_all));
chakraUtil.set(u"stack_ptr", VoidPointer::make(&stackutil::last_allocate));
chakraUtil.set(u"stack_utf8", VoidPointer::make(stackutil::to_utf8));
chakraUtil.set(u"stack_ansi", VoidPointer::make(stackutil::to_ansi));
chakraUtil.set(u"from_utf8", VoidPointer::make(stackutil::from_utf8));
chakraUtil.set(u"from_ansi", VoidPointer::make(stackutil::from_ansi));
chakraUtil.set(u"pointer_js2class", VoidPointer::make(stackutil::pointer_js2class));
chakraUtil.setMethodRaw(u"JsCreateFunction", [](const JsArguments& args)->JsValue {
VoidPointer* fnptr = args.at<VoidPointer*>(0);
if (fnptr == nullptr) throw JsException(u"Need *Pointer for the first parameter");
VoidPointer* paramptr = args.at<VoidPointer*>(1);

JsValueRef func;
JsErrorCode err = JsCreateFunction(
(JsNativeFunction)fnptr->getAddressRaw(),
paramptr->getAddressRawSafe(),
&func);

return (JsValue)(JsRawData)func;
});
chakraUtil.setMethodRaw(u"JsAddRef", [](const JsArguments& args)->JsValue {
unsigned int old;
JsErrorCode err = JsAddRef(args.at<JsValue>(0).getRaw(), &old);
if (err != JsNoError) throwJsErrorCode(err);
return old;
});
chakraUtil.setMethodRaw(u"JsRelease", [](const JsArguments& args)->JsValue {
unsigned int old;
JsErrorCode err = JsRelease(args.at<JsValue>(0).getRaw(), &old);
if (err != JsNoError) throwJsErrorCode(err);
return old;
});
chakraUtil.setMethodRaw(u"asJsValueRef", [](const JsArguments& args)->JsValue {
return VoidPointer::make(args.at<JsValue>(0).getRaw());
});
}

exports.set(u"ipfilter", getNetFilterNamespace());
exports.set(u"jshook", getJsHookNamespace());
exports.set(u"makefunc", getMakeFuncNamespace());
exports.set(u"runtimeError", runtimeError::getNamespace());
exports.set(u"pdb", getPdbNamespace());
exports.set(u"uv_async", getUvAsyncNamespace());
Expand All @@ -302,7 +428,6 @@ void nodegate::initNativeModule(void* exports_raw) noexcept
obj.set(u"cxxthrow", VoidPointer::make(cxxthrow));
obj.set(u"cxxthrowString", VoidPointer::make(cxxthrowString));
exports.set(u"cxxException", obj);

}
}
s_globalScope.create();
Expand Down
Loading

0 comments on commit e7e878d

Please sign in to comment.