nt_internals.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2015 The Crashpad Authors. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "util/win/nt_internals.h"
  15. #include "base/logging.h"
  16. #include "util/win/get_function.h"
  17. // Declarations that the system headers should provide but don’t.
  18. extern "C" {
  19. NTSTATUS NTAPI NtCreateThreadEx(PHANDLE ThreadHandle,
  20. ACCESS_MASK DesiredAccess,
  21. POBJECT_ATTRIBUTES ObjectAttributes,
  22. HANDLE ProcessHandle,
  23. PVOID StartRoutine,
  24. PVOID Argument,
  25. ULONG CreateFlags,
  26. SIZE_T ZeroBits,
  27. SIZE_T StackSize,
  28. SIZE_T MaximumStackSize,
  29. PVOID /*PPS_ATTRIBUTE_LIST*/ AttributeList);
  30. NTSTATUS NTAPI NtOpenThread(HANDLE* ThreadHandle,
  31. ACCESS_MASK DesiredAccess,
  32. OBJECT_ATTRIBUTES* ObjectAttributes,
  33. CLIENT_ID* ClientId);
  34. NTSTATUS NTAPI NtSuspendProcess(HANDLE);
  35. NTSTATUS NTAPI NtResumeProcess(HANDLE);
  36. VOID NTAPI RtlGetUnloadEventTraceEx(PULONG* ElementSize,
  37. PULONG* ElementCount,
  38. PVOID* EventTrace);
  39. } // extern "C"
  40. namespace crashpad {
  41. NTSTATUS NtClose(HANDLE handle) {
  42. static const auto nt_close = GET_FUNCTION_REQUIRED(L"ntdll.dll", ::NtClose);
  43. return nt_close(handle);
  44. }
  45. NTSTATUS
  46. NtCreateThreadEx(PHANDLE thread_handle,
  47. ACCESS_MASK desired_access,
  48. POBJECT_ATTRIBUTES object_attributes,
  49. HANDLE process_handle,
  50. PVOID start_routine,
  51. PVOID argument,
  52. ULONG create_flags,
  53. SIZE_T zero_bits,
  54. SIZE_T stack_size,
  55. SIZE_T maximum_stack_size,
  56. PVOID attribute_list) {
  57. static const auto nt_create_thread_ex =
  58. GET_FUNCTION_REQUIRED(L"ntdll.dll", ::NtCreateThreadEx);
  59. return nt_create_thread_ex(thread_handle,
  60. desired_access,
  61. object_attributes,
  62. process_handle,
  63. start_routine,
  64. argument,
  65. create_flags,
  66. zero_bits,
  67. stack_size,
  68. maximum_stack_size,
  69. attribute_list);
  70. }
  71. NTSTATUS NtQuerySystemInformation(
  72. SYSTEM_INFORMATION_CLASS system_information_class,
  73. PVOID system_information,
  74. ULONG system_information_length,
  75. PULONG return_length) {
  76. static const auto nt_query_system_information =
  77. GET_FUNCTION_REQUIRED(L"ntdll.dll", ::NtQuerySystemInformation);
  78. return nt_query_system_information(system_information_class,
  79. system_information,
  80. system_information_length,
  81. return_length);
  82. }
  83. NTSTATUS NtQueryInformationThread(HANDLE thread_handle,
  84. THREADINFOCLASS thread_information_class,
  85. PVOID thread_information,
  86. ULONG thread_information_length,
  87. PULONG return_length) {
  88. static const auto nt_query_information_thread =
  89. GET_FUNCTION_REQUIRED(L"ntdll.dll", ::NtQueryInformationThread);
  90. return nt_query_information_thread(thread_handle,
  91. thread_information_class,
  92. thread_information,
  93. thread_information_length,
  94. return_length);
  95. }
  96. template <class Traits>
  97. NTSTATUS NtOpenThread(PHANDLE thread_handle,
  98. ACCESS_MASK desired_access,
  99. POBJECT_ATTRIBUTES object_attributes,
  100. const process_types::CLIENT_ID<Traits>* client_id) {
  101. static const auto nt_open_thread =
  102. GET_FUNCTION_REQUIRED(L"ntdll.dll", ::NtOpenThread);
  103. return nt_open_thread(
  104. thread_handle,
  105. desired_access,
  106. object_attributes,
  107. const_cast<CLIENT_ID*>(reinterpret_cast<const CLIENT_ID*>(client_id)));
  108. }
  109. NTSTATUS NtQueryObject(HANDLE handle,
  110. OBJECT_INFORMATION_CLASS object_information_class,
  111. void* object_information,
  112. ULONG object_information_length,
  113. ULONG* return_length) {
  114. static const auto nt_query_object =
  115. GET_FUNCTION_REQUIRED(L"ntdll.dll", ::NtQueryObject);
  116. return nt_query_object(handle,
  117. object_information_class,
  118. object_information,
  119. object_information_length,
  120. return_length);
  121. }
  122. NTSTATUS NtSuspendProcess(HANDLE handle) {
  123. static const auto nt_suspend_process =
  124. GET_FUNCTION_REQUIRED(L"ntdll.dll", ::NtSuspendProcess);
  125. return nt_suspend_process(handle);
  126. }
  127. NTSTATUS NtResumeProcess(HANDLE handle) {
  128. static const auto nt_resume_process =
  129. GET_FUNCTION_REQUIRED(L"ntdll.dll", ::NtResumeProcess);
  130. return nt_resume_process(handle);
  131. }
  132. void RtlGetUnloadEventTraceEx(ULONG** element_size,
  133. ULONG** element_count,
  134. void** event_trace) {
  135. static const auto rtl_get_unload_event_trace_ex =
  136. GET_FUNCTION_REQUIRED(L"ntdll.dll", ::RtlGetUnloadEventTraceEx);
  137. rtl_get_unload_event_trace_ex(element_size, element_count, event_trace);
  138. }
  139. // Explicit instantiations with the only 2 valid template arguments to avoid
  140. // putting the body of the function in the header.
  141. template NTSTATUS NtOpenThread<process_types::internal::Traits32>(
  142. PHANDLE thread_handle,
  143. ACCESS_MASK desired_access,
  144. POBJECT_ATTRIBUTES object_attributes,
  145. const process_types::CLIENT_ID<process_types::internal::Traits32>*
  146. client_id);
  147. template NTSTATUS NtOpenThread<process_types::internal::Traits64>(
  148. PHANDLE thread_handle,
  149. ACCESS_MASK desired_access,
  150. POBJECT_ATTRIBUTES object_attributes,
  151. const process_types::CLIENT_ID<process_types::internal::Traits64>*
  152. client_id);
  153. } // namespace crashpad