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; diff --git a/llvm/test/Instrumentation/AddressSanitizer/AMDGPU/adaptive_constant_global_redzones.ll b/llvm/test/Instrumentation/AddressSanitizer/AMDGPU/adaptive_constant_global_redzones.ll --- a/llvm/test/Instrumentation/AddressSanitizer/AMDGPU/adaptive_constant_global_redzones.ll +++ b/llvm/test/Instrumentation/AddressSanitizer/AMDGPU/adaptive_constant_global_redzones.ll @@ -7,7 +7,7 @@ ; for objects in constant address space. @G10 = addrspace(4) global [10 x i8] zeroinitializer, align 1 -; CHECK: @G10 = addrspace(4) global { [10 x i8], [54 x i8] } +; CHECK: @G10 = addrspace(4) global { [10 x i8], [22 x i8] } @G31 = addrspace(4) global [31 x i8] zeroinitializer, align 1 @G32 = addrspace(4) global [32 x i8] zeroinitializer, align 1 diff --git a/llvm/test/Instrumentation/AddressSanitizer/AMDGPU/adaptive_global_redzones.ll b/llvm/test/Instrumentation/AddressSanitizer/AMDGPU/adaptive_global_redzones.ll --- a/llvm/test/Instrumentation/AddressSanitizer/AMDGPU/adaptive_global_redzones.ll +++ b/llvm/test/Instrumentation/AddressSanitizer/AMDGPU/adaptive_global_redzones.ll @@ -7,7 +7,7 @@ ; for objects in global address space. @G10 = addrspace(1) global [10 x i8] zeroinitializer, align 1 -; CHECK: @G10 = addrspace(1) global { [10 x i8], [54 x i8] } +; CHECK: @G10 = addrspace(1) global { [10 x i8], [22 x i8] } @G31 = addrspace(1) global [31 x i8] zeroinitializer, align 1 @G32 = addrspace(1) global [32 x i8] zeroinitializer, align 1 diff --git a/llvm/test/Instrumentation/AddressSanitizer/adaptive_global_redzones.ll b/llvm/test/Instrumentation/AddressSanitizer/adaptive_global_redzones.ll --- a/llvm/test/Instrumentation/AddressSanitizer/adaptive_global_redzones.ll +++ b/llvm/test/Instrumentation/AddressSanitizer/adaptive_global_redzones.ll @@ -8,7 +8,7 @@ ; Here we check that the global redzone sizes grow with the object size. @G10 = global [10 x i8] zeroinitializer, align 1 -; CHECK: @G10 = global { [10 x i8], [54 x i8] } +; CHECK: @G10 = global { [10 x i8], [22 x i8] } @G31 = global [31 x i8] zeroinitializer, align 1 @G32 = global [32 x i8] zeroinitializer, align 1 diff --git a/llvm/test/Instrumentation/AddressSanitizer/debug-info-global-var.ll b/llvm/test/Instrumentation/AddressSanitizer/debug-info-global-var.ll --- a/llvm/test/Instrumentation/AddressSanitizer/debug-info-global-var.ll +++ b/llvm/test/Instrumentation/AddressSanitizer/debug-info-global-var.ll @@ -3,7 +3,7 @@ source_filename = "version.c" target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-apple-macosx10.12.0" -; CHECK: @version = constant { [5 x i8], [59 x i8] } {{.*}}, !dbg ![[GV:.*]] +; CHECK: @version = constant { [5 x i8], [27 x i8] } {{.*}}, !dbg ![[GV:.*]] @version = constant [5 x i8] c"4.00\00", align 1, !dbg !0 diff --git a/llvm/test/Instrumentation/AddressSanitizer/global_cstring_darwin.ll b/llvm/test/Instrumentation/AddressSanitizer/global_cstring_darwin.ll --- a/llvm/test/Instrumentation/AddressSanitizer/global_cstring_darwin.ll +++ b/llvm/test/Instrumentation/AddressSanitizer/global_cstring_darwin.ll @@ -9,14 +9,14 @@ @.str.1 = private unnamed_addr constant [13 x i8] c"Hello world.\00", align 1 @.str.2 = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1 -; CHECK: @.str.1 = internal constant { [13 x i8], [51 x i8] } { [13 x i8] c"Hello world.\00", [51 x i8] zeroinitializer }, section "__TEXT,__asan_cstring,regular", align 32 -; CHECK: @.str.2 = internal constant { [4 x i8], [60 x i8] } { [4 x i8] c"%s\0A\00", [60 x i8] zeroinitializer }, section "__TEXT,__asan_cstring,regular", align 32 +; CHECK: @.str.1 = internal constant { [13 x i8], [19 x i8] } { [13 x i8] c"Hello world.\00", [19 x i8] zeroinitializer }, section "__TEXT,__asan_cstring,regular", align 32 +; CHECK: @.str.2 = internal constant { [4 x i8], [28 x i8] } { [4 x i8] c"%s\0A\00", [28 x i8] zeroinitializer }, section "__TEXT,__asan_cstring,regular", align 32 ; Shouldn't be put into special section: @.str.3 = private unnamed_addr constant [4 x i8] c"\00\01\02\03", align 1 @.str.4 = private unnamed_addr global [7 x i8] c"Hello.\00", align 1 @.str.5 = private unnamed_addr constant [8 x i8] c"Hello.\00\00", align 1 -; CHECK: @.str.3 = internal constant { [4 x i8], [60 x i8] } { [4 x i8] c"\00\01\02\03", [60 x i8] zeroinitializer }, align 32 -; CHECK: @.str.4 = private global { [7 x i8], [57 x i8] } { [7 x i8] c"Hello.\00", [57 x i8] zeroinitializer }, align 32 -; CHECK: @.str.5 = internal constant { [8 x i8], [56 x i8] } { [8 x i8] c"Hello.\00\00", [56 x i8] zeroinitializer }, align 32 +; CHECK: @.str.3 = internal constant { [4 x i8], [28 x i8] } { [4 x i8] c"\00\01\02\03", [28 x i8] zeroinitializer }, align 32 +; CHECK: @.str.4 = private global { [7 x i8], [25 x i8] } { [7 x i8] c"Hello.\00", [25 x i8] zeroinitializer }, align 32 +; CHECK: @.str.5 = internal constant { [8 x i8], [24 x i8] } { [8 x i8] c"Hello.\00\00", [24 x i8] zeroinitializer }, align 32 diff --git a/llvm/test/Instrumentation/AddressSanitizer/local_alias.ll b/llvm/test/Instrumentation/AddressSanitizer/local_alias.ll --- a/llvm/test/Instrumentation/AddressSanitizer/local_alias.ll +++ b/llvm/test/Instrumentation/AddressSanitizer/local_alias.ll @@ -24,11 +24,11 @@ ; CHECK-NOINDICATOR-NOT: __odr_asan_gen_a ; CHECK-NOALIAS-NOT: private alias ; CHECK-INDICATOR: @__odr_asan_gen_a = global i8 0, align 1 -; CHECK-ALIAS: @0 = private alias { [2 x i32], [56 x i8] }, { [2 x i32], [56 x i8] }* @a +; CHECK-ALIAS: @0 = private alias { [2 x i32], [24 x i8] }, { [2 x i32], [24 x i8] }* @a -; CHECK-ALIAS: @1 = private alias { [2 x i32], [56 x i8] }, { [2 x i32], [56 x i8] }* @b -; CHECK-ALIAS: @2 = private alias { [2 x i32], [56 x i8] }, { [2 x i32], [56 x i8] }* @c -; CHECK-ALIAS: @3 = private alias { [2 x i32], [56 x i8] }, { [2 x i32], [56 x i8] }* @d +; CHECK-ALIAS: @1 = private alias { [2 x i32], [24 x i8] }, { [2 x i32], [24 x i8] }* @b +; CHECK-ALIAS: @2 = private alias { [2 x i32], [24 x i8] }, { [2 x i32], [24 x i8] }* @c +; CHECK-ALIAS: @3 = private alias { [2 x i32], [24 x i8] }, { [2 x i32], [24 x i8] }* @d ; Function Attrs: nounwind sanitize_address uwtable define i32 @foo(i32 %M) #0 { diff --git a/llvm/test/Instrumentation/AddressSanitizer/win-string-literal.ll b/llvm/test/Instrumentation/AddressSanitizer/win-string-literal.ll --- a/llvm/test/Instrumentation/AddressSanitizer/win-string-literal.ll +++ b/llvm/test/Instrumentation/AddressSanitizer/win-string-literal.ll @@ -8,13 +8,13 @@ ; CHECK: $"??_C@_04JIHMPGLA@asdf?$AA@" = comdat any ; CHECK: @"??_C@_04JIHMPGLA@asdf?$AA@" = -; CHECK-SAME: linkonce_odr dso_local constant { [5 x i8], [59 x i8] } -; CHECK-SAME: { [5 x i8] c"asdf\00", [59 x i8] zeroinitializer }, comdat, align 32 +; CHECK-SAME: linkonce_odr dso_local constant { [5 x i8], [27 x i8] } +; CHECK-SAME: { [5 x i8] c"asdf\00", [27 x i8] zeroinitializer }, comdat, align 32 ; CHECK: @"__asan_global_??_C@_04JIHMPGLA@asdf?$AA@" = ; CHECK-SAME: private global { i64, i64, i64, i64, i64, i64, i64, i64 } -; CHECK-SAME: { i64 ptrtoint ({ [5 x i8], [59 x i8] }* @"??_C@_04JIHMPGLA@asdf?$AA@" to i64), -; CHECK-SAME: i64 5, i64 64, i64 ptrtoint ([17 x i8]* @___asan_gen_.1 to i64), +; CHECK-SAME: { i64 ptrtoint ({ [5 x i8], [27 x i8] }* @"??_C@_04JIHMPGLA@asdf?$AA@" to i64), +; CHECK-SAME: i64 5, i64 32, i64 ptrtoint ([17 x i8]* @___asan_gen_.1 to i64), ; CHECK-SAME: i64 ptrtoint ([8 x i8]* @___asan_gen_ to i64), i64 0, ; CHECK-SAME: i64 ptrtoint ({ [6 x i8]*, i32, i32 }* @___asan_gen_.3 to i64), i64 0 }, ; CHECK-SAME: section ".ASAN$GL", comdat($"??_C@_04JIHMPGLA@asdf?$AA@"), align 64