xref: /llvm-project/llvm/test/Transforms/SafeStack/X86/array.ll (revision d5c6755d83b34e663b6fbe497b52bcc73b55199a)
1; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
2; RUN: opt -safe-stack -S -mtriple=i386-pc-contiki-unknown < %s -o - | FileCheck -check-prefix=SINGLE-THREAD %s
3; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
4
5; array [4 x i8]
6; Requires protector.
7
8; CHECK: @__safestack_unsafe_stack_ptr = external thread_local(initialexec) global i8*
9; SINGLE-THREAD: @__safestack_unsafe_stack_ptr = external global i8*
10
11define void @foo(i8* %a) nounwind uwtable safestack {
12entry:
13  ; CHECK: %[[USP:.*]] = load i8*, i8** @__safestack_unsafe_stack_ptr
14
15  ; CHECK: %[[USST:.*]] = getelementptr i8, i8* %[[USP]], i32 -16
16
17  ; CHECK: store i8* %[[USST]], i8** @__safestack_unsafe_stack_ptr
18
19  %a.addr = alloca i8*, align 8
20  %buf = alloca [4 x i8], align 1
21
22  ; CHECK: %[[AADDR:.*]] = alloca i8*, align 8
23  ; CHECK: store i8* {{.*}}, i8** %[[AADDR]], align 8
24  store i8* %a, i8** %a.addr, align 8
25
26  ; CHECK: %[[BUFPTR:.*]] = getelementptr i8, i8* %[[USP]], i32 -4
27  ; CHECK: %[[BUFPTR2:.*]] = bitcast i8* %[[BUFPTR]] to [4 x i8]*
28  ; CHECK: %[[GEP:.*]] = getelementptr inbounds [4 x i8], [4 x i8]* %[[BUFPTR2]], i32 0, i32 0
29  %gep = getelementptr inbounds [4 x i8], [4 x i8]* %buf, i32 0, i32 0
30
31  ; CHECK: %[[A2:.*]] = load i8*, i8** %[[AADDR]], align 8
32  %a2 = load i8*, i8** %a.addr, align 8
33
34  ; CHECK: call i8* @strcpy(i8* %[[GEP]], i8* %[[A2]])
35  %call = call i8* @strcpy(i8* %gep, i8* %a2)
36
37  ; CHECK: store i8* %[[USP]], i8** @__safestack_unsafe_stack_ptr
38  ret void
39}
40
41; Load from an array at a fixed offset, no overflow.
42define i8 @StaticArrayFixedSafe() nounwind uwtable safestack {
43entry:
44  ; CHECK-LABEL: define i8 @StaticArrayFixedSafe(
45  ; CHECK-NOT: __safestack_unsafe_stack_ptr
46  ; CHECK: ret i8
47  %buf = alloca i8, i32 4, align 1
48  %gep = getelementptr inbounds i8, i8* %buf, i32 2
49  %x = load i8, i8* %gep, align 1
50  ret i8 %x
51}
52
53; Load from an array at a fixed offset with overflow.
54define i8 @StaticArrayFixedUnsafe() nounwind uwtable safestack {
55entry:
56  ; CHECK-LABEL: define i8 @StaticArrayFixedUnsafe(
57  ; CHECK: __safestack_unsafe_stack_ptr
58  ; CHECK: ret i8
59  %buf = alloca i8, i32 4, align 1
60  %gep = getelementptr inbounds i8, i8* %buf, i32 5
61  %x = load i8, i8* %gep, align 1
62  ret i8 %x
63}
64
65; Load from an array at an unknown offset.
66define i8 @StaticArrayVariableUnsafe(i32 %ofs) nounwind uwtable safestack {
67entry:
68  ; CHECK-LABEL: define i8 @StaticArrayVariableUnsafe(
69  ; CHECK: __safestack_unsafe_stack_ptr
70  ; CHECK: ret i8
71  %buf = alloca i8, i32 4, align 1
72  %gep = getelementptr inbounds i8, i8* %buf, i32 %ofs
73  %x = load i8, i8* %gep, align 1
74  ret i8 %x
75}
76
77; Load from an array of an unknown size.
78define i8 @DynamicArrayUnsafe(i32 %sz) nounwind uwtable safestack {
79entry:
80  ; CHECK-LABEL: define i8 @DynamicArrayUnsafe(
81  ; CHECK: __safestack_unsafe_stack_ptr
82  ; CHECK: ret i8
83  %buf = alloca i8, i32 %sz, align 1
84  %gep = getelementptr inbounds i8, i8* %buf, i32 2
85  %x = load i8, i8* %gep, align 1
86  ret i8 %x
87}
88
89declare i8* @strcpy(i8*, i8*)
90