Exemplo n.º 1
0
/**
 * Instruments global variable if they should be instrumented.
 * @param M module.
 * @param rw_config parsed rules to apply.
 * @return true if instrumentation of global variables was done without problems, false otherwise
 */
bool InstrumentGlobals(Module& M, Rewriter rw) {
	GlobalVarsRule rw_globals = rw.getGlobalsConfig();

	// If there is no rule for global variables, do not try to instrument
	if(rw_globals.inFunction.empty() || rw_globals.globalVar.globalVariable.empty()) // TODO this is not very nice
	  return true;

	// Iterate through global variables
	Module::global_iterator GI = M.global_begin(), GE = M.global_end();
	for ( ; GI != GE; ++GI) {
	    GlobalVariable *GV = dyn_cast<GlobalVariable>(GI);
	    if (!GV) continue;

	    if(rw_globals.inFunction == "*"){
	      //TODO
	      return false;
	      logger.write_error("Rule for global variables for instrumenting to all function not supported yet.");
	    }
	    else{
	      Function* F = M.getFunction(rw_globals.inFunction);
	      // Get operands of new instruction
	      map <string, Value*> variables;

	      if(rw_globals.globalVar.globalVariable != "*")
		variables[rw_globals.globalVar.globalVariable] = GV;
	      if(rw_globals.globalVar.globalVariable != "*"){
		LLVMContext &Context = getGlobalContext();
		variables[rw_globals.globalVar.getSizeTo] = ConstantInt::get(Type::getInt64Ty(Context), getGlobalVarSize(GV, &M));
	      }

	      // Try to instrument the code
	      if(checkAnalysis(rw_globals.condition,variables)) {
		// Try to apply rule
		inst_iterator IIterator = inst_begin(F);
		Instruction *firstI = &*IIterator; //TODO
		if(applyRule(M, firstI, rw_globals.newInstr, variables) == 1) {
		  logger.write_error("Cannot apply rule.");
		  return false;
		}
	      }
	    }
	}

	return true;
}