From 363459096b396d3c9c66b92f36088fa48e4b8b90 Mon Sep 17 00:00:00 2001
From: hzqst <113660872@qq.com>
Date: Sat, 1 Jun 2024 18:13:40 +0800
Subject: [PATCH 1/5] 1. Check the existence of "drvobj" before accessing
"drvobj->DriverUnload", to support driverless loader. 2. Remove unused
"__drvobj".
---
src/crt/vcruntime/sys_common.inl | 7 ++++---
src/ucrt/startup/exit.cpp | 3 ++-
2 files changed, 6 insertions(+), 4 deletions(-)
diff --git a/src/crt/vcruntime/sys_common.inl b/src/crt/vcruntime/sys_common.inl
index de97dbd..e2c6ad8 100644
--- a/src/crt/vcruntime/sys_common.inl
+++ b/src/crt/vcruntime/sys_common.inl
@@ -67,8 +67,9 @@ _CRTALLOC(".CRT$XIAA") static _PIFV pre_c_initializer = pre_c_initialization;
_CRTALLOC(".CRT$XCAA") static _PVFV pre_cpp_initializer = pre_cpp_initialization;
-extern PDRIVER_OBJECT __drvobj = nullptr;
+//extern PDRIVER_OBJECT __drvobj = nullptr;
static PDRIVER_UNLOAD __drv_unload = nullptr;
+
static __declspec(noinline) void __scrt_common_exit(PDRIVER_OBJECT drvobj)
{
if (__drv_unload && __drv_unload != &__scrt_common_exit)
@@ -83,7 +84,7 @@ static __declspec(noinline) void __scrt_common_exit(PDRIVER_OBJECT drvobj)
static __declspec(noinline) long __cdecl __scrt_common_main_seh(PDRIVER_OBJECT drvobj, PUNICODE_STRING regpath)
{
- __drvobj = drvobj;
+// __drvobj = drvobj;
if (!__scrt_initialize_crt())
__scrt_fastfail(FAST_FAIL_FATAL_APP_EXIT);
@@ -102,7 +103,7 @@ static __declspec(noinline) long __cdecl __scrt_common_main_seh(PDRIVER_OBJECT d
long const main_result = invoke_main(drvobj, regpath);
if (NT_SUCCESS(main_result))
{
- if (drvobj->DriverUnload)
+ if (drvobj && drvobj->DriverUnload)
{
__drv_unload = drvobj->DriverUnload;
drvobj->DriverUnload = &__scrt_common_exit;
diff --git a/src/ucrt/startup/exit.cpp b/src/ucrt/startup/exit.cpp
index 6b559db..a058b29 100644
--- a/src/ucrt/startup/exit.cpp
+++ b/src/ucrt/startup/exit.cpp
@@ -31,7 +31,8 @@ static int __cdecl atexit_exception_filter(unsigned long const _exception_code)
return EXCEPTION_CONTINUE_SEARCH;
}
-extern PDRIVER_OBJECT __drvobj;
+//extern PDRIVER_OBJECT __drvobj;
+
static void __cdecl common_exit(
int const /*return_code*/,
_crt_exit_cleanup_mode const cleanup_mode,
From 2374e94439b1470961378dd6b73295d71ea3fd0c Mon Sep 17 00:00:00 2001
From: hzqst <113660872@qq.com>
Date: Sat, 1 Jun 2024 18:23:25 +0800
Subject: [PATCH 2/5] Add support for PlatformToolsets:
"LLVM-MSVC_v141_KernelMode", "LLVM-MSVC_v142_KernelMode" and
"LLVM-MSVC_v143_KernelMode"
---
src/ucxxrt.props | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/src/ucxxrt.props b/src/ucxxrt.props
index 2133abb..3f12361 100644
--- a/src/ucxxrt.props
+++ b/src/ucxxrt.props
@@ -42,7 +42,10 @@
false
- true
+ true
From b715846f0287b35b979a6c0adaba85604a4a3ad7 Mon Sep 17 00:00:00 2001
From: hzqst <113660872@qq.com>
Date: Mon, 24 Jun 2024 21:26:07 +0800
Subject: [PATCH 3/5] Add LLVM-MSVC_v143_KernelMode support for ucxxrtlib.props
---
src/ucxxrtlib.props | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/src/ucxxrtlib.props b/src/ucxxrtlib.props
index 0361775..852bb60 100644
--- a/src/ucxxrtlib.props
+++ b/src/ucxxrtlib.props
@@ -44,7 +44,10 @@ xcopy /D /S /Y /V /F "$(SolutionDir)lib" "$(SolutionDir)ucxxrt\lib\" /EXCLUDE:$(
false
- true
+ true
From ba1e25a3d83daaa19e705a8686915da527b49cac Mon Sep 17 00:00:00 2001
From: hzqst <113660872@qq.com>
Date: Mon, 24 Jun 2024 21:32:38 +0800
Subject: [PATCH 4/5] Make the src compatible with clang-cl and llvm-msvc.
---
src/crt/vcruntime/ehdata.h | 4 ++
src/crt/vcruntime/frame.cpp | 58 +++++++++++++++++++++++++++
src/crt/vcruntime/per_thread_data.cpp | 6 +--
src/ucrt/internal/per_thread_data.cpp | 6 +--
src/ucrt/misc/invalid_parameter.cpp | 2 +-
src/ucrt/stdlib/rotl.cpp | 3 ++
src/ucrt/stdlib/rotr.cpp | 4 ++
7 files changed, 76 insertions(+), 7 deletions(-)
diff --git a/src/crt/vcruntime/ehdata.h b/src/crt/vcruntime/ehdata.h
index 5279400..b8b8328 100644
--- a/src/crt/vcruntime/ehdata.h
+++ b/src/crt/vcruntime/ehdata.h
@@ -8,6 +8,10 @@
#define _INC_EHDATA
#pragma once
+#if defined(__clang__)
+typedef const struct _s__ThrowInfo _ThrowInfo;
+#endif
+
#include "ehdata_values.h"
#include
diff --git a/src/crt/vcruntime/frame.cpp b/src/crt/vcruntime/frame.cpp
index 17ddd65..0da108f 100644
--- a/src/crt/vcruntime/frame.cpp
+++ b/src/crt/vcruntime/frame.cpp
@@ -361,11 +361,22 @@ EXCEPTION_DISPOSITION __InternalCxxFrameHandler(
EHTRACE_HANDLER_EXIT(ExceptionContinueSearch);
return ExceptionContinueSearch; // I don't think this value matters
}
+#if defined(__clang__)
+
+#if _EH_RELATIVE_FUNCINFO
+ auto tryBlockMap = typename T::TryBlockMap(pFuncInfo, pDC->ImageBase);
+#else
+ auto tryBlockMap = typename T::TryBlockMap(pFuncInfo, 0);
+#endif
+
+#else
#if _EH_RELATIVE_FUNCINFO
auto tryBlockMap = T::TryBlockMap(pFuncInfo, pDC->ImageBase);
#else
auto tryBlockMap = T::TryBlockMap(pFuncInfo, 0);
+#endif
+
#endif
if (tryBlockMap.getNumTryBlocks() != 0
//
@@ -614,12 +625,23 @@ static void FindHandler(
}
}
+#if defined(__clang__)
+
+#if _EH_RELATIVE_FUNCINFO
+ auto tryBlockMap = typename T::TryBlockMap(pFuncInfo, pDC->ImageBase);
+#else
+ auto tryBlockMap = typename T::TryBlockMap(pFuncInfo, 0);
+#endif
+
+#else
+
#if _EH_RELATIVE_FUNCINFO
auto tryBlockMap = T::TryBlockMap(pFuncInfo, pDC->ImageBase);
#else
auto tryBlockMap = T::TryBlockMap(pFuncInfo, 0);
#endif
+#endif
if (PER_IS_MSVC_EH(pExcept)) {
// Looks like it's ours. Let's see if we have a match:
//
@@ -646,11 +668,24 @@ static void FindHandler(
// Try block was in scope for current state. Scan catches for this
// try:
+#if defined(__clang__)
+
+#if _EH_RELATIVE_FUNCINFO
+ auto handlerMap = typename T::HandlerMap(&tryBlock, pDC->ImageBase, pDC->FunctionEntry->BeginAddress);
+#else
+ auto handlerMap = typename T::HandlerMap(&tryBlock, 0, 0);
+#endif
+
+#else
+
#if _EH_RELATIVE_FUNCINFO
auto handlerMap = T::HandlerMap(&tryBlock, pDC->ImageBase, pDC->FunctionEntry->BeginAddress);
#else
auto handlerMap = T::HandlerMap(&tryBlock, 0, 0);
#endif
+
+#endif
+
for (auto handler : handlerMap)
{
// Scan all types that thrown object can be converted to:
@@ -909,6 +944,15 @@ static void FindHandlerForForeignException(
return;
}
}
+#if defined(__clang__)
+
+#if _EH_RELATIVE_FUNCINFO
+ auto tryBlockMap = typename T::TryBlockMap(pFuncInfo, pDC->ImageBase);
+#else
+ auto tryBlockMap = typename T::TryBlockMap(pFuncInfo, 0);
+#endif
+
+#else
#if _EH_RELATIVE_FUNCINFO
auto tryBlockMap = T::TryBlockMap(pFuncInfo, pDC->ImageBase);
@@ -916,6 +960,7 @@ static void FindHandlerForForeignException(
auto tryBlockMap = T::TryBlockMap(pFuncInfo, 0);
#endif
+#endif
_VCRT_VERIFY(tryBlockMap.getNumTryBlocks() > 0);
if (tryBlockMap.getNumTryBlocks() > 0)
@@ -935,11 +980,24 @@ static void FindHandlerForForeignException(
}
// *and* the last catch in that try is an ellipsis (no other can be)
+#if defined(__clang__)
+
+#if _EH_RELATIVE_FUNCINFO
+ auto handlerMap = typename T::HandlerMap(&tryBlock, pDC->ImageBase, pDC->FunctionEntry->BeginAddress);
+#else
+ auto handlerMap = typename T::HandlerMap(&tryBlock, 0, 0);
+#endif
+
+#else
+
#if _EH_RELATIVE_FUNCINFO
auto handlerMap = T::HandlerMap(&tryBlock, pDC->ImageBase, pDC->FunctionEntry->BeginAddress);
#else
auto handlerMap = T::HandlerMap(&tryBlock, 0, 0);
#endif
+
+#endif
+
auto handler = handlerMap.getLastEntry();
if (!(HT_IS_TYPE_ELLIPSIS(*handler) && !HT_IS_STD_DOTDOT(*handler))) {
continue;
diff --git a/src/crt/vcruntime/per_thread_data.cpp b/src/crt/vcruntime/per_thread_data.cpp
index aa3dbe9..d16213f 100644
--- a/src/crt/vcruntime/per_thread_data.cpp
+++ b/src/crt/vcruntime/per_thread_data.cpp
@@ -66,7 +66,7 @@ static __vcrt_ptd* __cdecl store_and_initialize_ptd(__vcrt_ptd* const ptd)
BOOLEAN inserted = false;
__vcrt_ptd* const new_ptd = static_cast<__vcrt_ptd*>(RtlInsertElementGenericTableAvl(
- &__vcrt_startup_ptd_table, ptd, sizeof __vcrt_ptd_km, &inserted));
+ &__vcrt_startup_ptd_table, ptd, sizeof(__vcrt_ptd_km), &inserted));
if (!new_ptd)
{
@@ -77,7 +77,7 @@ static __vcrt_ptd* __cdecl store_and_initialize_ptd(__vcrt_ptd* const ptd)
if (__get_thread_uid(PsGetCurrentThread()) != static_cast<__vcrt_ptd_km*>(new_ptd)->uid)
{
inserted = true;
- RtlSecureZeroMemory(new_ptd, sizeof __vcrt_ptd); // not reset pid/uid
+ RtlSecureZeroMemory(new_ptd, sizeof(__vcrt_ptd) ); // not reset pid/uid
}
if (inserted)
@@ -92,7 +92,7 @@ static __vcrt_ptd* __cdecl store_and_initialize_ptd(__vcrt_ptd* const ptd)
extern "C" bool __cdecl __vcrt_initialize_ptd()
{
- constexpr auto size = ROUND_TO_SIZE(sizeof __vcrt_ptd_km + sizeof RTL_BALANCED_LINKS, sizeof(void*));
+ constexpr auto size = ROUND_TO_SIZE(sizeof(__vcrt_ptd_km) + sizeof(RTL_BALANCED_LINKS), sizeof(void*));
ExInitializeNPagedLookasideList(&__vcrt_startup_ptd_pools, nullptr, nullptr,
POOL_NX_ALLOCATION, size, __ucxxrt_tag, 0);
diff --git a/src/ucrt/internal/per_thread_data.cpp b/src/ucrt/internal/per_thread_data.cpp
index 39dd61a..39904c5 100644
--- a/src/ucrt/internal/per_thread_data.cpp
+++ b/src/ucrt/internal/per_thread_data.cpp
@@ -67,7 +67,7 @@ static __acrt_ptd* __cdecl store_and_initialize_ptd(__acrt_ptd* const ptd)
BOOLEAN inserted = false;
__acrt_ptd* const new_ptd = static_cast<__acrt_ptd*>(RtlInsertElementGenericTableAvl(
- &__acrt_startup_ptd_table, ptd, sizeof __acrt_ptd_km, &inserted));
+ &__acrt_startup_ptd_table, ptd, sizeof(__acrt_ptd_km), &inserted));
if (!new_ptd)
{
return nullptr;
@@ -77,7 +77,7 @@ static __acrt_ptd* __cdecl store_and_initialize_ptd(__acrt_ptd* const ptd)
if (__get_thread_uid(PsGetCurrentThread()) != static_cast<__acrt_ptd_km*>(new_ptd)->uid)
{
inserted = true;
- RtlSecureZeroMemory(new_ptd, sizeof __acrt_ptd); // not reset pid/uid
+ RtlSecureZeroMemory(new_ptd, sizeof(__acrt_ptd)); // not reset pid/uid
}
return new_ptd;
@@ -85,7 +85,7 @@ static __acrt_ptd* __cdecl store_and_initialize_ptd(__acrt_ptd* const ptd)
extern "C" bool __cdecl __acrt_initialize_ptd()
{
- constexpr auto size = ROUND_TO_SIZE(sizeof __acrt_ptd_km + sizeof RTL_BALANCED_LINKS, sizeof(void*));
+ constexpr auto size = ROUND_TO_SIZE(sizeof(__acrt_ptd_km) + sizeof(RTL_BALANCED_LINKS), sizeof(void*));
ExInitializeNPagedLookasideList(&__acrt_startup_ptd_pools, nullptr, nullptr,
POOL_NX_ALLOCATION, size, __ucxxrt_tag, 0);
diff --git a/src/ucrt/misc/invalid_parameter.cpp b/src/ucrt/misc/invalid_parameter.cpp
index d363a96..8142a00 100644
--- a/src/ucrt/misc/invalid_parameter.cpp
+++ b/src/ucrt/misc/invalid_parameter.cpp
@@ -32,7 +32,7 @@ extern "C" void __cdecl __acrt_initialize_invalid_parameter_handler(void* const
//
//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-extern "C" static void __cdecl _invalid_parameter_internal(
+extern "C" void __cdecl _invalid_parameter_internal(
wchar_t const* const expression,
wchar_t const* const function_name,
wchar_t const* const file_name,
diff --git a/src/ucrt/stdlib/rotl.cpp b/src/ucrt/stdlib/rotl.cpp
index c3b1ba2..31034b8 100644
--- a/src/ucrt/stdlib/rotl.cpp
+++ b/src/ucrt/stdlib/rotl.cpp
@@ -22,6 +22,7 @@
#endif
+#if !defined(__clang__)
extern "C" unsigned long __cdecl _lrotl(unsigned long value, int shift)
{
@@ -43,3 +44,5 @@ extern "C" unsigned __int64 __cdecl _rotl64(unsigned __int64 value, int shift)
value = (value >> (0x40 - shift)) | (value << shift);
return value;
}
+
+#endif
\ No newline at end of file
diff --git a/src/ucrt/stdlib/rotr.cpp b/src/ucrt/stdlib/rotr.cpp
index ccb5bff..8eed5e7 100644
--- a/src/ucrt/stdlib/rotr.cpp
+++ b/src/ucrt/stdlib/rotr.cpp
@@ -22,6 +22,8 @@
#endif
+#if !defined(__clang__)
+
extern "C" unsigned long __cdecl _lrotr(unsigned long value, int shift)
{
@@ -43,3 +45,5 @@ extern "C" unsigned __int64 __cdecl _rotr64(unsigned __int64 value, int shift)
value = (value << (0x40 - shift)) | (value >> shift);
return value;
}
+
+#endif
\ No newline at end of file
From 7c2ac1443b0e7ea1710882a16e80ab37910dba3a Mon Sep 17 00:00:00 2001
From: hzqst <113660872@qq.com>
Date: Mon, 24 Jun 2024 21:38:15 +0800
Subject: [PATCH 5/5] update README.md
---
README.md | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/README.md b/README.md
index 347889c..b4bdebb 100644
--- a/README.md
+++ b/README.md
@@ -109,9 +109,15 @@ Search for `ucxxrt`, choose the version that suits you, and then click "Install"
IDEļ¼Visual Studio 2022 latest version
+and Windows SDK
+
+and Windows Driver Kits
+
* `git clone --recurse-submodules https://github.com/MiroKaku/ucxxrt.git`
* Open `ucxxrt.sln` and build.
+* For clang-cl or llvm-msvc, you will have to add `-march=native` in the compiler flags.
+
## 4. Acknowledgements
Thanks to [JetBrains](https://www.jetbrains.com/?from=meesong) for providing free licenses such as [Resharper C++](https://www.jetbrains.com/resharper-cpp/?from=meesong) for my open-source projects.