transformation_add_function.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright (c) 2019 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_TRANSFORMATION_ADD_FUNCTION_H_
  15. #define SOURCE_FUZZ_TRANSFORMATION_ADD_FUNCTION_H_
  16. #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
  17. #include "source/fuzz/transformation.h"
  18. #include "source/fuzz/transformation_context.h"
  19. #include "source/opt/ir_context.h"
  20. namespace spvtools {
  21. namespace fuzz {
  22. class TransformationAddFunction : public Transformation {
  23. public:
  24. explicit TransformationAddFunction(
  25. protobufs::TransformationAddFunction message);
  26. // Creates a transformation to add a non live-safe function.
  27. explicit TransformationAddFunction(
  28. const std::vector<protobufs::Instruction>& instructions);
  29. // Creates a transformation to add a live-safe function.
  30. TransformationAddFunction(
  31. const std::vector<protobufs::Instruction>& instructions,
  32. uint32_t loop_limiter_variable_id, uint32_t loop_limit_constant_id,
  33. const std::vector<protobufs::LoopLimiterInfo>& loop_limiters,
  34. uint32_t kill_unreachable_return_value_id,
  35. const std::vector<protobufs::AccessChainClampingInfo>&
  36. access_chain_clampers);
  37. // - |message_.instruction| must correspond to a sufficiently well-formed
  38. // sequence of instructions that a function can be created from them
  39. // - If |message_.is_livesafe| holds then |message_| must contain suitable
  40. // ingredients to make the function livesafe, and the function must only
  41. // invoke other livesafe functions
  42. // - Adding the created function to the module must lead to a valid module.
  43. bool IsApplicable(
  44. opt::IRContext* ir_context,
  45. const TransformationContext& transformation_context) const override;
  46. // Adds the function defined by |message_.instruction| to the module, making
  47. // it livesafe if |message_.is_livesafe| holds.
  48. void Apply(opt::IRContext* ir_context,
  49. TransformationContext* transformation_context) const override;
  50. std::unordered_set<uint32_t> GetFreshIds() const override;
  51. protobufs::Transformation ToMessage() const override;
  52. // Helper method that, given composite type |composite_type_inst|, returns the
  53. // type of the sub-object at index |index_id|, which is required to be in-
  54. // bounds.
  55. static opt::Instruction* FollowCompositeIndex(
  56. opt::IRContext* ir_context, const opt::Instruction& composite_type_inst,
  57. uint32_t index_id);
  58. // Returns id of the back-edge block, given the corresponding
  59. // |loop_header_block_id|. |loop_header_block_id| must be the id of a loop
  60. // header block. Returns 0 if the loop has no back-edge block.
  61. static uint32_t GetBackEdgeBlockId(opt::IRContext* ir_context,
  62. uint32_t loop_header_block_id);
  63. // Attempts to create a function from the series of instructions in
  64. // |message_.instruction| and add it to |ir_context|.
  65. //
  66. // Returns false if adding the function is not possible due to the messages
  67. // not respecting the basic structure of a function, e.g. if there is no
  68. // OpFunction instruction or no blocks; in this case |ir_context| is left in
  69. // an indeterminate state.
  70. //
  71. // Otherwise returns true. Whether |ir_context| is valid after addition of
  72. // the function depends on the contents of |message_.instruction|.
  73. //
  74. // Intended usage:
  75. // - Perform a dry run of this method on a clone of a module, and use
  76. // the validator to check whether the resulting module is valid. Working
  77. // on a clone means it does not matter if the function fails to be cleanly
  78. // added, or leads to an invalid module.
  79. // - If the dry run succeeds, run the method on the real module of interest,
  80. // to add the function.
  81. bool TryToAddFunction(opt::IRContext* ir_context) const;
  82. private:
  83. // Should only be called if |message_.is_livesafe| holds. Attempts to make
  84. // the function livesafe (see FactFunctionIsLivesafe for a definition).
  85. // Returns false if this is not possible, due to |message_| or |ir_context|
  86. // not containing sufficient ingredients (such as types and fresh ids) to add
  87. // the instrumentation necessary to make the function livesafe.
  88. bool TryToMakeFunctionLivesafe(
  89. opt::IRContext* ir_context,
  90. const TransformationContext& transformation_context) const;
  91. // A helper for TryToMakeFunctionLivesafe that tries to add loop-limiting
  92. // logic.
  93. bool TryToAddLoopLimiters(opt::IRContext* ir_context,
  94. opt::Function* added_function) const;
  95. // A helper for TryToMakeFunctionLivesafe that tries to replace OpKill and
  96. // OpUnreachable instructions into return instructions.
  97. bool TryToTurnKillOrUnreachableIntoReturn(
  98. opt::IRContext* ir_context, opt::Function* added_function,
  99. opt::Instruction* kill_or_unreachable_inst) const;
  100. // A helper for TryToMakeFunctionLivesafe that tries to clamp access chain
  101. // indices so that they are guaranteed to be in-bounds.
  102. bool TryToClampAccessChainIndices(opt::IRContext* ir_context,
  103. opt::Instruction* access_chain_inst) const;
  104. protobufs::TransformationAddFunction message_;
  105. };
  106. } // namespace fuzz
  107. } // namespace spvtools
  108. #endif // SOURCE_FUZZ_TRANSFORMATION_ADD_FUNCTION_H_