1
次のコードは、BasicBlock
のすべてを反復処理するFunctionPass
を作成し、難読化のためにa + b
を(a xor b) + 2 * (a and b)
に変更しようとしています。この `FunctionPass`を無限ループにならないように修正するには?
ここでReplaceInstWithValue
を使用すると、イテレーターは無効になり、プログラムは無限ループに入ります。
これを修正するいくつかの方法を試しましたが、どれも有用であるとは証明されていません。
最初のadd
命令の無限ループに陥ることなく、プログラム内のすべての命令を反復するようにプログラムを変更するにはどうすればよいですか?
#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include <map>
#include <string>
using namespace llvm;
namespace {
struct CountOp : public FunctionPass {
std::map<std::string, int> bbNameToId;
static char ID;
CountOp() : FunctionPass(ID) {}
virtual bool runOnFunction(Function &F) {
for (Function::iterator bs = F.begin(), be = F.end(); bs != be; ++bs) {
for (BasicBlock::iterator is = bs->begin(), ie = be->end(); is != ie; ++is) {
Instruction& inst = *is;
BinaryOperator* binop = dyn_cast<BinaryOperator>(&inst);
if (!binop) {
continue;
}
unsigned opcode = binop->getOpcode();
errs() << binop->getOpcodeName() << "\n";
if (opcode != Instruction::Add) {
continue;
}
IRBuilder<> builder(binop);
Value* v = builder.CreateAdd(builder.CreateXor(binop->getOperand(0), binop->getOperand(1)),
builder.CreateMul(ConstantInt::get(binop->getType(), 2),
builder.CreateAnd(binop->getOperand(0), binop->getOperand(1))));
ReplaceInstWithValue(bs->getInstList(), is, v);
}
}
return true;
}
};
}
char CountOp::ID = 0;
static RegisterPass<CountOp> X("opChanger", "Change add operations", false, false);