Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

增加llvm-msvc支持,以及一些小改动 #43

Merged
merged 5 commits into from
Jul 1, 2024
Merged
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions src/crt/vcruntime/ehdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#define _INC_EHDATA
#pragma once

#if defined(__clang__)
typedef const struct _s__ThrowInfo _ThrowInfo;
#endif

#include "ehdata_values.h"

#include <excpt.h>
Expand Down
58 changes: 58 additions & 0 deletions src/crt/vcruntime/frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down Expand Up @@ -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:
//
Expand All @@ -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:
Expand Down Expand Up @@ -909,13 +944,23 @@ 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);
#else
auto tryBlockMap = T::TryBlockMap(pFuncInfo, 0);
#endif

#endif
_VCRT_VERIFY(tryBlockMap.getNumTryBlocks() > 0);

if (tryBlockMap.getNumTryBlocks() > 0)
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/crt/vcruntime/per_thread_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
Expand All @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions src/crt/vcruntime/sys_common.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/ucrt/internal/per_thread_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -77,15 +77,15 @@ 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;
}

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);
Expand Down
2 changes: 1 addition & 1 deletion src/ucrt/misc/invalid_parameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/ucrt/startup/exit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions src/ucrt/stdlib/rotl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#endif


#if !defined(__clang__)

extern "C" unsigned long __cdecl _lrotl(unsigned long value, int shift)
{
Expand All @@ -43,3 +44,5 @@ extern "C" unsigned __int64 __cdecl _rotl64(unsigned __int64 value, int shift)
value = (value >> (0x40 - shift)) | (value << shift);
return value;
}

#endif
4 changes: 4 additions & 0 deletions src/ucrt/stdlib/rotr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#endif


#if !defined(__clang__)


extern "C" unsigned long __cdecl _lrotr(unsigned long value, int shift)
{
Expand All @@ -43,3 +45,5 @@ extern "C" unsigned __int64 __cdecl _rotr64(unsigned __int64 value, int shift)
value = (value << (0x40 - shift)) | (value >> shift);
return value;
}

#endif
5 changes: 4 additions & 1 deletion src/ucxxrt.props
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@

<PropertyGroup>
<UCXXRT_KernelMode>false</UCXXRT_KernelMode>
<UCXXRT_KernelMode Condition=" ('$(PlatformToolset.TrimEnd(`0123456789.`))' == 'WindowsKernelModeDriver') ">true</UCXXRT_KernelMode>
<UCXXRT_KernelMode Condition="('$(PlatformToolset.TrimEnd(`0123456789.`))' == 'WindowsKernelModeDriver') Or
('$(PlatformToolset)' == 'LLVM-MSVC_v143_KernelMode') Or
('$(PlatformToolset)' == 'LLVM-MSVC_v142_KernelMode') Or
('$(PlatformToolset)' == 'LLVM-MSVC_v141_KernelMode') ">true</UCXXRT_KernelMode>
</PropertyGroup>

<PropertyGroup>
Expand Down
5 changes: 4 additions & 1 deletion src/ucxxrtlib.props
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ xcopy /D /S /Y /V /F "$(SolutionDir)lib" "$(SolutionDir)ucxxrt\lib\" /EXCLUDE:$(
<!-- ucxxrt Config -->
<PropertyGroup>
<UCXXRT_KernelMode>false</UCXXRT_KernelMode>
<UCXXRT_KernelMode Condition=" ('$(PlatformToolset.TrimEnd(`0123456789.`))' == 'WindowsKernelModeDriver') ">true</UCXXRT_KernelMode>
<UCXXRT_KernelMode Condition="('$(PlatformToolset.TrimEnd(`0123456789.`))' == 'WindowsKernelModeDriver') Or
('$(PlatformToolset)' == 'LLVM-MSVC_v143_KernelMode') Or
('$(PlatformToolset)' == 'LLVM-MSVC_v142_KernelMode') Or
('$(PlatformToolset)' == 'LLVM-MSVC_v141_KernelMode') ">true</UCXXRT_KernelMode>
</PropertyGroup>

<PropertyGroup Condition=" ('$(UCXXRT_KernelMode)'=='true') ">
Expand Down
Loading