GuardObjects.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /* Implementation of macros to ensure correct use of RAII Auto* objects. */
  6. #ifndef mozilla_GuardObjects_h
  7. #define mozilla_GuardObjects_h
  8. #include "mozilla/Assertions.h"
  9. #include "mozilla/Move.h"
  10. #include "mozilla/Types.h"
  11. #ifdef __cplusplus
  12. #ifdef DEBUG
  13. /**
  14. * A custom define is used rather than |mozPoisonValue()| due to cascading
  15. * build failures relating to how mfbt is linked on different operating
  16. * systems. See bug 1160253.
  17. */
  18. #define MOZ_POISON uintptr_t(-1)
  19. namespace mozilla {
  20. namespace detail {
  21. /*
  22. * The following classes are designed to cause assertions to detect
  23. * inadvertent use of guard objects as temporaries. In other words,
  24. * when we have a guard object whose only purpose is its constructor and
  25. * destructor (and is never otherwise referenced), the intended use
  26. * might be:
  27. *
  28. * AutoRestore savePainting(mIsPainting);
  29. *
  30. * but is is easy to accidentally write:
  31. *
  32. * AutoRestore(mIsPainting);
  33. *
  34. * which compiles just fine, but runs the destructor well before the
  35. * intended time.
  36. *
  37. * They work by adding (#ifdef DEBUG) an additional parameter to the
  38. * guard object's constructor, with a default value, so that users of
  39. * the guard object's API do not need to do anything. The default value
  40. * of this parameter is a temporary object. C++ (ISO/IEC 14882:1998),
  41. * section 12.2 [class.temporary], clauses 4 and 5 seem to assume a
  42. * guarantee that temporaries are destroyed in the reverse of their
  43. * construction order, but I actually can't find a statement that that
  44. * is true in the general case (beyond the two specific cases mentioned
  45. * there). However, it seems to be true.
  46. *
  47. * These classes are intended to be used only via the macros immediately
  48. * below them:
  49. *
  50. * MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER declares (ifdef DEBUG) a member
  51. * variable, and should be put where a declaration of a private
  52. * member variable would be placed.
  53. * MOZ_GUARD_OBJECT_NOTIFIER_PARAM should be placed at the end of the
  54. * parameters to each constructor of the guard object; it declares
  55. * (ifdef DEBUG) an additional parameter. (But use the *_ONLY_PARAM
  56. * variant for constructors that take no other parameters.)
  57. * MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL should likewise be used in
  58. * the implementation of such constructors when they are not inline.
  59. * MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT should be used in
  60. * the implementation of such constructors to pass the parameter to
  61. * a base class that also uses these macros
  62. * MOZ_GUARD_OBJECT_NOTIFIER_INIT is a statement that belongs in each
  63. * constructor. It uses the parameter declared by
  64. * MOZ_GUARD_OBJECT_NOTIFIER_PARAM.
  65. *
  66. * For more details, and examples of using these macros, see
  67. * https://developer.mozilla.org/en/Using_RAII_classes_in_Mozilla
  68. */
  69. class GuardObjectNotifier
  70. {
  71. private:
  72. bool* mStatementDone;
  73. public:
  74. GuardObjectNotifier()
  75. : mStatementDone(reinterpret_cast<bool*>(MOZ_POISON))
  76. {
  77. }
  78. ~GuardObjectNotifier()
  79. {
  80. // Assert that the GuardObjectNotifier has been properly initialized by
  81. // using the |MOZ_GUARD_OBJECT_NOTIFIER_INIT| macro. A poison value is
  82. // used rather than a null check to appease static analyzers that were
  83. // (incorrectly) detecting null pointer dereferences.
  84. MOZ_ASSERT(mStatementDone != reinterpret_cast<bool*>(MOZ_POISON));
  85. *mStatementDone = true;
  86. }
  87. void setStatementDone(bool* aStatementIsDone)
  88. {
  89. mStatementDone = aStatementIsDone;
  90. }
  91. };
  92. class GuardObjectNotificationReceiver
  93. {
  94. private:
  95. bool mStatementDone;
  96. public:
  97. GuardObjectNotificationReceiver() : mStatementDone(false) { }
  98. ~GuardObjectNotificationReceiver() {
  99. /*
  100. * Assert that the guard object was not used as a temporary. (Note that
  101. * this assert might also fire if init is not called because the guard
  102. * object's implementation is not using the above macros correctly.)
  103. */
  104. MOZ_ASSERT(mStatementDone);
  105. }
  106. void init(GuardObjectNotifier& aNotifier)
  107. {
  108. aNotifier.setStatementDone(&mStatementDone);
  109. }
  110. };
  111. } /* namespace detail */
  112. } /* namespace mozilla */
  113. #undef MOZ_POISON
  114. #endif /* DEBUG */
  115. #ifdef DEBUG
  116. # define MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER \
  117. mozilla::detail::GuardObjectNotificationReceiver _mCheckNotUsedAsTemporary;
  118. # define MOZ_GUARD_OBJECT_NOTIFIER_PARAM \
  119. , mozilla::detail::GuardObjectNotifier&& _notifier = \
  120. mozilla::detail::GuardObjectNotifier()
  121. # define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM \
  122. mozilla::detail::GuardObjectNotifier&& _notifier = \
  123. mozilla::detail::GuardObjectNotifier()
  124. # define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL \
  125. , mozilla::detail::GuardObjectNotifier&& _notifier
  126. # define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_IN_IMPL \
  127. mozilla::detail::GuardObjectNotifier&& _notifier
  128. # define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT \
  129. , mozilla::Move(_notifier)
  130. # define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_TO_PARENT \
  131. mozilla::Move(_notifier)
  132. # define MOZ_GUARD_OBJECT_NOTIFIER_INIT \
  133. do { _mCheckNotUsedAsTemporary.init(_notifier); } while (0)
  134. #else
  135. # define MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
  136. # define MOZ_GUARD_OBJECT_NOTIFIER_PARAM
  137. # define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM
  138. # define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL
  139. # define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_IN_IMPL
  140. # define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_TO_PARENT
  141. # define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT
  142. # define MOZ_GUARD_OBJECT_NOTIFIER_INIT do { } while (0)
  143. #endif
  144. #endif /* __cplusplus */
  145. #endif /* mozilla_GuardObjects_h */