Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
3.4 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. obvutil.c
  5. Abstract:
  6. This module implements various utilities required to do driver verification.
  7. Author:
  8. Adrian J. Oney (adriao) 20-Apr-1998
  9. Environment:
  10. Kernel mode
  11. Revision History:
  12. AdriaO 06/15/2000 - Seperated out from ntos\io\flunkirp.c
  13. --*/
  14. #include "obp.h"
  15. #include "obvutil.h"
  16. //
  17. // When enabled, everything is locked down on demand...
  18. //
  19. #ifdef ALLOC_PRAGMA
  20. #pragma alloc_text(PAGEVRFY, ObvUtilStartObRefMonitoring)
  21. #pragma alloc_text(PAGEVRFY, ObvUtilStopObRefMonitoring)
  22. #endif
  23. LONG
  24. ObvUtilStartObRefMonitoring(
  25. IN PDEVICE_OBJECT DeviceObject
  26. )
  27. /*++
  28. Description:
  29. Determines if ObRef has not been called between a call to this
  30. function and a subsequent call to ObvUtilStopObRefMonitoring.
  31. Arguments:
  32. Device object to monitor.
  33. Return Value:
  34. A start skew time to pass into ObvUtilStopObRefMonitoring.
  35. N.B. - A reference count is taken by this API and released
  36. by ObvUtilStopObRefMonitoring. That reference is not
  37. counted among the noticed calls to ObRef.
  38. --*/
  39. {
  40. #if DBG
  41. POBJECT_HEADER ObjectHeader;
  42. POBJECT_HEADER_NAME_INFO NameInfo;
  43. LONG startSkew, pointerCount ;
  44. ObReferenceObject(DeviceObject) ;
  45. ObjectHeader = OBJECT_TO_OBJECT_HEADER(DeviceObject);
  46. NameInfo = OBJECT_HEADER_TO_NAME_INFO( ObjectHeader );
  47. ASSERT(NameInfo) ;
  48. //
  49. // We will always decrement DbgDereferenceCount prior to PointerCount,
  50. // so any race conditions will look like an increment occured, which
  51. // is an allowable misread...
  52. //
  53. do {
  54. pointerCount = ObjectHeader->PointerCount ;
  55. startSkew = pointerCount - NameInfo->DbgDereferenceCount ;
  56. } while(pointerCount != ObjectHeader->PointerCount) ;
  57. return startSkew ;
  58. #else
  59. return 1;
  60. #endif
  61. }
  62. LONG
  63. ObvUtilStopObRefMonitoring(
  64. IN PDEVICE_OBJECT DeviceObject,
  65. IN LONG StartSkew
  66. )
  67. /*++
  68. Description:
  69. Determines if ObRef has not been called between a call to
  70. ObvUtilStartObRefMonitoring and a call to this API.
  71. In a race condition (say ObDereferenceObject is ran in-simo
  72. with this function), the return is gaurenteed to err on
  73. the side of a reference occuring.
  74. Arguments:
  75. Device Object and the skew returned by ObvUtilStartObRefMonitoring
  76. Return Value:
  77. Number of calls to ObRef that occured throughout the monitored timeframe.
  78. Note that the return could be positive even though the reference count
  79. actually dropped (ie, one ObRef and two ObDeref's).
  80. --*/
  81. {
  82. #if DBG
  83. POBJECT_HEADER ObjectHeader;
  84. POBJECT_HEADER_NAME_INFO NameInfo;
  85. LONG currentSkew, refDelta, pointerCount ;
  86. ObjectHeader = OBJECT_TO_OBJECT_HEADER(DeviceObject);
  87. NameInfo = OBJECT_HEADER_TO_NAME_INFO( ObjectHeader );
  88. ASSERT(NameInfo) ;
  89. //
  90. // We will always decrement DbgDereferenceCount prior to PointerCount,
  91. // so any race conditions will look like an increment occured, which
  92. // is an allowable misread...
  93. //
  94. do {
  95. pointerCount = ObjectHeader->PointerCount ;
  96. currentSkew = pointerCount - NameInfo->DbgDereferenceCount ;
  97. } while(pointerCount != ObjectHeader->PointerCount) ;
  98. refDelta = currentSkew - StartSkew ;
  99. ASSERT(refDelta>=0) ;
  100. ObDereferenceObject(DeviceObject) ;
  101. return refDelta ;
  102. #else
  103. return 1;
  104. #endif
  105. }