Skip to content

Commit db2aa9f

Browse files
committed
[FuzzMutate] change of format and comment for further code
Signed-off-by: Peter Rong <[email protected]>
1 parent f8c63a7 commit db2aa9f

File tree

12 files changed

+63
-67
lines changed

12 files changed

+63
-67
lines changed

llvm/include/llvm/FuzzMutate/FuzzerCLI.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ using FuzzerInitFun = int (*)(int *argc, char ***argv);
4848
///
4949
/// Useful for testing fuzz targets without linking to libFuzzer. Finds inputs
5050
/// in the argument list in a libFuzzer compatible way.
51-
int runFuzzerOnInputs(int ArgC, char *ArgV[], FuzzerTestFun TestOne,
52-
FuzzerInitFun Init = [](int *, char ***) { return 0; });
51+
int runFuzzerOnInputs(
52+
int ArgC, char *ArgV[], FuzzerTestFun TestOne,
53+
FuzzerInitFun Init = [](int *, char ***) { return 0; });
5354

5455
} // namespace llvm
5556

llvm/include/llvm/FuzzMutate/IRMutator.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class InjectorIRStrategy : public IRMutationStrategy {
9595
void mutate(BasicBlock &BB, RandomIRBuilder &IB) override;
9696
};
9797

98+
/// Strategy that deletes instructions when the Module is too large.
9899
class InstDeleterIRStrategy : public IRMutationStrategy {
99100
public:
100101
uint64_t getWeight(size_t CurrentSize, size_t MaxSize,
@@ -105,6 +106,7 @@ class InstDeleterIRStrategy : public IRMutationStrategy {
105106
void mutate(Instruction &Inst, RandomIRBuilder &IB) override;
106107
};
107108

109+
/// Strategy that modifies instruction attributes and operands.
108110
class InstModificationIRStrategy : public IRMutationStrategy {
109111
public:
110112
uint64_t getWeight(size_t CurrentSize, size_t MaxSize,
@@ -139,6 +141,6 @@ size_t writeModule(const Module &M, uint8_t *Dest, size_t MaxSize);
139141
std::unique_ptr<Module> parseAndVerify(const uint8_t *Data, size_t Size,
140142
LLVMContext &Context);
141143

142-
} // end llvm namespace
144+
} // namespace llvm
143145

144146
#endif // LLVM_FUZZMUTATE_IRMUTATOR_H

llvm/include/llvm/FuzzMutate/OpDescriptor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ static inline SourcePred matchScalarOfFirstType() {
216216
return {Pred, Make};
217217
}
218218

219-
} // end fuzzerop namespace
220-
} // end llvm namespace
219+
} // namespace fuzzerop
220+
} // namespace llvm
221221

222222
#endif // LLVM_FUZZMUTATE_OPDESCRIPTOR_H

llvm/include/llvm/FuzzMutate/Operations.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ OpDescriptor insertElementDescriptor(unsigned Weight);
4646
OpDescriptor shuffleVectorDescriptor(unsigned Weight);
4747
/// @}
4848

49-
} // end fuzzerop namespace
49+
} // namespace fuzzerop
5050

51-
} // end llvm namespace
51+
} // namespace llvm
5252

5353
#endif // LLVM_FUZZMUTATE_OPERATIONS_H

llvm/include/llvm/FuzzMutate/Random.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
#ifndef LLVM_FUZZMUTATE_RANDOM_H
1414
#define LLVM_FUZZMUTATE_RANDOM_H
1515

16-
#include <random>
1716
#include "llvm/Support/raw_ostream.h"
17+
#include <random>
1818
namespace llvm {
1919

2020
/// Return a uniformly distributed random value between \c Min and \c Max
@@ -46,7 +46,7 @@ template <typename T, typename GenT> class ReservoirSampler {
4646
return Selection;
4747
}
4848

49-
explicit operator bool() const { return !isEmpty();}
49+
explicit operator bool() const { return !isEmpty(); }
5050
const T &operator*() const { return getSelection(); }
5151

5252
/// Sample each item in \c Items with unit weight
@@ -91,6 +91,6 @@ ReservoirSampler<T, GenT> makeSampler(GenT &RandGen) {
9191
return ReservoirSampler<T, GenT>(RandGen);
9292
}
9393

94-
} // End llvm namespace
94+
} // namespace llvm
9595

9696
#endif // LLVM_FUZZMUTATE_RANDOM_H

