Skip to content

Commit

Permalink
* Bug,补充单元测试,并解决WindowsCreateStringReference、WindowsCompareStringOrdi…
Browse files Browse the repository at this point in the history
…nal单元测试不通过。

* Bug,解决内部函数MakeNtString字符串长度错误
* Opt, 将代码按YY-Thunks风格转换,并为release也开启asan
  • Loading branch information
mingkuang-Chuyu committed Jun 23, 2024
1 parent c059451 commit b9d5bc7
Show file tree
Hide file tree
Showing 7 changed files with 570 additions and 276 deletions.
62 changes: 62 additions & 0 deletions src/Shared/HStringPrivate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* PROJECT: YY-Thunks
* FILE: hstring_private.h
* PURPOSE: Extra definitions for the Windows Runtime String (HSTRING)
*
* LICENSE: The MIT License
*
* MAINTAINER: MouriNaruto ([email protected])
*/
#pragma once
#include <hstring.h>

#include "SharedDefs.h"

/*
* @remark The following definitions are dumped from the debug symbol from the
* Windows 10 Build 14347's combase.dll.
*/
namespace YY::Thunks::internal
{
namespace
{
/*
* @brief The flags of the Windows Runtime String.
* @remark Originally it's a C/C++ enum in the debug symbols.
*/
enum class RuntimeStringFlags : UINT32
{
WRHF_NONE = 0x00000000,
WRHF_STRING_REFERENCE = 0x00000001,
WRHF_VALID_UNICODE_FORMAT_INFO = 0x00000002,
WRHF_WELL_FORMED_UNICODE = 0x00000004,
WRHF_HAS_EMBEDDED_NULLS = 0x00000008,
WRHF_EMBEDDED_NULLS_COMPUTED = 0x00000010,
WRHF_RESERVED_FOR_PREALLOCATED_STRING_BUFFER = 0x80000000,
};
YY_APPLY_ENUM_CALSS_BIT_OPERATOR(RuntimeStringFlags)

/*
* @brief The internal structure of Windows Runtime String header.
*/
typedef struct _HSTRING_HEADER_INTERNAL
{
RuntimeStringFlags Flags;
UINT32 Length;
UINT32 Padding1;
UINT32 Padding2;
PCWSTR StringRef;
} HSTRING_HEADER_INTERNAL, * PHSTRING_HEADER_INTERNAL;
static_assert(sizeof(HSTRING_HEADER) == sizeof(HSTRING_HEADER_INTERNAL));

/*
* @brief The internal structure of heap allocated Windows Runtime String.
*/
typedef struct _STRING_OPAQUE
{
HSTRING_HEADER_INTERNAL Header;
volatile LONG RefCount;
WCHAR String[ANYSIZE_ARRAY];
} STRING_OPAQUE, * PSTRING_OPAQUE;
}
}
35 changes: 35 additions & 0 deletions src/Shared/SharedDefs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once
#include <type_traits>

#define YY_APPLY_ENUM_CALSS_BIT_OPERATOR(_ENUM) \
inline constexpr _ENUM& operator|=(_ENUM& _eLeft, _ENUM _eRight) \
{ \
using _Type = std::underlying_type<_ENUM>::type; \
(_Type&)_eLeft |= (_Type)_eRight; \
return _eLeft; \
} \
\
inline constexpr _ENUM operator|(_ENUM _eLeft, _ENUM _eRight) \
{ \
using _Type = std::underlying_type<_ENUM>::type; \
auto _Result = (_Type)_eLeft | (_Type)_eRight; \
return _ENUM(_Result); \
} \
\
inline constexpr _ENUM operator&(_ENUM _eLeft, _ENUM _eRight) \
{ \
using _Type = std::underlying_type<_ENUM>::type; \
return _ENUM((_Type)_eLeft & (_Type)_eRight); \
} \
\
inline constexpr _ENUM operator~(_ENUM _eLeft) \
{ \
using _Type = std::underlying_type<_ENUM>::type; \
return _ENUM(~(_Type)_eLeft); \
} \
\
inline constexpr bool HasFlags(_ENUM _eLeft, _ENUM _eRight) \
{ \
using _Type = std::underlying_type<_ENUM>::type; \
return (_Type)_eLeft & (_Type)_eRight; \
}
65 changes: 0 additions & 65 deletions src/Shared/hstring_private.h

This file was deleted.

4 changes: 2 additions & 2 deletions src/Thunks/YY_Thunks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -965,15 +965,15 @@ namespace YY::Thunks::internal
static constexpr UNICODE_STRING __fastcall MakeNtString(_In_z_ const wchar_t* _szString)
{
const auto _cbString = StringLength(_szString) * sizeof(_szString[0]);
UNICODE_STRING _Result = { (USHORT)max(UINT16_MAX, _cbString), (USHORT)max(UINT16_MAX, _cbString + sizeof(_szString[0])), const_cast<PWSTR>(_szString) };
UNICODE_STRING _Result = { (USHORT)min(UINT16_MAX, _cbString), (USHORT)min(UINT16_MAX, _cbString + sizeof(_szString[0])), const_cast<PWSTR>(_szString) };
return _Result;
}

static constexpr ANSI_STRING __fastcall MakeNtString(_In_z_ const char* _szString)
{
const auto _cbString = StringLength(_szString) * sizeof(_szString[0]);

ANSI_STRING _Result = { (USHORT)max(UINT16_MAX, _cbString), (USHORT)max(UINT16_MAX, _cbString + sizeof(_szString[0])), const_cast<PSTR>(_szString)};
ANSI_STRING _Result = { (USHORT)min(UINT16_MAX, _cbString), (USHORT)min(UINT16_MAX, _cbString + sizeof(_szString[0])), const_cast<PSTR>(_szString)};
return _Result;
}

Expand Down
Loading

0 comments on commit b9d5bc7

Please sign in to comment.