[llvm-commits] [poolalloc] r132343 - in /poolalloc/trunk: include/assistDS/TypeChecks.h lib/AssistDS/TypeChecks.cpp runtime/DynamicTypeChecks/TypeRuntime.c

Arushi Aggarwal aggarwa4 at illinois.edu
Tue May 31 10:06:04 PDT 2011


Author: aggarwa4
Date: Tue May 31 12:06:04 2011
New Revision: 132343

URL: http://llvm.org/viewvc/llvm-project?rev=132343&view=rev
Log:
Add a global that contains the metadata to type names
map, for better output on mismatch.

Also, some minor cleanup of code.

Modified:
    poolalloc/trunk/include/assistDS/TypeChecks.h
    poolalloc/trunk/lib/AssistDS/TypeChecks.cpp
    poolalloc/trunk/runtime/DynamicTypeChecks/TypeRuntime.c

Modified: poolalloc/trunk/include/assistDS/TypeChecks.h
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/include/assistDS/TypeChecks.h?rev=132343&r1=132342&r2=132343&view=diff
==============================================================================
--- poolalloc/trunk/include/assistDS/TypeChecks.h (original)
+++ poolalloc/trunk/include/assistDS/TypeChecks.h Tue May 31 12:06:04 2011
@@ -32,7 +32,10 @@
 class TypeChecks : public ModulePass {
 private:
   std::map<const Type *, unsigned int> UsedTypes;
-  std::map<Function *, Function *> VAListFunctions;
+  std::map<Function *, Function *> VAListFunctionsMap;
+  std::list<Function *> VAArgFunctions;
+  std::list<Function *> VAListFunctions;
+  std::list<Function *> ByValFunctions;
 
   // Analysis from other passes.
   TargetData *TD;
@@ -53,6 +56,7 @@
 
   bool initShadow(Module &M);
   bool unmapShadow(Module &M, Instruction &I);
+  void addTypeMapGlobal(Module &M) ;
   bool visitCallInst(Module &M, CallInst &CI);
   bool visitInvokeInst(Module &M, InvokeInst &CI);
   bool visitCallSite(Module &M, CallSite CS);

Modified: poolalloc/trunk/lib/AssistDS/TypeChecks.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/AssistDS/TypeChecks.cpp?rev=132343&r1=132342&r2=132343&view=diff
==============================================================================
--- poolalloc/trunk/lib/AssistDS/TypeChecks.cpp (original)
+++ poolalloc/trunk/lib/AssistDS/TypeChecks.cpp Tue May 31 12:06:04 2011
@@ -77,6 +77,8 @@
 
   UsedTypes.clear(); // Reset if run multiple times.
   VAListFunctions.clear();
+  VAArgFunctions.clear();
+  ByValFunctions.clear();
 
   Function *MainF = M.getFunction("main");
   if (MainF == 0 || MainF->isDeclaration()) {
@@ -101,15 +103,43 @@
     modified |= visitGlobal(M, *I, I->getInitializer(), *MainI, 0);
   }
 
+  // Iterate and find all byval argument functions
+
+  // Iterate and find all varargs functions
+  for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) {
+    Function &F = *MI;
+    if(F.isDeclaration())
+      continue;
+    if(F.isVarArg()) {
+      VAArgFunctions.push_back(&F);
+      continue;
+    }
+    bool isVAListFunc = false;
+    const Type *ListType  = M.getTypeByName("struct.__va_list_tag");
+    if(!ListType)
+      continue;
+
+    const Type *ListPtrType = ListType->getPointerTo();
+    for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
+      if(I->getType() == ListPtrType) {
+        isVAListFunc = true;
+        break;
+      }
+    }
+    if(isVAListFunc) {
+      VAListFunctions.push_back(&F);
+      continue;
+    }
+  }
+
+  // Iterate and find all VAList functions
   std::vector<Function *> toProcess;
-  std::vector<Function *> toProcess1;
   for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) {
     Function &F = *MI;
     if(F.isDeclaration())
       continue;
     // record all the original functions in the program
     toProcess.push_back(&F);
-    toProcess1.push_back(&F);
 
     // Loop over all of the instructions in the function, 
     // adding their return type as well as the types of their operands.
@@ -142,28 +172,87 @@
     Function *F = toProcess.back();
     toProcess.pop_back();
     modified |= visitByValFunction(M, *F);
+  }
+  
+  // NOTE:must visit before VAArgFunctions, to populate the map with the
+  // correct cloned functions.
+  while(!VAListFunctions.empty()) {
+    Function *F = VAListFunctions.back();
+    VAListFunctions.pop_back();
     modified |= visitVAListFunction(M, *F);
-    // NOTE:must visit first
   }
