File indexing completed on 2026-05-10 08:43:56
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef LLVM_FUZZMUTATE_RANDOMIRBUILDER_H
0014 #define LLVM_FUZZMUTATE_RANDOMIRBUILDER_H
0015
0016 #include "llvm/ADT/ArrayRef.h"
0017 #include "llvm/ADT/SmallVector.h"
0018 #include <random>
0019
0020 namespace llvm {
0021 class AllocaInst;
0022 class BasicBlock;
0023 class Function;
0024 class GlobalVariable;
0025 class Instruction;
0026 class LLVMContext;
0027 class Module;
0028 class Type;
0029 class Value;
0030
0031 namespace fuzzerop {
0032 class SourcePred;
0033 }
0034
0035 using RandomEngine = std::mt19937;
0036
0037 struct RandomIRBuilder {
0038 RandomEngine Rand;
0039 SmallVector<Type *, 16> KnownTypes;
0040
0041 uint64_t MinArgNum = 0;
0042 uint64_t MaxArgNum = 5;
0043 uint64_t MinFunctionNum = 1;
0044
0045 RandomIRBuilder(int Seed, ArrayRef<Type *> AllowedTypes)
0046 : Rand(Seed), KnownTypes(AllowedTypes) {}
0047
0048
0049
0050
0051
0052 AllocaInst *createStackMemory(Function *F, Type *Ty, Value *Init = nullptr);
0053
0054
0055
0056 std::pair<GlobalVariable *, bool>
0057 findOrCreateGlobalVariable(Module *M, ArrayRef<Value *> Srcs,
0058 fuzzerop::SourcePred Pred);
0059 enum SourceType {
0060 SrcFromInstInCurBlock,
0061 FunctionArgument,
0062 InstInDominator,
0063 SrcFromGlobalVariable,
0064 NewConstOrStack,
0065 EndOfValueSource,
0066 };
0067
0068
0069
0070 Value *findOrCreateSource(BasicBlock &BB, ArrayRef<Instruction *> Insts);
0071
0072
0073
0074
0075
0076 Value *findOrCreateSource(BasicBlock &BB, ArrayRef<Instruction *> Insts,
0077 ArrayRef<Value *> Srcs, fuzzerop::SourcePred Pred,
0078 bool allowConstant = true);
0079
0080 Value *newSource(BasicBlock &BB, ArrayRef<Instruction *> Insts,
0081 ArrayRef<Value *> Srcs, fuzzerop::SourcePred Pred,
0082 bool allowConstant = true);
0083
0084 enum SinkType {
0085
0086 SinkToInstInCurBlock,
0087 PointersInDominator,
0088 InstInDominatee,
0089 NewStore,
0090 SinkToGlobalVariable,
0091 EndOfValueSink,
0092 };
0093
0094
0095 Instruction *connectToSink(BasicBlock &BB, ArrayRef<Instruction *> Insts,
0096 Value *V);
0097
0098 Instruction *newSink(BasicBlock &BB, ArrayRef<Instruction *> Insts, Value *V);
0099 Value *findPointer(BasicBlock &BB, ArrayRef<Instruction *> Insts);
0100
0101 Type *randomType();
0102 Function *createFunctionDeclaration(Module &M, uint64_t ArgNum);
0103 Function *createFunctionDeclaration(Module &M);
0104 Function *createFunctionDefinition(Module &M, uint64_t ArgNum);
0105 Function *createFunctionDefinition(Module &M);
0106 };
0107
0108 }
0109
0110 #endif