scoped_handle.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_WIN_SCOPED_HANDLE_H_
  5. #define BASE_WIN_SCOPED_HANDLE_H_
  6. #include "base/win/windows_types.h"
  7. #include "base/base_export.h"
  8. #include "base/compiler_specific.h"
  9. #include "base/gtest_prod_util.h"
  10. #include "base/location.h"
  11. #include "base/logging.h"
  12. #include "base/macros.h"
  13. // TODO(rvargas): remove this with the rest of the verifier.
  14. #if defined(COMPILER_MSVC)
  15. #include <intrin.h>
  16. #define BASE_WIN_GET_CALLER _ReturnAddress()
  17. #elif defined(COMPILER_GCC)
  18. #define BASE_WIN_GET_CALLER \
  19. __builtin_extract_return_addr(__builtin_return_address(0))
  20. #endif
  21. namespace base {
  22. namespace win {
  23. // Generic wrapper for raw handles that takes care of closing handles
  24. // automatically. The class interface follows the style of
  25. // the ScopedFILE class with two additions:
  26. // - IsValid() method can tolerate multiple invalid handle values such as NULL
  27. // and INVALID_HANDLE_VALUE (-1) for Win32 handles.
  28. // - Set() (and the constructors and assignment operators that call it)
  29. // preserve the Windows LastError code. This ensures that GetLastError() can
  30. // be called after stashing a handle in a GenericScopedHandle object. Doing
  31. // this explicitly is necessary because of bug 528394 and VC++ 2015.
  32. template <class Traits, class Verifier>
  33. class GenericScopedHandle {
  34. public:
  35. using Handle = typename Traits::Handle;
  36. GenericScopedHandle() : handle_(Traits::NullHandle()) {}
  37. explicit GenericScopedHandle(Handle handle) : handle_(Traits::NullHandle()) {
  38. Set(handle);
  39. }
  40. GenericScopedHandle(GenericScopedHandle&& other)
  41. : handle_(Traits::NullHandle()) {
  42. Set(other.Take());
  43. }
  44. ~GenericScopedHandle() { Close(); }
  45. bool IsValid() const { return Traits::IsHandleValid(handle_); }
  46. GenericScopedHandle& operator=(GenericScopedHandle&& other) {
  47. DCHECK_NE(this, &other);
  48. Set(other.Take());
  49. return *this;
  50. }
  51. void Set(Handle handle) {
  52. if (handle_ != handle) {
  53. // Preserve old LastError to avoid bug 528394.
  54. auto last_error = ::GetLastError();
  55. Close();
  56. if (Traits::IsHandleValid(handle)) {
  57. handle_ = handle;
  58. Verifier::StartTracking(handle, this, BASE_WIN_GET_CALLER,
  59. GetProgramCounter());
  60. }
  61. ::SetLastError(last_error);
  62. }
  63. }
  64. Handle Get() const { return handle_; }
  65. // Transfers ownership away from this object.
  66. Handle Take() WARN_UNUSED_RESULT {
  67. Handle temp = handle_;
  68. handle_ = Traits::NullHandle();
  69. if (Traits::IsHandleValid(temp)) {
  70. Verifier::StopTracking(temp, this, BASE_WIN_GET_CALLER,
  71. GetProgramCounter());
  72. }
  73. return temp;
  74. }
  75. // Explicitly closes the owned handle.
  76. void Close() {
  77. if (Traits::IsHandleValid(handle_)) {
  78. Verifier::StopTracking(handle_, this, BASE_WIN_GET_CALLER,
  79. GetProgramCounter());
  80. Traits::CloseHandle(handle_);
  81. handle_ = Traits::NullHandle();
  82. }
  83. }
  84. private:
  85. FRIEND_TEST_ALL_PREFIXES(ScopedHandleTest, ActiveVerifierWrongOwner);
  86. FRIEND_TEST_ALL_PREFIXES(ScopedHandleTest, ActiveVerifierUntrackedHandle);
  87. Handle handle_;
  88. DISALLOW_COPY_AND_ASSIGN(GenericScopedHandle);
  89. };
  90. #undef BASE_WIN_GET_CALLER
  91. // The traits class for Win32 handles that can be closed via CloseHandle() API.
  92. class HandleTraits {
  93. public:
  94. using Handle = HANDLE;
  95. // Closes the handle.
  96. static bool BASE_EXPORT CloseHandle(HANDLE handle);
  97. // Returns true if the handle value is valid.
  98. static bool IsHandleValid(HANDLE handle) {
  99. return handle != nullptr && handle != INVALID_HANDLE_VALUE;
  100. }
  101. // Returns NULL handle value.
  102. static HANDLE NullHandle() { return nullptr; }
  103. private:
  104. DISALLOW_IMPLICIT_CONSTRUCTORS(HandleTraits);
  105. };
  106. // Do-nothing verifier.
  107. class DummyVerifierTraits {
  108. public:
  109. using Handle = HANDLE;
  110. static void StartTracking(HANDLE handle,
  111. const void* owner,
  112. const void* pc1,
  113. const void* pc2) {}
  114. static void StopTracking(HANDLE handle,
  115. const void* owner,
  116. const void* pc1,
  117. const void* pc2) {}
  118. private:
  119. DISALLOW_IMPLICIT_CONSTRUCTORS(DummyVerifierTraits);
  120. };
  121. // Performs actual run-time tracking.
  122. class BASE_EXPORT VerifierTraits {
  123. public:
  124. using Handle = HANDLE;
  125. static void StartTracking(HANDLE handle,
  126. const void* owner,
  127. const void* pc1,
  128. const void* pc2);
  129. static void StopTracking(HANDLE handle,
  130. const void* owner,
  131. const void* pc1,
  132. const void* pc2);
  133. private:
  134. DISALLOW_IMPLICIT_CONSTRUCTORS(VerifierTraits);
  135. };
  136. using ScopedHandle = GenericScopedHandle<HandleTraits, VerifierTraits>;
  137. // This function may be called by the embedder to disable the use of
  138. // VerifierTraits at runtime. It has no effect if DummyVerifierTraits is used
  139. // for ScopedHandle.
  140. BASE_EXPORT void DisableHandleVerifier();
  141. // This should be called whenever the OS is closing a handle, if extended
  142. // verification of improper handle closing is desired. If |handle| is being
  143. // tracked by the handle verifier and ScopedHandle is not the one closing it,
  144. // a CHECK is generated.
  145. BASE_EXPORT void OnHandleBeingClosed(HANDLE handle);
  146. } // namespace win
  147. } // namespace base
  148. #endif // BASE_WIN_SCOPED_HANDLE_H_