Пример #1
0
    bool _removeUnusedFromModule()
    {
        using namespace llvm;
        // do not slice away these functions no matter what
        // FIXME do it a vector and fill it dynamically according
        // to what is the setup (like for sv-comp or general..)
        const char *keep[] = {options.dgOptions.entryFunction.c_str(),
                              "klee_assume", nullptr};

        // when erasing while iterating the slicer crashes
        // so set the to be erased values into container
        // and then erase them
        std::set<Function *> funs;
        std::set<GlobalVariable *> globals;
        std::set<GlobalAlias *> aliases;

        for (auto I = M->begin(), E = M->end(); I != E; ++I) {
            Function *func = &*I;
            if (array_match(func->getName(), keep))
                continue;

            // if the function is unused or we haven't constructed it
            // at all in dependence graph, we can remove it
            // (it may have some uses though - like when one
            // unused func calls the other unused func
            if (func->hasNUses(0))
                funs.insert(func);
        }

        for (auto I = M->global_begin(), E = M->global_end(); I != E; ++I) {
            GlobalVariable *gv = &*I;
            if (gv->hasNUses(0))
                globals.insert(gv);
        }

        for (GlobalAlias& ga : M->getAliasList()) {
            if (ga.hasNUses(0))
                aliases.insert(&ga);
        }

        for (Function *f : funs)
            f->eraseFromParent();
        for (GlobalVariable *gv : globals)
            gv->eraseFromParent();
        for (GlobalAlias *ga : aliases)
            ga->eraseFromParent();

        return (!funs.empty() || !globals.empty() || !aliases.empty());
    }
Пример #2
0
bool UnsafeTypeCastingCheck::doInitialization(llvm::Module &mod) { 
  errs() << "initialization... \n"; 
  
  for (Module::global_iterator git = mod.global_begin() ;
       git != mod.global_end() ; 
       git++) {
    GlobalValue *gv = dyn_cast<GlobalValue>(git);
    string gv_name = gv->getName().str(); 
    if (gv_name.compare("blockDim") == 0 || 
	gv_name.compare("gridDim") == 0 || 
	gv_name.compare("blockIdx") == 0 || 
	gv_name.compare("threadIdx") == 0) {
      setPointedType(gv, UINT_UT); 
    }
  }

  // get utcc_assert function call (assertion) 
  code_modified = false; 
  assert_func = mod.getFunction("utcc_assert"); 
  assert(assert_func != NULL); 

  return false; 
}
Пример #3
0
/*!
 *  This method identify which is value sym and which is object sym
 */
