diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp --- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp @@ -2372,6 +2372,18 @@ return false; } + // Create a global zero-sized array precedence before GlobalsToChange[0] so + // that the underflow of the it could be observable. + ArrayType *ZeroSizedArrayTy = ArrayType::get(IRB.getInt8Ty(), 0); + GlobalVariable *UnderflowObserverGV = new GlobalVariable( + M, ZeroSizedArrayTy, /*isConstant*/ false, GlobalVariable::PrivateLinkage, + ConstantArray::get(ZeroSizedArrayTy, {}), kAsanGenPrefix, + GlobalsToChange[0]); + GlobalsToChange.insert(GlobalsToChange.begin(), UnderflowObserverGV); + + // Update size as we insert a dummy one. + n = GlobalsToChange.size(); + auto &DL = M.getDataLayout(); // A global is described by a structure