[asan] add experimental -asan-realign-stack option (true by default, which does not...
authorKostya Serebryany <kcc@google.com>
Tue, 4 Dec 2012 06:14:01 +0000 (06:14 +0000)
committerKostya Serebryany <kcc@google.com>
Tue, 4 Dec 2012 06:14:01 +0000 (06:14 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169216 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Instrumentation/AddressSanitizer.cpp

index 92a678c34e2e6767515148c1286a00d8e835c272..f095cff33c9b5f9f26fd3b89cafb8720333d675f 100644 (file)
@@ -115,6 +115,8 @@ static cl::opt<bool> ClInitializers("asan-initialization-order",
        cl::desc("Handle C++ initializer order"), cl::Hidden, cl::init(false));
 static cl::opt<bool> ClMemIntrin("asan-memintrin",
        cl::desc("Handle memset/memcpy/memmove"), cl::Hidden, cl::init(true));
+static cl::opt<bool> ClRealignStack("asan-realign-stack",
+       cl::desc("Realign stack to 32"), cl::Hidden, cl::init(true));
 static cl::opt<std::string> ClBlacklistFile("asan-blacklist",
        cl::desc("File containing the list of objects to ignore "
                 "during instrumentation"), cl::Hidden);
@@ -1159,6 +1161,7 @@ bool AddressSanitizer::poisonStackInFunction(Function &F) {
 
   // Filter out Alloca instructions we want (and can) handle.
   // Collect Ret instructions.
+  unsigned ResultAlignment = 1 << MappingScale();
   for (Function::iterator FI = F.begin(), FE = F.end();
        FI != FE; ++FI) {
     BasicBlock &BB = *FI;
@@ -1174,7 +1177,7 @@ bool AddressSanitizer::poisonStackInFunction(Function &F) {
       if (AI->isArrayAllocation()) continue;
       if (!AI->isStaticAlloca()) continue;
       if (!AI->getAllocatedType()->isSized()) continue;
-      if (AI->getAlignment() > RedzoneSize()) continue;
+      ResultAlignment = std::max(ResultAlignment, AI->getAlignment());
       AllocaVec.push_back(AI);
       uint64_t AlignedSize =  getAlignedAllocaSize(AI);
       TotalSize += AlignedSize;
@@ -1195,7 +1198,9 @@ bool AddressSanitizer::poisonStackInFunction(Function &F) {
   Type *ByteArrayTy = ArrayType::get(IRB.getInt8Ty(), LocalStackSize);
   AllocaInst *MyAlloca =
       new AllocaInst(ByteArrayTy, "MyAlloca", InsBefore);
-  MyAlloca->setAlignment(RedzoneSize());
+  if (ClRealignStack && ResultAlignment < RedzoneSize())
+    ResultAlignment = RedzoneSize();
+  MyAlloca->setAlignment(ResultAlignment);
   assert(MyAlloca->isStaticAlloca());
   Value *OrigStackBase = IRB.CreatePointerCast(MyAlloca, IntptrTy);
   Value *LocalStackBase = OrigStackBase;