- 
+
   // iterate through all the VAList funtions and modify call sites
   // to call the new function 
-  std::map<Function *, Function *>::iterator FI = VAListFunctions.begin(), FE = VAListFunctions.end();
+  std::map<Function *, Function *>::iterator FI = VAListFunctionsMap.begin(), FE = VAListFunctionsMap.end();
   for(; FI != FE; FI++) {
     visitVAListCall(FI->second);
   }
-  while(!toProcess1.empty()) {
-    Function *F = toProcess1.back();
-    toProcess1.pop_back();
-    if(F->isVarArg()) {
-      modified |= visitVarArgFunction(M, *F);
-    }
+  while(!VAArgFunctions.empty()) {
+    Function *F = VAArgFunctions.back();
+    VAArgFunctions.pop_back();
+    assert(F->isVarArg());
+    modified |= visitVarArgFunction(M, *F);
   }
 
+  addTypeMapGlobal(M);
   numTypes += UsedTypes.size();
 
   return modified;
 }
+  
+void 
+TypeChecks::addTypeMapGlobal(Module &M) {
+
+  // add a global that has the metadata -> typeString mapping
+  ArrayType*  AType = ArrayType::get(VoidPtrTy, UsedTypes.size() + 1);
+  std::vector<Constant *> Values;
+  Values.reserve(UsedTypes.size() + 1);
+  std::vector<Constant *> Indices;
+  Indices.push_back(ConstantInt::get(Int32Ty,0));
+  Indices.push_back(ConstantInt::get(Int32Ty,0));
+
+  // Add an entry for uninitialized(Type Number = 0)
+
+  Constant *CA = ConstantArray::get(M.getContext(), "UNINIT", true);
+  GlobalVariable *GV = new GlobalVariable(M, 
+                                          CA->getType(),
+                                          true,
+                                          GlobalValue::ExternalLinkage,
+                                          CA,
+                                          "");
+  GV->setInitializer(CA);
+  Constant *C = ConstantExpr::getGetElementPtr(GV, &Indices[0], Indices.size());
+  Values[0] = C;
+
+  std::map<const Type*, unsigned int >::iterator TI = UsedTypes.begin(), TE = UsedTypes.end(); 
+  for(;TI!=TE; ++TI) {
+    std::string *type = new std::string();
+    llvm::raw_string_ostream *test = new llvm::raw_string_ostream(*type);
+
+    WriteTypeSymbolic(*test, TI->first, &M);
+    Constant *CA = ConstantArray::get(M.getContext(), test->str(), true);
+    GlobalVariable *GV = new GlobalVariable(M, 
+                                            CA->getType(),
+                                            true,
+                                            GlobalValue::ExternalLinkage,
+                                            CA,
+                                            "");
+    GV->setInitializer(CA);
+    Constant *C = ConstantExpr::getGetElementPtr(GV, &Indices[0], Indices.size());
+    Values[TI->second]= C;
+  }
+
+  new GlobalVariable(M, 
+                     AType,
+                     true,
+                     GlobalValue::ExternalLinkage,
+                     ConstantArray::get(AType, &Values[0], UsedTypes.size() + 1),
+                     "typeNames"
+                    );
+
+  return;
+}
 
 void
 TypeChecks::visitVAListCall(Function *F) {
@@ -173,7 +262,7 @@
       if(!CI)
         continue;
       Function *CalledF = dyn_cast<Function>(CI->getCalledFunction());
-      if(VAListFunctions.find(CalledF) == VAListFunctions.end())
+      if(VAListFunctionsMap.find(CalledF) == VAListFunctionsMap.end())
         continue;
       Function::arg_iterator NII = F->arg_begin();
       std::vector<Value *>Args;
@@ -184,7 +273,7 @@
         // Add the original argument
         Args.push_back(CI->getOperand(i));
       }
-      CallInst *CINew = CallInst::Create(VAListFunctions[CalledF], Args.begin(), Args.end(), "", CI);
+      CallInst *CINew = CallInst::Create(VAListFunctionsMap[CalledF], Args.begin(), Args.end(), "", CI);
       CI->replaceAllUsesWith(CINew);
       CI->eraseFromParent();
     }
