void InstructionReplace::cloneFunctions(llvm::Module& M)
{
	for(llvm::Module::iterator F = M.begin(), ME = M.end(); F != ME; ++F) {
		//if(!F->getFnAttributes().hasAttribute(Attributes::AttrVal::MaskedCopy)) { continue; }
		cerr << "Masking " << F->getName().str();
		assert(F->arg_size() == 1);
		Type* rettype = F->getReturnType();
		auto args = F->arg_begin();
		Argument* a1 = args++;
		Type* paramtype = a1->getType();
		assert(isa<IntegerType>(rettype));
		assert(isa<IntegerType>(paramtype));
		stringstream ss("");
		ss << "__masked__" << F->getName().str();
		llvm::LLVMContext& Ctx = M.getContext();
		llvm::Constant* FunSym;
		std::vector<Type*> paramtypes;
		for(unsigned int i = 0; i <= MaskingOrder; i++) { paramtypes.push_back(paramtype); } //TODO riducibile?
		for(unsigned int i = 0; i <= MaskingOrder; i++) { paramtypes.push_back(rettype->getPointerTo()); }
		llvm::FunctionType* ftype = llvm::FunctionType::get(llvm::Type::getVoidTy(Ctx), llvm::ArrayRef<Type*>(paramtypes), false);
		FunSym = M.getOrInsertFunction(ss.str(), ftype);
		llvm::Function* newF = llvm::cast<llvm::Function>(FunSym);
		maskedfn[F] = newF;
		SmallVector<llvm::ReturnInst*, 4> rets;
		ValueToValueMapTy vmap;
		llvm::BasicBlock* Entry = llvm::BasicBlock::Create(Ctx, "entry", newF);
		llvm::IRBuilder<> ib_entry = llvm::IRBuilder<>(Entry->getContext());
		ib_entry.SetInsertPoint(Entry);
		NoCryptoFA::InstructionMetadata* md = new NoCryptoFA::InstructionMetadata();
		md->hasBeenMasked = true;
		auto arg = newF->arg_begin();
		for(unsigned int i = 0; i <= MaskingOrder; i++) { md->MaskedValues.push_back(arg++); }
		Value* fakevalue = ib_entry.CreateAdd(md->MaskedValues[0], md->MaskedValues[1]);
		md->my_instruction = cast<Instruction>(fakevalue);
		NoCryptoFA::known[ md->my_instruction] = md;
		deletionqueue.insert(md->my_instruction);
		vmap.insert(std::make_pair(a1, fakevalue));
		CloneFunctionInto(newF, F, vmap, true, rets);
		ib_entry.CreateBr(cast<BasicBlock>(vmap[&F->getEntryBlock()]));
		/*
		AttrBuilder toremove;
		//toremove.addAttribute(Attributes::AttrVal::MaskedCopy);
		toremove.addAttribute(Attributes::AttrVal::ZExt);
		toremove.addAttribute(Attributes::AttrVal::SExt);
		toremove.addAttribute(Attributes::AttrVal::NoAlias);
		toremove.addAttribute(Attributes::AttrVal::NoCapture);
		toremove.addAttribute(Attributes::AttrVal::StructRet);
		toremove.addAttribute(Attributes::AttrVal::ByVal);
		toremove.addAttribute(Attributes::AttrVal::Nest);
		newF->removeFnAttr(Attributes::get(Ctx, toremove));
		newF->removeAttribute(0, Attributes::get(Ctx, toremove)); //Thr..ehm,Zero is a magic number! Toglie gli attributi zeroext e simili dal valore di ritorno.
		*/
		TaggedData& td = getAnalysis<TaggedData>(*F);
		//td.markFunction(newF);
		//setFullyMasked(newF);
	}
}
Example #2
0
bool DeclareAssumePass::runOnModule(llvm::Module &M){
  bool modified_M = false;
  CheckModule::check_assume(&M);
  llvm::Function *F_assume = M.getFunction("__VERIFIER_assume");
  if(!F_assume){
    llvm::FunctionType *assumeTy;
    {
      llvm::Type *voidTy = llvm::Type::getVoidTy(M.getContext());
      llvm::Type *i1Ty = llvm::Type::getInt1Ty(M.getContext());
      assumeTy = llvm::FunctionType::get(voidTy,{i1Ty},false);
    }
    llvm::AttributeSet assumeAttrs =
      llvm::AttributeSet::get(M.getContext(),llvm::AttributeSet::FunctionIndex,
                              std::vector<llvm::Attribute::AttrKind>({llvm::Attribute::NoUnwind}));
    F_assume = llvm::dyn_cast<llvm::Function>(M.getOrInsertFunction("__VERIFIER_assume",assumeTy,assumeAttrs));
    assert(F_assume);
    modified_M = true;
  }
  return modified_M;
}
Example #3
0
    virtual llvm::Value* materializeValueFor(llvm::Value* v) {
        // llvm::errs() << "materializing\n";
        // v->dump();

        llvm::Value* r = NULL;
        if (llvm::Function* f = llvm::dyn_cast<llvm::Function>(v)) {
            // llvm::errs() << "is function\n";
            r = new_module->getOrInsertFunction(f->getName(), f->getFunctionType());
        } else if (llvm::GlobalVariable* gv = llvm::dyn_cast<llvm::GlobalVariable>(v)) {
            // llvm::errs() << " is gv\n";
            assert(gv->getLinkage() != llvm::GlobalVariable::PrivateLinkage);
            llvm::GlobalVariable* new_gv = llvm::cast<llvm::GlobalVariable>(
                new_module->getOrInsertGlobal(gv->getName(), gv->getType()->getElementType()));
            RELEASE_ASSERT(!gv->isThreadLocal(), "I don't think MCJIT supports thread-local variables yet");
            new_gv->setThreadLocalMode(gv->getThreadLocalMode());
            r = new_gv;
        } else if (llvm::GlobalAlias* alias = llvm::dyn_cast<llvm::GlobalAlias>(v)) {
#if LLVMREV < 209040
            llvm::Value* addressee = llvm::cast<llvm::Constant>(materializeValueFor(alias->getAliasedGlobal()));
#else
            llvm::Value* addressee = llvm::cast<llvm::Constant>(materializeValueFor(alias->getAliasee()));
#endif
            assert(addressee);
            assert(alias->getType() == addressee->getType());
            r = addressee;
            // r = new llvm::GlobalAlias(alias->getType(), alias->getLinkage(), alias->getName(), addressee,
            // new_module);
        } else if (llvm::isa<llvm::Constant>(v)) {
            // llvm::errs() << " is a constant\n";
            r = NULL;
        } else {
            r = v;
        }

        // if (r)
        // r->dump();
        // llvm::errs() << "---\n";
        return r;
    }