RandomIRBuilder.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/ArrayRef.h"
  20. #include "llvm/ADT/SmallVector.h"
  21. #include <random>
  22. namespace llvm {
  23. class BasicBlock;
  24. class Instruction;
  25. class LLVMContext;
  26. class Type;
  27. class Value;
  28. namespace fuzzerop {
  29. class SourcePred;
  30. }
  31. using RandomEngine = std::mt19937;
  32. struct RandomIRBuilder {
  33. RandomEngine Rand;
  34. SmallVector<Type *, 16> KnownTypes;
  35. RandomIRBuilder(int Seed, ArrayRef<Type *> AllowedTypes)
  36. : Rand(Seed), KnownTypes(AllowedTypes.begin(), AllowedTypes.end()) {}
  37. // TODO: Try to make this a bit less of a random mishmash of functions.
  38. /// Find a "source" for some operation, which will be used in one of the
  39. /// operation's operands. This either selects an instruction in \c Insts or
  40. /// returns some new arbitrary Value.
  41. Value *findOrCreateSource(BasicBlock &BB, ArrayRef<Instruction *> Insts);
  42. /// Find a "source" for some operation, which will be used in one of the
  43. /// operation's operands. This either selects an instruction in \c Insts that
  44. /// matches \c Pred, or returns some new Value that matches \c Pred. The
  45. /// values in \c Srcs should be source operands that have already been
  46. /// selected.
  47. Value *findOrCreateSource(BasicBlock &BB, ArrayRef<Instruction *> Insts,
  48. ArrayRef<Value *> Srcs, fuzzerop::SourcePred Pred,
  49. bool allowConstant = true);
  50. /// Create some Value suitable as a source for some operation.
  51. Value *newSource(BasicBlock &BB, ArrayRef<Instruction *> Insts,
  52. ArrayRef<Value *> Srcs, fuzzerop::SourcePred Pred,
  53. bool allowConstant = true);
  54. /// Find a viable user for \c V in \c Insts, which should all be contained in
  55. /// \c BB. This may also create some new instruction in \c BB and use that.
  56. void connectToSink(BasicBlock &BB, ArrayRef<Instruction *> Insts, Value *V);
  57. /// Create a user for \c V in \c BB.
  58. void newSink(BasicBlock &BB, ArrayRef<Instruction *> Insts, Value *V);
  59. Value *findPointer(BasicBlock &BB, ArrayRef<Instruction *> Insts,
  60. ArrayRef<Value *> Srcs, fuzzerop::SourcePred Pred);
  61. Type *chooseType(LLVMContext &Context, ArrayRef<Value *> Srcs,
  62. fuzzerop::SourcePred Pred);
  63. /// Return a uniformly choosen type from \c AllowedTypes
  64. Type *randomType();
  65. };
  66. } // namespace llvm
  67. #endif // LLVM_FUZZMUTATE_RANDOMIRBUILDER_H
  68. #ifdef __GNUC__
  69. #pragma GCC diagnostic pop
  70. #endif