@@ -192,113 +281,111 @@
 }
 
 bool
-TypeChecks::visitVAListFunction(Module &M, Function &F_orig) {
-  if(!F_orig.hasInternalLinkage())
-    return false;
+  TypeChecks::visitVAListFunction(Module &M, Function &F_orig) {
+    if(!F_orig.hasInternalLinkage())
+      return false;
 
-  int VAListArgNum = 0;
-  // Check if one of the arguments is a va_list
-  bool isVAListFunc = false;
-  const Type *ListType  = M.getTypeByName("struct.__va_list_tag");
-  const Type *ListPtrType = ListType->getPointerTo();
-  Argument *VAListArg = NULL; 
-  for (Function::arg_iterator I = F_orig.arg_begin(), E = F_orig.arg_end(); I != E; ++I) {
-    VAListArgNum ++;
-    if(I->getType() == ListPtrType) {
-      VAListArg = I;
-      isVAListFunc = true;
-      break;
+    int VAListArgNum = 0;
+    // Check if one of the arguments is a va_list
+    const Type *ListType  = M.getTypeByName("struct.__va_list_tag");
+    if(!ListType)
+      return false;
+    const Type *ListPtrType = ListType->getPointerTo();
+    Argument *VAListArg = NULL; 
+    for (Function::arg_iterator I = F_orig.arg_begin(), E = F_orig.arg_end(); I != E; ++I) {
+      VAListArgNum ++;
+      if(I->getType() == ListPtrType) {
+        VAListArg = I;
+        break;
+      }
     }
-  }
-  if(!isVAListFunc)
-    return false;
-
-  // Clone the function to add arguments for count, MD
-
-  // 1. Create the new argument types vector
-  std::vector<const Type*>TP;
-  TP.push_back(Int64Ty); // for count
-  TP.push_back(Int64Ty); // for count
-  TP.push_back(VoidPtrTy); // for MD
-  for (Function::arg_iterator I = F_orig.arg_begin(), E = F_orig.arg_end(); I != E; ++I) {
-    TP.push_back(I->getType());
-  }
-  // 2. Create the new function prototype
-  const FunctionType *NewFTy = FunctionType::get(F_orig.getReturnType(), TP, false);
-  Function *F = Function::Create(NewFTy,
-                                 GlobalValue::InternalLinkage,
-                                 F_orig.getNameStr() + ".INT",
-                                 &M);
-
-  // 3. Set the mapping for args
-  Function::arg_iterator NI = F->arg_begin();
-  DenseMap<const Value*, Value*> ValueMap;
-  NI->setName("TotalCount");
-  NI++;
-  NI->setName("CurrentCount");
-  NI++;
-  NI->setName("MD");
-  NI++;
-  for (Function::arg_iterator II = F_orig.arg_begin(); NI != F->arg_end(); ++II, ++NI) {
-    // Each new argument maps to the argument in the old function
-    // For these arguments, also copy over the attributes
-    ValueMap[II] = NI;
-    NI->setName(II->getName());
-    NI->addAttr(F_orig.getAttributes().getParamAttributes(II->getArgNo() + 1));
-  }
 
-  // 4. Copy over the attributes for the function.
-  F->setAttributes(F->getAttributes()
-                   .addAttr(0, F_orig.getAttributes().getRetAttributes()));
-  F->setAttributes(F->getAttributes().addAttr(~0, F_orig.getAttributes().getFnAttributes()));
+    // Clone the function to add arguments for count, MD
 
-  // 5. Perform the cloning.
-  SmallVector<ReturnInst*,100> Returns;
-  CloneFunctionInto(F, &F_orig, ValueMap, Returns);
-
-  VAListFunctions[&F_orig] =  F;
-  inst_iterator InsPt = inst_begin(F);
-
-  // Store the information
-  Function::arg_iterator NII = F->arg_begin();
-  AllocaInst *VASizeLoc = new AllocaInst(Int64Ty, "", &*InsPt);
-  new StoreInst(NII, VASizeLoc, &*InsPt);
-  NII++;
-  AllocaInst *Counter = new AllocaInst(Int64Ty, "",&*InsPt);
-  new StoreInst(NII, Counter, &*InsPt); 
-  NII++;
-  AllocaInst *VAMDLoc = new AllocaInst(VoidPtrTy, "", &*InsPt);
-  new StoreInst(NII, VAMDLoc, &*InsPt);
-
-  // instrument va_arg to increment the counter
-  for (Function::iterator B = F->begin(), FE = F->end(); B != FE; ++B) {
-    for (BasicBlock::iterator I = B->begin(), BE = B->end(); I != BE;) {
-      VAArgInst *VI = dyn_cast<VAArgInst>(I++);
-      if(!VI)
-        continue;
-      Constant *One = ConstantInt::get(Int64Ty, 1);
-      LoadInst *OldValue = new LoadInst(Counter, "count", VI);
-      Instruction *NewValue = BinaryOperator::Create(BinaryOperator::Add,
-                                                     OldValue,
-                                                     One,
-                                                     "count",
-                                                     VI);
-      new StoreInst(NewValue, Counter, VI);
-      std::vector<Value *> Args;
-      Instruction *VASize = new LoadInst(VASizeLoc, "", VI);
-      Instruction *VAMetaData = new LoadInst(VAMDLoc, "", VI);
-      Args.push_back(VASize);
-      Args.push_back(OldValue);
-      Args.push_back(ConstantInt::get(Int8Ty, getTypeMarker(VI->getType())));
-      Args.push_back(VAMetaData);
-      Args.push_back(ConstantInt::get(Int32Ty, tagCounter++));
-      Constant *Func = M.getOrInsertFunction("compareTypeAndNumber", VoidTy, Int64Ty, Int64Ty, Int8Ty, VoidPtrTy, Int32Ty, NULL);
-      CallInst::Create(Func, Args.begin(), Args.end(), "", VI);
+    // 1. Create the new argument types vector
+    std::vector<const Type*>TP;
+    TP.push_back(Int64Ty); // for count
+    TP.push_back(Int64Ty); // for count
+    TP.push_back(VoidPtrTy); // for MD
+    for (Function::arg_iterator I = F_orig.arg_begin(), E = F_orig.arg_end(); I != E; ++I) {
+      TP.push_back(I->getType());
+    }
+    // 2. Create the new function prototype
+    const FunctionType *NewFTy = FunctionType::get(F_orig.getReturnType(), TP, false);
+    Function *F = Function::Create(NewFTy,
+                                   GlobalValue::InternalLinkage,
+                                   F_orig.getNameStr() + ".INT",
+                                   &M);
+
+    // 3. Set the mapping for args
+    Function::arg_iterator NI = F->arg_begin();
+    DenseMap<const Value*, Value*> ValueMap;
+    NI->setName("TotalCount");
+    NI++;
+    NI->setName("CurrentCount");
+    NI++;
+    NI->setName("MD");
+    NI++;
+    for (Function::arg_iterator II = F_orig.arg_begin(); NI != F->arg_end(); ++II, ++NI) {
+      // Each new argument maps to the argument in the old function
+      // For these arguments, also copy over the attributes
+      ValueMap[II] = NI;
+      NI->setName(II->getName());
+      NI->addAttr(F_orig.getAttributes().getParamAttributes(II->getArgNo() + 1));
+    }
+
+    // 4. Copy over the attributes for the function.
+    F->setAttributes(F->getAttributes()
+                     .addAttr(0, F_orig.getAttributes().getRetAttributes()));
+    F->setAttributes(F->getAttributes().addAttr(~0, F_orig.getAttributes().getFnAttributes()));
+
+    // 5. Perform the cloning.
+    SmallVector<ReturnInst*,100> Returns;
+    CloneFunctionInto(F, &F_orig, ValueMap, Returns);
+
+    VAListFunctionsMap[&F_orig] =  F;
+    inst_iterator InsPt = inst_begin(F);
+
+    // Store the information
+    Function::arg_iterator NII = F->arg_begin();
+    AllocaInst *VASizeLoc = new AllocaInst(Int64Ty, "", &*InsPt);
+    new StoreInst(NII, VASizeLoc, &*InsPt);
+    NII++;
+    AllocaInst *Counter = new AllocaInst(Int64Ty, "",&*InsPt);
+    new StoreInst(NII, Counter, &*InsPt); 
+    NII++;
+    AllocaInst *VAMDLoc = new AllocaInst(VoidPtrTy, "", &*InsPt);
+    new StoreInst(NII, VAMDLoc, &*InsPt);
+
+    // instrument va_arg to increment the counter
+    for (Function::iterator B = F->begin(), FE = F->end(); B != FE; ++B) {
+      for (BasicBlock::iterator I = B->begin(), BE = B->end(); I != BE;) {
+        VAArgInst *VI = dyn_cast<VAArgInst>(I++);
+        if(!VI)
+          continue;
+        Constant *One = ConstantInt::get(Int64Ty, 1);
+        LoadInst *OldValue = new LoadInst(Counter, "count", VI);
+        Instruction *NewValue = BinaryOperator::Create(BinaryOperator::Add,
+                                                       OldValue,
+                                                       One,
+                                                       "count",
+                                                       VI);
+        new StoreInst(NewValue, Counter, VI);
+        std::vector<Value *> Args;
+        Instruction *VASize = new LoadInst(VASizeLoc, "", VI);
+        Instruction *VAMetaData = new LoadInst(VAMDLoc, "", VI);
+        Args.push_back(VASize);
+        Args.push_back(OldValue);
+        Args.push_back(ConstantInt::get(Int8Ty, getTypeMarker(VI->getType())));
+        Args.push_back(VAMetaData);
+        Args.push_back(ConstantInt::get(Int32Ty, tagCounter++));
+        Constant *Func = M.getOrInsertFunction("compareTypeAndNumber", VoidTy, Int64Ty, Int64Ty, Int8Ty, VoidPtrTy, Int32Ty, NULL);
+        CallInst::Create(Func, Args.begin(), Args.end(), "", VI);
+      }
     }
-  }
 
-return true;
-}
+    return true;
+  }
 
 // Transform Variable Argument functions, by also passing
 // the relavant metadata info
@@ -419,7 +506,7 @@
       if(!CI)
         continue;
       Function *CalledF = dyn_cast<Function>(CI->getCalledFunction());
-      if(VAListFunctions.find(CalledF) == VAListFunctions.end())
+      if(VAListFunctionsMap.find(CalledF) == VAListFunctionsMap.end())
         continue;
       std::vector<Value *>Args;
       Instruction *VASize = new LoadInst(VASizeLoc, "", CI);
@@ -432,7 +519,7 @@
         // Add the original argument
         Args.push_back(CI->getOperand(i));
       }
-      CallInst *CINew = CallInst::Create(VAListFunctions[CalledF], Args.begin(), Args.end(), "", CI);
+      CallInst *CINew = CallInst::Create(VAListFunctionsMap[CalledF], Args.begin(), Args.end(), "", CI);
       CI->replaceAllUsesWith(CINew);
       CI->eraseFromParent();
     }

