overflow_id_source.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (c) 2020 Google LLC
  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. #ifndef SOURCE_FUZZ_OVERFLOW_ID_SOURCE_H_
  15. #define SOURCE_FUZZ_OVERFLOW_ID_SOURCE_H_
  16. #include <cstdint>
  17. #include <unordered_set>
  18. namespace spvtools {
  19. namespace fuzz {
  20. // An implementation of this interface can be used to provide fresh ids on
  21. // demand when applying a transformation.
  22. //
  23. // During fuzzing this should never be required: a fuzzer pass should determine
  24. // all the fresh ids it requires to apply a transformation.
  25. //
  26. // However, during shrinking we can have the situation where, after removing
  27. // an early transformation, a later transformation needs more ids.
  28. //
  29. // As an example, suppose a SPIR-V function originally has this form:
  30. //
  31. // main() {
  32. // stmt1;
  33. // stmt2;
  34. // stmt3;
  35. // stmt4;
  36. // }
  37. //
  38. // Now suppose two *outlining* transformations are applied. The first
  39. // transformation, T1, outlines "stmt1; stmt2;" into a function foo, giving us:
  40. //
  41. // foo() {
  42. // stmt1;
  43. // stmt2;
  44. // }
  45. //
  46. // main() {
  47. // foo();
  48. // stmt3;
  49. // stmt4;
  50. // }
  51. //
  52. // The second transformation, T2, outlines "foo(); stmt3;" from main into a
  53. // function bar, giving us:
  54. //
  55. // foo() {
  56. // stmt1;
  57. // stmt2;
  58. // }
  59. //
  60. // bar() {
  61. // foo();
  62. // stmt3;
  63. // }
  64. //
  65. // main() {
  66. // bar();
  67. // stmt4;
  68. // }
  69. //
  70. // Suppose that T2 used a set of fresh ids, FRESH, in order to perform its
  71. // outlining.
  72. //
  73. // Now suppose that during shrinking we remove T1, but still want to apply T2.
  74. // The fresh ids used by T2 - FRESH - are sufficient to outline "foo(); stmt3;".
  75. // However, because we did not apply T1, "foo();" does not exist and instead the
  76. // task of T2 is to outline "stmt1; stmt2; stmt3;". The set FRESH contains
  77. // *some* of the fresh ids required to do this (those for "stmt3;"), but not all
  78. // of them (those for "stmt1; stmt2;" are missing).
  79. //
  80. // A source of overflow ids can be used to allow the shrinker to proceed
  81. // nevertheless.
  82. //
  83. // It is desirable to use overflow ids only when needed. In our worked example,
  84. // T2 should still use the ids from FRESH when handling "stmt3;", because later
  85. // transformations might refer to those ids and will become inapplicable if
  86. // overflow ids are used instead.
  87. class OverflowIdSource {
  88. public:
  89. virtual ~OverflowIdSource();
  90. // Returns true if and only if this source is capable of providing overflow
  91. // ids.
  92. virtual bool HasOverflowIds() const = 0;
  93. // Precondition: HasOverflowIds() must hold. Returns the next available
  94. // overflow id.
  95. virtual uint32_t GetNextOverflowId() = 0;
  96. // Returns the set of overflow ids from this source that have been previously
  97. // issued via calls to GetNextOverflowId().
  98. virtual const std::unordered_set<uint32_t>& GetIssuedOverflowIds() const = 0;
  99. };
  100. } // namespace fuzz
  101. } // namespace spvtools
  102. #endif // SOURCE_FUZZ_OVERFLOW_ID_SOURCE_H_