File: strlen-9.ll

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (91 lines) | stat: -rw-r--r-- 3,978 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
;
; Verify that strlen calls with unterminated constant arrays or with
; just past-the-end pointers to strings are not folded.
;
; RUN: opt < %s -passes=instcombine -S | FileCheck %s

declare i64 @strlen(i8*)

@a5 = constant [5 x i8] c"12345"
@s5 = constant [6 x i8] c"12345\00"
@z0 = constant [0 x i8] zeroinitializer
@z5 = constant [5 x i8] zeroinitializer


; Verify that all the invalid calls below are folded.  This is safer than
; making the library calls even though it prevents sanitizers from reporting
; the bugs.

define void @fold_strlen_no_nul(i64* %plen, i32 %i) {
; CHECK-LABEL: @fold_strlen_no_nul(
; CHECK-NEXT:    store i64 5, i64* [[PLEN:%.*]], align 4
; CHECK-NEXT:    [[PNA5_P5:%.*]] = getelementptr i64, i64* [[PLEN]], i64 1
; CHECK-NEXT:    store i64 0, i64* [[PNA5_P5]], align 4
; CHECK-NEXT:    [[PNS5_P6:%.*]] = getelementptr i64, i64* [[PLEN]], i64 2
; CHECK-NEXT:    store i64 0, i64* [[PNS5_P6]], align 4
; CHECK-NEXT:    [[TMP1:%.*]] = sext i32 [[I:%.*]] to i64
; CHECK-NEXT:    [[PA5_PI:%.*]] = getelementptr [5 x i8], [5 x i8]* @a5, i64 0, i64 [[TMP1]]
; CHECK-NEXT:    [[NA5_PI:%.*]] = call i64 @strlen(i8* noundef nonnull dereferenceable(1) [[PA5_PI]])
; CHECK-NEXT:    [[PNA5_PI:%.*]] = getelementptr i64, i64* [[PLEN]], i64 3
; CHECK-NEXT:    store i64 [[NA5_PI]], i64* [[PNA5_PI]], align 4
; CHECK-NEXT:    [[PNZ0_P0:%.*]] = getelementptr i64, i64* [[PLEN]], i64 4
; CHECK-NEXT:    store i64 0, i64* [[PNZ0_P0]], align 4
; CHECK-NEXT:    [[TMP2:%.*]] = sext i32 [[I]] to i64
; CHECK-NEXT:    [[PZ0_PI:%.*]] = getelementptr [0 x i8], [0 x i8]* @z0, i64 0, i64 [[TMP2]]
; CHECK-NEXT:    [[NZ0_PI:%.*]] = call i64 @strlen(i8* noundef nonnull dereferenceable(1) [[PZ0_PI]])
; CHECK-NEXT:    [[PNZ0_PI:%.*]] = getelementptr i64, i64* [[PLEN]], i64 5
; CHECK-NEXT:    store i64 [[NZ0_PI]], i64* [[PNZ0_PI]], align 4
; CHECK-NEXT:    [[PNZ5_P5:%.*]] = getelementptr i64, i64* [[PLEN]], i64 6
; CHECK-NEXT:    store i64 0, i64* [[PNZ5_P5]], align 4
; CHECK-NEXT:    ret void
;
; Verify that strlen(a5) is folded to 5.
  %pa0_p0 = getelementptr [5 x i8], [5 x i8]* @a5, i32 0, i32 0
  %na5_p0 = call i64 @strlen(i8* %pa0_p0)
  %pna5_p0 = getelementptr i64, i64* %plen, i64 0
  store i64 %na5_p0, i64* %pna5_p0

; Verify that strlen(a5 + 5) is folded to 0.
  %pa5_p5 = getelementptr [5 x i8], [5 x i8]* @a5, i32 0, i32 5
  %na5_p5 = call i64 @strlen(i8* %pa5_p5)
  %pna5_p5 = getelementptr i64, i64* %plen, i64 1
  store i64 %na5_p5, i64* %pna5_p5

; Verify that strlen(s5 + 6) is folded to 0.
  %ps5_p6 = getelementptr [6 x i8], [6 x i8]* @s5, i32 0, i32 6
  %ns5_p6 = call i64 @strlen(i8* %ps5_p6)
  %pns5_p6 = getelementptr i64, i64* %plen, i64 2
  store i64 %ns5_p6, i64* %pns5_p6

; TODO: Verify that strlen(a5 + i) is folded to 5 - i?  It's currently
; not folded because the variable offset makes getConstantDataArrayInfo
; fail.
  %pa5_pi = getelementptr [5 x i8], [5 x i8]* @a5, i32 0, i32 %i
  %na5_pi = call i64 @strlen(i8* %pa5_pi)
  %pna5_pi = getelementptr i64, i64* %plen, i64 3
  store i64 %na5_pi, i64* %pna5_pi

; Verify that strlen(z0) is folded to 0.
  %pz0_p0 = getelementptr [0 x i8], [0 x i8]* @z0, i32 0, i32 0
  %nz0_p0 = call i64 @strlen(i8* %pz0_p0)
  %pnz0_p0 = getelementptr i64, i64* %plen, i64 4
  store i64 %nz0_p0, i64* %pnz0_p0

; TODO: Verify that strlen(z0 + i) is folded to 0.  As the case above,
; this one is not folded either because the variable offset makes
; getConstantDataArrayInfo fail.

  %pz0_pi = getelementptr [0 x i8], [0 x i8]* @z0, i32 0, i32 %i
  %nz0_pi = call i64 @strlen(i8* %pz0_pi)
  %pnz0_pi = getelementptr i64, i64* %plen, i64 5
  store i64 %nz0_pi, i64* %pnz0_pi

; Verify that strlen(z5 + 5) is folded to 0.
  %pz5_p5 = getelementptr [5 x i8], [5 x i8]* @z5, i32 0, i32 5
  %nz5_p5 = call i64 @strlen(i8* %pz5_p5)
  %pnz5_p5 = getelementptr i64, i64* %plen, i64 6
  store i64 %nz5_p5, i64* %pnz5_p5

  ret void
}