Modified: poolalloc/trunk/runtime/DynamicTypeChecks/TypeRuntime.c
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/runtime/DynamicTypeChecks/TypeRuntime.c?rev=132343&r1=132342&r2=132343&view=diff
==============================================================================
--- poolalloc/trunk/runtime/DynamicTypeChecks/TypeRuntime.c (original)
+++ poolalloc/trunk/runtime/DynamicTypeChecks/TypeRuntime.c Tue May 31 12:06:04 2011
@@ -19,6 +19,8 @@
 uint8_t *shadow_begin;
 uint8_t *shadow_end;
 
+extern char* typeNames[];
+
 void trackInitInst(void *ptr, uint64_t size, uint32_t tag);
 
 uintptr_t maskAddress(void *ptr) {
@@ -116,7 +118,7 @@
  */
 void compareTypes(uint8_t typeNumberSrc, uint8_t typeNumberDest, uint32_t tag) {
   if(typeNumberSrc != typeNumberDest) {
-    printf("Type mismatch: detecting %u, expecting %u! %u \n", typeNumberDest, typeNumberSrc, tag);
+    printf("Type mismatch: detecting %u, expecting %u! %u %s, %s\n", typeNumberDest, typeNumberSrc, tag, typeNames[typeNumberDest], typeNames[typeNumberSrc]);
   }
 }
 
@@ -150,9 +152,8 @@
   /* Check if this an initialized but untyped memory.*/
   if (typeNumber != shadow_begin[p]) {
     if (shadow_begin[p] != 0xFF) {
-
-      printf("Type mismatch: detecting %p %u, expecting %u! %u \n", ptr, typeNumber, shadow_begin[p], tag);
-      i = size;
+      printf("Type mismatch: detecting %u, expecting %u! %u %s %s\n", typeNumber, shadow_begin[p], tag, typeNames[typeNumber], typeNames[shadow_begin[p]]);
+      return;
     } else {
       /* If so, set type to the type being read.
          Check that none of the bytes are typed.*/





More information about the llvm-commits mailing list