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

Arushi Aggarwal aggarwa4 at illinois.edu
Sat May 21 08:38:45 PDT 2011


Author: aggarwa4
Date: Sat May 21 10:38:45 2011
New Revision: 131790

URL: http://llvm.org/viewvc/llvm-project?rev=131790&view=rev
Log:
Better handling of var_args. Instead of
putting the type information before 
each argument, put it in an array passed 
in from the caller, and as the 2nd argument in
the va_args list.



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=131790&r1=131789&r2=131790&view=diff
==============================================================================
--- poolalloc/trunk/include/assistDS/TypeChecks.h (original)
+++ poolalloc/trunk/include/assistDS/TypeChecks.h Sat May 21 10:38:45 2011
@@ -60,7 +60,6 @@
 
   bool initShadow(Module &M);
   bool unmapShadow(Module &M, Instruction &I);
-  bool visitVAArgInst(Module &M, VAArgInst &VI);
   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=131790&r1=131789&r2=131790&view=diff
==============================================================================
--- poolalloc/trunk/lib/AssistDS/TypeChecks.cpp (original)
+++ poolalloc/trunk/lib/AssistDS/TypeChecks.cpp Sat May 21 10:38:45 2011
@@ -167,8 +167,6 @@
         modified |= visitInvokeInst(M, *II);
       } else if (AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
         modified |= visitAllocaInst(M, *AI);
-      } else if (VAArgInst *VI = dyn_cast<VAArgInst>(&I)) {
-        modified |= visitVAArgInst(M, *VI);
       }
     }
   }
@@ -197,40 +195,11 @@
   if(!F.hasInternalLinkage())
     return false;
   // FIXME:handle external functions
-
-  // Find all uses of the function
-  for(Value::use_iterator ui = F.use_begin(), ue = F.use_end();
-      ui != ue;)  {
-    // Check for call sites
-    CallInst *CI = dyn_cast<CallInst>(ui++);
-    if(!CI)
-      continue;
-    if(CI->getNumOperands() - 1 <= F.arg_size()) 
-      continue;
-    std::vector<Value *> Args;
-    unsigned int i = F.arg_size() + 1;
-    for(i = 1 ;i < CI->getNumOperands(); i++) {
-      // As the first vararg argument pass the number of var_arg arguments
-      if(i == F.arg_size() + 1) {
-        Args.push_back(ConstantInt::get(Int64Ty, 2*(CI->getNumOperands()-1 - F.arg_size())));
-      }
-      if(i > F.arg_size()) {
-        // For each vararg argument, also add its type information before it
-        Args.push_back(ConstantInt::get(Int8Ty, UsedTypes[CI->getOperand(i)->getType()]));
-      }
-
-      // Add the original argument
-      Args.push_back(CI->getOperand(i));
-    }
-
-    // Create the new call
-    CallInst *CI_New = CallInst::Create(CI->getCalledValue(), Args.begin(), Args.end(), "", CI);
-    CI->replaceAllUsesWith(CI_New);
-    CI->eraseFromParent();
-  }
-
+  
   // Modify the function to add a call to get the num of arguments
   VAArgInst *VASize = NULL;
+  VAArgInst *VAMetaData = NULL;
+  CallInst *VAStart = NULL;
   for (Function::iterator B = F.begin(), FE = F.end(); B != FE; ++B) {
     for (BasicBlock::iterator I = B->begin(), BE = B->end(); I != BE;) {
       CallInst *CI = dyn_cast<CallInst>(I++);
@@ -243,13 +212,23 @@
         continue;
       if(CalledF->getIntrinsicID() != Intrinsic::vastart) 
         continue;
+      VAStart = CI;
       VASize = new VAArgInst(CI->getOperand(1), Int64Ty, "NumArgs");
+      VAMetaData = new VAArgInst(CI->getOperand(1), VoidPtrTy, "MD");
       VASize->insertAfter(CI);
+      VAMetaData->insertAfter(VASize);
       break;
     }
   }
   assert(VASize && "Varargs function without a call to VAStart???");
 
+  // FIXME:handle external functions
+  // For every va_arg instruction,
+  // In a transformed function, we pass in the type metadata before the actual argument
+  // Read metadata from va_list
+  // And then add a compare in the runtime
+
+
   // Modify function to add checks on every var_arg call to ensure that we
   // are not accessing more arguments than we passed in.
 
@@ -258,7 +237,6 @@
   new StoreInst(ConstantInt::get(Int64Ty, 0), Counter, VASize); 
 
   // 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++);
@@ -266,6 +244,8 @@
         continue;
       if(VI == VASize)
         continue;
