HyperPlatform Programmer's Reference
kernel_stl.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2017, Satoshi Tanda. All rights reserved.
2 // Use of this source code is governed by a MIT-style license that can be
3 // found in the LICENSE file.
4 
7 
8 #include <fltKernel.h>
9 #undef _HAS_EXCEPTIONS
10 #define _HAS_EXCEPTIONS 0
11 
12 // See common.h for details
13 #pragma prefast(disable : 30030)
14 
16 //
17 // macro utilities
18 //
19 
21 //
22 // constants and macros
23 //
24 
26 static const ULONG kKstlpPoolTag = 'LTSK';
27 
29 //
30 // types
31 //
32 
34 //
35 // prototypes
36 //
37 
39 //
40 // variables
41 //
42 
44 //
45 // implementations
46 //
47 
48 // An alternative implementation of a C++ exception handler. Issues a bug check.
49 DECLSPEC_NORETURN static void KernelStlpRaiseException(
50  _In_ ULONG bug_check_code) {
51  KdBreakPoint();
52 #pragma warning(push)
53 #pragma warning(disable : 28159)
54  KeBugCheck(bug_check_code);
55 #pragma warning(pop)
56 }
57 
58 DECLSPEC_NORETURN void __cdecl _invalid_parameter_noinfo_noreturn() {
59  KernelStlpRaiseException(KMODE_EXCEPTION_NOT_HANDLED);
60 }
61 
62 namespace std {
63 
64 DECLSPEC_NORETURN void __cdecl _Xbad_alloc() {
65  KernelStlpRaiseException(KMODE_EXCEPTION_NOT_HANDLED);
66 }
67 DECLSPEC_NORETURN void __cdecl _Xinvalid_argument(_In_z_ const char *) {
68  KernelStlpRaiseException(KMODE_EXCEPTION_NOT_HANDLED);
69 }
70 DECLSPEC_NORETURN void __cdecl _Xlength_error(_In_z_ const char *) {
71  KernelStlpRaiseException(KMODE_EXCEPTION_NOT_HANDLED);
72 }
73 DECLSPEC_NORETURN void __cdecl _Xout_of_range(_In_z_ const char *) {
74  KernelStlpRaiseException(KMODE_EXCEPTION_NOT_HANDLED);
75 }
76 DECLSPEC_NORETURN void __cdecl _Xoverflow_error(_In_z_ const char *) {
77  KernelStlpRaiseException(KMODE_EXCEPTION_NOT_HANDLED);
78 }
79 DECLSPEC_NORETURN void __cdecl _Xruntime_error(_In_z_ const char *) {
80  KernelStlpRaiseException(KMODE_EXCEPTION_NOT_HANDLED);
81 }
82 
83 } // namespace std
84 
85 // An alternative implementation of the new operator
86 _IRQL_requires_max_(DISPATCH_LEVEL) void *__cdecl operator new(
87  _In_ size_t size) {
88  if (size == 0) {
89  size = 1;
90  }
91 
92  const auto p = ExAllocatePoolWithTag(NonPagedPool, size, kKstlpPoolTag);
93  if (!p) {
94  KernelStlpRaiseException(MUST_SUCCEED_POOL_EMPTY);
95  }
96  return p;
97 }
98 
99 // An alternative implementation of the new operator
100 _IRQL_requires_max_(DISPATCH_LEVEL) void __cdecl operator delete(_In_ void *p) {
101  if (p) {
102  ExFreePoolWithTag(p, kKstlpPoolTag);
103  }
104 }
105 
106 // An alternative implementation of the new operator
107 _IRQL_requires_max_(DISPATCH_LEVEL) void __cdecl operator delete(
108  _In_ void *p, _In_ size_t size) {
109  UNREFERENCED_PARAMETER(size);
110  if (p) {
111  ExFreePoolWithTag(p, kKstlpPoolTag);
112  }
113 }
114 
115 // An alternative implementation of __stdio_common_vsprintf_s
116 _Success_(return >= 0) EXTERN_C inline int __cdecl __stdio_common_vsprintf_s(
117  _In_ unsigned __int64 _Options, _Out_writes_z_(_BufferCount) char *_Buffer,
118  _In_ size_t _BufferCount,
119  _In_z_ _Printf_format_string_params_(2) char const *_Format,
120  _In_opt_ _locale_t _Locale, va_list _ArgList) {
121  UNREFERENCED_PARAMETER(_Options);
122  UNREFERENCED_PARAMETER(_Locale);
123 
124  // Calls _vsnprintf exported by ntoskrnl
125  using _vsnprintf_type = int __cdecl(char *, size_t, const char *, va_list);
126  static _vsnprintf_type *local__vsnprintf = nullptr;
127  if (!local__vsnprintf) {
128  UNICODE_STRING proc_name_U = {};
129  RtlInitUnicodeString(&proc_name_U, L"_vsnprintf");
130  local__vsnprintf = reinterpret_cast<_vsnprintf_type *>(
131  MmGetSystemRoutineAddress(&proc_name_U));
132  }
133 
134  return local__vsnprintf(_Buffer, _BufferCount, _Format, _ArgList);
135 }
136 
137 // An alternative implementation of __stdio_common_vswprintf_s
138 _Success_(return >= 0) _Check_return_opt_ EXTERN_C
139  inline int __cdecl __stdio_common_vswprintf_s(
140  _In_ unsigned __int64 _Options,
141  _Out_writes_z_(_BufferCount) wchar_t *_Buffer, _In_ size_t _BufferCount,
142  _In_z_ _Printf_format_string_params_(2) wchar_t const *_Format,
143  _In_opt_ _locale_t _Locale, va_list _ArgList) {
144  UNREFERENCED_PARAMETER(_Options);
145  UNREFERENCED_PARAMETER(_Locale);
146 
147  // Calls _vsnwprintf exported by ntoskrnl
148  using _vsnwprintf_type =
149  int __cdecl(wchar_t *, size_t, const wchar_t *, va_list);
150  static _vsnwprintf_type *local__vsnwprintf = nullptr;
151  if (!local__vsnwprintf) {
152  UNICODE_STRING proc_name_U = {};
153  RtlInitUnicodeString(&proc_name_U, L"_vsnwprintf");
154  local__vsnwprintf = reinterpret_cast<_vsnwprintf_type *>(
155  MmGetSystemRoutineAddress(&proc_name_U));
156  }
157 
158  return local__vsnwprintf(_Buffer, _BufferCount, _Format, _ArgList);
159 }
_Check_return_opt_ EXTERN_C int __cdecl __stdio_common_vswprintf_s(_In_ unsigned __int64 _Options, _Out_writes_z_(_BufferCount) wchar_t *_Buffer, _In_ size_t _BufferCount, _In_z_ _Printf_format_string_params_(2) wchar_t const *_Format, _In_opt_ _locale_t _Locale, va_list _ArgList)
Definition: kernel_stl.cpp:139
EXTERN_C int __cdecl __stdio_common_vsprintf_s(_In_ unsigned __int64 _Options, _Out_writes_z_(_BufferCount) char *_Buffer, _In_ size_t _BufferCount, _In_z_ _Printf_format_string_params_(2) char const *_Format, _In_opt_ _locale_t _Locale, va_list _ArgList)
Definition: kernel_stl.cpp:116
DECLSPEC_NORETURN void __cdecl _Xlength_error(_In_z_ const char *)
Definition: kernel_stl.cpp:70
DECLSPEC_NORETURN void __cdecl _Xout_of_range(_In_z_ const char *)
Definition: kernel_stl.cpp:73
static const ULONG kKstlpPoolTag
A pool tag for this module.
Definition: kernel_stl.cpp:26
DECLSPEC_NORETURN void __cdecl _invalid_parameter_noinfo_noreturn()
Definition: kernel_stl.cpp:58
static DECLSPEC_NORETURN void KernelStlpRaiseException(_In_ ULONG bug_check_code)
Definition: kernel_stl.cpp:49
DECLSPEC_NORETURN void __cdecl _Xbad_alloc()
Definition: kernel_stl.cpp:64
DECLSPEC_NORETURN void __cdecl _Xruntime_error(_In_z_ const char *)
Definition: kernel_stl.cpp:79
DECLSPEC_NORETURN void __cdecl _Xoverflow_error(_In_z_ const char *)
Definition: kernel_stl.cpp:76
DECLSPEC_NORETURN void __cdecl _Xinvalid_argument(_In_z_ const char *)
Definition: kernel_stl.cpp:67