liberty/lib/SLAMP/SLAMP.cpp
/// Go through all global variables and call `SLAMP_init_global_vars`
Go through all global variables and call `SLAMP_init_global_vars`
Go through all global variables and call ` SLAMP_init_global_vars `
void SLAMP::instrumentGlobalVars(Module &m, Function *ctor) {
  const DataLayout &td = m.getDataLayout();
  BasicBlock *entry = &(ctor->getEntryBlock());
  auto *init_gvars = cast<Function>(
      m.getOrInsertFunction("SLAMP_init_global_vars", Void, I64, I64)
          .getCallee());
  for (GlobalVariable &gvr : m.globals()) {
    GlobalVariable *gv = &gvr;
    if (gv->getName() == "llvm.global_ctors") 
      continue;
    else if (gv->getName() == "llvm.global_dtors") 
      continue;
    auto *ty = dyn_cast<PointerType>(gv->getType());
    assert(ty);
    InstInsertPt pt = InstInsertPt::Before(entry->getTerminator());
    uint64_t size = td.getTypeStoreSize(ty->getElementType());
    Value *args[] = {castToInt64Ty(gv, pt), ConstantInt::get(I64, size)};
    pt << CallInst::Create(init_gvars, args);
  }
  for (auto &&fi : m) {
    Function *func = &fi;
    if (func->isIntrinsic())
      continue;
    uint64_t size = td.getTypeStoreSize(func->getType());
    InstInsertPt pt = InstInsertPt::Before(entry->getTerminator());
    Value *args[] = {castToInt64Ty(func, pt), ConstantInt::get(I64, size)};
    pt << CallInst::Create(init_gvars, args);
  }
}
void SLAMP :: instrumentGlobalVars ( Module & m , Function * ctor ) { const DataLayout & td = m . getDataLayout ( ) ; BasicBlock * entry = & ( ctor -> getEntryBlock ( ) ) ; auto * init_gvars = cast < Function > ( m . getOrInsertFunction ( " " , Void , I64 , I64 ) . getCallee ( ) ) ; for ( GlobalVariable & gvr : m . globals ( ) ) { GlobalVariable * gv = & gvr ; if ( gv -> getName ( ) == " " ) continue ; else if ( gv -> getName ( ) == " " ) continue ; auto * ty = dyn_cast < PointerType > ( gv -> getType ( ) ) ; assert ( ty ) ; InstInsertPt pt = InstInsertPt :: Before ( entry -> getTerminator ( ) ) ; uint64_t size = td . getTypeStoreSize ( ty -> getElementType ( ) ) ; Value * args [ ] = { castToInt64Ty ( gv , pt ) , ConstantInt :: get ( I64 , size ) } ; pt << CallInst :: Create ( init_gvars , args ) ; } for ( auto && fi : m ) { Function * func = & fi ; if ( func -> isIntrinsic ( ) ) continue ; uint64_t size = td . getTypeStoreSize ( func -> getType ( ) ) ; InstInsertPt pt = InstInsertPt :: Before ( entry -> getTerminator ( ) ) ; Value * args [ ] = { castToInt64Ty ( func , pt ) , ConstantInt :: get ( I64 , size ) } ; pt << CallInst :: Create ( init_gvars , args ) ; } }
Go through all global variables and call `SLAMP_init_global_vars`
Go through all global variables and call ` SLAMP_init_global_vars `
// DataLayout& td = getAnalysis<DataLayout>(); // call SLAMP_init_global_vars function to initialize shadow memory for // global variables // explicitly skip global ctor // explicitly skip global dtor