Skip to content

Commit

Permalink
Merge pull request #44 from isaacdbeans/mutate
Browse files Browse the repository at this point in the history
FNEG and addition FP value mutations
  • Loading branch information
Hatsunespica authored Aug 26, 2024
2 parents 148f211 + 7fedf72 commit 06319aa
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 30 deletions.
30 changes: 28 additions & 2 deletions tools/mutator-utils/mutator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,21 @@ void FunctionMutator::init(std::shared_ptr<FunctionMutator> self) {
if (llvm::Value *val = it->getOperand(i);
val != nullptr && llvm::isa<llvm::ConstantInt>(*val)) {
Random::addUsedInt(((llvm::ConstantInt *)val)->getLimitedValue());

}
// if value is a FP value send it to either the used floats or used
// doubles array
else if (llvm::Value *val = it->getOperand(i);
val != nullptr && llvm::isa<llvm::ConstantFP>(*val)) {
if (it->getOperand(i)->getType()->isDoubleTy()) {
Random::addUsedDouble(llvm::dyn_cast<llvm::ConstantFP>(val)
->getValueAPF()
.convertToDouble());
} else if (it->getOperand(i)->getType()->isFloatTy()) {
Random::addUsedFloat(llvm::dyn_cast<llvm::ConstantFP>(val)
->getValueAPF()
.convertToFloat());
}
}
}
}
Expand Down Expand Up @@ -182,6 +197,11 @@ void FunctionMutator::init(std::shared_ptr<FunctionMutator> self) {
whenMoveToNextInstFuncs.push_back(helpers.size() - 1);
}

if (UnaryInstHelper::canMutate(currentFunction)) {
helpers.push_back(std::make_unique<UnaryInstHelper>(self));
whenMoveToNextInstFuncs.push_back(helpers.size() - 1);
}