llvm/include/llvm/FuzzMutate/RandomIRBuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@ struct RandomIRBuilder {
6363
fuzzerop::SourcePred Pred);
6464
};
6565

66-
} // end llvm namespace
66+
} // namespace llvm
6767

6868
#endif // LLVM_FUZZMUTATE_RANDOMIRBUILDER_H

llvm/lib/FuzzMutate/IRMutator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ void InstDeleterIRStrategy::mutate(Function &F, RandomIRBuilder &IB) {
162162
auto RS = makeSampler<Instruction *>(IB.Rand);
163163
for (Instruction &Inst : instructions(F)) {
164164
// TODO: We can't handle these instructions.
165-
if (Inst.isTerminator() || Inst.isEHPad() ||
166-
Inst.isSwiftError() || isa<PHINode>(Inst))
165+
if (Inst.isTerminator() || Inst.isEHPad() || Inst.isSwiftError() ||
166+
isa<PHINode>(Inst))
167167
continue;
168168

169169
RS.sample(&Inst, /*Weight=*/1);

llvm/lib/FuzzMutate/Operations.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ OpDescriptor llvm::fuzzerop::insertElementDescriptor(unsigned Weight) {
298298
auto buildInsert = [](ArrayRef<Value *> Srcs, Instruction *Inst) {
299299
return InsertElementInst::Create(Srcs[0], Srcs[1], Srcs[2], "I", Inst);
300300
};
301-
// TODO: Try to avoid undefined accesses.
301+
// TODO: Try to avoid undefined accesses.
302302
return {Weight,
303303
{anyVectorType(), matchScalarOfFirstType(), anyIntType()},
304304
buildInsert};
@@ -313,8 +313,8 @@ static SourcePred validShuffleVectorIndex() {
313313
auto *Int32Ty = Type::getInt32Ty(Cur[0]->getContext());
314314
// TODO: It's straighforward to make up reasonable values, but listing them
315315
// exhaustively would be insane. Come up with a couple of sensible ones.
316-
return std::vector<Constant *>{UndefValue::get(
317-
VectorType::get(Int32Ty, FirstTy->getElementCount()))};
316+
return std::vector<Constant *>{
317+
UndefValue::get(VectorType::get(Int32Ty, FirstTy->getElementCount()))};
318318
};
319319
return {Pred, Make};
320320
}

llvm/unittests/FuzzMutate/OperationsTest.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,18 @@ using namespace llvm;
4444

4545
using testing::AllOf;
4646
using testing::AnyOf;
47+
using testing::Each;
4748
using testing::ElementsAre;
4849
using testing::Eq;
4950
using testing::Ge;
50-
using testing::Each;
51-
using testing::Truly;
5251
using testing::NotNull;
5352
using testing::PrintToString;
5453
using testing::SizeIs;
54+
using testing::Truly;
5555

5656
namespace {
57-
std::unique_ptr<Module> parseAssembly(
58-
const char *Assembly, LLVMContext &Context) {
57+
std::unique_ptr<Module> parseAssembly(const char *Assembly,
58+
LLVMContext &Context) {
5959

6060
SMDiagnostic Error;
6161
std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context);
@@ -88,8 +88,7 @@ TEST(OperationsTest, SourcePreds) {
8888
Constant *f16 = ConstantFP::getInfinity(Type::getHalfTy(Ctx));
8989
Constant *f32 = ConstantFP::get(Type::getFloatTy(Ctx), 0.0);
9090
Constant *f64 = ConstantFP::get(Type::getDoubleTy(Ctx), 123.45);
91-
Constant *s =
92-
ConstantStruct::get(StructType::create(Ctx, "OpaqueStruct"));
91+
Constant *s = ConstantStruct::get(StructType::create(Ctx, "OpaqueStruct"));
9392
Constant *a =
9493
ConstantArray::get(ArrayType::get(i32->getType(), 2), {i32, i32});
9594
Constant *v8i8 = ConstantVector::getSplat(ElementCount::getFixed(8), i8);
@@ -166,8 +165,7 @@ TEST(OperationsTest, SourcePreds) {
166165
EXPECT_FALSE(First.matches({v4f16, f64}, f64));
167166

168167
EXPECT_THAT(First.generate({i8}, {}), Each(TypesMatch(i8)));
169-
EXPECT_THAT(First.generate({f16}, {i8->getType()}),
170-
Each(TypesMatch(f16)));
168+
EXPECT_THAT(First.generate({f16}, {i8->getType()}), Each(TypesMatch(f16)));
171169
EXPECT_THAT(First.generate({v8i8, i32}, {}), Each(TypesMatch(v8i8)));
172170
}
173171

@@ -235,7 +233,7 @@ TEST(OperationsTest, SplitEHBlock) {
235233

236234
fuzzerop::OpDescriptor Descr = fuzzerop::splitBlockDescriptor(1);
237235

238-
Descr.BuilderFunc({ConstantInt::getTrue(Ctx)},&*BB.getFirstInsertionPt());
236+
Descr.BuilderFunc({ConstantInt::getTrue(Ctx)}, &*BB.getFirstInsertionPt());
239237
ASSERT_TRUE(!verifyModule(*M, &errs()));
240238
}
241239

@@ -299,7 +297,6 @@ TEST(OperationsTest, GEP) {
299297
EXPECT_FALSE(verifyModule(M, &errs()));
300298
}
301299

302-
303300
TEST(OperationsTest, GEPPointerOperand) {
304301
// Check that we only pick sized pointers for the GEP instructions
305302

@@ -401,4 +398,4 @@ TEST(OperationsTest, ExtractAndInsertValue) {
401398
ElementsAre(ConstantInt::get(Int32Ty, 1)));
402399
}
403400

404-
}
401+
} // namespace

llvm/unittests/FuzzMutate/RandomIRBuilderTest.cpp

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ static constexpr int Seed = 5;
2828

2929
namespace {
3030

31-
std::unique_ptr<Module> parseAssembly(
32-
const char *Assembly, LLVMContext &Context) {
31+
std::unique_ptr<Module> parseAssembly(const char *Assembly,
32+
LLVMContext &Context) {
3333

3434
SMDiagnostic Error;
3535
std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context);
@@ -87,19 +87,18 @@ TEST(RandomIRBuilderTest, InsertValueIndexes) {
8787
// Check that we will generate correct indexes for the insertvalue operation
8888

8989
LLVMContext Ctx;
90-
const char *Source =
91-
"%T = type {i8, i32, i64}\n"
92-
"define void @test() {\n"
93-
" %A = alloca %T\n"
94-
" %L = load %T, %T* %A"
95-
" ret void\n"
96-
"}";
90+
const char *Source = "%T = type {i8, i32, i64}\n"
91+
"define void @test() {\n"
92+
" %A = alloca %T\n"
93+
" %L = load %T, %T* %A"
94+
" ret void\n"
95+
"}";
9796
auto M = parseAssembly(Source, Ctx);
9897

9998
fuzzerop::OpDescriptor IVDescr = fuzzerop::insertValueDescriptor(1);
10099

101-
std::vector<Type *> Types =
102-
{Type::getInt8Ty(Ctx), Type::getInt32Ty(Ctx), Type::getInt64Ty(Ctx)};
100+
std::vector<Type *> Types = {Type::getInt8Ty(Ctx), Type::getInt32Ty(Ctx),
101+
Type::getInt64Ty(Ctx)};
103102
RandomIRBuilder IB(Seed, Types);
104103

105104
// Get first basic block of the first function
@@ -115,15 +114,15 @@ TEST(RandomIRBuilderTest, InsertValueIndexes) {
115114

116115
// Generate constants for each of the types and check that we pick correct
117116
// index for the given type
118-
for (auto *T: Types) {
117+
for (auto *T : Types) {
119118
// Loop to account for possible random decisions
120119
for (int i = 0; i < 10; ++i) {
121120
// Create value we want to insert. Only it's type matters.
122121
Srcs[1] = ConstantInt::get(T, 5);
123122

124123
// Try to pick correct index
125-
Value *Src = IB.findOrCreateSource(
126-
BB, &*BB.begin(), Srcs, IVDescr.SourcePreds[2]);
124+
Value *Src =
125+
IB.findOrCreateSource(BB, &*BB.begin(), Srcs, IVDescr.SourcePreds[2]);
127126
ASSERT_TRUE(IVDescr.SourcePreds[2].matches(Srcs, Src));
128127
}
129128
}
@@ -167,18 +166,17 @@ TEST(RandomIRBuilderTest, InsertValueArray) {
167166
// Check that we can generate insertvalue for the vector operations
168167

169168
LLVMContext Ctx;
170-
const char *SourceCode =
171-
"define void @test() {\n"
172-
" %A = alloca [8 x i32]\n"
173-
" %L = load [8 x i32], [8 x i32]* %A"
174-
" ret void\n"
175-
"}";
169+
const char *SourceCode = "define void @test() {\n"
170+
" %A = alloca [8 x i32]\n"
171+
" %L = load [8 x i32], [8 x i32]* %A"
172+
" ret void\n"
173+
"}";
176174
auto M = parseAssembly(SourceCode, Ctx);
177175

178176
fuzzerop::OpDescriptor Descr = fuzzerop::insertValueDescriptor(1);
179177

180-
std::vector<Type *> Types =
181-
{Type::getInt8Ty(Ctx), Type::getInt32Ty(Ctx), Type::getInt64Ty(Ctx)};
178+
std::vector<Type *> Types = {Type::getInt8Ty(Ctx), Type::getInt32Ty(Ctx),
179+
Type::getInt64Ty(Ctx)};
182180
RandomIRBuilder IB(Seed, Types);
183181

184182
// Get first basic block of the first function
@@ -218,7 +216,6 @@ TEST(RandomIRBuilderTest, Invokes) {
218216
"}";
219217
auto M = parseAssembly(SourceCode, Ctx);
220218

221-
222219
std::vector<Type *> Types = {Type::getInt8Ty(Ctx)};
223220
RandomIRBuilder IB(Seed, Types);
224221

@@ -295,4 +292,4 @@ TEST(RandomIRBuilderTest, SwiftError) {
295292
}
296293
}
297294

298-
}
295+
} // namespace

llvm/unittests/FuzzMutate/ReservoirSamplerTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ TEST(ReservoirSamplerTest, Uniform) {
6060
// v=9 | 2.088 | 3.325 | 5.899 | 8.343 | 11.39 | 16.92 | 21.67 |
6161
//
6262
// Check that we're in the likely range of results.
63-
//if (V < 2.088 || V > 21.67)
63+
// if (V < 2.088 || V > 21.67)
6464
if (V < 2.088 || V > 21.67)
6565
++Failures;
6666
}

llvm/unittests/FuzzMutate/StrategiesTest.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ std::unique_ptr<IRMutator> createInjectorMutator() {
3131
Type::getInt64Ty, Type::getFloatTy, Type::getDoubleTy};
3232

3333
std::vector<std::unique_ptr<IRMutationStrategy>> Strategies;
34-
Strategies.push_back(
35-
std::make_unique<InjectorIRStrategy>(
36-
InjectorIRStrategy::getDefaultOps()));
34+
Strategies.push_back(std::make_unique<InjectorIRStrategy>(
35+
InjectorIRStrategy::getDefaultOps()));
3736

3837
return std::make_unique<IRMutator>(std::move(Types), std::move(Strategies));
3938
}
@@ -60,8 +59,8 @@ std::unique_ptr<IRMutator> createInstModifierMutator() {
6059
return std::make_unique<IRMutator>(std::move(Types), std::move(Strategies));
6160
}
6261

63-
std::unique_ptr<Module> parseAssembly(
64-
const char *Assembly, LLVMContext &Context) {
62+
std::unique_ptr<Module> parseAssembly(const char *Assembly,
63+
LLVMContext &Context) {
6564

6665
SMDiagnostic Error;
6766
std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context);
@@ -104,15 +103,15 @@ TEST(InstDeleterIRStrategyTest, EmptyFunction) {
104103
// Test that we don't crash even if we can't remove from one of the functions.
105104

106105
StringRef Source = ""
107-
"define <8 x i32> @func1() {\n"
108-
"ret <8 x i32> undef\n"
109-
"}\n"
110-
"\n"
111-
"define i32 @func2() {\n"
112-
"%A9 = alloca i32\n"
113-
"%L6 = load i32, i32* %A9\n"
114-
"ret i32 %L6\n"
115-
"}\n";
106+
"define <8 x i32> @func1() {\n"
107+
"ret <8 x i32> undef\n"
108+
"}\n"
109+
"\n"
110+
"define i32 @func2() {\n"
111+
"%A9 = alloca i32\n"
112+
"%L6 = load i32, i32* %A9\n"
113+
"ret i32 %L6\n"
114+
"}\n";
116115

117116
auto Mutator = createDeleterMutator();
118117
ASSERT_TRUE(Mutator);
@@ -240,4 +239,4 @@ TEST(InstModificationIRStrategyTest, GEP) {
240239

241240
EXPECT_TRUE(FoundInbounds);
242241
}
243-
}
242+
} // namespace

0 commit comments

Comments
 (0)