void SymbolTableInfo::buildMemModel(llvm::Module& module) {
    analysisUtil::increaseStackSize();

    prePassSchedule(module);

    mod = &module;

    maxFieldLimit = maxFieldNumLimit;

    // Object #0 is black hole the object that may point to any object
    assert(totalSymNum == BlackHole && "Something changed!");
    symTyMap.insert(std::make_pair(totalSymNum++, BlackHole));
    createBlkOrConstantObj(BlackHole);

    // Object #1 always represents the constant
    assert(totalSymNum == ConstantObj && "Something changed!");
    symTyMap.insert(std::make_pair(totalSymNum++, ConstantObj));
    createBlkOrConstantObj(ConstantObj);

    // Pointer #2 always represents the pointer points-to black hole.
    assert(totalSymNum == BlkPtr && "Something changed!");
    symTyMap.insert(std::make_pair(totalSymNum++, BlkPtr));

    // Pointer #3 always represents the null pointer.
    assert(totalSymNum == NullPtr && "Something changed!");
    symTyMap.insert(std::make_pair(totalSymNum, NullPtr));

    // Add symbols for all the globals .
    for (Module::global_iterator I = module.global_begin(), E =
                module.global_end(); I != E; ++I) {
        collectSym(&*I);
    }

    // Add symbols for all the global aliases
    for (Module::alias_iterator I = module.alias_begin(), E =
                module.alias_end(); I != E; I++) {
        collectSym(&*I);
    }

    // Add symbols for all of the functions and the instructions in them.
    for (Module::iterator F = module.begin(), E = module.end(); F != E; ++F) {
        collectSym(&*F);
        collectRet(&*F);
        if (F->getFunctionType()->isVarArg())
            collectVararg(&*F);

        // Add symbols for all formal parameters.
        for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
                I != E; ++I) {
            collectSym(&*I);
        }

        // collect and create symbols inside the function body
        for (inst_iterator II = inst_begin(&*F), E = inst_end(&*F); II != E; ++II) {
            const Instruction *inst = &*II;
            collectSym(inst);

            // initialization for some special instructions
            //{@
            if (const StoreInst *st = dyn_cast<StoreInst>(inst)) {
                collectSym(st->getPointerOperand());
                collectSym(st->getValueOperand());
            }

            else if (const LoadInst *ld = dyn_cast<LoadInst>(inst)) {
                collectSym(ld->getPointerOperand());
            }

            else if (const PHINode *phi = dyn_cast<PHINode>(inst)) {
                for (u32_t i = 0; i < phi->getNumIncomingValues(); ++i) {
                    collectSym(phi->getIncomingValue(i));
                }
            }

            else if (const GetElementPtrInst *gep = dyn_cast<GetElementPtrInst>(
                    inst)) {
                collectSym(gep->getPointerOperand());
            }

            else if (const SelectInst *sel = dyn_cast<SelectInst>(inst)) {
                collectSym(sel->getTrueValue());
                collectSym(sel->getFalseValue());
            }

            else if (const CastInst *cast = dyn_cast<CastInst>(inst)) {
                collectSym(cast->getOperand(0));
            }
            else if (const ReturnInst *ret = dyn_cast<ReturnInst>(inst)) {
                if(ret->getReturnValue())
                    collectSym(ret->getReturnValue());
            }
            else if (isCallSite(inst) && isInstrinsicDbgInst(inst)==false) {

                CallSite cs = analysisUtil::getLLVMCallSite(inst);
                callSiteSet.insert(cs);
                for (CallSite::arg_iterator it = cs.arg_begin();
                        it != cs.arg_end(); ++it) {
                    collectSym(*it);
                }
                // Calls to inline asm need to be added as well because the callee isn't
                // referenced anywhere else.
                const Value *Callee = cs.getCalledValue();
                collectSym(Callee);

                //TODO handle inlineAsm
                ///if (isa<InlineAsm>(Callee))

            }
            //@}
        }
    }
}
/// ValueEnumerator - Enumerate module-level information.
ValueEnumerator::ValueEnumerator(const llvm::Module &M,
                                 bool ShouldPreserveUseListOrder)
    : HasMDString(false), HasDILocation(false),
    ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
  assert(!ShouldPreserveUseListOrder &&
         "not supported UseListOrders = predictUseListOrder(M)");

  // Enumerate the global variables.
  for (llvm::Module::const_global_iterator I = M.global_begin(), E = M.global_end();
       I != E; ++I)
    EnumerateValue(I);

  // Enumerate the functions.
  for (llvm::Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
    EnumerateValue(I);
    EnumerateAttributes(cast<Function>(I)->getAttributes());
  }

  // Enumerate the aliases.
  for (llvm::Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
       I != E; ++I)
    EnumerateValue(I);

  // Remember what is the cutoff between globalvalue's and other constants.
  unsigned FirstConstant = Values.size();

  // Enumerate the global variable initializers.
  for (llvm::Module::const_global_iterator I = M.global_begin(), E = M.global_end();
       I != E; ++I)
    if (I->hasInitializer())
      EnumerateValue(I->getInitializer());

  // Enumerate the aliasees.
  for (llvm::Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
       I != E; ++I)
    EnumerateValue(I->getAliasee());

  // Enumerate the metadata type.
  //
  // TODO: Move this to ValueEnumerator::EnumerateOperandType() once bitcode
  // only encodes the metadata type when it's used as a value.
  EnumerateType(Type::getMetadataTy(M.getContext()));

  // Insert constants and metadata that are named at module level into the slot
  // pool so that the module symbol table can refer to them...
  EnumerateValueSymbolTable(M.getValueSymbolTable());
  EnumerateNamedMetadata(M);

  SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;

  // Enumerate types used by function bodies and argument lists.
  for (const Function &F : M) {
    for (const Argument &A : F.args())
      EnumerateType(A.getType());

    for (const BasicBlock &BB : F)
      for (const Instruction &I : BB) {
        for (const Use &Op : I.operands()) {
          auto *MD = dyn_cast<MetadataAsValue>(&Op);
          if (!MD) {
            EnumerateOperandType(Op);
            continue;
          }

          // Local metadata is enumerated during function-incorporation.
          if (isa<LocalAsMetadata>(MD->getMetadata()))
            continue;

          EnumerateMetadata(MD->getMetadata());
        }
        EnumerateType(I.getType());
        if (const CallInst *CI = dyn_cast<CallInst>(&I))
          EnumerateAttributes(CI->getAttributes());
        else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I))
          EnumerateAttributes(II->getAttributes());

        // Enumerate metadata attached with this instruction.
        MDs.clear();
        I.getAllMetadataOtherThanDebugLoc(MDs);
        for (unsigned i = 0, e = MDs.size(); i != e; ++i)
          EnumerateMetadata(MDs[i].second);

#if LLVM_VERSION >= 37
        if (I.getDebugLoc()) {
          MDNode* Scope = I.getDebugLoc().getScope();
          if (Scope) EnumerateMetadata(Scope);
          DILocation *IA = I.getDebugLoc().getInlinedAt();
          if (IA) EnumerateMetadata(IA);
        }
#else
        if (!I.getDebugLoc().isUnknown()) {
          MDNode *Scope, *IA;
          I.getDebugLoc().getScopeAndInlinedAt(Scope, IA, I.getContext());
          if (Scope) EnumerateMetadata(Scope);
          if (IA) EnumerateMetadata(IA);
        }
#endif
      }
  }

  // Optimize constant ordering.
  OptimizeConstants(FirstConstant, Values.size());
}