Skip to content

Commit 6a23d27

Browse files
committed
[FuzzMutate] Don't insert instructions after musttail call
1 parent 3251ba2 commit 6a23d27

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

llvm/lib/FuzzMutate/IRMutator.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ void InjectorIRStrategy::mutate(BasicBlock &BB, RandomIRBuilder &IB) {
116116
auto InstsBefore = makeArrayRef(Insts).slice(0, IP);
117117
auto InstsAfter = makeArrayRef(Insts).slice(IP);
118118

119+
if (!InstsBefore.empty()) {
120+
// Don't insert instructions after a musttail call.
121+
Instruction *InstBefore = InstsBefore.back();
122+
if (isa<BitCastInst>(InstBefore))
123+
InstBefore = InstBefore->getPrevNode();
124+
CallBase *CallBefore = dyn_cast_or_null<CallBase>(InstBefore);
125+
if (CallBefore && CallBefore->isMustTailCall())
126+
return;
127+
}
128+
119129
// Choose a source, which will be used to constrain the operation selection.
120130
SmallVector<Value *, 2> Srcs;
121131
Srcs.push_back(IB.findOrCreateSource(BB, InstsBefore));

llvm/unittests/FuzzMutate/StrategiesTest.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,36 @@ TEST(InjectorIRStrategyTest, EmptyModule) {
100100
EXPECT_TRUE(!verifyModule(*M, &errs()));
101101
}
102102

103+
TEST(InjectorIRStrategyTest, MustTailCall) {
104+
// Test that we don't insert after a musttail call.
105+
StringRef Source = ""
106+
"define i32 @func() {\n"
107+
"%v = musttail call i32 @func()\n"
108+
"ret i32 %v\n"
109+
"}\n";
110+
111+
auto Mutator = createInjectorMutator();
112+
ASSERT_TRUE(Mutator);
113+
114+
IterateOnSource(Source, *Mutator);
115+
}
116+
117+
TEST(InjectorIRStrategyTest, MustTailCallBitCast) {
118+
// Test that we don't insert after a musttail call bitcast.
119+
StringRef Source = ""
120+
"declare i32* @func2()\n"
121+
"define i8* @func() {\n"
122+
"%v = musttail call i32* @func2()\n"
123+
"%v2 = bitcast i32* %v to i8*\n"
124+
"ret i8* %v2\n"
125+
"}\n";
126+
127+
auto Mutator = createInjectorMutator();
128+
ASSERT_TRUE(Mutator);
129+
130+
IterateOnSource(Source, *Mutator);
131+
}
132+
103133
TEST(InstDeleterIRStrategyTest, EmptyFunction) {
104134
// Test that we don't crash even if we can't remove from one of the functions.
105135

0 commit comments

Comments
 (0)