for
(BasicBlock::iterator i
=
alteredBB
-
>begin(), e
=
alteredBB
-
>end() ; i !
=
e;
+
+
i){
/
/
in
the case we find binary operator, we modify slightly this part by randomly
/
/
insert some instructions
if
(i
-
>isBinaryOp()){
/
/
binary instructions
unsigned opcode
=
i
-
>getOpcode();
BinaryOperator
*
op,
*
op1
=
NULL;
Twine
*
var
=
new Twine(
"_"
);
/
/
treat differently
float
or
int
/
/
Binary
int
if
(opcode
=
=
Instruction::Add || opcode
=
=
Instruction::Sub ||
opcode
=
=
Instruction::Mul || opcode
=
=
Instruction::UDiv ||
opcode
=
=
Instruction::SDiv || opcode
=
=
Instruction::URem ||
opcode
=
=
Instruction::SRem || opcode
=
=
Instruction::Shl ||
opcode
=
=
Instruction::LShr || opcode
=
=
Instruction::AShr ||
opcode
=
=
Instruction::And || opcode
=
=
Instruction::Or ||
opcode
=
=
Instruction::Xor){
for
(
int
random
=
(
int
)llvm::cryptoutils
-
>get_range(
10
); random <
10
;
+
+
random){
switch(llvm::cryptoutils
-
>get_range(
4
)){
/
/
to improve
case
0
:
/
/
do nothing
break
;
case
1
: op
=
BinaryOperator::CreateNeg(i
-
>getOperand(
0
),
*
var,&
*
i);
op1
=
BinaryOperator::Create(Instruction::Add,op,
i
-
>getOperand(
1
),
"gen"
,&
*
i);
break
;
case
2
: op1
=
BinaryOperator::Create(Instruction::Sub,
i
-
>getOperand(
0
),
i
-
>getOperand(
1
),
*
var,&
*
i);
op
=
BinaryOperator::Create(Instruction::Mul,op1,
i
-
>getOperand(
1
),
"gen"
,&
*
i);
break
;
case
3
: op
=
BinaryOperator::Create(Instruction::Shl,
i
-
>getOperand(
0
),
i
-
>getOperand(
1
),
*
var,&
*
i);
break
;
}
}
}
/
/
Binary
float
if
(opcode
=
=
Instruction::FAdd || opcode
=
=
Instruction::FSub ||
opcode
=
=
Instruction::FMul || opcode
=
=
Instruction::FDiv ||
opcode
=
=
Instruction::FRem){
for
(
int
random
=
(
int
)llvm::cryptoutils
-
>get_range(
10
); random <
10
;
+
+
random){
switch(llvm::cryptoutils
-
>get_range(
3
)){
/
/
can be improved
case
0
:
/
/
do nothing
break
;
case
1
: op
=
BinaryOperator::CreateFNeg(i
-
>getOperand(
0
),
*
var,&
*
i);
op1
=
BinaryOperator::Create(Instruction::FAdd,op,
i
-
>getOperand(
1
),
"gen"
,&
*
i);
break
;
case
2
: op
=
BinaryOperator::Create(Instruction::FSub,
i
-
>getOperand(
0
),
i
-
>getOperand(
1
),
*
var,&
*
i);
op1
=
BinaryOperator::Create(Instruction::FMul,op,
i
-
>getOperand(
1
),
"gen"
,&
*
i);
break
;
}
}
}
if
(opcode
=
=
Instruction::ICmp){
/
/
Condition (with
int
)
ICmpInst
*
currentI
=
(ICmpInst
*
)(&i);
switch(llvm::cryptoutils
-
>get_range(
3
)){
/
/
must be improved
case
0
:
/
/
do nothing
break
;
case
1
: currentI
-
>swapOperands();
break
;
case
2
:
/
/
randomly change the predicate
switch(llvm::cryptoutils
-
>get_range(
10
)){
case
0
: currentI
-
>setPredicate(ICmpInst::ICMP_EQ);
break
;
/
/
equal
case
1
: currentI
-
>setPredicate(ICmpInst::ICMP_NE);
break
;
/
/
not
equal
case
2
: currentI
-
>setPredicate(ICmpInst::ICMP_UGT);
break
;
/
/
unsigned greater than
case
3
: currentI
-
>setPredicate(ICmpInst::ICMP_UGE);
break
;
/
/
unsigned greater
or
equal
case
4
: currentI
-
>setPredicate(ICmpInst::ICMP_ULT);
break
;
/
/
unsigned less than
case
5
: currentI
-
>setPredicate(ICmpInst::ICMP_ULE);
break
;
/
/
unsigned less
or
equal
case
6
: currentI
-
>setPredicate(ICmpInst::ICMP_SGT);
break
;
/
/
signed greater than
case
7
: currentI
-
>setPredicate(ICmpInst::ICMP_SGE);
break
;
/
/
signed greater
or
equal
case
8
: currentI
-
>setPredicate(ICmpInst::ICMP_SLT);
break
;
/
/
signed less than
case
9
: currentI
-
>setPredicate(ICmpInst::ICMP_SLE);
break
;
/
/
signed less
or
equal
}
break
;
}
}
if
(opcode
=
=
Instruction::FCmp){
/
/
Conditions (with
float
)
FCmpInst
*
currentI
=
(FCmpInst
*
)(&i);
switch(llvm::cryptoutils
-
>get_range(
3
)){
/
/
must be improved
case
0
:
/
/
do nothing
break
;
case
1
: currentI
-
>swapOperands();
break
;
case
2
:
/
/
randomly change the predicate
switch(llvm::cryptoutils
-
>get_range(
10
)){
case
0
: currentI
-
>setPredicate(FCmpInst::FCMP_OEQ);
break
;
/
/
ordered
and
equal
case
1
: currentI
-
>setPredicate(FCmpInst::FCMP_ONE);
break
;
/
/
ordered
and
operands are unequal
case
2
: currentI
-
>setPredicate(FCmpInst::FCMP_UGT);
break
;
/
/
unordered
or
greater than
case
3
: currentI
-
>setPredicate(FCmpInst::FCMP_UGE);
break
;
/
/
unordered,
or
greater than,
or
equal
case
4
: currentI
-
>setPredicate(FCmpInst::FCMP_ULT);
break
;
/
/
unordered
or
less than
case
5
: currentI
-
>setPredicate(FCmpInst::FCMP_ULE);
break
;
/
/
unordered,
or
less than,
or
equal
case
6
: currentI
-
>setPredicate(FCmpInst::FCMP_OGT);
break
;
/
/
ordered
and
greater than
case
7
: currentI
-
>setPredicate(FCmpInst::FCMP_OGE);
break
;
/
/
ordered
and
greater than
or
equal
case
8
: currentI
-
>setPredicate(FCmpInst::FCMP_OLT);
break
;
/
/
ordered
and
less than
case
9
: currentI
-
>setPredicate(FCmpInst::FCMP_OLE);
break
;
/
/
ordered
or
less than,
or
equal
}
break
;
}
}
}
}