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 @@ -2552,13 +2552,20 @@ constexpr uint64_t kMaxRZ = 1 << 18; const uint64_t MinRZ = getMinRedzoneSizeForGlobal(); - // Calculate RZ, where MinRZ <= RZ <= MaxRZ, and RZ ~ 1/4 * SizeInBytes. - uint64_t RZ = - std::max(MinRZ, std::min(kMaxRZ, (SizeInBytes / MinRZ / 4) * MinRZ)); + uint64_t RZ = 0; + // Reduce redzone size for small size objects, e.g. int, char[1]. MinRZ is at + // least 32 bytes, optimize when SizeInBytes is less than half of MinRZ. + if (SizeInBytes < MinRZ / 2) { + RZ = MinRZ - SizeInBytes; + } else { + // Calculate RZ, where MinRZ <= RZ <= MaxRZ, and RZ ~ 1/4 * SizeInBytes. + RZ = std::max(MinRZ, std::min(kMaxRZ, (SizeInBytes / MinRZ / 4) * MinRZ)); + + // Round up to multiple of MinRZ. + if (SizeInBytes % MinRZ) + RZ += MinRZ - (SizeInBytes % MinRZ); + } - // Round up to multiple of MinRZ. - if (SizeInBytes % MinRZ) - RZ += MinRZ - (SizeInBytes % MinRZ); assert((RZ + SizeInBytes) % MinRZ == 0); return RZ;