for (size_t i = 0; i < helpers.size(); ++i) {
helpers[i]->init();
}
Expand Down Expand Up @@ -469,9 +489,15 @@ llvm::Value *FunctionMutator::getRandomConstant(llvm::Type *ty) {
ty->getContext(),
mutator_util::getRandomLLVMInt((llvm::IntegerType *)ty));
}
if (ty->isFloatingPointTy()) {
return llvm::ConstantFP::get(ty, Random::getRandomDouble());
if (ty->isDoubleTy()) {
return llvm::ConstantFP::get(ty->getContext(),
mutator_util::getRandomLLVMDouble());
}
if (ty->isFloatTy()) {
return llvm::ConstantFP::get(ty->getContext(),
mutator_util::getRandomLLVMFloat());
}

if (auto vecTy = llvm::dyn_cast<llvm::FixedVectorType>(ty);
vecTy && vecTy->getElementType()) {
return mutator_util::getRandomLLVMIntegerVector(vecTy);
Expand Down
14 changes: 7 additions & 7 deletions tools/mutator-utils/mutator.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@
class Mutator {
protected:
bool debug;

llvm::LLVMContext context;
llvm::ExitOnError ExitOnErr;
std::shared_ptr<llvm::Module> pm;

public:
Mutator(bool debug = false) : debug(debug), pm(nullptr){};
virtual ~Mutator(){};
Mutator(bool debug = false) : debug(debug), pm(nullptr) {};
virtual ~Mutator() {};

bool openInputFile(const string &inputFile);
virtual bool init() = 0;
Expand Down Expand Up @@ -76,8 +75,8 @@ class StubMutator : public Mutator {
void moveToNextFunction();

public:
StubMutator(bool debug) : Mutator(debug){};
virtual ~StubMutator(){};
StubMutator(bool debug) : Mutator(debug) {};
virtual ~StubMutator() {};
virtual bool init() override;
virtual void mutateModule(const std::string &outputFileName) override;
virtual void saveModule(const std::string &outputFileName) override;
Expand Down Expand Up @@ -110,6 +109,7 @@ class FunctionMutator {
friend class BinaryInstructionHelper;
friend class EliminateUndefHelper;
friend class ResizeIntegerHelper;
friend class UnaryInstHelper;

llvm::Function *currentFunction, *functionInTmp;
llvm::ValueToValueMapTy &vMap;
Expand Down Expand Up @@ -226,7 +226,7 @@ class ModuleMutator : public Mutator {
void resetTmpModule();

public:
ModuleMutator(bool debug = false) : Mutator(debug){};
ModuleMutator(bool debug = false) : Mutator(debug) {};
ModuleMutator(std::shared_ptr<llvm::Module> pm_,
const llvm::StringSet<> &invalidFunctions, bool debug = false,
bool onEveryFunction = false, bool randomMutate = false)
Expand All @@ -241,7 +241,7 @@ class ModuleMutator : public Mutator {
curFunction(0) {
pm = pm_;
}
~ModuleMutator(){};
~ModuleMutator() {};
virtual bool init() override;
virtual void mutateModule(const std::string &outputFileName) override;
virtual std::string getCurrentFunction() const override {
Expand Down
49 changes: 48 additions & 1 deletion tools/mutator-utils/mutator_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1131,4 +1131,51 @@ void ResizeIntegerHelper::debug() {
llvm::errs() << "integer resized\n";
mutator->iitInTmp->getParent()->print(llvm::errs());
llvm::errs() << "\n";
}
}

void UnaryInstHelper::mutate() {
if (llvm::isa<llvm::BinaryOperator>(&*mutator->iitInTmp)) {
llvm::BinaryOperator *binInst =
(llvm::BinaryOperator *)(&*mutator->iitInTmp);

if (Random::getRandomBool()) {
llvm::UnaryOperator *newInst =
llvm::UnaryOperator::Create(llvm::AddrSpaceCastInst::UnaryOps::FNeg,
binInst->getOperand(0), "", binInst);
binInst->replaceAllUsesWith(newInst);
binInst->eraseFromParent();
mutator->iitInTmp = newInst->getIterator();
} else {
llvm::UnaryOperator *newInst =
llvm::UnaryOperator::Create(llvm::AddrSpaceCastInst::UnaryOps::FNeg,
binInst->getOperand(1), "", binInst);
binInst->replaceAllUsesWith(newInst);
binInst->eraseFromParent();
mutator->iitInTmp = newInst->getIterator();
}

} else {
assert(false && "not supported binary instruction");
}
updated = true;
}

bool UnaryInstHelper::shouldMutate() {
if (llvm::isa<llvm::BinaryOperator>(&*mutator->iitInTmp)) {
llvm::BinaryOperator *binInst =
(llvm::BinaryOperator *)(&*mutator->iitInTmp);
return binInst->getOperand(0)->getType()->isFPOrFPVectorTy() &&
binInst->getOperand(1)->getType()->isFPOrFPVectorTy() && !updated;
}
return false;
}

bool UnaryInstHelper::canMutate(llvm::Function *func) {
return true;
}

void UnaryInstHelper::debug() {
llvm::errs() << "unary inst created\n";
mutator->iitInTmp->getParent()->print(llvm::errs());
llvm::errs() << "\n";
}
21 changes: 21 additions & 0 deletions tools/mutator-utils/mutator_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,24 @@ class ResizeIntegerHelper : public MutationHelper {
updated = false;
}
};

class UnaryInstHelper : public MutationHelper {
bool updated;

public:
UnaryInstHelper(std::shared_ptr<FunctionMutator> mutator)
: MutationHelper(mutator), updated(false) {};
virtual void init() override {
updated = false;
};
virtual void reset() override {
updated = false;
}
static bool canMutate(llvm::Function *func);
virtual void mutate() override;
virtual bool shouldMutate() override;
virtual void debug() override;
virtual void whenMoveToNextInst() override {
updated = false;
}
};
64 changes: 46 additions & 18 deletions tools/mutator-utils/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,33 @@ unsigned Random::getBitmask(llvm::IntegerType *ty) {
}

double Random::getExtremeDouble() {
return 0;
std::uniform_real_distribution<double> dist(0, __DBL_MAX__);
double random_double = dist(mt);
if (getRandomBool()) {
return random_double * -1.0;
}
return random_double;
}

float Random::getExtremeFloat() {
return 0;
std::uniform_real_distribution<float> dist(0, __FLT_MAX__);
double random_float = dist(mt);
if (getRandomBool()) {
return random_float * -1.0;
}
return random_float;
}

double Random::getRandomDouble() {
return (double)getRandomUnsigned();
std::uniform_real_distribution<double> dist(-1.0, 1.0);
double random_double = dist(mt);
return random_double;
}

float Random::getRandomFloat() {
return 0;
std::uniform_real_distribution<float> dist(-1.0, 1.0);
double random_float = dist(mt);
return random_float;
}

unsigned Random::getUsedInt(llvm::IntegerType *ty) {
Expand All @@ -67,11 +81,25 @@ unsigned Random::getUsedInt(llvm::IntegerType *ty) {
}

double Random::getUsedDouble() {
return 0;
if (usedDoubles.empty()) {
return getRandomDouble();
} else {
std::uniform_int_distribution<int> dist(0, usedDoubles.size() - 1);
int index = dist(mt);
return usedDoubles[index];
}
return getRandomDouble();
}

float Random::getUsedFloat() {
return 0;
if (usedFloats.empty()) {
return getRandomFloat();
} else {
std::uniform_int_distribution<int> dist(0, usedFloats.size() - 1);
int index = dist(mt);
return usedFloats[index];
}
return getRandomFloat();
}

llvm::APInt mutator_util::getRandomLLVMInt(llvm::IntegerType *ty) {
Expand All @@ -90,29 +118,29 @@ llvm::APInt mutator_util::getRandomLLVMInt(llvm::IntegerType *ty) {
}
}

double mutator_util::getRandomLLVMDouble() {
switch (Random::getRandomUnsigned() % 3) {
llvm::APFloat mutator_util::getRandomLLVMDouble() {
switch (Random::getRandomUnsigned(2)) {
case 0:
return Random::getUsedDouble();
return llvm::APFloat(Random::getRandomDouble());
case 1:
return Random::getExtremeDouble();
return llvm::APFloat(Random::getExtremeDouble());
case 2:
return Random::getRandomDouble();
return llvm::APFloat(Random::getUsedDouble());
default:
return Random::getRandomDouble();
return llvm::APFloat(Random::getRandomDouble());
}
}

float mutator_util::getRandomLLVMFloat() {
switch (Random::getRandomUnsigned() % 3) {
llvm::APFloat mutator_util::getRandomLLVMFloat() {
switch (Random::getRandomUnsigned(2)) {
case 0:
return Random::getUsedFloat();
return llvm::APFloat(Random::getRandomFloat());
case 1:
return Random::getExtremeFloat();
return llvm::APFloat(Random::getExtremeFloat());
case 2:
return Random::getRandomFloat();
return llvm::APFloat(Random::getUsedFloat());
default:
return Random::getRandomFloat();
return llvm::APFloat(Random::getRandomFloat());
}
}

Expand Down
10 changes: 8 additions & 2 deletions tools/mutator-utils/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ class Random {
static void addUsedInt(unsigned i) {
usedInts.push_back(i);
}
static void addUsedFloat(float i) {
usedFloats.push_back(i);
}
static void addUsedDouble(double i) {
usedDoubles.push_back(i);
}
static double getRandomDouble();
static float getRandomFloat();
static unsigned getRandomUnsigned() {
Expand Down Expand Up @@ -331,8 +337,8 @@ class mutator_util {
}

static llvm::APInt getRandomLLVMInt(llvm::IntegerType *ty);
static double getRandomLLVMDouble();
static float getRandomLLVMFloat();
static llvm::APFloat getRandomLLVMDouble();
static llvm::APFloat getRandomLLVMFloat();
static llvm::ConstantRange getRandomLLVMConstantRange(llvm::IntegerType *ty);
static llvm::Constant *getRandomLLVMIntegerVector(llvm::FixedVectorType *ty);
static llvm::Constant *updateIntegerVector(llvm::ConstantVector *ty);
Expand Down

0 comments on commit 06319aa

Please sign in to comment.