+      if(VI == VAMetaData)
+        continue;
       Constant *One = ConstantInt::get(Int64Ty, 1);
       LoadInst *OldValue = new LoadInst(Counter, "count", VI);
       Instruction *NewValue = BinaryOperator::Create(BinaryOperator::Add,
@@ -274,17 +254,58 @@
                                                      "count",
                                                      VI);
       new StoreInst(NewValue, Counter, VI);
-
-
       std::vector<Value *> Args;
       Args.push_back(VASize);
-      Args.push_back(NewValue);
+      Args.push_back(OldValue);
+      Args.push_back(ConstantInt::get(Int8Ty, UsedTypes[VI->getType()]));
+      Args.push_back(VAMetaData);
       Args.push_back(ConstantInt::get(Int32Ty, tagCounter++));
-      Constant *Func = M.getOrInsertFunction("compareNumber", VoidTy, Int64Ty, Int64Ty,Int32Ty,  NULL);
+      Constant *Func = M.getOrInsertFunction("compareTypeAndNumber", VoidTy, Int64Ty, Int64Ty, Int8Ty, VoidPtrTy, Int32Ty, NULL);
       CallInst::Create(Func, Args.begin(), Args.end(), "", VI);
+    }
+  }
 
+  // Find all uses of the function
+  for(Value::use_iterator ui = F.use_begin(), ue = F.use_end();
+      ui != ue;)  {
+    // Check for call sites
+    CallInst *CI = dyn_cast<CallInst>(ui++);
+    if(!CI)
+      continue;
+    if(CI->getNumOperands() - 1 <= F.arg_size()) 
+      continue;
+    std::vector<Value *> Args;
+    unsigned int i;
+    unsigned int NumVarArgs = CI->getNumOperands() -F.arg_size() - 1;
+    Value *NumArgs = ConstantInt::get(Int32Ty, NumVarArgs);
+    AllocaInst *AI = new AllocaInst(Int8Ty, NumArgs, "", CI);
+    // set the metadata for the varargs in AI
+    unsigned int j =0;
+    for(i = F.arg_size() + 1; i <CI->getNumOperands(); i++) {
+      Value *Idx[2];
+      Idx[0] = ConstantInt::get(Int32Ty, j++);
+      // For each vararg argument, also add its type information before it
+      GetElementPtrInst *GEP = GetElementPtrInst::CreateInBounds(AI, Idx, Idx + 1, "", CI);
+      new StoreInst(ConstantInt::get(Int8Ty, UsedTypes[CI->getOperand(i)->getType()]), GEP, CI);
+    }
+
+    for(i = 1 ;i < CI->getNumOperands(); i++) {
+      // As the first vararg argument pass the number of var_arg arguments
+      if(i == F.arg_size() + 1) {
+        Args.push_back(ConstantInt::get(Int64Ty, NumVarArgs));
+        Args.push_back(AI);
+      }
+
+      // Add the original argument
+      Args.push_back(CI->getOperand(i));
     }
+
+    // Create the new call
+    CallInst *CI_New = CallInst::Create(CI->getCalledValue(), Args.begin(), Args.end(), "", CI);
+    CI->replaceAllUsesWith(CI_New);
+    CI->eraseFromParent();
   }
+
   return true;
 }
 
@@ -448,7 +469,7 @@
 
         if (RAttrs)
           AttributesVec.push_back(AttributeWithIndex::get(0, RAttrs));
-        
+
         Function::arg_iterator II = F.arg_begin();
 
         for(unsigned j =1;j<CI->getNumOperands();j++, II++) {
@@ -635,40 +656,40 @@
   return true;
 }
 
