nt_code.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* Proposed SG14 status_code
  2. (C) 2018-2021 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
  3. File Created: Feb 2018
  4. Boost Software License - Version 1.0 - August 17th, 2003
  5. Permission is hereby granted, free of charge, to any person or organization
  6. obtaining a copy of the software and accompanying documentation covered by
  7. this license (the "Software") to use, reproduce, display, distribute,
  8. execute, and transmit the Software, and to prepare derivative works of the
  9. Software, and to permit third-parties to whom the Software is furnished to
  10. do so, all subject to the following:
  11. The copyright notices in the Software and this entire statement, including
  12. the above license grant, this restriction and the following disclaimer,
  13. must be included in all copies of the Software, in whole or in part, and
  14. all derivative works of the Software, unless such copies or derivative
  15. works are solely in the form of machine-executable object code generated by
  16. a source language processor.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  20. SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  21. FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  22. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NT_CODE_HPP
  26. #define BOOST_OUTCOME_SYSTEM_ERROR2_NT_CODE_HPP
  27. #if !defined(_WIN32) && !defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
  28. #error This file should only be included on Windows
  29. #endif
  30. #include "win32_code.hpp"
  31. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
  32. //! \exclude
  33. namespace win32
  34. {
  35. // A Win32 NTSTATUS
  36. using NTSTATUS = long;
  37. // A Win32 HMODULE
  38. using HMODULE = void *;
  39. // Used to retrieve where the NTDLL DLL is mapped into memory
  40. extern HMODULE __stdcall GetModuleHandleW(const wchar_t *lpModuleName);
  41. #pragma comment(lib, "kernel32.lib")
  42. #if(defined(__x86_64__) || defined(_M_X64)) || (defined(__aarch64__) || defined(_M_ARM64))
  43. #pragma comment(linker, "/alternatename:?GetModuleHandleW@win32@system_error2@@YAPEAXPEB_W@Z=GetModuleHandleW")
  44. #elif defined(__x86__) || defined(_M_IX86) || defined(__i386__)
  45. #pragma comment(linker, "/alternatename:?GetModuleHandleW@win32@system_error2@@YGPAXPB_W@Z=__imp__GetModuleHandleW@4")
  46. #elif defined(__arm__) || defined(_M_ARM)
  47. #pragma comment(linker, "/alternatename:?GetModuleHandleW@win32@system_error2@@YAPAXPB_W@Z=GetModuleHandleW")
  48. #else
  49. #error Unknown architecture
  50. #endif
  51. } // namespace win32
  52. class _nt_code_domain;
  53. //! (Windows only) A NT error code, those returned by NT kernel functions.
  54. using nt_code = status_code<_nt_code_domain>;
  55. //! (Windows only) A specialisation of `status_error` for the NT error code domain.
  56. using nt_error = status_error<_nt_code_domain>;
  57. /*! (Windows only) The implementation of the domain for NT error codes, those returned by NT kernel functions.
  58. */
  59. class _nt_code_domain : public status_code_domain
  60. {
  61. template <class DomainType> friend class status_code;
  62. template <class StatusCode> friend class detail::indirecting_domain;
  63. friend class _com_code_domain;
  64. using _base = status_code_domain;
  65. static int _nt_code_to_errno(win32::NTSTATUS c)
  66. {
  67. if(c >= 0)
  68. {
  69. return 0; // success
  70. }
  71. switch(static_cast<unsigned>(c))
  72. {
  73. #include "detail/nt_code_to_generic_code.ipp"
  74. }
  75. return -1;
  76. }
  77. static win32::DWORD _nt_code_to_win32_code(win32::NTSTATUS c) // NOLINT
  78. {
  79. if(c >= 0)
  80. {
  81. return 0; // success
  82. }
  83. switch(static_cast<unsigned>(c))
  84. {
  85. #include "detail/nt_code_to_win32_code.ipp"
  86. }
  87. return static_cast<win32::DWORD>(-1);
  88. }
  89. //! Construct from a NT error code
  90. static _base::string_ref _make_string_ref(win32::NTSTATUS c) noexcept
  91. {
  92. wchar_t buffer[32768];
  93. static win32::HMODULE ntdll = win32::GetModuleHandleW(L"NTDLL.DLL");
  94. win32::DWORD wlen = win32::FormatMessageW(0x00000800 /*FORMAT_MESSAGE_FROM_HMODULE*/ | 0x00001000 /*FORMAT_MESSAGE_FROM_SYSTEM*/ | 0x00000200 /*FORMAT_MESSAGE_IGNORE_INSERTS*/, ntdll, c, (1 << 10) /*MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)*/, buffer, 32768, nullptr);
  95. size_t allocation = wlen + (wlen >> 1);
  96. win32::DWORD bytes;
  97. if(wlen == 0)
  98. {
  99. return _base::string_ref("failed to get message from system");
  100. }
  101. for(;;)
  102. {
  103. auto *p = static_cast<char *>(malloc(allocation)); // NOLINT
  104. if(p == nullptr)
  105. {
  106. return _base::string_ref("failed to get message from system");
  107. }
  108. bytes = win32::WideCharToMultiByte(65001 /*CP_UTF8*/, 0, buffer, (int) (wlen + 1), p, (int) allocation, nullptr, nullptr);
  109. if(bytes != 0)
  110. {
  111. char *end = strchr(p, 0);
  112. while(end[-1] == 10 || end[-1] == 13)
  113. {
  114. --end;
  115. }
  116. *end = 0; // NOLINT
  117. return _base::atomic_refcounted_string_ref(p, end - p);
  118. }
  119. free(p); // NOLINT
  120. if(win32::GetLastError() == 0x7a /*ERROR_INSUFFICIENT_BUFFER*/)
  121. {
  122. allocation += allocation >> 2;
  123. continue;
  124. }
  125. return _base::string_ref("failed to get message from system");
  126. }
  127. }
  128. public:
  129. //! The value type of the NT code, which is a `win32::NTSTATUS`
  130. using value_type = win32::NTSTATUS;
  131. using _base::string_ref;
  132. public:
  133. //! Default constructor
  134. constexpr explicit _nt_code_domain(typename _base::unique_id_type id = 0x93f3b4487e4af25b) noexcept
  135. : _base(id)
  136. {
  137. }
  138. _nt_code_domain(const _nt_code_domain &) = default;
  139. _nt_code_domain(_nt_code_domain &&) = default;
  140. _nt_code_domain &operator=(const _nt_code_domain &) = default;
  141. _nt_code_domain &operator=(_nt_code_domain &&) = default;
  142. ~_nt_code_domain() = default;
  143. //! Constexpr singleton getter. Returns the constexpr nt_code_domain variable.
  144. static inline constexpr const _nt_code_domain &get();
  145. virtual string_ref name() const noexcept override { return string_ref("NT domain"); } // NOLINT
  146. protected:
  147. virtual bool _do_failure(const status_code<void> &code) const noexcept override // NOLINT
  148. {
  149. assert(code.domain() == *this);
  150. return static_cast<const nt_code &>(code).value() < 0; // NOLINT
  151. }
  152. virtual bool _do_equivalent(const status_code<void> &code1, const status_code<void> &code2) const noexcept override // NOLINT
  153. {
  154. assert(code1.domain() == *this);
  155. const auto &c1 = static_cast<const nt_code &>(code1); // NOLINT
  156. if(code2.domain() == *this)
  157. {
  158. const auto &c2 = static_cast<const nt_code &>(code2); // NOLINT
  159. return c1.value() == c2.value();
  160. }
  161. if(code2.domain() == generic_code_domain)
  162. {
  163. const auto &c2 = static_cast<const generic_code &>(code2); // NOLINT
  164. if(static_cast<int>(c2.value()) == _nt_code_to_errno(c1.value()))
  165. {
  166. return true;
  167. }
  168. }
  169. if(code2.domain() == win32_code_domain)
  170. {
  171. const auto &c2 = static_cast<const win32_code &>(code2); // NOLINT
  172. if(c2.value() == _nt_code_to_win32_code(c1.value()))
  173. {
  174. return true;
  175. }
  176. }
  177. return false;
  178. }
  179. virtual generic_code _generic_code(const status_code<void> &code) const noexcept override // NOLINT
  180. {
  181. assert(code.domain() == *this);
  182. const auto &c = static_cast<const nt_code &>(code); // NOLINT
  183. return generic_code(static_cast<errc>(_nt_code_to_errno(c.value())));
  184. }
  185. virtual string_ref _do_message(const status_code<void> &code) const noexcept override // NOLINT
  186. {
  187. assert(code.domain() == *this);
  188. const auto &c = static_cast<const nt_code &>(code); // NOLINT
  189. return _make_string_ref(c.value());
  190. }
  191. #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
  192. BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN virtual void _do_throw_exception(const status_code<void> &code) const override // NOLINT
  193. {
  194. assert(code.domain() == *this);
  195. const auto &c = static_cast<const nt_code &>(code); // NOLINT
  196. throw status_error<_nt_code_domain>(c);
  197. }
  198. #endif
  199. };
  200. //! (Windows only) A constexpr source variable for the NT code domain, which is that of NT kernel functions. Returned by `_nt_code_domain::get()`.
  201. constexpr _nt_code_domain nt_code_domain;
  202. inline constexpr const _nt_code_domain &_nt_code_domain::get()
  203. {
  204. return nt_code_domain;
  205. }
  206. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
  207. #endif