[clang] [compiler-rt] [Sanitizer] add signed-integer-wrap sanitizer (PR #80089)

Justin Stitt via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 31 13:44:24 PST 2024


================
@@ -0,0 +1,77 @@
+// Check that -fsanitize=signed-integer-wrap instruments with -fwrapv
+// RUN: %clang_cc1 -fwrapv -triple x86_64-apple-darwin -emit-llvm -o - %s -fsanitize=signed-integer-wrap | FileCheck %s --check-prefix=CHECK
----------------
JustinStitt wrote:

> `CHECK` is the implicit default prefix if otherwise unspecified. Being explicit here is not necessary and is discouraged (unlike the zen of python).

Gotcha, should I rename Check to something else or just drop the --check-prefix entirely?


Also, the non-instrumented vs instrumented version don't have too much in common. Should I still opt for this shared --check-prefixes approach? 

Here's what we're workin' with:

<details>
<summary>clang -cc1 -fwrapv -triple x86_64-apple-darwin -emit-llvm -o - -fsanitize=signed-integer-wrap san.c
</summary>
<br>

```

; ModuleID = 'san.c'
source_filename = "san.c"
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-darwin"

@b = external global i32, align 4
@c = external global i32, align 4
@.src = private unnamed_addr constant [6 x i8] c"san.c\00", align 1
@0 = private unnamed_addr constant { i16, i16, [6 x i8] } { i16 0, i16 11, [6 x i8] c"'int'\00" }
@1 = private unnamed_addr global { { ptr, i32, i32 }, ptr } { { ptr, i32, i32 } { ptr @.src, i32 4, i32 9 }, ptr @0 }
@a = external global i32, align 4

; Function Attrs: noinline nounwind optnone
define void @test_add_overflow() #0 {
entry:
  %0 = load volatile i32, ptr @b, align 4
  %1 = load volatile i32, ptr @c, align 4
  %2 = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %0, i32 %1), !nosanitize !2
  %3 = extractvalue { i32, i1 } %2, 0, !nosanitize !2
  %4 = extractvalue { i32, i1 } %2, 1, !nosanitize !2
  %5 = xor i1 %4, true, !nosanitize !2
  br i1 %5, label %cont, label %handler.add_overflow, !prof !3, !nosanitize !2

handler.add_overflow:                             ; preds = %entry
  %6 = zext i32 %0 to i64, !nosanitize !2
  %7 = zext i32 %1 to i64, !nosanitize !2
  call void @__ubsan_handle_add_overflow_abort(ptr @1, i64 %6, i64 %7) #3, !nosanitize !2
  unreachable, !nosanitize !2

cont:                                             ; preds = %entry
  store volatile i32 %3, ptr @a, align 4
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare { i32, i1 } @llvm.sadd.with.overflow.i32(i32, i32) #1

; Function Attrs: noreturn nounwind uwtable
declare void @__ubsan_handle_add_overflow_abort(ptr, i64, i64) #2

attributes #0 = { noinline nounwind optnone "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-features"="+cx8,+mmx,+sse,+sse2,+x87" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { noreturn nounwind uwtable }
attributes #3 = { noreturn nounwind }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang version 19.0.0git (https://github.com/JustinStitt/llvm-project.git 3766054a825a7dd4ae69feb73c767029f86f912e)"}
!2 = !{}
!3 = !{!"branch_weights", i32 1048575, i32 1}

```
</details>

<details>
<summary>clang -cc1 -fwrapv -triple x86_64-apple-darwin -emit-llvm -o - -fsanitize=signed-integer-overflow san.c

</summary>
<br>

```

; ModuleID = 'san.c'
source_filename = "san.c"
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-darwin"

@b = external global i32, align 4
@c = external global i32, align 4
@a = external global i32, align 4

; Function Attrs: noinline nounwind optnone
define void @test_add_overflow() #0 {
entry:
  %0 = load volatile i32, ptr @b, align 4
  %1 = load volatile i32, ptr @c, align 4
  %add = add i32 %0, %1
  store volatile i32 %add, ptr @a, align 4
  ret void
}

attributes #0 = { noinline nounwind optnone "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-features"="+cx8,+mmx,+sse,+sse2,+x87" }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang version 19.0.0git (https://github.com/JustinStitt/llvm-project.git 3766054a825a7dd4ae69feb73c767029f86f912e)"}

```

</details>

<details open>
<summary>diff</summary>
<br>


```diff

8,10d7
< @.src = private unnamed_addr constant [6 x i8] c"san.c\00", align 1
< @0 = private unnamed_addr constant { i16, i16, [6 x i8] } { i16 0, i16 11, [6 x i8] c"'int'\00" }
< @1 = private unnamed_addr global { { ptr, i32, i32 }, ptr } { { ptr, i32, i32 } { ptr @.src, i32 4, i32 9 }, ptr @0 }
18,31c15,16
<   %2 = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %0, i32 %1), !nosanitize !2
<   %3 = extractvalue { i32, i1 } %2, 0, !nosanitize !2
<   %4 = extractvalue { i32, i1 } %2, 1, !nosanitize !2
<   %5 = xor i1 %4, true, !nosanitize !2
<   br i1 %5, label %cont, label %handler.add_overflow, !prof !3, !nosanitize !2
< 
< handler.add_overflow:                             ; preds = %entry
<   %6 = zext i32 %0 to i64, !nosanitize !2
<   %7 = zext i32 %1 to i64, !nosanitize !2
<   call void @__ubsan_handle_add_overflow_abort(ptr @1, i64 %6, i64 %7) #3, !nosanitize !2
<   unreachable, !nosanitize !2
< 
< cont:                                             ; preds = %entry
<   store volatile i32 %3, ptr @a, align 4
---
>   %add = add i32 %0, %1
>   store volatile i32 %add, ptr @a, align 4
35,40d19
< ; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
< declare { i32, i1 } @llvm.sadd.with.overflow.i32(i32, i32) #1
< 
< ; Function Attrs: noreturn nounwind uwtable
< declare void @__ubsan_handle_add_overflow_abort(ptr, i64, i64) #2
< 
42,44d20
< attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
< attributes #2 = { noreturn nounwind uwtable }
< attributes #3 = { noreturn nounwind }
51,52d26
< !2 = !{}
< !3 = !{!"branch_weights", i32 1048575, i32 1}


```

</details>


https://github.com/llvm/llvm-project/pull/80089


More information about the cfe-commits mailing list