LLVM 20.0.0git
RandomIRBuilder.h
Go to the documentation of this file.
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
13#ifndef LLVM_FUZZMUTATE_RANDOMIRBUILDER_H
14#define LLVM_FUZZMUTATE_RANDOMIRBUILDER_H
15
16#include "llvm/ADT/ArrayRef.h"
18#include <random>
19
20namespace llvm {
21class AllocaInst;
22class BasicBlock;
23class Function;
24class GlobalVariable;
25class Instruction;
26class LLVMContext;
27class Module;
28class Type;
29class Value;
30
31namespace fuzzerop {
32class SourcePred;
33}
34
35using RandomEngine = std::mt19937;
36
40
44
46 : Rand(Seed), KnownTypes(AllowedTypes) {}
47
48 // TODO: Try to make this a bit less of a random mishmash of functions.
49
50 /// Create a stack memory at the head of the function, store \c Init to the
51 /// memory if provided.
53 /// Find or create a global variable. It will be initialized by random
54 /// constants that satisfies \c Pred. It will also report whether this global
55 /// variable found or created.
56 std::pair<GlobalVariable *, bool>
66 };
67 /// Find a "source" for some operation, which will be used in one of the
68 /// operation's operands. This either selects an instruction in \c Insts or
69 /// returns some new arbitrary Value.
71 /// Find a "source" for some operation, which will be used in one of the
72 /// operation's operands. This either selects an instruction in \c Insts that
73 /// matches \c Pred, or returns some new Value that matches \c Pred. The
74 /// values in \c Srcs should be source operands that have already been
75 /// selected.
78 bool allowConstant = true);
79 /// Create some Value suitable as a source for some operation.
82 bool allowConstant = true);
83
84 enum SinkType {
85 /// TODO: Also consider pointers in function argument.
92 };
93 /// Find a viable user for \c V in \c Insts, which should all be contained in
94 /// \c BB. This may also create some new instruction in \c BB and use that.
96 Value *V);
97 /// Create a user for \c V in \c BB.
100 /// Return a uniformly choosen type from \c AllowedTypes
101 Type *randomType();
106};
107
108} // namespace llvm
109
110#endif // LLVM_FUZZMUTATE_RANDOMIRBUILDER_H
RelocType Type
Definition: COFFYAML.cpp:391
#define F(x, y, z)
Definition: MD5.cpp:55
Machine Check Debug Module
static ManagedStatic< cl::opt< uint64_t >, CreateSeed > Seed
This file defines the SmallVector class.
an instruction to allocate memory on the stack
Definition: Instructions.h:61
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
A matcher/generator for finding suitable values for the next source in an operation's partially compl...
Definition: OpDescriptor.h:43
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::mt19937 RandomEngine
Function * createFunctionDeclaration(Module &M, uint64_t ArgNum)
SmallVector< Type *, 16 > KnownTypes
RandomIRBuilder(int Seed, ArrayRef< Type * > AllowedTypes)
std::pair< GlobalVariable *, bool > findOrCreateGlobalVariable(Module *M, ArrayRef< Value * > Srcs, fuzzerop::SourcePred Pred)
Find or create a global variable.
Value * findOrCreateSource(BasicBlock &BB, ArrayRef< Instruction * > Insts)
Find a "source" for some operation, which will be used in one of the operation's operands.
AllocaInst * createStackMemory(Function *F, Type *Ty, Value *Init=nullptr)
Create a stack memory at the head of the function, store Init to the memory if provided.
Instruction * newSink(BasicBlock &BB, ArrayRef< Instruction * > Insts, Value *V)
Create a user for V in BB.
Function * createFunctionDefinition(Module &M, uint64_t ArgNum)
Value * newSource(BasicBlock &BB, ArrayRef< Instruction * > Insts, ArrayRef< Value * > Srcs, fuzzerop::SourcePred Pred, bool allowConstant=true)
Create some Value suitable as a source for some operation.
Instruction * connectToSink(BasicBlock &BB, ArrayRef< Instruction * > Insts, Value *V)
Find a viable user for V in Insts, which should all be contained in BB.
@ SinkToInstInCurBlock
TODO: Also consider pointers in function argument.
Value * findPointer(BasicBlock &BB, ArrayRef< Instruction * > Insts)
Type * randomType()
Return a uniformly choosen type from AllowedTypes.