RandomIRBuilder.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //===- RandomIRBuilder.h - Utils for randomly mutation IR -------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Provides the Mutator class, which is used to mutate IR for fuzzing.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_FUZZMUTATE_RANDOMIRBUILDER_H
  13. #define LLVM_FUZZMUTATE_RANDOMIRBUILDER_H
  14. #include "llvm/ADT/SmallPtrSet.h"
  15. #include "llvm/FuzzMutate/IRMutator.h"
  16. #include "llvm/FuzzMutate/Random.h"
  17. #include <random>
  18. namespace llvm {
  19. using RandomEngine = std::mt19937;
  20. struct RandomIRBuilder {
  21. RandomEngine Rand;
  22. SmallVector<Type *, 16> KnownTypes;
  23. RandomIRBuilder(int Seed, ArrayRef<Type *> AllowedTypes)
  24. : Rand(Seed), KnownTypes(AllowedTypes.begin(), AllowedTypes.end()) {}
  25. // TODO: Try to make this a bit less of a random mishmash of functions.
  26. /// Find a "source" for some operation, which will be used in one of the
  27. /// operation's operands. This either selects an instruction in \c Insts or
  28. /// returns some new arbitrary Value.
  29. Value *findOrCreateSource(BasicBlock &BB, ArrayRef<Instruction *> Insts);
  30. /// Find a "source" for some operation, which will be used in one of the
  31. /// operation's operands. This either selects an instruction in \c Insts that
  32. /// matches \c Pred, or returns some new Value that matches \c Pred. The
  33. /// values in \c Srcs should be source operands that have already been
  34. /// selected.
  35. Value *findOrCreateSource(BasicBlock &BB, ArrayRef<Instruction *> Insts,
  36. ArrayRef<Value *> Srcs, fuzzerop::SourcePred Pred);
  37. /// Create some Value suitable as a source for some operation.
  38. Value *newSource(BasicBlock &BB, ArrayRef<Instruction *> Insts,
  39. ArrayRef<Value *> Srcs, fuzzerop::SourcePred Pred);
  40. /// Find a viable user for \c V in \c Insts, which should all be contained in
  41. /// \c BB. This may also create some new instruction in \c BB and use that.
  42. void connectToSink(BasicBlock &BB, ArrayRef<Instruction *> Insts, Value *V);
  43. /// Create a user for \c V in \c BB.
  44. void newSink(BasicBlock &BB, ArrayRef<Instruction *> Insts, Value *V);
  45. Value *findPointer(BasicBlock &BB, ArrayRef<Instruction *> Insts,
  46. ArrayRef<Value *> Srcs, fuzzerop::SourcePred Pred);
  47. Type *chooseType(LLVMContext &Context, ArrayRef<Value *> Srcs,
  48. fuzzerop::SourcePred Pred);
  49. };
  50. } // end llvm namespace
  51. #endif // LLVM_FUZZMUTATE_RANDOMIRBUILDER_H