LLVM 22.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"
19#include <random>
20
21namespace llvm {
22class AllocaInst;
23class BasicBlock;
24class Function;
25class GlobalVariable;
26class Instruction;
27class LLVMContext;
28class Module;
29class Type;
30class Value;
31
32namespace fuzzerop {
33class SourcePred;
34}
35
36using RandomEngine = std::mt19937;
37
41
45
47 : Rand(Seed), KnownTypes(AllowedTypes) {}
48
49 // TODO: Try to make this a bit less of a random mishmash of functions.
50
51 /// Create a stack memory at the head of the function, store \c Init to the
52 /// memory if provided.
54 Value *Init = nullptr);
55 /// Find or create a global variable. It will be initialized by random
56 /// constants that satisfies \c Pred. It will also report whether this global
57 /// variable found or created.
58 LLVM_ABI std::pair<GlobalVariable *, bool>
68 };
69 /// Find a "source" for some operation, which will be used in one of the
70 /// operation's operands. This either selects an instruction in \c Insts or
71 /// returns some new arbitrary Value.
74 /// Find a "source" for some operation, which will be used in one of the
75 /// operation's operands. This either selects an instruction in \c Insts that
76 /// matches \c Pred, or returns some new Value that matches \c Pred. The
77 /// values in \c Srcs should be source operands that have already been
78 /// selected.
83 bool allowConstant = true);
84 /// Create some Value suitable as a source for some operation.
87 bool allowConstant = true);
88
89 enum SinkType {
90 /// TODO: Also consider pointers in function argument.
97 };
98 /// Find a viable user for \c V in \c Insts, which should all be contained in
99 /// \c BB. This may also create some new instruction in \c BB and use that.
102 /// Create a user for \c V in \c BB.
104 Value *V);
106 /// Return a uniformly choosen type from \c AllowedTypes
112};
113
114} // namespace llvm
115
116#endif // LLVM_FUZZMUTATE_RANDOMIRBUILDER_H
RelocType Type
Definition: COFFYAML.cpp:410
#define LLVM_ABI
Definition: Compiler.h:213
#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:64
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:62
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
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:75
A matcher/generator for finding suitable values for the next source in an operation's partially compl...
Definition: OpDescriptor.h:44
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:81
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::mt19937 RandomEngine
LLVM_ABI Function * createFunctionDeclaration(Module &M, uint64_t ArgNum)
SmallVector< Type *, 16 > KnownTypes
RandomIRBuilder(int Seed, ArrayRef< Type * > AllowedTypes)
LLVM_ABI std::pair< GlobalVariable *, bool > findOrCreateGlobalVariable(Module *M, ArrayRef< Value * > Srcs, fuzzerop::SourcePred Pred)
Find or create a global variable.
LLVM_ABI Value * findOrCreateSource(BasicBlock &BB, ArrayRef< Instruction * > Insts)
Find a "source" for some operation, which will be used in one of the operation's operands.
LLVM_ABI 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.
LLVM_ABI Instruction * newSink(BasicBlock &BB, ArrayRef< Instruction * > Insts, Value *V)
Create a user for V in BB.
LLVM_ABI Function * createFunctionDefinition(Module &M, uint64_t ArgNum)
LLVM_ABI 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.
LLVM_ABI 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.
LLVM_ABI Value * findPointer(BasicBlock &BB, ArrayRef< Instruction * > Insts)
LLVM_ABI Type * randomType()
Return a uniformly choosen type from AllowedTypes.