liberty/lib/SLAMP/SLAMP.cpp
/// Add SLAMP_main_entry as the first thing in main
Add SLAMP_main_entry as the first thing in main
Add SLAMP_main_entry as the first thing in main
void SLAMP::instrumentMainFunction(Module &m) {
  for (Module::iterator fi = m.begin(), fe = m.end(); fi != fe; fi++) {
    Function *func = &*fi;
    if (func->getName() != "main")
      continue;
    BasicBlock *entry = &(func->getEntryBlock());
    auto *f_main_entry = cast<Function>(
        m.getOrInsertFunction("SLAMP_main_entry", Void, I32,
                              I8Ptr->getPointerTo(), I8Ptr->getPointerTo())
            .getCallee());
    vector<Value *> main_args;
    for (auto &&ai : func->args())
      main_args.push_back(&ai);
    if (main_args.size() != 3) { 
      Value *zeroarg = ConstantInt::get(I32, 0);
      Value *nullarg = ConstantPointerNull::get(I8Ptr->getPointerTo());
      if (main_args.size() == 0) { 
        main_args.push_back(zeroarg); 
        main_args.push_back(nullarg); 
        main_args.push_back(nullarg); 
      } else if (main_args.size() == 2) { 
        main_args.push_back(nullarg);
      } else {
        assert(false && "Only have one argument (argc) in main, do not conform to standard");
      }
    }
    InstInsertPt pt;
    if (isa<LandingPadInst>(entry->getFirstNonPHI()))
      pt = InstInsertPt::After(entry->getFirstNonPHI());
    else
      pt = InstInsertPt::Before(entry->getFirstNonPHI());
    pt << CallInst::Create(f_main_entry, main_args, "");
  }
}
void SLAMP :: instrumentMainFunction ( Module & m ) { for ( Module :: iterator fi = m . begin ( ) , fe = m . end ( ) ; fi != fe ; fi ++ ) { Function * func = & * fi ; if ( func -> getName ( ) != " " ) continue ; BasicBlock * entry = & ( func -> getEntryBlock ( ) ) ; auto * f_main_entry = cast < Function > ( m . getOrInsertFunction ( " " , Void , I32 , I8Ptr -> getPointerTo ( ) , I8Ptr -> getPointerTo ( ) ) . getCallee ( ) ) ; vector < Value * > main_args ; for ( auto && ai : func -> args ( ) ) main_args . push_back ( & ai ) ; if ( main_args . size ( ) != 3 ) { Value * zeroarg = ConstantInt :: get ( I32 , 0 ) ; Value * nullarg = ConstantPointerNull :: get ( I8Ptr -> getPointerTo ( ) ) ; if ( main_args . size ( ) == 0 ) { main_args . push_back ( zeroarg ) ; main_args . push_back ( nullarg ) ; main_args . push_back ( nullarg ) ; } else if ( main_args . size ( ) == 2 ) { main_args . push_back ( nullarg ) ; } else { assert ( false && " " ) ; } } InstInsertPt pt ; if ( isa < LandingPadInst > ( entry -> getFirstNonPHI ( ) ) ) pt = InstInsertPt :: After ( entry -> getFirstNonPHI ( ) ) ; else pt = InstInsertPt :: Before ( entry -> getFirstNonPHI ( ) ) ; pt << CallInst :: Create ( f_main_entry , main_args , " " ) ; } }
Add SLAMP_main_entry as the first thing in main
Add SLAMP_main_entry as the first thing in main
// if the function is a main function, add special instrumentation to handle // command line arguments // make up arguments // if not all of argc, argv, evn are given // no command line input // argc // argv // envp // only argc, argv given // // read rsp and push it into main_args // FunctionType *fty = FunctionType::get(I64, false); // // get the static pointer %rsp // InlineAsm *get_rsp = InlineAsm::get( //     fty, "mov %rsp, $0;", "=r,~{dirflag},~{fpsr},~{flags}", false); // CallInst *rsp = CallInst::Create(get_rsp, "");