RandomIRBuilder.h 2.8 KB

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