-bool TypeChecks::visitMain(Module &M, Function &MainFunc) {
-  if(MainFunc.arg_size() != 2)
-    // No need to register
-    return false;
+  bool TypeChecks::visitMain(Module &M, Function &MainFunc) {
+    if(MainFunc.arg_size() != 2)
+      // No need to register
+      return false;
 
-  Function::arg_iterator AI = MainFunc.arg_begin();
-  Value *Argc = AI;
-  Value *Argv = ++AI;
-
-  Instruction *InsertPt = MainFunc.front().begin();
-  Constant * RegisterArgv = M.getOrInsertFunction("trackArgvType", VoidPtrTy, Argc->getType(), Argv->getType(), NULL);
-  std::vector<Value *> fargs;
-  fargs.push_back (Argc);
-  fargs.push_back (Argv);
-  CallInst *CI = CallInst::Create (RegisterArgv, fargs.begin(), fargs.end(), "", InsertPt);
-  CastInst *BCII = BitCastInst::CreatePointerCast(CI, Argv->getType());
-  BCII->insertAfter(CI);
-  std::vector<User *> Uses;
-  Value::use_iterator UI = Argv->use_begin();
-  for (; UI != Argv->use_end(); ++UI) {
-    if (Instruction * Use = dyn_cast<Instruction>(UI))
-      if (CI != Use) {
-        Uses.push_back (*UI);
-      }
-  }
+    Function::arg_iterator AI = MainFunc.arg_begin();
+    Value *Argc = AI;
+    Value *Argv = ++AI;
+
+    Instruction *InsertPt = MainFunc.front().begin();
+    Constant * RegisterArgv = M.getOrInsertFunction("trackArgvType", VoidPtrTy, Argc->getType(), Argv->getType(), NULL);
+    std::vector<Value *> fargs;
+    fargs.push_back (Argc);
+    fargs.push_back (Argv);
+    CallInst *CI = CallInst::Create (RegisterArgv, fargs.begin(), fargs.end(), "", InsertPt);
+    CastInst *BCII = BitCastInst::CreatePointerCast(CI, Argv->getType());
+    BCII->insertAfter(CI);
+    std::vector<User *> Uses;
+    Value::use_iterator UI = Argv->use_begin();
+    for (; UI != Argv->use_end(); ++UI) {
+      if (Instruction * Use = dyn_cast<Instruction>(UI))
+        if (CI != Use) {
+          Uses.push_back (*UI);
+        }
+    }
 
-  while (Uses.size()) {
-    User *Use = Uses.back();
-    Uses.pop_back();
-    Use->replaceUsesOfWith (Argv, BCII);
-  }
+    while (Uses.size()) {
+      User *Use = Uses.back();
+      Uses.pop_back();
+      Use->replaceUsesOfWith (Argv, BCII);
+    }
 
-  return true;
-}
+    return true;
+  }
 
 bool TypeChecks::visitGlobal(Module &M, GlobalVariable &GV, 
                              Constant *C, Instruction &I, unsigned offset) {
@@ -830,27 +851,6 @@
   return true;
 }
 
-// Insert runtime check for va_arg instructions
-bool TypeChecks::visitVAArgInst(Module &M, VAArgInst &VI) {
-  Function *Func = VI.getParent()->getParent();
-  if(!Func->hasInternalLinkage())
-    return false;
-
-  // FIXME:handle external functions
-  // For every va_arg instruction,
-  // In a transformed function, we pass in the type metadata before the actual argument
-  // Read metadata from va_list
-  // And then add a compare in the runtime
-  VAArgInst *VI_Type = new VAArgInst(VI.getOperand(0), Int8Ty, "", &VI);
-  std::vector<Value *> Args;
-  Args.push_back(VI_Type);
-  Args.push_back(ConstantInt::get(Int8Ty, UsedTypes[VI.getType()]));
-  Args.push_back(ConstantInt::get(Int32Ty, tagCounter++));
-  Constant *F = M.getOrInsertFunction("compareTypes", VoidTy, Int8Ty, Int8Ty,Int32Ty,  NULL);
-  CallInst::Create(F, Args.begin(), Args.end(), "", &VI);
-  return true;
-}
-
 // Insert runtime checks for certain call instructions
 bool TypeChecks::visitCallInst(Module &M, CallInst &CI) {
   return visitCallSite(M, &CI);

Modified: poolalloc/trunk/runtime/DynamicTypeChecks/TypeRuntime.c
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/runtime/DynamicTypeChecks/TypeRuntime.c?rev=131790&r1=131789&r2=131790&view=diff
==============================================================================
--- poolalloc/trunk/runtime/DynamicTypeChecks/TypeRuntime.c (original)
+++ poolalloc/trunk/runtime/DynamicTypeChecks/TypeRuntime.c Sat May 21 10:38:45 2011
@@ -139,6 +139,12 @@
     printf("Type mismatch: Accessing variable %lu, passed only %lu! %u \n", ArgAccessed, NumArgsPassed, tag);
   }
 }
+
+void compareTypeAndNumber(uint64_t NumArgsPassed, uint64_t ArgAccessed, uint8_t TypeAccessed, void *MD, uint32_t tag) {
+  compareNumber(NumArgsPassed, ArgAccessed, tag);
+  compareTypes(TypeAccessed, ((uint8_t*)MD)[ArgAccessed], tag);
+}
+
 /**
  * Check the loaded type against the type recorded in the shadow memory.
  */





More information about the llvm-commits mailing list